unexport SetNodes, add SetHtml tests

This commit is contained in:
Martin Angers
2017-02-12 11:34:10 -05:00
parent 755fd592bb
commit 46c32cef0e
2 changed files with 37 additions and 17 deletions

View File

@@ -270,25 +270,13 @@ func (s *Selection) ReplaceWithNodes(ns ...*html.Node) *Selection {
return s.Remove() return s.Remove()
} }
// Set nodes to specified nodes. // Set the html content of each element in the selection to specified html string.
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.
func (s *Selection) SetHtml(html string) *Selection { 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 { func (s *Selection) SetText(text string) *Selection {
return s.SetHtml(html.EscapeString(text)) return s.SetHtml(html.EscapeString(text))
} }
@@ -504,6 +492,18 @@ func parseHtml(h string) []*html.Node {
return nodes 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 // Get the first child that is an ElementNode
func getFirstChildEl(n *html.Node) *html.Node { func getFirstChildEl(n *html.Node) *html.Node {
c := n.FirstChild c := n.FirstChild

View File

@@ -281,7 +281,7 @@ func TestReplaceWithHtml(t *testing.T) {
func TestSetHtml(t *testing.T) { func TestSetHtml(t *testing.T) {
doc := Doc2Clone() doc := Doc2Clone()
q := doc.Find("#main, #foot") 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("#replace").Nodes, 2)
assertLength(t, doc.Find("#main, #foot").Nodes, 2) assertLength(t, doc.Find("#main, #foot").Nodes, 2)
@@ -293,6 +293,26 @@ func TestSetHtml(t *testing.T) {
printSel(t, doc.Selection) 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) { func TestSetText(t *testing.T) {
doc := Doc2Clone() doc := Doc2Clone()
q := doc.Find("#main, #foot") q := doc.Find("#main, #foot")