8000 Prevent adding non-DOMElement elements in DomCrawler by stof · Pull Request #16058 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Prevent adding non-DOMElement elements in DomCrawler #16058

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

Merged
merged 1 commit into from
Oct 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments. 8000
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prevent adding non-DOMElement elements in DomCrawler
Many methods of the DomCrawler component are relying on the DOMElement
API, not only on the DOMNode API.
  • Loading branch information
stof committed Oct 2, 2015
commit 9f362a12f616984676182f69aab18c662b47445d
17 changes: 13 additions & 4 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,10 +308,14 @@ public function addNodes(array $nodes)
public function addNode(\DOMNode $node)
{
if ($node instanceof \DOMDocument) {
parent::attach($node->documentElement);
} else {
parent::attach($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)));
}

parent::attach($node);
}

// Serializing and unserializing a crawler creates DOM objects in a corrupted state. DOM elements are not properly serializable.
Expand Down Expand Up @@ -974,7 +978,12 @@ private function filterRelativeXPath($xpath)

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

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

return $crawler;
Expand Down
26 changes: 22 additions & 4 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public function testConstructor()
$crawler = new Crawler();
$this->assertCount(0, $crawler, '__construct() returns an empty crawler');

$crawler = new Crawler(new \DOMNode());
$doc = new \DOMDocument();
$node = $doc->createElement('test');

$crawler = new Crawler($node);
$this->assertCount(1, $crawler, '__construct() takes a node as a first argument');
}

Expand All @@ -37,6 +40,7 @@ public function testAdd()
$crawler->add($this->createNodeList());
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMNodeList');

$list = array();
foreach ($this->createNodeList() as $node) {
$list[] = $node;
}
Expand All @@ -56,12 +60,22 @@ public function testAdd()
/**
* @expectedException \InvalidArgumentException
*/
public function testAddInvalidNode()
public function testAddInvalidType()
{
$crawler = new Crawler();
$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());
}
Copy link
Member

Choose a reason for hiding this comment

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

This test does not pass anymore because of the trigger added by the other PR. @stof Can you submit a PR to fix this? Thanks.

Copy link
Member Author

Choose a reason for hiding this comment

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

this is why I said I would rebase to fix conflicts: the order of triggers should be reverted (first the validation, then the deprecation). I will send a PR

Copy link
Member Author

Choose a reason for hiding this comment

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

see #16073 for the PR


/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/
Expand Down Expand Up @@ -267,6 +281,7 @@ public function testAddNodeList()
*/
public function testAddNodes()
{
$list = array();
foreach ($this->createNodeList() as $node) {
$list[] = $node;
}
Expand All @@ -290,7 +305,10 @@ public function testAddNode()

public function testClear()
{
$crawler = new Crawler(new \DOMNode());
$doc = new \DOMDocument();
$node = $doc->createElement('test');

$crawler = new Crawler($node);
$crawler->clear();
$this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler');
}
Expand Down Expand Up @@ -526,7 +544,7 @@ public function testFilterXPathWithAttributeAxis()

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