Skip to content

Commit

Permalink
fix #92
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengchun committed Oct 26, 2023
1 parent 588960c commit f86ee5a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,12 @@ func (b *builder) processFunctionNode(root *functionNode) (query, error) {
if arg2, err = b.processNode(root.Args[1]); err != nil {
return nil, err
}
// Issue #92, testing the regular expression before.
if q, ok := arg2.(*constantQuery); ok {
if _, err = getRegexp(q.Val.(string)); err != nil {
return nil, fmt.Errorf("matches() got error. %v", err)
}
}
qyOutput = &functionQuery{Input: b.firstInput, Func: matchesFunc(arg1, arg2)}
case "substring":
//substring( string , start [, length] )
Expand Down
10 changes: 10 additions & 0 deletions xpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,16 @@ func TestFunction(t *testing.T) {
testXPath3(t, html, "//li/preceding::*[1]", selectNode(html, "//h1"))
}

func TestFunction_matches(t *testing.T) {
// testing unexpected the regular expression.
if _, err := build(`//*[matches(., '^[\u0621-\u064AA-Za-z\-]+')]`, nil); err == nil {
t.Fatal("matches() should got error, but nil")
}
if _, err := build(`//*[matches(., '//*[matches(., '\w+`, nil); err == nil {
t.Fatal("matches() should got error, but nil")
}
}

func TestTransformFunctionReverse(t *testing.T) {
nodes := selectNodes(html, "reverse(//li)")
expectedReversedNodeValues := []string{"", "login", "about", "Home"}
Expand Down

0 comments on commit f86ee5a

Please sign in to comment.