In some cases Compile didn't report error in the predicate, which caused a runtime panic later during a query.
github.com/antchfx/htmlquery v1.3.2
github.com/antchfx/xpath v1.3.2-0.20240724042431-14e235f5e127
package main
import (
"strings"
"github.com/antchfx/htmlquery"
"github.com/antchfx/xpath"
"golang.org/x/net/html"
)
func main() {
h := `<div>
<span></span>
<span>v</span>
</div>
`
doc, err := html.Parse(strings.NewReader(h))
if err != nil {
return
}
query(doc, `//*[contains(normalize-space(),"v")]//text()`) // panic
query(doc, `//*[contains(normalize-space(),"v")]`) // print error
query(doc, `//*[contains(normalize-space(.),"v")]`) // correct
}
func query(n *html.Node, expr string) {
p, err := xpath.Compile(expr)
if err != nil {
println(err.Error())
return
}
htmlquery.QuerySelector(n, p)
}
The first two xpaths miss parameter of function normalize-space, but only the second one report an error, and the first one will panic.
By the way, I think normalize-space() should be equal to normalize-space(.) according to the specification
I didn't dive into the codes, but the debugger reported
|
qyGrandInput, _ = b.processNode(input.Input, flagsEnum.SmartDesc, props) |
This assignment discarded the error.
I don't know whether this is expected, but I think it makes Compile unreliable.
In some cases
Compiledidn't report error in the predicate, which caused a runtime panic later during a query.The first two xpaths miss parameter of function
normalize-space, but only the second one report an error, and the first one will panic.By the way, I think
normalize-space()should be equal tonormalize-space(.)according to the specificationI didn't dive into the codes, but the debugger reported
xpath/build.go
Line 109 in 14e235f
This assignment discarded the error.
I don't know whether this is expected, but I think it makes
Compileunreliable.