Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

3.5 KB
Raw Blame
/**
 * OPT-IN, tag-name-based MathML namespace approximation for happy-dom.
 *
 * happy-dom does not parse `<math>` subtrees into the MathML namespace at
 * all (no tokenizer/tree-construction support for MathML foreign content),
 * so `Element.prototype.namespaceURI` reports the HTML namespace for every
 * MathML tag regardless of where it sits in the tree. DOMPurify treats that
 * as a namespace mismatch and strips KaTeX's accessibility subtree
 * (`annotation`, `semantics`, `mrow`, ...) even though it survives untouched
 * in a real browser.
 *
 * This function patches `namespaceURI` to return the MathML namespace for
 * any element whose tagName is a known MathML tag name -- WITHOUT regard to
 * tree position. That is a coarse approximation good enough to let
 * legitimately-rendered KaTeX markup survive DOMPurify in assertions that
 * check the accessibility subtree exists (e.g. `querySelector('math
 * annotation')`). It is NOT a model of namespace inheritance or of the
 * parser's foreign-content / integration-point rules
 * (`annotation-xml[encoding="text/html"]`, `mglyph`, `mtext` reverting to
 * HTML parsing inside a table or form), which is exactly the axis
 * mXSS namespace-confusion payloads exploit.
 *
 * DO NOT install this in a suite that asserts a namespace-confusion / mXSS
 * invariant (checking that no HTML-namespaced element has a foreign-
 * namespaced parent, or similar): because this getter answers purely from
 * tag name, such a check can never observe real namespace confusion --  it
 * would report "confused" or "not confused" consistently with the input's
 * tag names, never with where a real parser would actually have put those
 * tags. tests/js/security/mxss-reparse-invariant.test.js deliberately does
 * NOT install this for that reason; see its header for what it uses instead
 * (happy-dom's real, tree-position-based SVG namespace tracking, and an
 * explicit acknowledgment that MathML foreign-content confusion is not
 * modeled by this test harness at all).
 *
 * Suites that only need KaTeX's rendered MathML markup to survive
 * sanitization for a non-adversarial rendering assertion (tests/js/services/
 * ui.test.js's "renderMarkdown with KaTeX math" suite, and the render-XSS
 * suites under tests/js/security/ that assert benign KaTeX output alongside
 * hostile payloads) should call this explicitly in their own `beforeAll`,
 * before importing 'dompurify'.
 */
export function installMathMlNamespaceApproximation() {
    const mathMlTags = new Set([
        'math', 'semantics', 'annotation', 'annotation-xml', 'maction',
        'maligngroup', 'malignmark', 'menclose', 'merror', 'mfenced',
        'mfrac', 'mglyph', 'mi', 'mlabeledtr', 'mlongdiv', 'mmultiscripts',
        'mn', 'mo', 'mover', 'mpadded', 'mphantom', 'mprescripts', 'mroot',
        'mrow', 'ms', 'mscarries', 'msgroup', 'msline', 'mspace', 'msqrt',
        'msrow', 'mstack', 'mstyle', 'msub', 'msubsup', 'msup', 'mtable',
        'mtd', 'mtext', 'mtr', 'munder', 'munderover', 'none',
    ]);
    const originalNamespace = Object.getOwnPropertyDescriptor(
        window.Element.prototype,
        'namespaceURI',
    );
    Object.defineProperty(window.Element.prototype, 'namespaceURI', {
        configurable: true,
        get() {
            if (typeof this.tagName === 'string'
                && mathMlTags.has(this.tagName.toLowerCase())) {
                return 'http://www.w3.org/1998/Math/MathML';
            }
            return originalNamespace?.get
                ? originalNamespace.get.call(this)
                : undefined;
        },
    });
}