antchfx / htmlquery

htmlquery is golang XPath package for HTML query.
https://github.com/antchfx/xpath
MIT License
727 stars 73 forks source link

panic when calling QueryAll with expression starting with $ #22

Closed WithoutPants closed 4 years ago

WithoutPants commented 4 years ago

For example, an xpath query expression of $example causes a panic when calling QueryAll. As far as I can tell, this is an invalid query expression. I'd expect getQuery to return an error when compiling, but instead it looks like it produces an Expr object with a nil query field.

Minimum unit test to reproduce:

package xpathTest

import (
    "strings"
    "testing"
    "github.com/antchfx/htmlquery"
)

func TestLoadInvalidXPath(t *testing.T) {
    reader := strings.NewReader("<html></html>")
    doc, err := htmlquery.Parse(reader)

    if err != nil {
        t.Errorf("Error loading document: %s", err.Error())
        return
    }

    _, err = htmlquery.QueryAll(doc, `$example`)
    if err == nil {
        t.Error("Expected error")
    }
}

Edit: just realised this is probably a problem with the xpath package. Should I raise there instead?