Skip to content

Commit

Permalink
Refactor code to allow multiple namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdos committed Nov 30, 2023
1 parent cd5c8d0 commit f4cd4d3
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 122 deletions.
7 changes: 7 additions & 0 deletions ext/dom/tests/DOMXPath_callables.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ $xpath->registerNamespace("php", "http://php.net/xpath");
$xpath->registerCycle();
$xpath->evaluate("//a[php:function('cycle', string(@href))]");

echo "--- Legit cases: reset to null ---\n";

$xpath->registerPhpFunctions(null);
$xpath->evaluate("//a[php:function('var_dump', string(@href))]");

echo "--- Error cases ---\n";

$xpath = new DOMXPath($doc);
Expand Down Expand Up @@ -118,6 +123,8 @@ string(15) "https://php.net"
No callback handler "notinset" registered
--- Legit cases: set with cycle ---
dummy: https://php.net
--- Legit cases: reset to null ---
string(15) "https://php.net"
--- Error cases ---
DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be a callable, function "nonexistent" not found or invalid function name
DOMXPath::registerPhpFunctions(): Argument #1 ($restrict) must be of type array|string|null, Closure given
Expand Down
15 changes: 10 additions & 5 deletions ext/dom/xpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ static void dom_xpath_ext_function_php(xmlXPathParserContextPtr ctxt, int nargs,
"xmlExtFunctionTest: failed to get the internal object\n");
error = true;
}
else if (intern->xpath_callbacks.mode == PHP_DOM_REG_FUNC_MODE_NONE) {
zend_throw_error(NULL, "No callbacks were registered");
error = true;
}
}

if (error) {
Expand Down Expand Up @@ -381,7 +377,16 @@ PHP_METHOD(DOMXPath, evaluate)
PHP_METHOD(DOMXPath, registerPhpFunctions)
{
dom_xpath_object *intern = Z_XPATHOBJ_P(ZEND_THIS);
php_dom_xpath_callbacks_update_method_handler(&intern->xpath_callbacks, INTERNAL_FUNCTION_PARAM_PASSTHRU);

zend_string *name = NULL;
HashTable *callable_ht = NULL;

ZEND_PARSE_PARAMETERS_START(0, 1)
Z_PARAM_OPTIONAL
Z_PARAM_ARRAY_HT_OR_STR_OR_NULL(callable_ht, name)
ZEND_PARSE_PARAMETERS_END();

php_dom_xpath_callbacks_update_method_handler(&intern->xpath_callbacks, NULL, name, callable_ht);
}
/* }}} end dom_xpath_register_php_functions */

Expand Down
Loading

0 comments on commit f4cd4d3

Please sign in to comment.