Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions manipulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,9 +331,19 @@ func (s *Selection) SetHtml(htmlStr string) *Selection {
}

// SetText sets the content of each element in the selection to specified content.
// The provided text string is escaped.
// The text is stored as a single text node, so it is preserved exactly as
// provided, including in raw-text elements such as script or style. It is
// escaped as needed when the document is serialized back to HTML.
func (s *Selection) SetText(text string) *Selection {
return s.SetHtml(html.EscapeString(text))
for _, node := range s.Nodes {
for c := node.FirstChild; c != nil; c = node.FirstChild {
node.RemoveChild(c)
}
if text != "" {
node.AppendChild(&html.Node{Type: html.TextNode, Data: text})
}
}
return s
}

// Unwrap removes the parents of the set of matched elements, leaving the matched
Expand Down
53 changes: 53 additions & 0 deletions manipulation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package goquery

import (
"log"
"strings"
"testing"
)

Expand Down Expand Up @@ -488,6 +489,58 @@ func TestSetText(t *testing.T) {
printSel(t, doc.Selection)
}

func TestSetTextInRawTextElement(t *testing.T) {
doc, err := NewDocumentFromReader(strings.NewReader(
`<html><head><script type="application/json">old</script><style>old</style></head><body><div>old</div></body></html>`))
if err != nil {
t.Fatal(err)
}

repl := `{"label":"<b>&"}`
doc.Find("script").SetText(repl)
doc.Find("style").SetText(repl)
doc.Find("div").SetText(repl)

for _, sel := range []string{"script", "style", "div"} {
if got := doc.Find(sel).Text(); got != repl {
t.Errorf("%s: expected text to be %q, found %q", sel, repl, got)
}
}

// In raw-text elements the content is serialized literally when the
// element itself is rendered.
h, err := OuterHtml(doc.Find("script"))
if err != nil {
t.Fatal(err)
}
want := `<script type="application/json">` + repl + `</script>`
if h != want {
t.Errorf("script: expected outer html to be %q, found %q", want, h)
}

// In ordinary elements the text node is escaped on serialization.
h, err = doc.Find("div").Html()
if err != nil {
t.Fatal(err)
}
esc := "{&#34;label&#34;:&#34;&lt;b&gt;&amp;&#34;}"
if h != esc {
t.Errorf("div: expected html to be %q, found %q", esc, h)
}
}

func TestSetTextEmpty(t *testing.T) {
doc := Doc2Clone()
sel := doc.Find("#main")
sel.SetText("")
if sel.Text() != "" {
t.Errorf("expected empty text, found %q", sel.Text())
}
if sel.Nodes[0].FirstChild != nil {
t.Error("expected no children after SetText with an empty string")
}
}

func TestReplaceWithSelection(t *testing.T) {
doc := Doc2Clone()
sel := doc.Find("#nf6").ReplaceWithSelection(doc.Find("#nf5"))
Expand Down