Skip to content

[DomCrawler] Revert previous restriction, allow selection of every DOMNode object #17035

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 7 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
35 changes: 21 additions & 14 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\CssSelector\CssSelectorConverter;

/**
* Crawler eases navigation of a list of \DOMElement objects.
* Crawler eases navigation of a list of \DOMNode objects.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
Expand Down Expand Up @@ -290,10 +290,6 @@ public function addNode(\DOMNode $node)
$node = $node->documentElement;
}

if (!$node instanceof \DOMElement) {
throw new \InvalidArgumentException(sprintf('Nodes set in a Crawler must be DOMElement or DOMDocument instances, "%s" given.', get_class($node)));
}

if (null !== $this->document && $this->document !== $node->ownerDocument) {
@trigger_error('Attaching DOM nodes from multiple documents in a Crawler is deprecated as of 2.8 and will be forbidden in 3.0.', E_USER_DEPRECATED);
}
Expand Down Expand Up @@ -699,7 +695,7 @@ public function selectButton($value)
*
* @return Link A Link instance
*
* @throws \InvalidArgumentException If the current node list is empty
* @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
*/
public function link($method = 'get')
{
Expand All @@ -709,18 +705,28 @@ public function link($method = 'get')

$node = $this->getNode(0);

if (!$node instanceof \DOMElement) {
throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node)));
}

return new Link($node, $this->baseHref, $method);
}

/**
* Returns an array of Link objects for the nodes in the list.
*
* @return Link[] An array of Link instances
*
* @throws \InvalidArgumentException If the current node list contains non-DOMElement instances
*/
public function links()
{
$links = array();
foreach ($this as $node) {
if (!$node instanceof \DOMElement) {
throw new \InvalidArgumentException(sprintf('The current node list should contain only DOMElement instances, "%s" found.', get_class($node)));
}

$links[] = new Link($node, $this->baseHref, 'get');
}

Expand All @@ -735,15 +741,21 @@ public function links()
*
* @return Form A Form instance
*
* @throws \InvalidArgumentException If the current node list is empty
* @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
*/
public function form(array $values = null, $method = null)
{
if (!count($this)) {
throw new \InvalidArgumentException('The current node list is empty.');
}

$form = new Form($this->getNode(0), $this->uri, $method, $this->baseHref);
$node = $this->getNode(0);

if (!$node instanceof \DOMElement) {
throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node)));
}

$form = new Form($node, $this->uri, $method, $this->baseHref);

if (null !== $values) {
$form->setValues($values);
Expand Down Expand Up @@ -965,12 +977,7 @@ private function filterRelativeXPath($xpath)

foreach ($this as $node) {
$domxpath = $this->createDOMXPath($node->ownerDocument, $prefixes);

foreach ($domxpath->query($xpath, $node) as $subNode) {
if ($subNode->nodeType === 1) {
$crawler->add($subNode);
}
}
$crawler->add($domxpath->query($xpath, $node));
}

return $crawler;
Expand Down
46 changes: 33 additions & 13 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function testAdd()

$crawler = new Crawler();
$crawler->add($this->createNodeList()->item(0));
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMElement');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNode');

$crawler = new Crawler();
$crawler->add('<html><body>Foo</body></html>');
Expand All @@ -63,16 +63,6 @@ public function testAddInvalidType()
$crawler->add(1);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Nodes set in a Crawler must be DOMElement or DOMDocument instances, "DOMNode" given.
*/
public function testAddInvalidNode()
{
$crawler = new Crawler();
$crawler->add(new \DOMNode());
}

public function testAddHtmlContent()
{
$crawler = new Crawler();
Expand Down Expand Up @@ -264,7 +254,7 @@ public function testAddNode()
$crawler = new Crawler();
$crawler->addNode($this->createNodeList()->item(0));

$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from a \DOMElement');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from a \DOMNode');
}

public function testClear()
Expand Down Expand Up @@ -527,7 +517,7 @@ public function testFilterXPathWithAttributeAxis()

public function testFilterXPathWithAttributeAxisAfterElementAxis()
{
$this->assertCount(0, $this->createTestCrawler()->filterXPath('//form/button/attribute::*'), '->filterXPath() handles attribute axes properly when they are preceded by an element filtering axis');
$this->assertCount(3, $this->createTestCrawler()->filterXPath('//form/button/attribute::*'), '->filterXPath() handles attribute axes properly when they are preceded by an element filtering axis');
}

public function testFilterXPathWithChildAxis()
Expand Down Expand Up @@ -745,6 +735,26 @@ public function testLink()
}
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The selected node should be instance of DOMElement
*/
public function testInvalidLink()
{
$crawler = $this->createTestCrawler('http://example.com/bar/');
$crawler->filterXPath('//li/text()')->link();
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The selected node should be instance of DOMElement
*/
public function testInvalidLinks()
{
$crawler = $this->createTestCrawler('http://example.com/bar/');
$crawler->filterXPath('//li/text()')->link();
}

public function testSelectLinkAndLinkFiltered()
{
$html = <<<HTML
Expand Down Expand Up @@ -817,6 +827,16 @@ public function testForm()
}
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage The selected node should be instance of DOMElement
*/
public function testInvalidForm()
{
$crawler = $this->createTestCrawler('http://example.com/bar/');
$crawler->filterXPath('//li/text()')->form();
}

public function testLast()
{
$crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
Expand Down