Skip to content
This repository has been archived by the owner on Mar 24, 2022. It is now read-only.

Commit

Permalink
fix(parser): Set endTag loc for mixed-case foreign elements (inikul…
Browse files Browse the repository at this point in the history
…in#353)

Co-authored-by: Felix Böhm <188768+fb55@users.noreply.github.com>
  • Loading branch information
pmdartus and fb55 committed Mar 2, 2022
1 parent 8b96699 commit cb2265d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion packages/parse5/lib/parser/index.ts
Expand Up @@ -3783,7 +3783,11 @@ function endTagInForeignContent<T extends TreeAdapterTypeMap>(p: Parser<T>, toke
break;
}

if (p.treeAdapter.getTagName(element).toLowerCase() === token.tagName) {
const tagName = p.treeAdapter.getTagName(element);

if (tagName.toLowerCase() === token.tagName) {
//NOTE: update token tag name for `_setEndLocation`.
token.tagName = tagName;
p.openElements.shortenToLength(i);
break;
}
Expand Down
20 changes: 20 additions & 0 deletions packages/parse5/lib/parser/parser-location-info.test.ts
Expand Up @@ -132,6 +132,26 @@ generateTestsForEachTreeAdapter('location-info-parser', (treeAdapter) => {

assert.ok(!location.endTag);
});

test('Regression - location.endTag should be available adjusted SVG elements (GH-352)', () => {
const html = '<svg><foreignObject></foreignObject></svg>';

const opts = {
treeAdapter,
sourceCodeLocationInfo: true,
};

const fragment = parse5.parseFragment(html, opts);
const svg = treeAdapter.getChildNodes(fragment)[0];
const foreignObject = treeAdapter.getChildNodes(svg)[0];
const location = treeAdapter.getNodeSourceCodeLocation(foreignObject);

assert.ok(location && location.startTag && location.endTag);
assert.strictEqual(
html.slice(location.startTag.startOffset, location.endTag.endOffset),
'<foreignObject></foreignObject>'
);
});
});

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

0 comments on commit cb2265d

Please sign in to comment.