Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transformFunctionQuery and its first usage xpath 2.0's reverse() #46

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,15 @@ func (b *builder) processFunctionNode(root *functionNode) (query, error) {
args = append(args, q)
}
qyOutput = &functionQuery{Input: b.firstInput, Func: concatFunc(args...)}
case "reverse":
if len(root.Args) == 0 {
return nil, fmt.Errorf("xpath: reverse(node-sets) function must with have parameters node-sets")
}
argQuery, err := b.processNode(root.Args[0])
if err != nil {
return nil, err
}
qyOutput = &transformFunctionQuery{Input: argQuery, Func: reverseFunc}
default:
return nil, fmt.Errorf("not yet support this function %s()", root.FuncName)
}
Expand Down
20 changes: 20 additions & 0 deletions func.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,23 @@ func functionArgs(q query) query {
}
return q.Clone()
}

func reverseFunc(q query, t iterator) func() NodeNavigator {
var list []NodeNavigator
for {
node := q.Select(t)
if node == nil {
break
}
list = append(list, node.Copy())
}
i := len(list)
return func() NodeNavigator {
if i <= 0 {
return nil
}
i--
node := list[i]
return node
}
}
33 changes: 31 additions & 2 deletions query.go
Original file line number Diff line number Diff line change
Expand Up @@ -590,8 +590,9 @@ func (f *filterQuery) Clone() query {
return &filterQuery{Input: f.Input.Clone(), Predicate: f.Predicate.Clone()}
}

// functionQuery is an XPath function that call a function to returns
// value of current NodeNavigator node.
// functionQuery is an XPath function that returns a computed value for
// the Evaluate call of the current NodeNavigator node. Select call isn't
// applicable for functionQuery.
type functionQuery struct {
Input query // Node Set
Func func(query, iterator) interface{} // The xpath function.
Expand All @@ -611,6 +612,34 @@ func (f *functionQuery) Clone() query {
return &functionQuery{Input: f.Input.Clone(), Func: f.Func}
}

// transformFunctionQuery diffs from functionQuery where the latter computes a scalar
// value (number,string,boolean) for the current NodeNavigator node while the former
// (transformFunctionQuery) performs a mapping or transform of the current NodeNavigator
// and returns a new NodeNavigator. It is used for non-scalar XPath functions such as
// reverse(), remove(), subsequence(), unordered(), etc.
type transformFunctionQuery struct {
Input query
Func func(query, iterator) func() NodeNavigator
iterator func() NodeNavigator
}

func (f *transformFunctionQuery) Select(t iterator) NodeNavigator {
if f.iterator == nil {
f.iterator = f.Func(f.Input, t)
}
return f.iterator()
}

func (f *transformFunctionQuery) Evaluate(t iterator) interface{} {
f.Input.Evaluate(t)
f.iterator = nil
return f
}

func (f *transformFunctionQuery) Clone() query {
return &transformFunctionQuery{Input: f.Input.Clone(), Func: f.Func}
}

// constantQuery is an XPath constant operand.
type constantQuery struct {
Val interface{}
Expand Down
23 changes: 23 additions & 0 deletions xpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,29 @@ func TestFunction(t *testing.T) {
testXPath3(t, html, "//li/preceding::*[1]", selectNode(html, "//h1"))
}

func TestTransformFunctionReverse(t *testing.T) {
nodes := selectNodes(html, "reverse(//li)")
expectedReversedNodeValues := []string { "", "login", "about", "Home" }
if len(nodes) != len(expectedReversedNodeValues) {
t.Fatalf("reverse(//li) should return %d <li> nodes", len(expectedReversedNodeValues))
}
for i := 0; i < len(expectedReversedNodeValues); i++ {
if nodes[i].Value() != expectedReversedNodeValues[i] {
t.Fatalf("reverse(//li)[%d].Value() should be '%s', instead, got '%s'",
i, expectedReversedNodeValues[i], nodes[i].Value())
}
}

// Although this xpath itself doesn't make much sense, it does exercise the call path to provide coverage
// for transformFunctionQuery.Evaluate()
testXPath2(t, html, "//h1[reverse(.) = reverse(.)]", 1)

// Test reverse() parsing error: missing node-sets argument.
assertPanic(t, func() { testXPath2(t, html, "reverse()", 0) })
// Test reverse() parsing error: invalid node-sets argument.
assertPanic(t, func() { testXPath2(t, html, "reverse(concat())", 0) })
}

func TestPanic(t *testing.T) {
// starts-with
assertPanic(t, func() { testXPath(t, html, "//*[starts-with(0, 0)]", "") })
Expand Down