Skip to content

Fix SetText storing escaped entity syntax in raw-text elements - #643

Open
draarivpatel-ui wants to merge 1 commit into
PuerkitoBio:masterfrom
draarivpatel-ui:fix-settext-raw-text-elements
Open

draarivpatel-ui wants to merge 1 commit into
PuerkitoBio:masterfrom
draarivpatel-ui:fix-settext-raw-text-elements

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Summary

SetText escapes its input and routes through SetHtml, 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:

doc, _ := goquery.NewDocumentFromReader(strings.NewReader(`<script type="application/json">old</script>`))
s := doc.Find("script")
s.SetText(`{"label":"<b>&"}`)
s.Text() // before: {&#34;label&#34;:&#34;&lt;b&gt;&amp;&#34;}
         // after:  {"label":"<b>&"}

The corrupted content also leaks into serialized output: x/net/html renders 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.TextNode child instead of round-tripping through escape + fragment parsing. This matches the DOM textContent setter that SetText models. The exact string is preserved for every element, and x/net/html still applies the escaping the markup requires when serializing: none in raw-text elements, normal escaping elsewhere, so Html() output for ordinary elements is unchanged (covered by the existing TestSetText).

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

  • New TestSetTextInRawTextElement: exact round-trip through Text() for script, style and div; literal serialization in the rendered script element; escaping preserved in the serialized div.
  • New TestSetTextEmpty for the empty-string edge case.
  • go test ./... passes.

Fixes #636

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
Copilot AI balanced review requested due to automatic review settings November 25, 2026 23:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SetText stores escaped entity syntax in raw-text elements

2 participants