Skip to content

Commit

Permalink
remove Document.Root, Document is itself a Selection (using embedded …
Browse files Browse the repository at this point in the history
…struct)
  • Loading branch information
mna committed Oct 14, 2012
1 parent cbddb7a commit 6669ef0
Show file tree
Hide file tree
Showing 20 changed files with 339 additions and 339 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -24,7 +24,7 @@ To run benchmarks, run this command in goquery's source directory:

## Changelog

* **v0.2.0** : Add support for negative indices in Slice(). *Upcoming* : add jQuery's Closest() method.
* **v0.2.0** : (*in progress on master branch*) Add support for negative indices in Slice(). *Upcoming* : add jQuery's Closest() method.
* **v0.1.1** : Add benchmarks to use as baseline for refactorings, refactor Next...() and Prev...() methods to use the new html package's linked list features (Next/PrevSibling, FirstChild). Good performance boost (40+% in some cases).
* **v0.1.0** : Initial release. See [TODOs](#a1) for a list of upcoming features.

Expand Down
2 changes: 1 addition & 1 deletion array.go
Expand Up @@ -63,7 +63,7 @@ func (this *Selection) Index() int {
// not found.
func (this *Selection) IndexSelector(selector string) int {
if len(this.Nodes) > 0 {
sel := this.document.Root.Find(selector)
sel := this.document.Find(selector)
return indexInSlice(sel.Nodes, this.Nodes[0])
}
return -1
Expand Down
52 changes: 26 additions & 26 deletions array_test.go
Expand Up @@ -5,112 +5,112 @@ import (
)

func TestFirst(t *testing.T) {
sel := Doc().Root.Find(".pvk-content").First()
sel := Doc().Find(".pvk-content").First()
AssertLength(t, sel.Nodes, 1)
}

func TestFirstEmpty(t *testing.T) {
defer AssertPanic(t)
Doc().Root.Find(".pvk-zzcontentzz").First()
Doc().Find(".pvk-zzcontentzz").First()
}

func TestFirstRollback(t *testing.T) {
sel := Doc().Root.Find(".pvk-content")
sel := Doc().Find(".pvk-content")
sel2 := sel.First().End()
AssertEqual(t, sel, sel2)
}

func TestLast(t *testing.T) {
sel := Doc().Root.Find(".pvk-content").Last()
sel := Doc().Find(".pvk-content").Last()
AssertLength(t, sel.Nodes, 1)

// Should contain Footer
foot := Doc().Root.Find(".footer")
foot := Doc().Find(".footer")
if !sel.Contains(foot.Nodes[0]) {
t.Error("Last .pvk-content should contain .footer.")
}
}

func TestLastRollback(t *testing.T) {
sel := Doc().Root.Find(".pvk-content")
sel := Doc().Find(".pvk-content")
sel2 := sel.Last().End()
AssertEqual(t, sel, sel2)
}

func TestEq(t *testing.T) {
sel := Doc().Root.Find(".pvk-content").Eq(1)
sel := Doc().Find(".pvk-content").Eq(1)
AssertLength(t, sel.Nodes, 1)
}

func TestEqNegative(t *testing.T) {
sel := Doc().Root.Find(".pvk-content").Eq(-1)
sel := Doc().Find(".pvk-content").Eq(-1)
AssertLength(t, sel.Nodes, 1)

// Should contain Footer
foot := Doc().Root.Find(".footer")
foot := Doc().Find(".footer")
if !sel.Contains(foot.Nodes[0]) {
t.Error("Index -1 of .pvk-content should contain .footer.")
}
}

func TestEqRollback(t *testing.T) {
sel := Doc().Root.Find(".pvk-content")
sel := Doc().Find(".pvk-content")
sel2 := sel.Eq(1).End()
AssertEqual(t, sel, sel2)
}

func TestSlice(t *testing.T) {
sel := Doc().Root.Find(".pvk-content").Slice(0, 2)
sel := Doc().Find(".pvk-content").Slice(0, 2)

AssertLength(t, sel.Nodes, 2)
}

func TestSliceOutOfBounds(t *testing.T) {
defer AssertPanic(t)
Doc().Root.Find(".pvk-content").Slice(2, 12)
Doc().Find(".pvk-content").Slice(2, 12)
}

func TestNegativeSliceStart(t *testing.T) {
sel := Doc().Root.Find(".container-fluid").Slice(-2, 3)
sel := Doc().Find(".container-fluid").Slice(-2, 3)
AssertLength(t, sel.Nodes, 1)
AssertSelectionIs(t, sel.Eq(0), "#cf3")
}

func TestNegativeSliceEnd(t *testing.T) {
sel := Doc().Root.Find(".container-fluid").Slice(1, -1)
sel := Doc().Find(".container-fluid").Slice(1, -1)
AssertLength(t, sel.Nodes, 2)
AssertSelectionIs(t, sel.Eq(0), "#cf2")
AssertSelectionIs(t, sel.Eq(1), "#cf3")
}

func TestNegativeSliceBoth(t *testing.T) {
sel := Doc().Root.Find(".container-fluid").Slice(-3, -1)
sel := Doc().Find(".container-fluid").Slice(-3, -1)
AssertLength(t, sel.Nodes, 2)
AssertSelectionIs(t, sel.Eq(0), "#cf2")
AssertSelectionIs(t, sel.Eq(1), "#cf3")
}

func TestNegativeSliceOutOfBounds(t *testing.T) {
defer AssertPanic(t)
Doc().Root.Find(".container-fluid").Slice(-12, -7)
Doc().Find(".container-fluid").Slice(-12, -7)
}

func TestSliceRollback(t *testing.T) {
sel := Doc().Root.Find(".pvk-content")
sel := Doc().Find(".pvk-content")
sel2 := sel.Slice(0, 2).End()
AssertEqual(t, sel, sel2)
}

func TestGet(t *testing.T) {
sel := Doc().Root.Find(".pvk-content")
sel := Doc().Find(".pvk-content")
node := sel.Get(1)
if sel.Nodes[1] != node {
t.Errorf("Expected node %v to be %v.", node, sel.Nodes[1])
}
}

func TestGetNegative(t *testing.T) {
sel := Doc().Root.Find(".pvk-content")
sel := Doc().Find(".pvk-content")
node := sel.Get(-3)
if sel.Nodes[0] != node {
t.Errorf("Expected node %v to be %v.", node, sel.Nodes[0])
Expand All @@ -119,41 +119,41 @@ func TestGetNegative(t *testing.T) {

func TestGetInvalid(t *testing.T) {
defer AssertPanic(t)
sel := Doc().Root.Find(".pvk-content")
sel := Doc().Find(".pvk-content")
sel.Get(129)
}

func TestIndex(t *testing.T) {
sel := Doc().Root.Find(".pvk-content")
sel := Doc().Find(".pvk-content")
if i := sel.Index(); i != 1 {
t.Errorf("Expected index of 1, got %v.", i)
}
}

func TestIndexSelector(t *testing.T) {
sel := Doc().Root.Find(".hero-unit")
sel := Doc().Find(".hero-unit")
if i := sel.IndexSelector("div"); i != 4 {
t.Errorf("Expected index of 4, got %v.", i)
}
}

func TestIndexOfNode(t *testing.T) {
sel := Doc().Root.Find("div.pvk-gutter")
sel := Doc().Find("div.pvk-gutter")
if i := sel.IndexOfNode(sel.Nodes[1]); i != 1 {
t.Errorf("Expected index of 1, got %v.", i)
}
}

func TestIndexOfNilNode(t *testing.T) {
sel := Doc().Root.Find("div.pvk-gutter")
sel := Doc().Find("div.pvk-gutter")
if i := sel.IndexOfNode(nil); i != -1 {
t.Errorf("Expected index of -1, got %v.", i)
}
}

func TestIndexOfSelection(t *testing.T) {
sel := Doc().Root.Find("div")
sel2 := Doc().Root.Find(".hero-unit")
sel := Doc().Find("div")
sel2 := Doc().Find(".hero-unit")
if i := sel.IndexOfSelection(sel2); i != 4 {
t.Errorf("Expected index of 4, got %v.", i)
}
Expand Down
22 changes: 11 additions & 11 deletions bench_array_test.go
Expand Up @@ -6,7 +6,7 @@ import (

func BenchmarkFirst(b *testing.B) {
b.StopTimer()
sel := DocB().Root.Find("dd")
sel := DocB().Find("dd")
b.StartTimer()
for i := 0; i < b.N; i++ {
sel.First()
Expand All @@ -15,7 +15,7 @@ func BenchmarkFirst(b *testing.B) {

func BenchmarkLast(b *testing.B) {
b.StopTimer()
sel := DocB().Root.Find("dd")
sel := DocB().Find("dd")
b.StartTimer()
for i := 0; i < b.N; i++ {
sel.Last()
Expand All @@ -24,7 +24,7 @@ func BenchmarkLast(b *testing.B) {

func BenchmarkEq(b *testing.B) {
b.StopTimer()
sel := DocB().Root.Find("dd")
sel := DocB().Find("dd")
j := 0
b.StartTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -37,7 +37,7 @@ func BenchmarkEq(b *testing.B) {

func BenchmarkSlice(b *testing.B) {
b.StopTimer()
sel := DocB().Root.Find("dd")
sel := DocB().Find("dd")
j := 0
b.StartTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -50,7 +50,7 @@ func BenchmarkSlice(b *testing.B) {

func BenchmarkGet(b *testing.B) {
b.StopTimer()
sel := DocB().Root.Find("dd")
sel := DocB().Find("dd")
j := 0
b.StartTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -65,7 +65,7 @@ func BenchmarkIndex(b *testing.B) {
var j int

b.StopTimer()
sel := DocB().Root.Find("#Main")
sel := DocB().Find("#Main")
b.StartTimer()
for i := 0; i < b.N; i++ {
j = sel.Index()
Expand All @@ -77,7 +77,7 @@ func BenchmarkIndexSelector(b *testing.B) {
var j int

b.StopTimer()
sel := DocB().Root.Find("#manual-nav dl dd:nth-child(1)")
sel := DocB().Find("#manual-nav dl dd:nth-child(1)")
b.StartTimer()
for i := 0; i < b.N; i++ {
j = sel.IndexSelector("dd")
Expand All @@ -89,8 +89,8 @@ func BenchmarkIndexOfNode(b *testing.B) {
var j int

b.StopTimer()
sel := DocB().Root.Find("span a")
sel2 := DocB().Root.Find("span a:nth-child(3)")
sel := DocB().Find("span a")
sel2 := DocB().Find("span a:nth-child(3)")
n := sel2.Get(0)
b.StartTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -102,8 +102,8 @@ func BenchmarkIndexOfNode(b *testing.B) {
func BenchmarkIndexOfSelection(b *testing.B) {
var j int
b.StopTimer()
sel := DocB().Root.Find("span a")
sel2 := DocB().Root.Find("span a:nth-child(3)")
sel := DocB().Find("span a")
sel2 := DocB().Find("span a:nth-child(3)")
b.StartTimer()
for i := 0; i < b.N; i++ {
j = sel.IndexOfSelection(sel2)
Expand Down
2 changes: 1 addition & 1 deletion bench_example_test.go
Expand Up @@ -15,7 +15,7 @@ func BenchmarkMetalReviewExample(b *testing.B) {
doc := LoadDoc("metalreview.html")
b.StartTimer()
for i := 0; i < b.N; i++ {
doc.Root.Find(".slider-row:nth-child(1) .slider-item").Each(func(i int, s *Selection) {
doc.Find(".slider-row:nth-child(1) .slider-item").Each(func(i int, s *Selection) {
var band, title string
var score float64
var e error
Expand Down
12 changes: 6 additions & 6 deletions bench_expand_test.go
Expand Up @@ -8,7 +8,7 @@ func BenchmarkAdd(b *testing.B) {
var n int

b.StopTimer()
sel := DocB().Root.Find("dd")
sel := DocB().Find("dd")
b.StartTimer()
for i := 0; i < b.N; i++ {
if n == 0 {
Expand All @@ -24,8 +24,8 @@ func BenchmarkAddSelection(b *testing.B) {
var n int

b.StopTimer()
sel := DocB().Root.Find("dd")
sel2 := DocB().Root.Find("h2[title]")
sel := DocB().Find("dd")
sel2 := DocB().Find("h2[title]")
b.StartTimer()
for i := 0; i < b.N; i++ {
if n == 0 {
Expand All @@ -41,8 +41,8 @@ func BenchmarkAddNodes(b *testing.B) {
var n int

b.StopTimer()
sel := DocB().Root.Find("dd")
sel2 := DocB().Root.Find("h2[title]")
sel := DocB().Find("dd")
sel2 := DocB().Find("h2[title]")
nodes := sel2.Nodes
b.StartTimer()
for i := 0; i < b.N; i++ {
Expand All @@ -59,7 +59,7 @@ func BenchmarkAndSelf(b *testing.B) {
var n int

b.StopTimer()
sel := DocB().Root.Find("dd").Parent()
sel := DocB().Find("dd").Parent()
b.StartTimer()
for i := 0; i < b.N; i++ {
if n == 0 {
Expand Down

0 comments on commit 6669ef0

Please sign in to comment.