Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[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 1 commit
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
Next Next commit
Backport to 2.8 of [DomCrawler] Revert previous restriction, allow se…
…lection of every DOMNode object #17021
  • Loading branch information
EdgarPE committed Dec 22, 2015
commit 4ee27990a3a698bd0cad26e6968ebe85a61976a6
35 changes: 21 additions & 14 deletions 35 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 contains non DOMElement instances
*/
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 current node list should contain only DOMElement instances, '%s' found.", get_class($node)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Enclose the message in single quotes please.

}

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)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Enclose the message in single quotes please.

}

$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 contains non DOMElement instances
*/
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 current node list should contain only DOMElement instances, '%s' found.", get_class($node)));
Copy link
Contributor

Choose a reason for hiding this comment

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

Enclose the message in single quotes please.

}

$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
45 changes: 37 additions & 8 deletions 45 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 an \DOMNode');

$crawler = new Crawler();
$crawler->add('<html><body>Foo</body></html>');
Expand All @@ -64,13 +64,12 @@ public function testAddInvalidType()
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Nodes set in a Crawler must be DOMElement or DOMDocument instances, "DOMNode" given.
* @group legacy
*/
public function testAddInvalidNode()
public function testAddMultipleDocumentNode()
{
$crawler = new Crawler();
$crawler->add(new \DOMNode());
$crawler = $this->createTestCrawler();
$crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
Copy link
Member

Choose a reason for hiding this comment

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

Why are you adding a test without any assertion here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I wanted to test the deprecation notice, but it's out of scope of this PR, so just removed it.

}

public function testAddHtmlContent()
Expand Down Expand Up @@ -264,7 +263,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 an \DOMNode');
}

public function testClear()
Expand Down Expand Up @@ -527,7 +526,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 +744,26 @@ public function testLink()
}
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /^The current node list should contain only DOMElement instances/
*/
public function testInvalidLink()
{
$crawler = $this->createTestCrawler('http://example.com/bar/');
$crawler->filterXPath('//li/text()')->link();
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /^The current node list should contain only DOMElement instances/
*/
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 +836,16 @@ public function testForm()
}
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /^The current node list should contain only DOMElement instances/
*/
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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.