Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(parser): Set endTag loc for mixed-case foreign elements #353

Merged
merged 3 commits into from Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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