mirror of
https://github.com/PuerkitoBio/goquery.git
synced 2026-10-19 16:15:33 +00:00
manipulation: Add Empty() function.
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user