Repository navigation
Fix SetText storing escaped entity syntax in raw-text elements - #643
Open
draarivpatel-ui wants to merge 1 commit into
Open
draarivpatel-ui wants to merge 1 commit into
draarivpatel-ui wants to merge 1 commit into
Conversation
SetText escaped its input and went through SetHtml, so the stored text was produced by parsing a fragment. Fragment parsing does not decode entities in raw-text elements such as script or style, so the text of those elements ended up holding the escaped entity syntax rather than the supplied string, and serializing the document wrote the corrupted content literally. Store the text as a single text node instead, which is how the DOM textContent setter that this method models behaves. The exact string is preserved in every element, and x/net/html escapes it on serialization where the markup requires it. Fixes PuerkitoBio#636
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.
Summary
SetTextescapes its input and routes throughSetHtml, so the stored content is produced by parsing an HTML fragment. Fragment parsing does not decode entities in raw-text elements (script,style,xmp,iframe,noembed,noframes, ...), so for those elements the text node ends up holding the escaped entity syntax instead of the supplied string, as reported in #636:The corrupted content also leaks into serialized output:
x/net/htmlrenders raw-text element children literally (WHATWG 13.3), so rendering the document wrote the entity syntax verbatim.Fix
Store the text as a single
html.TextNodechild instead of round-tripping through escape + fragment parsing. This matches the DOMtextContentsetter thatSetTextmodels. The exact string is preserved for every element, andx/net/htmlstill applies the escaping the markup requires when serializing: none in raw-text elements, normal escaping elsewhere, soHtml()output for ordinary elements is unchanged (covered by the existingTestSetText).An empty string now leaves the element with no children rather than with whatever an empty fragment parse produced (nothing), which is unchanged behavior.
Test plan
TestSetTextInRawTextElement: exact round-trip throughText()forscript,styleanddiv; literal serialization in the renderedscriptelement; escaping preserved in the serializeddiv.TestSetTextEmptyfor the empty-string edge case.go test ./...passes.Fixes #636