diff --git a/document_value_test.go b/document_value_test.go new file mode 100644 index 0000000..513ce22 --- /dev/null +++ b/document_value_test.go @@ -0,0 +1,25 @@ +package htmlquery + +import ( + "github.com/antchfx/xpath" + "strings" + "testing" +) + +func TestXPathDocumentStringValue(t *testing.T) { + doc, err := Parse(strings.NewReader(`
C
`)) + if err != nil { + t.Fatal(err) + } + for _, tc := range []struct { + expr string + want interface{} + }{{`string(/)`, "ABC"}, {`string()`, "ABC"}, {`contains(/, 'BC')`, true}, {`string(//p)`, "C"}, {`string(//comment())`, "excluded"}} { + t.Run(tc.expr, func(t *testing.T) { + got := xpath.MustCompile(tc.expr).Evaluate(CreateXPathNavigator(doc)) + if got != tc.want { + t.Errorf("got %#v want %#v", got, tc.want) + } + }) + } +} diff --git a/query.go b/query.go index 4930aa1..29ef33d 100644 --- a/query.go +++ b/query.go @@ -290,6 +290,8 @@ func (h *NodeNavigator) Value() string { return InnerText(h.curr) case html.TextNode: return h.curr.Data + case html.DocumentNode: + return InnerText(h.curr) } return "" }