diff --git a/SidebarMathJaxShared.ts b/SidebarMathJaxShared.ts index 4bdc4fa..7959506 100644 --- a/SidebarMathJaxShared.ts +++ b/SidebarMathJaxShared.ts @@ -256,12 +256,19 @@ async function renderEquationPngWithMathJax( equationTimeoutMs, "typesetting an equation" ); - const svgFragments = Array.from(result.querySelectorAll("svg")) as SVGSVGElement[]; + // MathJax can embed small SVGs inside the root SVG for stretchy delimiters. A + // multi-row cases environment, for example, has one complete equation SVG plus + // two nested brace segments. Only direct SVG children are separate render + // results; treating every descendant as a fragment rejects otherwise valid TeX. + const svgFragments = Array.from(result.children).filter( + (child): child is SVGSVGElement => child.tagName.toLowerCase() === "svg" + ); if (svgFragments.length === 0) { throw new Error("MathJax did not return an SVG element."); } - // REASON: If a future MathJax change defeats grouping, fail visibly before - // rasterization instead of silently replacing an equation with its first term. + // REASON: If a future MathJax change creates multiple top-level render results, + // fail visibly before rasterization instead of silently replacing an equation + // with its first term. Nested SVGs are delimiter internals, not fragments. if (svgFragments.length !== 1) { throw new Error(`MathJax returned ${svgFragments.length} SVG fragments for one equation.`); } diff --git a/tests/mathjax-svg-fragmentation.test.js b/tests/mathjax-svg-fragmentation.test.js index 90f926f..f688c9e 100644 --- a/tests/mathjax-svg-fragmentation.test.js +++ b/tests/mathjax-svg-fragmentation.test.js @@ -23,7 +23,7 @@ test.before(async () => { // Exercise the production renderer with real MathJax typesetting. Only adapt // liteDOM's SVG lookup to the browser API and stop at the layout/canvas boundary; // these tests do not pretend to verify Apps Script insertion or PNG pixels. -async function captureSelectedSvg(equation, inline = true, extraFragment = false) { +async function captureSelectedSvg(equation, inline = true, extraTopLevelFragment = false) { const adaptor = mathJax.startup.adaptor; const reachedLayout = new Error("reached layout boundary"); let selectedXml; @@ -36,17 +36,22 @@ async function captureSelectedSvg(equation, inline = true, extraFragment = false tex2svgPromise: async (input, options) => { const result = await mathJax.tex2svgPromise(input, options); const fragments = adaptor.tags(result, "svg"); - if (extraFragment) fragments.push(fragments[0]); + const topLevelFragments = extraTopLevelFragment ? [fragments[0], fragments[0]] : [fragments[0]]; fragmentCount = fragments.length; const wrap = svg => ({ xml: adaptor.serializeXML(svg), querySelector: () => null, classList: { add() {} }, style: {}, + tagName: "svg", }); return { querySelector: () => fragments.length ? wrap(fragments[0]) : null, querySelectorAll: () => fragments.map(wrap), + // `fragments` deliberately contains every nested SVG just as + // querySelectorAll("svg") does in a browser. `children` models the + // root MathJax container, whose only direct SVG is the full equation. + children: topLevelFragments.map(wrap), }; }, }, @@ -96,6 +101,15 @@ test("display equations, fractions and explicit multiline environments stay inta } }); +test("multi-row cases ignores nested stretchy-brace SVGs", async () => { + const { selectedXml, fragmentCount } = await captureSelectedSvg( + "\\begin{cases}K &: a < x \\le b\\\\ K &: a < x \\le b\\\\ 0 &: \\text{otherwise}\\end{cases}", + false + ); + assert.equal(fragmentCount, 3, "real MathJax emits two nested brace SVGs"); + assert.match(selectedXml, /data-c=\"1D43E\"/, "the outer SVG contains the full cases expression"); +}); + test("unexpected fragmented output fails visibly before rasterization", async () => { await assert.rejects(captureSelectedSvg("12", true, true), /2 SVG fragments/); });