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

Fixed handling text nodes by XPath parser #715

Merged
merged 2 commits into from
Dec 23, 2021
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
21 changes: 21 additions & 0 deletions pkg/drivers/http/element_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,4 +427,25 @@ func TestElement(t *testing.T) {
So(err, ShouldBeNil)
So(v, ShouldEqual, 4)
})

Convey(".XPath", t, func() {
Convey("Text nodes", 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("/head/title/text()"))

So(err, ShouldBeNil)
So(nt.String(), ShouldEqual, "[\"Album example for Bootstrap\"]")
})
})
}
24 changes: 19 additions & 5 deletions pkg/drivers/http/xpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ func EvalXPathToNodesWith(selection *goquery.Selection, expression string, mappe
return nil, err
}

items.Push(item)
if item != nil {
items.Push(item)
}
}

return items, nil
Expand All @@ -80,13 +82,25 @@ func EvalXPathTo(selection *goquery.Selection, expression string) (core.Value, e
items := values.NewArray(10)

for res.MoveNext() {
item, err := parseXPathNode(res.Current().(*htmlquery.NodeNavigator).Current())
var item core.Value

if err != nil {
return nil, err
node := res.Current().(*htmlquery.NodeNavigator).Current()

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

if err != nil {
return nil, err
}

item = i
}

items.Push(item)
if item != nil {
items.Push(item)
}
}

return items, nil
Expand Down