Skip to content

Commit 8d50c25

Browse files
committed
fix #112#113
1 parent b9e198d commit 8d50c25

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

func.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,10 @@ func numberFunc(arg1 query) func(query, iterator) interface{} {
287287

288288
// stringFunc is a XPath functions string([node-set]).
289289
func stringFunc(arg1 query) func(query, iterator) interface{} {
290-
return func(_ query, t iterator) interface{} {
290+
return func(q query, t iterator) interface{} {
291+
if arg1 == nil {
292+
return t.Current().Value()
293+
}
291294
v := functionArgs(arg1).Evaluate(t)
292295
return asString(t, v)
293296
}

xpath_function_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,18 @@ func Test_func_string(t *testing.T) {
100100
test_xpath_eval(t, empty_example, `string(3)`, "3")
101101
test_xpath_eval(t, book_example, `string(//book/@category)`, "cooking")
102102
}
103+
func Test_func_string_empty(t *testing.T) {
104+
// https://github.com/antchfx/xpath/issues/113
105+
// Create the equivalent of <div>hi</div> using TNode
106+
doc := createNode("", RootNode)
107+
div := doc.createChildNode("div", ElementNode)
108+
div.lines = 1 // Assign a line number for the test helper
109+
div.createChildNode("hi", TextNode)
110+
111+
// Use the existing test helper to evaluate the XPath
112+
// The expression selects the div if its string value ("hi") is true in a boolean context.
113+
test_xpath_values(t, doc, `//div[string()]`, "hi")
114+
}
103115

104116
func Test_func_string_join(t *testing.T) {
105117
//(t, empty_example, `string-join(('Now', 'is', 'the', 'time', '...'), '')`, "Now is the time ...")

0 commit comments

Comments
 (0)