mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 16:56:57 +08:00
unexport SetNodes, add SetHtml tests
This commit is contained in:
@@ -270,25 +270,13 @@ func (s *Selection) ReplaceWithNodes(ns ...*html.Node) *Selection {
|
||||
return s.Remove()
|
||||
}
|
||||
|
||||
// Set nodes to specified nodes.
|
||||
func SetNodes(s *Selection, ns ...*html.Node) *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
for c := n.FirstChild; c != nil; c = n.FirstChild {
|
||||
n.RemoveChild(c)
|
||||
}
|
||||
for _, c := range ns {
|
||||
n.AppendChild(cloneNode(c))
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Sets HTML of selected nodes to specified string.
|
||||
// Set the html content of each element in the selection to specified html string.
|
||||
func (s *Selection) SetHtml(html string) *Selection {
|
||||
return SetNodes(s, parseHtml(html)...)
|
||||
return setHtmlNodes(s, parseHtml(html)...)
|
||||
}
|
||||
|
||||
// Sets text of selected nodes to specified string. Text is HTML escaped
|
||||
// Set the content of each element in the selection to specified content. The
|
||||
// provided text string is escaped.
|
||||
func (s *Selection) SetText(text string) *Selection {
|
||||
return s.SetHtml(html.EscapeString(text))
|
||||
}
|
||||
@@ -504,6 +492,18 @@ func parseHtml(h string) []*html.Node {
|
||||
return nodes
|
||||
}
|
||||
|
||||
func setHtmlNodes(s *Selection, ns ...*html.Node) *Selection {
|
||||
for _, n := range s.Nodes {
|
||||
for c := n.FirstChild; c != nil; c = n.FirstChild {
|
||||
n.RemoveChild(c)
|
||||
}
|
||||
for _, c := range ns {
|
||||
n.AppendChild(cloneNode(c))
|
||||
}
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// Get the first child that is an ElementNode
|
||||
func getFirstChildEl(n *html.Node) *html.Node {
|
||||
c := n.FirstChild
|
||||
|
@@ -281,7 +281,7 @@ func TestReplaceWithHtml(t *testing.T) {
|
||||
func TestSetHtml(t *testing.T) {
|
||||
doc := Doc2Clone()
|
||||
q := doc.Find("#main, #foot")
|
||||
q.SetHtml("<div id=\"replace\">test</div>")
|
||||
q.SetHtml(`<div id="replace">test</div>`)
|
||||
|
||||
assertLength(t, doc.Find("#replace").Nodes, 2)
|
||||
assertLength(t, doc.Find("#main, #foot").Nodes, 2)
|
||||
@@ -293,6 +293,26 @@ func TestSetHtml(t *testing.T) {
|
||||
printSel(t, doc.Selection)
|
||||
}
|
||||
|
||||
func TestSetHtmlNoMatch(t *testing.T) {
|
||||
doc := Doc2Clone()
|
||||
q := doc.Find("#notthere")
|
||||
q.SetHtml(`<div id="replace">test</div>`)
|
||||
|
||||
assertLength(t, doc.Find("#replace").Nodes, 0)
|
||||
|
||||
printSel(t, doc.Selection)
|
||||
}
|
||||
|
||||
func TestSetHtmlEmpty(t *testing.T) {
|
||||
doc := Doc2Clone()
|
||||
q := doc.Find("#main")
|
||||
q.SetHtml(``)
|
||||
|
||||
assertLength(t, doc.Find("#main").Nodes, 1)
|
||||
assertLength(t, doc.Find("#main").Children().Nodes, 0)
|
||||
printSel(t, doc.Selection)
|
||||
}
|
||||
|
||||
func TestSetText(t *testing.T) {
|
||||
doc := Doc2Clone()
|
||||
q := doc.Find("#main, #foot")
|
||||
|
Reference in New Issue
Block a user