From 5481aef473ee298d297387f5b69e2768df09e0c4 Mon Sep 17 00:00:00 2001 From: zhengchun Date: Sun, 23 Jun 2024 20:03:25 +0800 Subject: [PATCH] #98. remove the duplicate elements filter, avoid `htmlquery` and `xmlquery` packages re-use the cached element in some cases. #94 --- xpath.go | 27 +++++++++------------------ xpath_test.go | 12 +++++++++++- 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/xpath.go b/xpath.go index aa27370..04bbe8d 100644 --- a/xpath.go +++ b/xpath.go @@ -74,7 +74,6 @@ type NodeNavigator interface { type NodeIterator struct { node NodeNavigator query query - table map[uint64]bool } // Current returns current node which matched. @@ -84,22 +83,14 @@ func (t *NodeIterator) Current() NodeNavigator { // MoveNext moves Navigator to the next match node. func (t *NodeIterator) MoveNext() bool { - for { - n := t.query.Select(t) - if n == nil { - return false - } - if !t.node.MoveTo(n) { - t.node = n.Copy() - } - // https://github.com/antchfx/xpath/issues/94 - id := getHashCode(n.Copy()) - if _, ok := t.table[id]; ok { - continue - } - t.table[id] = true - return true + n := t.query.Select(t) + if n == nil { + return false } + if !t.node.MoveTo(n) { + t.node = n.Copy() + } + return true } // Select selects a node set using the specified XPath expression. @@ -130,14 +121,14 @@ func (expr *Expr) Evaluate(root NodeNavigator) interface{} { val := expr.q.Evaluate(iteratorFunc(func() NodeNavigator { return root })) switch val.(type) { case query: - return &NodeIterator{query: expr.q.Clone(), node: root, table: make(map[uint64]bool)} + return &NodeIterator{query: expr.q.Clone(), node: root} } return val } // Select selects a node set using the specified XPath expression. func (expr *Expr) Select(root NodeNavigator) *NodeIterator { - return &NodeIterator{query: expr.q.Clone(), node: root, table: make(map[uint64]bool)} + return &NodeIterator{query: expr.q.Clone(), node: root} } // String returns XPath expression string. diff --git a/xpath_test.go b/xpath_test.go index cd62243..6e2a79c 100644 --- a/xpath_test.go +++ b/xpath_test.go @@ -324,7 +324,17 @@ func selectNode(root *TNode, expr string) *TNode { func selectNodes(root *TNode, expr string) []*TNode { t := Select(createNavigator(root), expr) - return iterateNodes(t) + c := make(map[uint64]bool) + var list []*TNode + for _, n := range iterateNodes(t) { + m := getHashCode(createNavigator(n)) + if _, ok := c[m]; ok { + continue + } + c[m] = true + list = append(list, n) + } + return list } func joinValues(nodes []*TNode) string {