Skip to content

[DomCrawler/CssSelector] Fixed namespace using in filter by xpath #5886

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -487,13 +487,14 @@ public function extract($attributes)
/**
* Filters the list of nodes with an XPath expression.
*
* @param string $xpath An XPath expression
* @param string $xpath An XPath expression
* @param array $namespaces An associative array of namespaces that contains (prefix => uri) elements
*
* @return Crawler A new instance of Crawler with the filtered list of nodes
*
* @api
*/
public function filterXPath($xpath)
public function filterXPath($xpath, array $namespaces = array())
{
$document = new \DOMDocument('1.0', 'UTF-8');
$root = $document->appendChild($document->createElement('_root'));
Expand All @@ -502,6 +503,9 @@ public function filterXPath($xpath)
}

$domxpath = new \DOMXPath($document);
foreach($namespaces as $prefix => $uri) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a missing space after foreach.

$domxpath->registerNamespace($prefix, $uri);
}

return new static($domxpath->query($xpath), $this->uri);
}
Expand All @@ -511,23 +515,24 @@ public function filterXPath($xpath)
*
* This method only works if you have installed the CssSelector Symfony Component.
*
* @param string $selector A CSS selector
* @param string $selector A CSS selector
* @param array $namespaces An associative array of namespaces that contains (prefix => uri) elements
*
* @return Crawler A new instance of Crawler with the filtered list of nodes
*
* @throws \RuntimeException if the CssSelector Component is not available
*
* @api
*/
public function filter($selector)
public function filter($selector, array $namespaces = array())
{
if (!class_exists('Symfony\\Component\\CssSelector\\CssSelector')) {
// @codeCoverageIgnoreStart
throw new \RuntimeException('Unable to filter with a CSS selector as the Symfony CssSelector is not installed (you can use filterXPath instead).');
// @codeCoverageIgnoreEnd
}

return $this->filterXPath(CssSelector::toXPath($selector));
return $this->filterXPath(CssSelector::toXPath($selector), $namespaces);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/Symfony/Tests/Component/DomCrawler/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,29 @@ public function testFilterXPath()
$this->assertEquals(6, count($crawler->filterXPath('//li')), '->filterXPath() filters the node list with the XPath expression');
}

/**
* @covers \Symfony\Component\DomCrawler\Crawler::filterXPath
*/
public function testFilterXPathWithNamespaceSupport()
{
$crawler = $this->createTestCrawlerWithNamespace();
$this->assertNotSame($crawler, $crawler->filterXPath('yt|accessControl'), '->filterXPath() returns a new instance of a crawler');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Crawler', $crawler, '->filterXPath() returns a new instance of a crawler');

$controls = $crawler->filter('ns1|accessControl', array('ns1' => 'http://namespace.uir.com/schema/'));
$this->assertCount(3, $controls , '->filterXPath() filters the node list with the XPath expression');
}

/**
* @expectedException \PHPUnit_Framework_Error_Warning
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not appropriate exception we should expect here.

* @covers \Symfony\Component\DomCrawler\Crawler::filterXPath
*/
public function testFilterXPathWithoutNamespacePassed()
{
$crawler = $this->createTestCrawlerWithNamespace();
$crawler->filter('ns1|accessControl');
}

/**
* @covers Symfony\Component\DomCrawler\Crawler::filter
*/
Expand Down Expand Up @@ -496,6 +519,20 @@ public function createTestCrawler($uri = null)
return new Crawler($dom, $uri);
}

public function createTestCrawlerWithNamespace($uri = null)
{
$dom = new \DOMDocument();
$dom->loadXML('<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:ns1="http://namespace.uir.com/schema/">
<ns1:accessControl action="comment" permission="allowed"/>
<ns1:accessControl action="comment" permission="allowed"/>
<ns1:accessControl action="comment" permission="allowed"/>
</entry>
');

return new Crawler($dom, $uri);
}

protected function createDomDocument()
{
$dom = new \DOMDocument();
Expand Down