From f86ee5a6c2840795dcc6ea1b84ecf02e383bdec0 Mon Sep 17 00:00:00 2001 From: zhengchun Date: Thu, 26 Oct 2023 13:34:16 +0800 Subject: [PATCH] fix #92 --- build.go | 6 ++++++ xpath_test.go | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/build.go b/build.go index ff494f1..2977bbc 100644 --- a/build.go +++ b/build.go @@ -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] ) diff --git a/xpath_test.go b/xpath_test.go index d87a0e0..7441d33 100644 --- a/xpath_test.go +++ b/xpath_test.go @@ -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"}