manipulation: Add Empty() function.

This commit is contained in:
Andrew Stone
2014-10-24 11:49:05 -04:00
parent b800859dea
commit b7dabffd85
2 changed files with 22 additions and 0 deletions
+15
View File
@@ -131,6 +131,21 @@ func (s *Selection) Clone() *Selection {
return ns
}
// Remove all children nodes from the set of matched elements.
// Returns the children nodes in a new Selection on the current Selection stack.
func (s *Selection) Empty() *Selection {
nodes := make([]*html.Node, 0)
for _, n := range s.Nodes {
for c := n.FirstChild; c != nil; c = n.FirstChild {
n.RemoveChild(c)
nodes = append(nodes, c)
}
}
return pushStack(s, nodes)
}
// Remove the set of matched elements from the document.
// Returns the same selection, now consisting of nodes not in the document.
func (s *Selection) Remove() *Selection {
+7
View File
@@ -45,6 +45,13 @@ func TestAppendHtml(t *testing.T) {
AssertLength(t, doc.Find("strong").Nodes, 14)
}
func TestEmpty(t *testing.T) {
doc := Doc2Clone()
s := doc.Find("#main").Empty()
AssertLength(t, doc.Find("#main").Children().Nodes, 0)
AssertLength(t, s.Filter("div").Nodes, 6)
}
func TestRemove(t *testing.T) {
doc := Doc2Clone()