From af4a62048b28cb113123d4f7384c790dc5ae7932 Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Tue, 4 Sep 2012 13:44:20 -0400 Subject: [PATCH] add PrevAll() and PrevAllFiltered() with tests --- traversal.go | 13 +++++++++++++ traversal_test.go | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/traversal.go b/traversal.go index 6e2a1fc..d89d3b1 100644 --- a/traversal.go +++ b/traversal.go @@ -203,6 +203,19 @@ func (this *Selection) PrevFiltered(selector string) *Selection { return filterAndPush(this, getSiblingNodes(this.Nodes, siblingPrev), selector) } +// PrevAll() gets all the preceding siblings of each element in the +// Selection. It returns a new Selection object containing the matched elements. +func (this *Selection) PrevAll() *Selection { + return pushStack(this, getSiblingNodes(this.Nodes, siblintPrevAll)) +} + +// PrevAllFiltered() gets all the preceding siblings of each element in the +// Selection filtered by a selector. It returns a new Selection object +// containing the matched elements. +func (this *Selection) PrevAllFiltered(selector string) *Selection { + return filterAndPush(this, getSiblingNodes(this.Nodes, siblintPrevAll), selector) +} + // Filter and push filters the nodes based on a selector, and pushes the results // on the stack, with the srcSel as previous selection. func filterAndPush(srcSel *Selection, nodes []*html.Node, selector string) *Selection { diff --git a/traversal_test.go b/traversal_test.go index 40b8bf9..1d1c69e 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -178,3 +178,43 @@ func TestPrevFiltered(t *testing.T) { sel := Doc().Root.Find(".row-fluid").PrevFiltered(".row-fluid") AssertLength(t, sel.Nodes, 5) } + +func TestNextAll(t *testing.T) { + sel := Doc().Root.Find("#cf2 div:nth-child(1)").NextAll() + AssertLength(t, sel.Nodes, 3) +} + +func TestNextAll2(t *testing.T) { + sel := Doc().Root.Find("div[ng-cloak]").NextAll() + AssertLength(t, sel.Nodes, 1) +} + +func TestNextAllNone(t *testing.T) { + sel := Doc().Root.Find(".footer").NextAll() + AssertLength(t, sel.Nodes, 0) +} + +func TestNextAllFiltered(t *testing.T) { + sel := Doc().Root.Find("#cf2 .row-fluid").NextAllFiltered("[ng-cloak]") + AssertLength(t, sel.Nodes, 2) +} + +func TestNextAllFiltered2(t *testing.T) { + sel := Doc().Root.Find(".close").NextAllFiltered("h4") + AssertLength(t, sel.Nodes, 1) +} + +func TestPrevAll(t *testing.T) { + sel := Doc().Root.Find("[ng-view]").PrevAll() + AssertLength(t, sel.Nodes, 2) +} + +func TestPrevAll2(t *testing.T) { + sel := Doc().Root.Find(".pvk-gutter").PrevAll() + AssertLength(t, sel.Nodes, 6) +} + +func TestPrevAllFiltered(t *testing.T) { + sel := Doc().Root.Find(".pvk-gutter").PrevAllFiltered(".pvk-content") + AssertLength(t, sel.Nodes, 3) +}