fix(webui): prevent single-dollar math from crossing code spans (#6646)

This commit is contained in:
chengyongru authored and GitHub committed 2026-11-13 14:04:05 +08:00
1 parent 8cce73465b
commit c7a1eb910d
9 files changed
+245 -2

No files matched your search

+3
View File
@@ -44,6 +44,7 @@ import { ComposerActionFixture } from "./composer-action-fixture";
import { MobileChatLayoutFixture } from "./mobile-chat-layout-fixture";
import { ThemeToggleFixture } from "./theme-toggle-fixture";
import { DisplayMathFixture } from "./display-math-fixture";
import { MarkdownDollarRefsFixture } from "./markdown-dollar-refs-fixture";
import { AttachmentLayoutFixture } from "./attachment-layout-fixture";
import { LoadedSessionSearchFixture } from "./loaded-session-search-fixture";
import { AppearanceFixture } from "./appearance-fixture";
@@ -267,6 +268,8 @@ createRoot(document.getElementById("root")!).render(<TooltipProvider>
? <ChatFailureFixture parameters={parameters} />
: parameters.get("case") === "display-math"
? <DisplayMathFixture />
: parameters.get("case") === "markdown-dollar-refs"
? <MarkdownDollarRefsFixture />
: parameters.get("case") === "code-wrapping"
? <CodeWrappingFixture />
: parameters.get("case") === "typography"
@@ -0,0 +1,34 @@
import { useState } from "react";
import { MessageBubble } from "@/components/MessageBubble";
import dollarRefsFixture from "@/tests/fixtures/markdown-dollar-refs.md?raw";
const source = dollarRefsFixture.replaceAll("\r\n", "\n");
const codeStart = source.indexOf("`$defs`");
const ends = [codeStart + 1, codeStart + 2, codeStart + 4, codeStart + 7, source.length];
const precedenceExamples = [
"**$ref** supports `$defs / definitions`.",
"**$ref** in C:\\Users\\me uses `$defs`.",
"A $x`$th`",
"Explicit \\(f(`x')\\)th`code`.",
].join("\n\n");
export function MarkdownDollarRefsFixture() {
const [step, setStep] = useState(0);
const [streaming, setStreaming] = useState(true);
return <main className="mx-auto max-w-3xl space-y-4 p-6">
<nav className="flex gap-4">
<button onClick={() => setStep((value) => Math.min(value + 1, ends.length - 1))}>Advance stream</button>
<button onClick={() => setStreaming(false)}>Complete</button>
</nav>
<section aria-label="Reported assistant reply">
<MessageBubble message={{ id: "reported", role: "assistant", content: source.slice(0, ends[step]), createdAt: 1, isStreaming: streaming }} />
</section>
<section aria-label="Inline formulas">
<MessageBubble message={{ id: "math", role: "assistant", content: "The $n$th term: $\\text{`value`}$.", createdAt: 2 }} />
</section>
<section aria-label="Code and math precedence">
<MessageBubble message={{ id: "precedence", role: "assistant", content: precedenceExamples, createdAt: 3, isStreaming: streaming }} />
</section>
</main>;
}
@@ -0,0 +1,37 @@
import { expect, test } from "@playwright/test";
for (const width of [1280, 390]) {
test(`preserves dollar references while streaming at ${width}px`, async ({ page }, info) => {
const errors: string[] = [];
page.on("pageerror", (error) => errors.push(error.message));
await page.setViewportSize({ width, height: 844 });
await page.goto("/browser/fixture.html?case=markdown-dollar-refs");
const reply = page.getByRole("region", { name: "Reported assistant reply" });
const formulas = page.getByRole("region", { name: "Inline formulas" });
const precedence = page.getByRole("region", { name: "Code and math precedence" });
async function expectPrecedence() {
await expect(precedence.locator("strong")).toHaveText(["$ref", "$ref"]);
await expect(precedence.locator("code")).toHaveText(["$defs / definitions", "$defs", "$th", "code"]);
await expect(precedence.locator("annotation")).toHaveText(["f(`x')"]);
await expect(precedence.locator(".katex-error")).toHaveCount(0);
}
await expectPrecedence();
await expect(formulas.locator("annotation")).toHaveText(["n", "\\text{`value`}"]);
await expect(formulas.locator(".katex-error")).toHaveCount(0);
for (let step = 0; step < 5; step++) {
await expect(reply.locator("strong").filter({ hasText: "已经有一份完整的 $ref 解析器" })).toHaveCount(1);
await expect(reply.locator(".katex")).toHaveCount(0);
if (step < 4) await page.getByRole("button", { name: "Advance stream" }).click();
}
await expect(reply.locator("code").filter({ hasText: "_resolve_local_schema_ref" })).toHaveCount(1);
await expect(reply.locator("code").filter({ hasText: "$defs" }).first()).toHaveText("$defs");
await expect(reply).toContainText("用一个词总结这个 PR");
await page.getByRole("button", { name: "Complete" }).click();
await expect(reply.locator("[data-assistant-selectable=true]")).toHaveCount(1);
await expect(reply.locator("strong").filter({ hasText: "已经有一份完整的 $ref 解析器" })).toHaveCount(1);
await expect(reply.locator(".katex")).toHaveCount(0);
await expectPrecedence();
expect(errors).toEqual([]);
if (width === 1280) await page.screenshot({ path: info.outputPath("reported-message.png"), fullPage: true });
});
}