Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions SidebarMathJaxShared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.`);
}
Expand Down
18 changes: 16 additions & 2 deletions tests/mathjax-svg-fragmentation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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),
};
},
},
Expand Down Expand Up @@ -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/);
});
Loading