Skip to content

Commit 8388fe6

Browse files
committed
ext/dom: Return numeric results from XPath callbacks as numbers not as string
see https://www.w3.org/TR/xpath-10/#numbers
1 parent cd2e060 commit 8388fe6

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Returning scalar values from Dom\XPath callback
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
$dom = Dom\XMLDocument::createFromString('<root/>');
8+
$xpath = new Dom\XPath($dom);
9+
$xpath->registerPhpFunctionNs('urn:x', 'get-number', fn() => 42);
10+
$xpath->registerPhpFunctionNs('urn:x', 'get-string', fn() => "test");
11+
$xpath->registerPhpFunctionNs('urn:x', 'get-bool', fn(bool $b) => $b);
12+
$xpath->registerNamespace('x', 'urn:x');
13+
var_dump($xpath->evaluate('x:get-number()'));
14+
var_dump($xpath->evaluate('x:get-string()'));
15+
var_dump($xpath->evaluate('x:get-bool(1)'));
16+
var_dump($xpath->evaluate('x:get-bool(0)'));
17+
?>
18+
--EXPECT--
19+
float(42)
20+
string(4) "test"
21+
bool(true)
22+
bool(false)

ext/dom/xpath_callbacks.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,10 @@ static zend_result php_dom_xpath_callback_dispatch(php_dom_xpath_callbacks *xpat
438438
valuePush(ctxt, xmlXPathNewNodeSet(nodep));
439439
} else if (Z_TYPE(callback_retval) == IS_FALSE || Z_TYPE(callback_retval) == IS_TRUE) {
440440
valuePush(ctxt, xmlXPathNewBoolean(Z_TYPE(callback_retval) == IS_TRUE));
441+
} else if (Z_TYPE(callback_retval) == IS_LONG) {
442+
valuePush(ctxt, xmlXPathNewFloat(Z_LVAL(callback_retval)));
443+
} else if (Z_TYPE(callback_retval) == IS_DOUBLE) {
444+
valuePush(ctxt, xmlXPathNewFloat(Z_DVAL(callback_retval)));
441445
} else if (Z_TYPE(callback_retval) == IS_OBJECT) {
442446
zend_type_error("Only objects that are instances of DOM nodes can be converted to an XPath expression");
443447
zval_ptr_dtor(&callback_retval);

0 commit comments

Comments
 (0)