Skip to content

Commit

Permalink
add cacheQuery to fix last() not working on subgroup. #76, #78
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Jul 8, 2022
1 parent 2afacf4 commit dfece7e
Show file tree
Hide file tree
Showing 6 changed files with 172 additions and 56 deletions.
3 changes: 3 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,9 @@ func (b *builder) processNode(root node) (q query, err error) {
return
}
q = &groupQuery{Input: q}
// fix https://github.com/antchfx/xpath/issues/76
q = &cacheQuery{Input: q}
b.firstInput = q
}
return
}
Expand Down
20 changes: 16 additions & 4 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ func positionFunc(q query, t iterator) interface{} {

// lastFunc is a XPath Node Set functions last().
func lastFunc(q query, t iterator) interface{} {
//
type Counter interface {
count() int
}
if p, ok := q.(Counter); ok {
return float64(p.count())
}

var (
count = 0
node = t.Current().Copy()
Expand Down Expand Up @@ -158,7 +166,8 @@ func nameFunc(arg query) func(query, iterator) interface{} {
if arg == nil {
v = t.Current()
} else {
v = arg.Clone().Select(t)
arg.Reset()
v = arg.Select(t)
if v == nil {
return ""
}
Expand All @@ -178,7 +187,8 @@ func localNameFunc(arg query) func(query, iterator) interface{} {
if arg == nil {
v = t.Current()
} else {
v = arg.Clone().Select(t)
arg.Reset()
v = arg.Select(t)
if v == nil {
return ""
}
Expand All @@ -195,7 +205,8 @@ func namespaceFunc(arg query) func(query, iterator) interface{} {
v = t.Current()
} else {
// Get the first node in the node-set if specified.
v = arg.Clone().Select(t)
arg.Reset()
v = arg.Select(t)
if v == nil {
return ""
}
Expand Down Expand Up @@ -592,7 +603,8 @@ func functionArgs(q query) query {
if _, ok := q.(*functionQuery); ok {
return q
}
return q.Clone()
q.Reset()
return q
}

func reverseFunc(q query, t iterator) func() NodeNavigator {
Expand Down
3 changes: 1 addition & 2 deletions func_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ func (t testQuery) Select(_ iterator) NodeNavigator {
panic("implement me")
}

func (t testQuery) Clone() query {
return t
func (t testQuery) Reset() {
}

func (t testQuery) Evaluate(_ iterator) interface{} {
Expand Down
Loading

0 comments on commit dfece7e

Please sign in to comment.