Skip to content

Commit

Permalink
Fixed handling text nodes by XPath parser (#715)
Browse files Browse the repository at this point in the history
* Fixed handling text nodes by XPath parser

* Formatting
  • Loading branch information
ziflex authored Dec 23, 2021
1 parent 767fb7f commit 4e94caa
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 5 deletions.
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

0 comments on commit 4e94caa

Please sign in to comment.