From 72a98acf57c8e972a316cfbf78d1daa5e62f53c8 Mon Sep 17 00:00:00 2001 From: Pierre-Marie Dartus Date: Tue, 15 Feb 2022 18:01:07 +0100 Subject: [PATCH] fix(parser): Set `endTag` loc for mixed-case foreign elements (#353) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Felix Böhm <188768+fb55@users.noreply.github.com> --- packages/parse5/lib/parser/index.ts | 6 +++++- .../lib/parser/parser-location-info.test.ts | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/packages/parse5/lib/parser/index.ts b/packages/parse5/lib/parser/index.ts index 97b528237..840bd4e34 100644 --- a/packages/parse5/lib/parser/index.ts +++ b/packages/parse5/lib/parser/index.ts @@ -3783,7 +3783,11 @@ function endTagInForeignContent(p: Parser, 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; } diff --git a/packages/parse5/lib/parser/parser-location-info.test.ts b/packages/parse5/lib/parser/parser-location-info.test.ts index cd053d6c1..71c02b963 100644 --- a/packages/parse5/lib/parser/parser-location-info.test.ts +++ b/packages/parse5/lib/parser/parser-location-info.test.ts @@ -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 = ''; + + 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), + '' + ); + }); }); describe('location-info-parser', () => {