mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-10-05 16:56:57 +08:00
add PrevFilteredUntil...() with tests
This commit is contained in:
@@ -78,9 +78,8 @@ Taken from example_test.go:
|
|||||||
|
|
||||||
## TODOs
|
## TODOs
|
||||||
|
|
||||||
* Implement PrevFilteredUntil...().
|
|
||||||
* Refactor Next...() and Prev...() to use the new NextSibling/PrevSibling fields.
|
|
||||||
* Benchmarks so that future changes have a baseline to compare to.
|
* Benchmarks so that future changes have a baseline to compare to.
|
||||||
|
* Refactor Next...() and Prev...() to use the new NextSibling/PrevSibling fields.
|
||||||
* Add jQuery's `Closest()`? Other missing functions?
|
* Add jQuery's `Closest()`? Other missing functions?
|
||||||
* Support negative indices in `Slice()`, like jQuery.
|
* Support negative indices in `Slice()`, like jQuery.
|
||||||
|
|
||||||
|
26
traversal.go
26
traversal.go
@@ -298,6 +298,32 @@ func (this *Selection) NextFilteredUntilNodes(filterSelector string, nodes ...*h
|
|||||||
"", nodes), filterSelector)
|
"", nodes), filterSelector)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PrevFilteredUntil() is like PrevUntil(), 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) PrevFilteredUntil(filterSelector string, untilSelector string) *Selection {
|
||||||
|
return filterAndPush(this, getSiblingNodes(this.Nodes, siblingPrevUntil,
|
||||||
|
untilSelector, nil), filterSelector)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrevFilteredUntilSelection() is like PrevUntilSelection(), 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) PrevFilteredUntilSelection(filterSelector string, sel *Selection) *Selection {
|
||||||
|
if sel == nil {
|
||||||
|
return this.PrevFiltered(filterSelector)
|
||||||
|
}
|
||||||
|
return this.PrevFilteredUntilNodes(filterSelector, sel.Nodes...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrevFilteredUntilNodes() is like PrevUntilNodes(), 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) PrevFilteredUntilNodes(filterSelector string, nodes ...*html.Node) *Selection {
|
||||||
|
return filterAndPush(this, getSiblingNodes(this.Nodes, siblingPrevUntil,
|
||||||
|
"", nodes), filterSelector)
|
||||||
|
}
|
||||||
|
|
||||||
// Filter and push filters the nodes based on a selector, and pushes the results
|
// Filter and push filters the nodes based on a selector, and pushes the results
|
||||||
// on the stack, with the srcSel as previous selection.
|
// on the stack, with the srcSel as previous selection.
|
||||||
func filterAndPush(srcSel *Selection, nodes []*html.Node, selector string) *Selection {
|
func filterAndPush(srcSel *Selection, nodes []*html.Node, selector string) *Selection {
|
||||||
|
@@ -534,3 +534,45 @@ func TestNextFilteredUntilNodesRollback(t *testing.T) {
|
|||||||
sel3 := sel.NextFilteredUntilNodes(".odd", sel2.Nodes...).End()
|
sel3 := sel.NextFilteredUntilNodes(".odd", sel2.Nodes...).End()
|
||||||
AssertEqual(t, sel, sel3)
|
AssertEqual(t, sel, sel3)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestPrevFilteredUntil(t *testing.T) {
|
||||||
|
sel := Doc2().Root.Find(".five").PrevFilteredUntil(".odd", ".one")
|
||||||
|
AssertLength(t, sel.Nodes, 4)
|
||||||
|
AssertSelectionIs(t, sel, "#n4", "#n2", "#nf4", "#nf2")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrevFilteredUntilRollback(t *testing.T) {
|
||||||
|
sel := Doc2().Root.Find(".four")
|
||||||
|
sel2 := sel.PrevFilteredUntil(".odd", ".one").End()
|
||||||
|
AssertEqual(t, sel, sel2)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrevFilteredUntilSelection(t *testing.T) {
|
||||||
|
sel := Doc2().Root.Find(".odd")
|
||||||
|
sel2 := Doc2().Root.Find(".two")
|
||||||
|
sel = sel.PrevFilteredUntilSelection(".odd", sel2)
|
||||||
|
AssertLength(t, sel.Nodes, 2)
|
||||||
|
AssertSelectionIs(t, sel, "#n4", "#nf4")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrevFilteredUntilSelectionRollback(t *testing.T) {
|
||||||
|
sel := Doc2().Root.Find(".even")
|
||||||
|
sel2 := Doc2().Root.Find(".five")
|
||||||
|
sel3 := sel.PrevFilteredUntilSelection(".even", sel2).End()
|
||||||
|
AssertEqual(t, sel, sel3)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrevFilteredUntilNodes(t *testing.T) {
|
||||||
|
sel := Doc2().Root.Find(".even")
|
||||||
|
sel2 := Doc2().Root.Find(".four")
|
||||||
|
sel = sel.PrevFilteredUntilNodes(".odd", sel2.Nodes...)
|
||||||
|
AssertLength(t, sel.Nodes, 2)
|
||||||
|
AssertSelectionIs(t, sel, "#n2", "#nf2")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPrevFilteredUntilNodesRollback(t *testing.T) {
|
||||||
|
sel := Doc2().Root.Find(".even")
|
||||||
|
sel2 := Doc2().Root.Find(".four")
|
||||||
|
sel3 := sel.PrevFilteredUntilNodes(".odd", sel2.Nodes...).End()
|
||||||
|
AssertEqual(t, sel, sel3)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user