From 4e94caa55f89fa4d18df1d44536b52f3858c3248 Mon Sep 17 00:00:00 2001 From: Tim Voronov Date: Wed, 22 Dec 2021 20:20:02 -0500 Subject: [PATCH] Fixed handling text nodes by XPath parser (#715) * Fixed handling text nodes by XPath parser * Formatting --- pkg/drivers/http/element_test.go | 21 +++++++++++++++++++++ pkg/drivers/http/xpath.go | 24 +++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/pkg/drivers/http/element_test.go b/pkg/drivers/http/element_test.go index 2f8c0bb8..4911cc3a 100644 --- a/pkg/drivers/http/element_test.go +++ b/pkg/drivers/http/element_test.go @@ -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\"]") + }) + }) } diff --git a/pkg/drivers/http/xpath.go b/pkg/drivers/http/xpath.go index e32ba107..73258b92 100644 --- a/pkg/drivers/http/xpath.go +++ b/pkg/drivers/http/xpath.go @@ -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 @@ -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