From 6ae9223aa117a9a955b123d1501bc9588c1dfd1b Mon Sep 17 00:00:00 2001 From: Pierre-Marie Dartus Date: Thu, 22 Jul 2021 08:27:48 +0200 Subject: [PATCH 1/2] Update setEndLocation to account for tag name with uppercase --- .../extensions/location-info/parser-mixin.js | 3 ++- .../parse5/test/location-info-parser.test.js | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/parse5/lib/extensions/location-info/parser-mixin.js b/packages/parse5/lib/extensions/location-info/parser-mixin.js index e7d3e2da1..c389da380 100644 --- a/packages/parse5/lib/extensions/location-info/parser-mixin.js +++ b/packages/parse5/lib/extensions/location-info/parser-mixin.js @@ -42,7 +42,8 @@ class LocationInfoParserMixin extends Mixin { // NOTE: For cases like

- First 'p' closes without a closing // tag and for cases like

- 'p' closes without a closing tag. - const isClosingEndTag = closingToken.type === Tokenizer.END_TAG_TOKEN && tn === closingToken.tagName; + const isClosingEndTag = + closingToken.type === Tokenizer.END_TAG_TOKEN && tn.toLowerCase() === closingToken.tagName; const endLoc = {}; if (isClosingEndTag) { endLoc.endTag = Object.assign({}, ctLoc); diff --git a/packages/parse5/test/location-info-parser.test.js b/packages/parse5/test/location-info-parser.test.js index 4d4ce6879..732247547 100644 --- a/packages/parse5/test/location-info-parser.test.js +++ b/packages/parse5/test/location-info-parser.test.js @@ -126,6 +126,26 @@ generateTestsForEachTreeAdapter(module.exports, (_test, treeAdapter) => { assert.ok(!location.endTag); }; + + _test['Regression - location.endTag should be available adjusted SVG elements (GH-352)'] = function() { + const html = ''; + + const opts = { + treeAdapter: 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.endTag); + assert.strictEqual( + html.slice(location.startTag.startOffset, location.endTag.endOffset), + '' + ); + }; }); exports['Updating node source code location (GH-314)'] = function() { From 013ae5a9aa95da0b39fcdfe18c9b3afa96e407d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20B=C3=B6hm?= <188768+fb55@users.noreply.github.com> Date: Tue, 15 Feb 2022 16:58:10 +0000 Subject: [PATCH 2/2] fix(parser): Set `location.endTag` for mixed-case foreign elements --- 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', () => {