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

Bugfix/http driver xpath #725

Merged
merged 3 commits into from
Jan 28, 2022
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
8 changes: 8 additions & 0 deletions e2e/tests/dynamic/element/xpath/attrs.fql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
LET url = @lab.cdn.dynamic
LET page = DOCUMENT(url, true)

LET actual = XPATH(page, "//body/@class")

T::NOT::EMPTY(actual)

RETURN T::EQ(actual[0], "text-center")
51 changes: 51 additions & 0 deletions pkg/drivers/http/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/MontFerret/ferret/pkg/drivers"
"github.com/MontFerret/ferret/pkg/drivers/http"
"github.com/MontFerret/ferret/pkg/runtime/values"
"github.com/MontFerret/ferret/pkg/runtime/values/types"
"github.com/PuerkitoBio/goquery"
. "github.com/smartystreets/goconvey/convey"
"testing"
Expand Down Expand Up @@ -447,5 +448,55 @@ func TestElement(t *testing.T) {
So(err, ShouldBeNil)
So(nt.String(), ShouldEqual, "[\"Album example for Bootstrap\"]")
})

Convey("Func", func() {
buff := bytes.NewBuffer([]byte(doc))

buff.Write([]byte(doc))

doc, err := goquery.NewDocumentFromReader(buff)

So(err, ShouldBeNil)

el, err := http.NewHTMLElement(doc.Find("html"))

So(err, ShouldBeNil)

nt, err := el.XPath(context.Background(), values.NewString("count(//div)"))

So(err, ShouldBeNil)
So(nt.Type().String(), ShouldEqual, types.Float.String())
})

Convey("Attributes", func() {
buff := bytes.NewBuffer([]byte(`<!DOCTYPE html><body><div><a title="30"/></div></body></html>`))
godoc, err := goquery.NewDocumentFromReader(buff)
So(err, ShouldBeNil)

doc, err := http.NewRootHTMLDocument(godoc, "localhost:9090")
So(err, ShouldBeNil)

nt, err := doc.XPath(context.Background(), values.NewString("//a/@title"))

So(err, ShouldBeNil)
So(nt.Type().String(), ShouldEqual, types.Array.String())
So(nt.(*values.Array).First().Type().String(), ShouldEqual, types.String.String())
So(nt.(*values.Array).First().String(), ShouldEqual, "30")
})

Convey("Element node", func() {
buff := bytes.NewBuffer([]byte(`<!DOCTYPE html><body><div><a title="30"/></div></body></html>`))
godoc, err := goquery.NewDocumentFromReader(buff)
So(err, ShouldBeNil)

doc, err := http.NewRootHTMLDocument(godoc, "localhost:9090")
So(err, ShouldBeNil)

nt, err := doc.XPath(context.Background(), values.NewString("//div"))

So(err, ShouldBeNil)
So(nt.Type().String(), ShouldEqual, types.Array.String())
So(nt.(*values.Array).First().Type().String(), ShouldEqual, drivers.HTMLElementType.String())
})
})
}
15 changes: 9 additions & 6 deletions pkg/drivers/http/xpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,15 @@ func EvalXPathTo(selection *goquery.Selection, expression string) (core.Value, e
for res.MoveNext() {
var item core.Value

node := res.Current().(*htmlquery.NodeNavigator).Current()

if node.Type == html.TextNode {
item = values.NewString(node.Data)
} else {
i, err := parseXPathNode(node)
node := res.Current()

switch node.NodeType() {
case xpath.TextNode:
item = values.NewString(node.Value())
case xpath.AttributeNode:
item = values.NewString(node.Value())
default:
i, err := parseXPathNode(node.(*htmlquery.NodeNavigator).Current())

if err != nil {
return nil, err
Expand Down