Skip to content

Commit b9093da

Browse files
fb55claude
andcommitted
fix(parser): match foreign end tags ASCII case-insensitively
The foreign-content "any other end tag" steps compare the node's name converted to ASCII lowercase. `String#toLowerCase` also folds non-ASCII characters (U+212A KELVIN SIGN becomes `k`), so an element could fail to match its own end tag or match another one. The stored name is compared against the token name and the SVG adjustment-map name instead: start-tag names are ASCII-lowercased by the tokenizer and tree construction only adds ASCII uppercase through that map, so the comparison is ASCII-exact and allocates nothing. Co-Authored-By: Claude Fable 5.1 <[email protected]>
1 parent 61676e2 commit b9093da

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

‎packages/parse5/lib/parser/index.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ describe('parser', () => {
8484
});
8585
});
8686

87+
// TODO: upstream to html5lib-tests
88+
it.each([
89+
// U+212A KELVIN SIGN is not an ASCII upper alpha: `toLowerCase` turns it into "k".
90+
['<svg><a\u212A><desc></a\u212A>x', '<svg><a\u212A><desc></desc></a\u212A>x</svg>'],
91+
['<svg><a\u212A><desc></ak>x', '<svg><a\u212A><desc>x</desc></a\u212A></svg>'],
92+
])('foreign end tags are matched ASCII case-insensitively - %s', (input, expected) => {
93+
expect(serialize(parseFragment(input))).toBe(expected);
94+
});
95+
8796
describe("Regression - Don't inherit from Object when creating collections (GH-119)", () => {
8897
beforeEach(() => {
8998
// @ts-expect-error Adding unknown prototype method

‎packages/parse5/lib/parser/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3814,7 +3814,9 @@ function endTagInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, toke
38143814

38153815
const tagName = p.treeAdapter.getTagName(element);
38163816

3817-
if (tagName.toLowerCase() === token.tagName) {
3817+
// The tokenizer lowercases foreign start-tag names and tree construction adds ASCII uppercase only through
3818+
// the SVG adjustment map, so these two comparisons are the ASCII case-insensitive match for every name created.
3819+
if (tagName === token.tagName || tagName === foreignContent.SVG_TAG_NAMES_ADJUSTMENT_MAP.get(token.tagName)) {
38183820
//NOTE: update the token tag name for `_setEndLocation`.
38193821
token.tagName = tagName;
38203822
p.openElements.shortenToLength(i);

0 commit comments

Comments
 (0)