mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 16:56:57 +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
|
// - PrevAll() - Tree traversal
|
||||||
// - PrevUntil() - Tree traversal
|
// - PrevUntil() - Tree traversal
|
||||||
// x PushStack()
|
// x PushStack()
|
||||||
// - Siblings() - Tree traversal
|
// x Siblings() - Tree traversal
|
||||||
// x Slice()
|
// x Slice()
|
||||||
// x Text() - DOM Manipulation
|
// x Text() - DOM Manipulation
|
||||||
// x ToArray()
|
// 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.
|
// Children() gets the child elements of each element in the Selection.
|
||||||
// It returns a new Selection object containing these elements.
|
// It returns a new Selection object containing these elements.
|
||||||
func (this *Selection) Children() *Selection {
|
func (this *Selection) Children() *Selection {
|
||||||
|
// TODO : Refactor using siblings?
|
||||||
return pushStack(this, getSelectionChildren(this, true))
|
return pushStack(this, getSelectionChildren(this, true))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,6 +185,19 @@ func (this *Selection) Siblings() *Selection {
|
|||||||
return pushStack(this, getSiblingNodes(this.Nodes))
|
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
|
// Internal implementation to get all parent nodes, stopping at the specified
|
||||||
// node (or nil if no stop).
|
// node (or nil if no stop).
|
||||||
func getParentsNodes(nodes []*html.Node, stopSelector string, stopNodes []*html.Node) []*html.Node {
|
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()
|
sel := Doc().Root.Find(".pvk-gutter").Siblings()
|
||||||
AssertLength(t, sel.Nodes, 9)
|
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