add SiblingsFiltered() and tests

This commit is contained in:
Martin Angers
2012-09-04 11:20:20 -04:00
parent b26e37d262
commit ff0cbc32c4
3 changed files with 25 additions and 1 deletions

2
doc.go
View File

@@ -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()

View File

@@ -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 {

View File

@@ -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)
}