From 5da274a53d285efe08e2dc21fbb0f1fd65054c01 Mon Sep 17 00:00:00 2001 From: Martin Angers Date: Thu, 6 Sep 2012 14:40:15 -0400 Subject: [PATCH] add NextUntil...() with tests --- traversal.go | 26 ++++++++++++++++++ traversal_test.go | 70 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) diff --git a/traversal.go b/traversal.go index 5b6ef49..474dce2 100644 --- a/traversal.go +++ b/traversal.go @@ -220,6 +220,32 @@ func (this *Selection) PrevAllFiltered(selector string) *Selection { return filterAndPush(this, getSiblingNodes(this.Nodes, siblingPrevAll, "", nil), selector) } +// NextUntil() gets all following siblings of each element up to but not +// including the element matched by the selector. It returns a new Selection +// object containing the matched elements. +func (this *Selection) NextUntil(selector string) *Selection { + return pushStack(this, getSiblingNodes(this.Nodes, siblingNextUntil, + selector, nil)) +} + +// NextUntilSelection() gets all following siblings of each element up to but not +// including the element matched by the Selection. It returns a new Selection +// object containing the matched elements. +func (this *Selection) NextUntilSelection(sel *Selection) *Selection { + if sel == nil { + return this.NextAll() + } + return this.NextUntilNodes(sel.Nodes...) +} + +// NextUntilNodes() gets all following siblings of each element up to but not +// including the element matched by the nodes. It returns a new Selection +// object containing the matched elements. +func (this *Selection) NextUntilNodes(nodes ...*html.Node) *Selection { + return pushStack(this, getSiblingNodes(this.Nodes, siblingNextUntil, + "", nodes)) +} + // PrevUntil() gets all preceding siblings of each element up to but not // including the element matched by the selector. It returns a new Selection // object containing the matched elements. diff --git a/traversal_test.go b/traversal_test.go index 00471da..8961acd 100644 --- a/traversal_test.go +++ b/traversal_test.go @@ -407,6 +407,76 @@ func TestPrevAllFilteredRollback(t *testing.T) { AssertEqual(t, sel, sel2) } +func TestNextUntil(t *testing.T) { + sel := Doc().Root.Find(".alert a").NextUntil("p") + AssertLength(t, sel.Nodes, 1) + if !sel.Eq(0).Is("h4") { + t.Errorf("Expected node 0 to be h4, found %+v.", sel.Get(0)) + } +} + +func TestNextUntil2(t *testing.T) { + sel := Doc().Root.Find("#cf2-1").NextUntil("[ng-cloak]") + AssertLength(t, sel.Nodes, 1) + if !sel.Eq(0).Is("#cf2-2") { + t.Errorf("Expected node 0 to be cf2-2, found %+v.", sel.Get(0)) + } +} + +func TestNextUntilOrder(t *testing.T) { + sel := Doc().Root.Find("#cf2-1").NextUntil("#cf2-4") + AssertLength(t, sel.Nodes, 2) + if !sel.Eq(0).Is("#cf2-2") { + t.Errorf("Expected node 0 to be cf2-2, found %+v.", sel.Get(0)) + } + if !sel.Eq(1).Is("#cf2-3") { + t.Errorf("Expected node 1 to be cf2-3, found %+v.", sel.Get(1)) + } +} + +func TestNextUntilRollback(t *testing.T) { + sel := Doc().Root.Find("#cf2-1") + sel2 := sel.PrevUntil("#cf2-4").End() + AssertEqual(t, sel, sel2) +} + +func TestNextUntilSelection(t *testing.T) { + sel := Doc2().Root.Find("#n2") + sel2 := Doc2().Root.Find("#n4") + sel2 = sel.NextUntilSelection(sel2) + AssertLength(t, sel2.Nodes, 1) + if !sel2.Eq(0).Is("#n3") { + t.Errorf("Expected node 0 to be n3, found %+v.", sel2.Get(0)) + } +} + +func TestNextUntilSelectionRollback(t *testing.T) { + sel := Doc2().Root.Find("#n2") + sel2 := Doc2().Root.Find("#n4") + sel2 = sel.NextUntilSelection(sel2).End() + AssertEqual(t, sel, sel2) +} + +func TestNextUntilNodes(t *testing.T) { + sel := Doc2().Root.Find("#n2") + sel2 := Doc2().Root.Find("#n5") + sel2 = sel.NextUntilNodes(sel2.Nodes...) + AssertLength(t, sel2.Nodes, 2) + if !sel2.Eq(0).Is("#n3") { + t.Errorf("Expected node 0 to be n3, found %+v.", sel2.Get(0)) + } + if !sel2.Eq(1).Is("#n4") { + t.Errorf("Expected node 1 to be n4, found %+v.", sel2.Get(1)) + } +} + +func TestNextUntilNodesRollback(t *testing.T) { + sel := Doc2().Root.Find("#n2") + sel2 := Doc2().Root.Find("#n5") + sel2 = sel.NextUntilNodes(sel2.Nodes...).End() + AssertEqual(t, sel, sel2) +} + func TestPrevUntil(t *testing.T) { sel := Doc().Root.Find(".alert p").PrevUntil("a") AssertLength(t, sel.Nodes, 1)