From c2f167bf78a2f34dd27a84bd6c750ec1a4dd56e6 Mon Sep 17 00:00:00 2001 From: chengyongru Date: Wed, 9 Sep 2026 11:32:51 +0800 Subject: [PATCH] fix(webui): preserve less-than comparisons in streaming math --- webui/src/components/MarkdownTextRenderer.tsx | 4 + .../src/tests/markdown-text-renderer.test.tsx | 97 ++++++++++++++++++- 2 files changed, 97 insertions(+), 4 deletions(-) diff --git a/webui/src/components/MarkdownTextRenderer.tsx b/webui/src/components/MarkdownTextRenderer.tsx index 05f4f8c1f..7a87a60ca 100644 --- a/webui/src/components/MarkdownTextRenderer.tsx +++ b/webui/src/components/MarkdownTextRenderer.tsx @@ -287,6 +287,9 @@ const remarkPlugins: NonNullable = [ ]; const rehypePlugins: NonNullable = [rehypeKatex]; +// Remend mistakes math comparisons like `j { expect(screen.queryByRole("button", { name: "Code" })).not.toBeInTheDocument(); }); - it("renders a safe subset of inline HTML", () => { + it.each([false, true])("renders a safe subset of inline HTML (streaming=%s)", (streaming) => { const { container } = render( - + {"高亮文本\n\n上标:x2\n下标:H2O"} , ); @@ -389,9 +389,9 @@ describe("MarkdownTextRenderer", () => { expect(container.querySelector("sub")).toHaveTextContent("2"); }); - it("keeps unsafe HTML as text", () => { + it.each([false, true])("keeps unsafe HTML as text (streaming=%s)", (streaming) => { const { container } = render( - + {"\n\nbad"} , ); @@ -402,6 +402,16 @@ describe("MarkdownTextRenderer", () => { expect(container).toHaveTextContent("bad"); }); + it("keeps incomplete unsafe HTML inert throughout streaming", () => { + const source = ''; + const { container, rerender } = render({""}); + for (let end = 1; end <= source.length; end += 1) { + rerender({source.slice(0, end)}); + expect(container.querySelector("img, [onerror]")).toBeNull(); + } + expect(container).toHaveTextContent(source); + }); + it("renders safe details blocks", () => { const { container } = render( @@ -694,6 +704,85 @@ describe("MarkdownTextRenderer", () => { } }); + it.each([false, true])("preserves less-than comparisons in math and following content (streaming=%s)", (streaming) => { + const formula = String.raw`\hat d(u,v) = \sum_{i} \frac{1}{z_i}\,\alpha_i\, T_i, \qquad T_i = \prod_{j + {"## Expected inverse depth\n\n$$\n" + formula + "\n$$\n\n## Supervision loss\n\nThe next section stays visible."} + , + ); + + expect(container.querySelector(".katex-error")).toBeNull(); + expect(container.querySelector(".katex-display annotation")?.textContent).toBe(formula); + expect(screen.getByRole("heading", { name: "Supervision loss" })).toBeInTheDocument(); + expect(container).toHaveTextContent("The next section stays visible."); + }); + + it.each([ + "$j { + const { container, rerender } = render({""}); + rerender({source}); + expect(container.querySelector(".katex-error")).toBeNull(); + expect(container.querySelector("annotation")?.textContent).toBe("j{source}); + expect(container.querySelector(".katex-error")).toBeNull(); + expect(container.querySelector("annotation")?.textContent).toBe("j { + const { container, rerender } = render({""}); + for (let end = 1; end <= source.length; end += 1) { + rerender({source.slice(0, end)}); + } + expect(container.querySelector(".katex-error")).toBeNull(); + expect(container.querySelector("annotation")?.textContent).toBe("j { + const prefix = "$$\n" + String.raw`T_i = \prod_{j`; + const comparison = prefix + "{prefix}); + + rerender({comparison}); + expect(container).toHaveTextContent("{source}); + expect(container.querySelector(".katex-error")).toBeNull(); + expect(container.querySelector(".katex-display annotation")?.textContent).toBe( + String.raw`T_i = \prod_{j{content}); + expect(container.querySelector(".katex-error")).toBeNull(); + expect(container.querySelector(".katex-display annotation")?.textContent).toBe( + String.raw`T_i = \prod_{j { + const { container } = render( + + {"Inline `$j, + ); + expect(container.querySelector(".katex")).toBeNull(); + expect(screen.getByText("$j { const source = "$$C\n= x_i$$"; const { container } = render(