Skip to content

Commit 46c32ce

Browse files
committed
unexport SetNodes, add SetHtml tests
1 parent 755fd59 commit 46c32ce

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

Diff for: manipulation.go

+16-16
Original file line numberDiff line numberDiff line change
@@ -270,25 +270,13 @@ func (s *Selection) ReplaceWithNodes(ns ...*html.Node) *Selection {
270270
return s.Remove()
271271
}
272272

273-
// Set nodes to specified nodes.
274-
func SetNodes(s *Selection, ns ...*html.Node) *Selection {
275-
for _, n := range s.Nodes {
276-
for c := n.FirstChild; c != nil; c = n.FirstChild {
277-
n.RemoveChild(c)
278-
}
279-
for _, c := range ns {
280-
n.AppendChild(cloneNode(c))
281-
}
282-
}
283-
return s
284-
}
285-
286-
// Sets HTML of selected nodes to specified string.
273+
// Set the html content of each element in the selection to specified html string.
287274
func (s *Selection) SetHtml(html string) *Selection {
288-
return SetNodes(s, parseHtml(html)...)
275+
return setHtmlNodes(s, parseHtml(html)...)
289276
}
290277

291-
// Sets text of selected nodes to specified string. Text is HTML escaped
278+
// Set the content of each element in the selection to specified content. The
279+
// provided text string is escaped.
292280
func (s *Selection) SetText(text string) *Selection {
293281
return s.SetHtml(html.EscapeString(text))
294282
}
@@ -504,6 +492,18 @@ func parseHtml(h string) []*html.Node {
504492
return nodes
505493
}
506494

495+
func setHtmlNodes(s *Selection, ns ...*html.Node) *Selection {
496+
for _, n := range s.Nodes {
497+
for c := n.FirstChild; c != nil; c = n.FirstChild {
498+
n.RemoveChild(c)
499+
}
500+
for _, c := range ns {
501+
n.AppendChild(cloneNode(c))
502+
}
503+
}
504+
return s
505+
}
506+
507507
// Get the first child that is an ElementNode
508508
func getFirstChildEl(n *html.Node) *html.Node {
509509
c := n.FirstChild

Diff for: manipulation_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func TestReplaceWithHtml(t *testing.T) {
281281
func TestSetHtml(t *testing.T) {
282282
doc := Doc2Clone()
283283
q := doc.Find("#main, #foot")
284-
q.SetHtml("<div id=\"replace\">test</div>")
284+
q.SetHtml(`<div id="replace">test</div>`)
285285

286286
assertLength(t, doc.Find("#replace").Nodes, 2)
287287
assertLength(t, doc.Find("#main, #foot").Nodes, 2)
@@ -293,6 +293,26 @@ func TestSetHtml(t *testing.T) {
293293
printSel(t, doc.Selection)
294294
}
295295

296+
func TestSetHtmlNoMatch(t *testing.T) {
297+
doc := Doc2Clone()
298+
q := doc.Find("#notthere")
299+
q.SetHtml(`<div id="replace">test</div>`)
300+
301+
assertLength(t, doc.Find("#replace").Nodes, 0)
302+
303+
printSel(t, doc.Selection)
304+
}
305+
306+
func TestSetHtmlEmpty(t *testing.T) {
307+
doc := Doc2Clone()
308+
q := doc.Find("#main")
309+
q.SetHtml(``)
310+
311+
assertLength(t, doc.Find("#main").Nodes, 1)
312+
assertLength(t, doc.Find("#main").Children().Nodes, 0)
313+
printSel(t, doc.Selection)
314+
}
315+
296316
func TestSetText(t *testing.T) {
297317
doc := Doc2Clone()
298318
q := doc.Find("#main, #foot")

0 commit comments

Comments
 (0)