diff --git a/doc.go b/doc.go index 7c19c49..03edec5 100644 --- a/doc.go +++ b/doc.go @@ -145,7 +145,7 @@ package goquery // - PrevAll() - Tree traversal // - PrevUntil() - Tree traversal // x PushStack() -// - Siblings() - Tree traversal +// x Siblings() - Tree traversal // x Slice() // x Text() - DOM Manipulation // x ToArray() diff --git a/traversal.go b/traversal.go index 56bcb3c..9febae3 100644 --- a/traversal.go +++ b/traversal.go @@ -63,6 +63,7 @@ func (this *Selection) ContentsFiltered(selector string) *Selection { // Children() gets the child elements of each element in the Selection. // It returns a new Selection object containing these elements. func (this *Selection) Children() *Selection { + // TODO : Refactor using siblings? return pushStack(this, getSelectionChildren(this, true)) } @@ -184,6 +185,19 @@ func (this *Selection) Siblings() *Selection { return pushStack(this, getSiblingNodes(this.Nodes)) } +// SiblingsFiltered() gets the siblings of each element in the Selection +// filtered by a selector. It returns a new Selection object containing the +// matched elements. +func (this *Selection) SiblingsFiltered(selector string) *Selection { + // Get the Siblings() unfiltered + n := getSiblingNodes(this.Nodes) + // Create a temporary Selection to filter using winnow + sel := &Selection{n, this.document, nil} + // Filter based on selector + n = winnow(sel, selector, true) + return pushStack(this, n) +} + // Internal implementation to get all parent nodes, stopping at the specified // node (or nil if no stop). func getParentsNodes(nodes []*html.Node, stopSelector string, stopNodes []*html.Node) []*html.Node { diff --git a/traversal_test.go b/traversal_test.go index 102714f..fdccfcf 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -122,3 +122,13 @@ func TestSiblings2(t *testing.T) { sel := Doc().Root.Find(".pvk-gutter").Siblings() AssertLength(t, sel.Nodes, 9) } + +func TestSiblings3(t *testing.T) { + sel := Doc().Root.Find("body>.container-fluid").Siblings() + AssertLength(t, sel.Nodes, 0) +} + +func TestSiblingsFiltered(t *testing.T) { + sel := Doc().Root.Find(".pvk-gutter").SiblingsFiltered(".pvk-content") + AssertLength(t, sel.Nodes, 3) +}