Skip to content

fix: sync sourceCodeLocation.attrs keys with adjusted SVG/MathML/XML attribute names#1705

Open
TrevorBurnham wants to merge 1 commit intoinikulin:masterfrom
TrevorBurnham:fix-svg-attr-location-case
Open

fix: sync sourceCodeLocation.attrs keys with adjusted SVG/MathML/XML attribute names#1705
TrevorBurnham wants to merge 1 commit intoinikulin:masterfrom
TrevorBurnham:fix-svg-attr-location-case

Conversation

@TrevorBurnham
Copy link
Copy Markdown
Contributor

@TrevorBurnham TrevorBurnham commented Mar 6, 2026

Closes #318

Summary

When parsing foreign content (SVG, MathML), parse5 adjusts attribute names from their tokenizer-lowercased form to spec-correct casing (e.g. viewboxviewBox). However, the keys in sourceCodeLocation.attrs were not updated to match, so element.attrs would have viewBox while sourceCodeLocation.attrs only had viewbox.

Changes

  • packages/parse5/lib/common/foreign-content.ts — Updated adjustTokenSVGAttrs, adjustTokenMathMLAttrs, and adjustTokenXMLAttrs to also rename the corresponding key in token.location.attrs when adjusting an attribute name.
  • packages/parse5/lib/parser/parser-location-info.test.ts — Added a regression test parsing <svg viewBox="0 0 100 100"> and asserting that location.attrs uses the camelCase key viewBox, not viewbox.

Before

const fragment = parseFragment('<svg viewBox="0 0 100 100"></svg>', {
  sourceCodeLocationInfo: true,
});
const svg = fragment.childNodes[0];

svg.attrs[0].name;                         // 'viewBox' ✓
Object.keys(svg.sourceCodeLocation.attrs); // ['viewbox'] ✗ — mismatch

After

svg.attrs[0].name;                         // 'viewBox' ✓
Object.keys(svg.sourceCodeLocation.attrs); // ['viewBox'] ✓ — matches

…attribute names

When parsing foreign content (SVG, MathML), attribute names are adjusted
from lowercase to their spec-correct casing (e.g. viewbox -> viewBox).
The location info keys in token.location.attrs were not updated to match,
causing a mismatch where attrs[i].name and Object.keys(location.attrs)
would differ.

Fixes inikulin#318
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

lookup fails on SVG viewBox attribute.

1 participant