Skip to content
This repository has been archived by the owner on Jan 29, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/dom_xpathphpfunction' of https://github.com/sas…
Browse files Browse the repository at this point in the history
…ezaki/zf2 into feature/dom-xpath-php
  • Loading branch information
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ class Query
* @var array
*/
protected $_xpathNamespaces = array();

/**
* XPath PHP Functions
* @var mixed
*/
protected $_xpathPhpFunctions;

/**
* Constructor
Expand Down Expand Up @@ -270,6 +276,17 @@ public function registerXpathNamespaces($xpathNamespaces)
$this->_xpathNamespaces = $xpathNamespaces;
}

/**
* Register PHP Functions to use in internal DOMXPath
*
* @param mixed $restrict
* @return void
*/
public function registerXpathPhpFunctions($xpathPhpFunctions = true)
{
$this->_xpathPhpFunctions = $xpathPhpFunctions;
}

/**
* Prepare node list
*
Expand All @@ -283,6 +300,12 @@ protected function _getNodeList($document, $xpathQuery)
foreach ($this->_xpathNamespaces as $prefix => $namespaceUri) {
$xpath->registerNamespace($prefix, $namespaceUri);
}
if ($this->_xpathPhpFunctions) {
$xpath->registerNamespace("php", "http://php.net/xpath");
($this->_xpathPhpFunctions === true) ?
$xpath->registerPHPFunctions()
: $xpath->registerPHPFunctions($this->_xpathPhpFunctions);
}
$xpathQuery = (string) $xpathQuery;
return $xpath->query($xpathQuery);
}
Expand Down
34 changes: 34 additions & 0 deletions test/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,40 @@ public function testQueryXpathShouldAllowQueryingArbitraryUsingXpath()
$this->assertEquals(2, count($result), $result->getXpathQuery());
}

public function testXpathPhpFunctionsShouldBeDisableByDefault()
{
$this->loadHtml();
try {
$this->query->queryXpath('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]');
} catch (\Exception $e) {
return ;
}
$this->assertFails('XPath PHPFunctions should be disable by default');
}

public function testXpathPhpFunctionsShouldBeEnableWithoutParameter()
{
$this->loadHtml();
$this->query->registerXpathPhpFunctions();
$result = $this->query->queryXpath('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]');
$this->assertEquals('content-type',
strtolower($result->current()->getAttribute('http-equiv')),
$result->getXpathQuery());
}

public function testXpathPhpFunctionsShouldBeNotCalledWhenSpecifiedFunction()
{
$this->loadHtml();
try {
$this->query->registerXpathPhpFunctions('stripos');
$this->query->queryXpath('//meta[php:functionString("strtolower", @http-equiv) = "content-type"]');
} catch (\Exception $e) {
// $e->getMessage() - Not allowed to call handler 'strtolower()
return ;
}
$this->assertFails('Not allowed to call handler strtolower()');
}

/**
* @group ZF-9243
*/
Expand Down

0 comments on commit 282273e

Please sign in to comment.