From 49b8168ddba574da3a284554dc11b40291dbf726 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Thu, 12 Oct 2023 22:58:52 +0200 Subject: [PATCH] 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. --- .../DOMXPath_evaluate_namespace_node_set.phpt | 33 +++++++++++++++++++ .../DOMXPath_evaluate_node_set_to_string.phpt | 26 +++++++++++++++ ext/dom/xpath.c | 1 - 3 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 ext/dom/tests/DOMXPath_evaluate_namespace_node_set.phpt create mode 100644 ext/dom/tests/DOMXPath_evaluate_node_set_to_string.phpt diff --git a/ext/dom/tests/DOMXPath_evaluate_namespace_node_set.phpt b/ext/dom/tests/DOMXPath_evaluate_namespace_node_set.phpt new file mode 100644 index 0000000000000..6b0297c2f0aa5 --- /dev/null +++ b/ext/dom/tests/DOMXPath_evaluate_namespace_node_set.phpt @@ -0,0 +1,33 @@ +--TEST-- +DOMXPath::evaluate() with PHP function passing a namespace node-set +--EXTENSIONS-- +dom +--FILE-- +loadXML(<< + +

hi

+
+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) diff --git a/ext/dom/tests/DOMXPath_evaluate_node_set_to_string.phpt b/ext/dom/tests/DOMXPath_evaluate_node_set_to_string.phpt new file mode 100644 index 0000000000000..772e1f757811d --- /dev/null +++ b/ext/dom/tests/DOMXPath_evaluate_node_set_to_string.phpt @@ -0,0 +1,26 @@ +--TEST-- +DOMXPath::evaluate() with PHP function passing node-set returning a string +--EXTENSIONS-- +dom +--FILE-- +loadXML(<< + +

hi

+
+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" diff --git a/ext/dom/xpath.c b/ext/dom/xpath.c index 3c44a5b7f4ff8..95656bcbac9f2 100644 --- a/ext/dom/xpath.c +++ b/ext/dom/xpath.c @@ -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;