Skip to content

Commit

Permalink
Cover more paths in dom_xpath_ext_function_php() with tests
Browse files Browse the repository at this point in the history
Also removes an incorrect comment: we *do* need the special namespace
node handling code, otherwise we'd segfault.
  • Loading branch information
nielsdos committed Oct 12, 2023
1 parent 3a41dc8 commit 49b8168
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
33 changes: 33 additions & 0 deletions ext/dom/tests/DOMXPath_evaluate_namespace_node_set.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
DOMXPath::evaluate() with PHP function passing a namespace node-set
--EXTENSIONS--
dom
--FILE--
<?php

$dom = new DOMDocument();
$dom->loadXML(<<<XML
<?xml version="1.0"?>
<container>
<p>hi</p>
</container>
XML);

$xpath = new DOMXPath($dom);

function node_test($nodes) {
echo "nodes count: ", count($nodes), "\n";
return array_sum(array_map(fn ($node) => strlen($node->nodeName), $nodes));
}

$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPhpFunctions(['node_test']);
var_dump($xpath->evaluate('number(php:function("node_test", //namespace::*))'));
var_dump($xpath->evaluate('boolean(php:function("node_test", //namespace::*))'));

?>
--EXPECT--
nodes count: 2
float(18)
nodes count: 2
bool(true)
26 changes: 26 additions & 0 deletions ext/dom/tests/DOMXPath_evaluate_node_set_to_string.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
DOMXPath::evaluate() with PHP function passing node-set returning a string
--EXTENSIONS--
dom
--FILE--
<?php

$dom = new DOMDocument();
$dom->loadXML(<<<XML
<?xml version="1.0"?>
<container>
<p>hi</p>
</container>
XML);

$xpath = new DOMXPath($dom);

$xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerPhpFunctions(['strrev']);
var_dump($xpath->evaluate('php:functionString("strrev", //p)'));
var_dump($xpath->evaluate('php:functionString("strrev", //namespace::*)'));

?>
--EXPECT--
string(2) "ih"
string(36) "ecapseman/8991/LMX/gro.3w.www//:ptth"
1 change: 0 additions & 1 deletion ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs,
for (j = 0; j < obj->nodesetval->nodeNr; j++) {
xmlNodePtr node = obj->nodesetval->nodeTab[j];
zval child;
/* not sure, if we need this... it's copied from xpath.c */
if (node->type == XML_NAMESPACE_DECL) {
xmlNodePtr nsparent = node->_private;
xmlNsPtr original = (xmlNsPtr) node;
Expand Down

0 comments on commit 49b8168

Please sign in to comment.