mathjax / mathjax/MathJax

MathJax typesets removed DOM elements causing measureTextNodeWithCache pollution

Open
#3,384 1 comment 0 reactions 1 assignee View on GitHub

@dpvc is already working on this.

Since Aug 13, 2025.

Accepted Code Example Ready for Development v4
Dominant language
JavaScript
Stars
10.9k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

Issue Summary

In MathJax 4.0.0.beta7, due to some asynchronous execution issues, I sometimes call typeset on the element after the element is removed.

Some of my CJK characters are folded together. I looked at the source code and it was roughly due to the following reasons.

When Mathjax (svg) encounters a cjk character, it calls jax.unknownText to create a text element, and then calls jax.measureTextNodeWithCache to calculate the text width (or read the cache)

If the dom is removed, the em/ex value calculated by Metrics will be NaN, which will cause the element created by jax.unknownText to get fontsize:NaNpx, and measureTextNodeWithCache will get a value close to 0.

Image

So far, everything is fine, because the rendering result will not actually be on window, but measureTextNodeWithCache will record the calculation result in a persistent cache, which will eventually cause all subsequent width calculations of this CJK character to fail.

Image

Steps to Reproduce:
const mathjax = await getMathJax();
const div1 = document.createElement('div');
div1.innerText = '\\(\\ce{ZnCO3 \\xlongequal{高温} ZnO + CO2 ^}\\)';
document.body.appendChild(div1);
const p = mathjax.typesetPromise([div1]);
div1.remove();
await p;
const div2 = document.createElement('div');
div2.innerText = '\\(\\ce{ZnCO3 \\xlongequal{高温} ZnO + CO2 ^}\\)';
document.body.appendChild(div2);
mathjax.typesetPromise([div2]);
Technical details:
  • MathJax Version: 4.0.0.beta7
  • Client OS: (e.g., Mac OS X 10.8.4)
  • Browser: (e.g., Chrome 29.0.1547.57)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.