[DomCrawler] Enabled default namespace prefix overloading.
This commit is contained in:
parent
943d446e61
commit
be1e4e6585
@ -27,6 +27,11 @@ class Crawler extends \SplObjectStorage
|
|||||||
*/
|
*/
|
||||||
protected $uri;
|
protected $uri;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string The default namespace prefix to be used with XPath and CSS expressions
|
||||||
|
*/
|
||||||
|
private $defaultNamespacePrefix = 'default';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
@ -708,6 +713,16 @@ class Crawler extends \SplObjectStorage
|
|||||||
return $form;
|
return $form;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overloads a default namespace prefix to be used with XPath and CSS expressions.
|
||||||
|
*
|
||||||
|
* @param string $prefix
|
||||||
|
*/
|
||||||
|
public function setDefaultNamespacePrefix($prefix)
|
||||||
|
{
|
||||||
|
$this->defaultNamespacePrefix = $prefix;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts string for XPath expressions.
|
* Converts string for XPath expressions.
|
||||||
*
|
*
|
||||||
@ -806,7 +821,7 @@ class Crawler extends \SplObjectStorage
|
|||||||
|
|
||||||
foreach ($prefixes as $prefix) {
|
foreach ($prefixes as $prefix) {
|
||||||
// ask for one namespace, otherwise we'd get a collection with an item for each node
|
// ask for one namespace, otherwise we'd get a collection with an item for each node
|
||||||
$namespaces = $domxpath->query(sprintf('(//namespace::*[name()="%s"])[last()]', 'default' === $prefix ? '' : $prefix));
|
$namespaces = $domxpath->query(sprintf('(//namespace::*[name()="%s"])[last()]', $this->defaultNamespacePrefix === $prefix ? '' : $prefix));
|
||||||
if ($node = $namespaces->item(0)) {
|
if ($node = $namespaces->item(0)) {
|
||||||
$domxpath->registerNamespace($prefix, $node->nodeValue);
|
$domxpath->registerNamespace($prefix, $node->nodeValue);
|
||||||
} else {
|
} else {
|
||||||
@ -824,7 +839,7 @@ class Crawler extends \SplObjectStorage
|
|||||||
*/
|
*/
|
||||||
private function findNamespacePrefixes($xpath)
|
private function findNamespacePrefixes($xpath)
|
||||||
{
|
{
|
||||||
if (preg_match_all('/(?P<prefix>[a-zA-Z_][a-zA-Z_0-9\-\.]+):[^:]/', $xpath, $matches)) {
|
if (preg_match_all('/(?P<prefix>[a-zA-Z_][a-zA-Z_0-9\-\.]*):[^:]/', $xpath, $matches)) {
|
||||||
return array_unique($matches['prefix']);
|
return array_unique($matches['prefix']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -378,6 +378,16 @@ EOF
|
|||||||
$this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
|
$this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFilterXPathWithCustomDefaultNamespace()
|
||||||
|
{
|
||||||
|
$crawler = $this->createTestXmlCrawler();
|
||||||
|
$crawler->setDefaultNamespacePrefix('x');
|
||||||
|
$crawler = $crawler->filterXPath('//x:entry/x:id');
|
||||||
|
|
||||||
|
$this->assertCount(1, $crawler, '->filterXPath() automatically registers a namespace');
|
||||||
|
$this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
public function testFilterXPathWithNamespace()
|
public function testFilterXPathWithNamespace()
|
||||||
{
|
{
|
||||||
$crawler = $this->createTestXmlCrawler()->filterXPath('//yt:accessControl');
|
$crawler = $this->createTestXmlCrawler()->filterXPath('//yt:accessControl');
|
||||||
|
Reference in New Issue
Block a user