Repository navigation
fix: parse innerHTML of an SVG element as SVG (#326) - #346
Open
jonasporto wants to merge 1 commit into
Open
jonasporto wants to merge 1 commit into
jonasporto wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #326
Problem
In an HTML document, setting
innerHTMLon an SVG element parses the markup as if it were HTML content: the/>of a self-closing tag is ignored, so siblings end up nested, and the new children areHTMLElements in the XHTML namespace.Parsing the same markup as part of a document (
<body><svg><circle/><circle/></svg>) is already right: htmlparser2 enters its foreign context when it sees<svg>, andparseFromStringcreates SVG elements under it. The fragment passed toinnerHTMLhas no<svg>around it, so neither happens.Reproduced on 0.18.12 and on
main(3fcf145).Fix
setInnerHtml(esm/mixin/inner-html.js): when the element is an SVG element in an HTML document, the markup is parsed wrapped in<svg>…</svg>and the wrapper's children are used. This reuses the existing SVG path ofparseFromString(no change there, so it does not overlap with #330), and htmlparser2's own foreign-context handling closes/>.The parser points
ownerSVGElementof what it creates at the temporary wrapper, so those are set to the element itself when it is an<svg>, or to itsownerSVGElementotherwise.HTML/XML/SVG documents and non-SVG elements take the same path as before. CJS regenerated with
npm run cjs.Tests
Added to
test/svg/element.js: siblings stay siblings, the children areSVGElements with the rightownerSVGElement, the serialization, andinnerHTMLon a nested<g>. The new test fails without the change (Expected: 2 / Got instead: 1).npm testpasses. Coverage formixin/inner-html.jsis 100%. The overall function figure (99.75%, frominterface/element.jsat 97.01%) is the same onmainwithout this change.