mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-12-24 12:38:00 +08:00
Revert "Re-orangize code for compitable"
This reverts commit 6802fc5555.
This commit is contained in:
@@ -1,48 +0,0 @@
|
|||||||
//go:build go1.23
|
|
||||||
// +build go1.23
|
|
||||||
|
|
||||||
package goquery
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func BenchmarkEachIter123(b *testing.B) {
|
|
||||||
var tmp, n int
|
|
||||||
|
|
||||||
b.StopTimer()
|
|
||||||
sel := DocW().Find("td")
|
|
||||||
b.StartTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
for range sel.EachIter() {
|
|
||||||
tmp++
|
|
||||||
}
|
|
||||||
if n == 0 {
|
|
||||||
n = tmp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if n != 59 {
|
|
||||||
b.Fatalf("want 59, got %d", n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func BenchmarkEachIterWithBreak123(b *testing.B) {
|
|
||||||
var tmp, n int
|
|
||||||
|
|
||||||
b.StopTimer()
|
|
||||||
sel := DocW().Find("td")
|
|
||||||
b.StartTimer()
|
|
||||||
for i := 0; i < b.N; i++ {
|
|
||||||
tmp = 0
|
|
||||||
for range sel.EachIter() {
|
|
||||||
tmp++
|
|
||||||
if tmp >= 10 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if n == 0 {
|
|
||||||
n = tmp
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if n != 10 {
|
|
||||||
b.Fatalf("want 10, got %d", n)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -32,10 +32,9 @@ func BenchmarkEachIter(b *testing.B) {
|
|||||||
sel := DocW().Find("td")
|
sel := DocW().Find("td")
|
||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
sel.EachIter()(func(i int, s *Selection) bool {
|
for range sel.EachIter() {
|
||||||
tmp++
|
tmp++
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
n = tmp
|
n = tmp
|
||||||
}
|
}
|
||||||
@@ -53,11 +52,12 @@ func BenchmarkEachIterWithBreak(b *testing.B) {
|
|||||||
b.StartTimer()
|
b.StartTimer()
|
||||||
for i := 0; i < b.N; i++ {
|
for i := 0; i < b.N; i++ {
|
||||||
tmp = 0
|
tmp = 0
|
||||||
sel.EachIter()(func(i int, s *Selection) bool {
|
for range sel.EachIter() {
|
||||||
tmp++
|
tmp++
|
||||||
return tmp < 10
|
if tmp >= 10 {
|
||||||
})
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
n = tmp
|
n = tmp
|
||||||
}
|
}
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -5,4 +5,4 @@ require (
|
|||||||
golang.org/x/net v0.27.0
|
golang.org/x/net v0.27.0
|
||||||
)
|
)
|
||||||
|
|
||||||
go 1.18
|
go 1.23
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
package goquery
|
package goquery
|
||||||
|
|
||||||
|
import "iter"
|
||||||
|
|
||||||
// Each iterates over a Selection object, executing a function for each
|
// Each iterates over a Selection object, executing a function for each
|
||||||
// matched element. It returns the current Selection object. The function
|
// matched element. It returns the current Selection object. The function
|
||||||
// f is called for each element in the selection with the index of the
|
// f is called for each element in the selection with the index of the
|
||||||
@@ -14,7 +16,7 @@ func (s *Selection) Each(f func(int, *Selection)) *Selection {
|
|||||||
|
|
||||||
// EachIter returns an iterator that yields the Selection object in order.
|
// EachIter returns an iterator that yields the Selection object in order.
|
||||||
// The implementation is similar to Each, but it returns an iterator instead.
|
// The implementation is similar to Each, but it returns an iterator instead.
|
||||||
func (s *Selection) EachIter() func(yield func(int, *Selection) bool) {
|
func (s *Selection) EachIter() iter.Seq2[int, *Selection] {
|
||||||
return func(yield func(int, *Selection) bool) {
|
return func(yield func(int, *Selection) bool) {
|
||||||
for i, n := range s.Nodes {
|
for i, n := range s.Nodes {
|
||||||
if !yield(i, newSingleSelection(n, s.document)) {
|
if !yield(i, newSingleSelection(n, s.document)) {
|
||||||
|
|||||||
@@ -1,42 +0,0 @@
|
|||||||
//go:build go1.23
|
|
||||||
// +build go1.23
|
|
||||||
|
|
||||||
package goquery
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestEachIter123(t *testing.T) {
|
|
||||||
var cnt int
|
|
||||||
|
|
||||||
sel := Doc().Find(".hero-unit .row-fluid")
|
|
||||||
|
|
||||||
for i, s := range sel.EachIter() {
|
|
||||||
cnt++
|
|
||||||
t.Logf("At index %v, node %v", i, s.Nodes[0].Data)
|
|
||||||
}
|
|
||||||
|
|
||||||
sel = sel.Find("a")
|
|
||||||
|
|
||||||
if cnt != 4 {
|
|
||||||
t.Errorf("Expected EachIter() to call function 4 times, got %v times.", cnt)
|
|
||||||
}
|
|
||||||
assertLength(t, sel.Nodes, 6)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestEachIterWithBreak123(t *testing.T) {
|
|
||||||
var cnt int
|
|
||||||
|
|
||||||
sel := Doc().Find(".hero-unit .row-fluid")
|
|
||||||
for i, s := range sel.EachIter() {
|
|
||||||
cnt++
|
|
||||||
t.Logf("At index %v, node %v", i, s.Nodes[0].Data)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
sel = sel.Find("a")
|
|
||||||
|
|
||||||
if cnt != 1 {
|
|
||||||
t.Errorf("Expected EachIter() to call function 1 time, got %v times.", cnt)
|
|
||||||
}
|
|
||||||
assertLength(t, sel.Nodes, 6)
|
|
||||||
}
|
|
||||||
@@ -110,15 +110,16 @@ func TestEachIter(t *testing.T) {
|
|||||||
var cnt int
|
var cnt int
|
||||||
|
|
||||||
sel := Doc().Find(".hero-unit .row-fluid")
|
sel := Doc().Find(".hero-unit .row-fluid")
|
||||||
sel.EachIter()(func(i int, n *Selection) bool {
|
|
||||||
|
for i, s := range sel.EachIter() {
|
||||||
cnt++
|
cnt++
|
||||||
t.Logf("At index %v, node %v", i, n.Nodes[0].Data)
|
t.Logf("At index %v, node %v", i, s.Nodes[0].Data)
|
||||||
return true
|
}
|
||||||
})
|
|
||||||
sel = sel.Find("a")
|
sel = sel.Find("a")
|
||||||
|
|
||||||
if cnt != 4 {
|
if cnt != 4 {
|
||||||
t.Errorf("Expected EachIter() to call function 4 time, got %v times.", cnt)
|
t.Errorf("Expected Each() to call function 4 times, got %v times.", cnt)
|
||||||
}
|
}
|
||||||
assertLength(t, sel.Nodes, 6)
|
assertLength(t, sel.Nodes, 6)
|
||||||
}
|
}
|
||||||
@@ -127,15 +128,16 @@ func TestEachIterWithBreak(t *testing.T) {
|
|||||||
var cnt int
|
var cnt int
|
||||||
|
|
||||||
sel := Doc().Find(".hero-unit .row-fluid")
|
sel := Doc().Find(".hero-unit .row-fluid")
|
||||||
sel.EachIter()(func(i int, n *Selection) bool {
|
for i, s := range sel.EachIter() {
|
||||||
cnt++
|
cnt++
|
||||||
t.Logf("At index %v, node %v", i, n.Nodes[0].Data)
|
t.Logf("At index %v, node %v", i, s.Nodes[0].Data)
|
||||||
return false
|
break
|
||||||
})
|
}
|
||||||
|
|
||||||
sel = sel.Find("a")
|
sel = sel.Find("a")
|
||||||
|
|
||||||
if cnt != 1 {
|
if cnt != 1 {
|
||||||
t.Errorf("Expected EachIter() to call function 1 time, got %v times.", cnt)
|
t.Errorf("Expected Each() to call function 1 time, got %v times.", cnt)
|
||||||
}
|
}
|
||||||
assertLength(t, sel.Nodes, 6)
|
assertLength(t, sel.Nodes, 6)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user