Skip to content

Commit be1e4e6

Browse files
committed
[DomCrawler] Enabled default namespace prefix overloading.
1 parent 943d446 commit be1e4e6

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class Crawler extends \SplObjectStorage
2727
*/
2828
protected $uri;
2929

30+
/**
31+
* @var string The default namespace prefix to be used with XPath and CSS expressions
32+
*/
33+
private $defaultNamespacePrefix = 'default';
34+
3035
/**
3136
* Constructor.
3237
*
@@ -708,6 +713,16 @@ public function form(array $values = null, $method = null)
708713
return $form;
709714
}
710715

716+
/**
717+
* Overloads a default namespace prefix to be used with XPath and CSS expressions.
718+
*
719+
* @param string $prefix
720+
*/
721+
public function setDefaultNamespacePrefix($prefix)
722+
{
723+
$this->defaultNamespacePrefix = $prefix;
724+
}
725+
711726
/**
712727
* Converts string for XPath expressions.
713728
*
@@ -806,7 +821,7 @@ private function createDOMXPath(\DOMDocument $document, array $prefixes = array(
806821

807822
foreach ($prefixes as $prefix) {
808823
// ask for one namespace, otherwise we'd get a collection with an item for each node
809-
$namespaces = $domxpath->query(sprintf('(//namespace::*[name()="%s"])[last()]', 'default' === $prefix ? '' : $prefix));
824+
$namespaces = $domxpath->query(sprintf('(//namespace::*[name()="%s"])[last()]', $this->defaultNamespacePrefix === $prefix ? '' : $prefix));
810825
if ($node = $namespaces->item(0)) {
811826
$domxpath->registerNamespace($prefix, $node->nodeValue);
812827
} else {
@@ -824,7 +839,7 @@ private function createDOMXPath(\DOMDocument $document, array $prefixes = array(
824839
*/
825840
private function findNamespacePrefixes($xpath)
826841
{
827-
if (preg_match_all('/(?P<prefix>[a-zA-Z_][a-zA-Z_0-9\-\.]+):[^:]/', $xpath, $matches)) {
842+
if (preg_match_all('/(?P<prefix>[a-zA-Z_][a-zA-Z_0-9\-\.]*):[^:]/', $xpath, $matches)) {
828843
return array_unique($matches['prefix']);
829844
}
830845

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,16 @@ public function testFilterXPathWithDefaultNamespace()
378378
$this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
379379
}
380380

381+
public function testFilterXPathWithCustomDefaultNamespace()
382+
{
383+
$crawler = $this->createTestXmlCrawler();
384+
$crawler->setDefaultNamespacePrefix('x');
385+
$crawler = $crawler->filterXPath('//x:entry/x:id');
386+
387+
$this->assertCount(1, $crawler, '->filterXPath() automatically registers a namespace');
388+
$this->assertSame('tag:youtube.com,2008:video:kgZRZmEc9j4', $crawler->text());
389+
}
390+
381391
public function testFilterXPathWithNamespace()
382392
{
383393
$crawler = $this->createTestXmlCrawler()->filterXPath('//yt:accessControl');

0 commit comments

Comments
 (0)