mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-09-29 22:22:06 +08:00
add ParentsFilteredUntil...() with tests
This commit is contained in:
6
doc.go
6
doc.go
@@ -138,9 +138,9 @@ package goquery
|
||||
// - NextAll() - Tree traversal
|
||||
// - NextUntil() - Tree traversal
|
||||
// x Not()
|
||||
// - Parent() - Tree traversal
|
||||
// - Parents() - Tree traversal
|
||||
// - ParentsUntil() - Tree traversal
|
||||
// x Parent() - Tree traversal
|
||||
// x Parents() - Tree traversal
|
||||
// x ParentsUntil() - Tree traversal
|
||||
// - Prev() - Tree traversal
|
||||
// - PrevAll() - Tree traversal
|
||||
// - PrevUntil() - Tree traversal
|
||||
|
38
traversal.go
38
traversal.go
@@ -140,6 +140,44 @@ func (this *Selection) ParentsUntilNodes(nodes ...*html.Node) *Selection {
|
||||
return pushStack(this, getParentsNodes(this.Nodes, "", nodes))
|
||||
}
|
||||
|
||||
// ParentsFilteredUntil() is like ParentsUntil(), with the option to filter the
|
||||
// results based on a selector string. It returns a new Selection
|
||||
// object containing the matched elements.
|
||||
func (this *Selection) ParentsFilteredUntil(filterSelector string, untilSelector string) *Selection {
|
||||
|
||||
// Get the ParentsUntil() unfiltered
|
||||
nodes := getParentsNodes(this.Nodes, untilSelector, nil)
|
||||
// Create a temporary Selection to filter using winnow
|
||||
sel := &Selection{nodes, this.document, nil}
|
||||
// Filter based on selector
|
||||
nodes = winnow(sel, filterSelector, true)
|
||||
return pushStack(this, nodes)
|
||||
}
|
||||
|
||||
// ParentsFilteredUntilSelection() is like ParentsUntilSelection(), with the
|
||||
// option to filter the results based on a selector string. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (this *Selection) ParentsFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {
|
||||
if sel == nil {
|
||||
return this.ParentsFiltered(filterSelector)
|
||||
}
|
||||
return this.ParentsFilteredUntilNodes(filterSelector, sel.Nodes...)
|
||||
}
|
||||
|
||||
// ParentsFilteredUntilNodes() is like ParentsUntilNodes(), with the
|
||||
// option to filter the results based on a selector string. It returns a new
|
||||
// Selection object containing the matched elements.
|
||||
func (this *Selection) ParentsFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection {
|
||||
|
||||
// Get the ParentsUntilNodes() unfiltered
|
||||
n := getParentsNodes(this.Nodes, "", nodes)
|
||||
// Create a temporary Selection to filter using winnow
|
||||
sel := &Selection{n, this.document, nil}
|
||||
// Filter based on selector
|
||||
n = winnow(sel, filterSelector, 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 {
|
||||
|
@@ -93,3 +93,22 @@ func TestParentsUntilNodes(t *testing.T) {
|
||||
sel = sel.ParentsUntilNodes(sel2.Nodes...)
|
||||
AssertLength(t, sel.Nodes, 2)
|
||||
}
|
||||
|
||||
func TestParentsFilteredUntil(t *testing.T) {
|
||||
sel := Doc().Root.Find(".container-fluid").ParentsFilteredUntil(".pvk-content", "body")
|
||||
AssertLength(t, sel.Nodes, 2)
|
||||
}
|
||||
|
||||
func TestParentsFilteredUntilSelection(t *testing.T) {
|
||||
sel := Doc().Root.Find(".container-fluid")
|
||||
sel2 := Doc().Root.Find(".row-fluid")
|
||||
sel = sel.ParentsFilteredUntilSelection("div", sel2)
|
||||
AssertLength(t, sel.Nodes, 3)
|
||||
}
|
||||
|
||||
func TestParentsFilteredUntilNodes(t *testing.T) {
|
||||
sel := Doc().Root.Find(".container-fluid")
|
||||
sel2 := Doc().Root.Find(".row-fluid")
|
||||
sel = sel.ParentsFilteredUntilNodes("body", sel2.Nodes...)
|
||||
AssertLength(t, sel.Nodes, 1)
|
||||
}
|
||||
|
Reference in New Issue
Block a user