mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-06 01:07:06 +08:00
add SiblingsFiltered() and tests
This commit is contained in:
2
doc.go
2
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()
|
||||
|
14
traversal.go
14
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 {
|
||||
|
@@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user