diff --git a/manipulation.go b/manipulation.go index 214785f..699aa79 100644 --- a/manipulation.go +++ b/manipulation.go @@ -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 { diff --git a/manipulation_test.go b/manipulation_test.go index cd3d609..85c34a6 100644 --- a/manipulation_test.go +++ b/manipulation_test.go @@ -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()