Skip to content

Commit 49b8168

Browse files
committed
Cover more paths in dom_xpath_ext_function_php() with tests
Also removes an incorrect comment: we *do* need the special namespace node handling code, otherwise we'd segfault.
1 parent 3a41dc8 commit 49b8168

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
DOMXPath::evaluate() with PHP function passing a namespace node-set
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = new DOMDocument();
9+
$dom->loadXML(<<<XML
10+
<?xml version="1.0"?>
11+
<container>
12+
<p>hi</p>
13+
</container>
14+
XML);
15+
16+
$xpath = new DOMXPath($dom);
17+
18+
function node_test($nodes) {
19+
echo "nodes count: ", count($nodes), "\n";
20+
return array_sum(array_map(fn ($node) => strlen($node->nodeName), $nodes));
21+
}
22+
23+
$xpath->registerNamespace("php", "http://php.net/xpath");
24+
$xpath->registerPhpFunctions(['node_test']);
25+
var_dump($xpath->evaluate('number(php:function("node_test", //namespace::*))'));
26+
var_dump($xpath->evaluate('boolean(php:function("node_test", //namespace::*))'));
27+
28+
?>
29+
--EXPECT--
30+
nodes count: 2
31+
float(18)
32+
nodes count: 2
33+
bool(true)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
DOMXPath::evaluate() with PHP function passing node-set returning a string
3+
--EXTENSIONS--
4+
dom
5+
--FILE--
6+
<?php
7+
8+
$dom = new DOMDocument();
9+
$dom->loadXML(<<<XML
10+
<?xml version="1.0"?>
11+
<container>
12+
<p>hi</p>
13+
</container>
14+
XML);
15+
16+
$xpath = new DOMXPath($dom);
17+
18+
$xpath->registerNamespace("php", "http://php.net/xpath");
19+
$xpath->registerPhpFunctions(['strrev']);
20+
var_dump($xpath->evaluate('php:functionString("strrev", //p)'));
21+
var_dump($xpath->evaluate('php:functionString("strrev", //namespace::*)'));
22+
23+
?>
24+
--EXPECT--
25+
string(2) "ih"
26+
string(36) "ecapseman/8991/LMX/gro.3w.www//:ptth"

ext/dom/xpath.c

-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs,
100100
for (j = 0; j < obj->nodesetval->nodeNr; j++) {
101101
xmlNodePtr node = obj->nodesetval->nodeTab[j];
102102
zval child;
103-
/* not sure, if we need this... it's copied from xpath.c */
104103
if (node->type == XML_NAMESPACE_DECL) {
105104
xmlNodePtr nsparent = node->_private;
106105
xmlNsPtr original = (xmlNsPtr) node;

0 commit comments

Comments
 (0)