Skip to content

Commit 72a98ac

Browse files
pmdartusfb55
andauthored
fix(parser): Set endTag loc for mixed-case foreign elements (#353)
Co-authored-by: Felix Böhm <[email protected]>
1 parent b3338ff commit 72a98ac

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

packages/parse5/lib/parser/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3783,7 +3783,11 @@ function endTagInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, toke
37833783
break;
37843784
}
37853785

3786-
if (p.treeAdapter.getTagName(element).toLowerCase() === token.tagName) {
3786+
const tagName = p.treeAdapter.getTagName(element);
3787+
3788+
if (tagName.toLowerCase() === token.tagName) {
3789+
//NOTE: update token tag name for `_setEndLocation`.
3790+
token.tagName = tagName;
37873791
p.openElements.shortenToLength(i);
37883792
break;
37893793
}

packages/parse5/lib/parser/parser-location-info.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,26 @@ generateTestsForEachTreeAdapter('location-info-parser', (treeAdapter) => {
132132

133133
assert.ok(!location.endTag);
134134
});
135+
136+
test('Regression - location.endTag should be available adjusted SVG elements (GH-352)', () => {
137+
const html = '<svg><foreignObject></foreignObject></svg>';
138+
139+
const opts = {
140+
treeAdapter,
141+
sourceCodeLocationInfo: true,
142+
};
143+
144+
const fragment = parse5.parseFragment(html, opts);
145+
const svg = treeAdapter.getChildNodes(fragment)[0];
146+
const foreignObject = treeAdapter.getChildNodes(svg)[0];
147+
const location = treeAdapter.getNodeSourceCodeLocation(foreignObject);
148+
149+
assert.ok(location && location.startTag && location.endTag);
150+
assert.strictEqual(
151+
html.slice(location.startTag.startOffset, location.endTag.endOffset),
152+
'<foreignObject></foreignObject>'
153+
);
154+
});
135155
});
136156

137157
describe('location-info-parser', () => {

0 commit comments

Comments
 (0)