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

Commit d2872a3

Browse filesBrowse files
EdgarPEjakzal
authored andcommitted
[DomCrawler] Revert previous restriction, allow selection of every DOMNode object
1 parent 352049c commit d2872a3
Copy full SHA for d2872a3

File tree

Expand file treeCollapse file tree

2 files changed

+54
-27
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+54
-27
lines changed

‎src/Symfony/Component/DomCrawler/Crawler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Crawler.php
+21-14Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\CssSelector\CssSelectorConverter;
1515

1616
/**
17-
* Crawler eases navigation of a list of \DOMElement objects.
17+
* Crawler eases navigation of a list of \DOMNode objects.
1818
*
1919
* @author Fabien Potencier <fabien@symfony.com>
2020
*/
@@ -290,10 +290,6 @@ public function addNode(\DOMNode $node)
290290
$node = $node->documentElement;
291291
}
292292

293-
if (!$node instanceof \DOMElement) {
294-
throw new \InvalidArgumentException(sprintf('Nodes set in a Crawler must be DOMElement or DOMDocument instances, "%s" given.', get_class($node)));
295-
}
296-
297293
if (null !== $this->document && $this->document !== $node->ownerDocument) {
298294
@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);
299295
}
@@ -699,7 +695,7 @@ public function selectButton($value)
699695
*
700696
* @return Link A Link instance
701697
*
702-
* @throws \InvalidArgumentException If the current node list is empty
698+
* @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
703699
*/
704700
public function link($method = 'get')
705701
{
@@ -709,18 +705,28 @@ public function link($method = 'get')
709705

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

708+
if (!$node instanceof \DOMElement) {
709+
throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node)));
710+
}
711+
712712
return new Link($node, $this->baseHref, $method);
713713
}
714714

715715
/**
716716
* Returns an array of Link objects for the nodes in the list.
717717
*
718718
* @return Link[] An array of Link instances
719+
*
720+
* @throws \InvalidArgumentException If the current node list contains non-DOMElement instances
719721
*/
720722
public function links()
721723
{
722724
$links = array();
723725
foreach ($this as $node) {
726+
if (!$node instanceof \DOMElement) {
727+
throw new \InvalidArgumentException(sprintf('The current node list should contain only DOMElement instances, "%s" found.', get_class($node)));
728+
}
729+
724730
$links[] = new Link($node, $this->baseHref, 'get');
725731
}
726732

@@ -735,15 +741,21 @@ public function links()
735741
*
736742
* @return Form A Form instance
737743
*
738-
* @throws \InvalidArgumentException If the current node list is empty
744+
* @throws \InvalidArgumentException If the current node list is empty or the selected node is not instance of DOMElement
739745
*/
740746
public function form(array $values = null, $method = null)
741747
{
742748
if (!count($this)) {
743749
throw new \InvalidArgumentException('The current node list is empty.');
744750
}
745751

746-
$form = new Form($this->getNode(0), $this->uri, $method, $this->baseHref);
752+
$node = $this->getNode(0);
753+
754+
if (!$node instanceof \DOMElement) {
755+
throw new \InvalidArgumentException(sprintf('The selected node should be instance of DOMElement, got "%s".', get_class($node)));
756+
}
757+
758+
$form = new Form($node, $this->uri, $method, $this->baseHref);
747759

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

966978
foreach ($this as $node) {
967979
$domxpath = $this->createDOMXPath($node->ownerDocument, $prefixes);
968-
969-
foreach ($domxpath->query($xpath, $node) as $subNode) {
970-
if ($subNode->nodeType === 1) {
971-
$crawler->add($subNode);
972-
}
973-
}
980+
$crawler->add($domxpath->query($xpath, $node));
974981
}
975982

976983
return $crawler;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
+33-13Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function testAdd()
4747

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

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

66-
/**
67-
* @expectedException \InvalidArgumentException
68-
* @expectedExceptionMessage Nodes set in a Crawler must be DOMElement or DOMDocument instances, "DOMNode" given.
69-
*/
70-
public function testAddInvalidNode()
71-
{
72-
$crawler = new Crawler();
73-
$crawler->add(new \DOMNode());
74-
}
75-
7666
public function testAddHtmlContent()
7767
{
7868
$crawler = new Crawler();
@@ -264,7 +254,7 @@ public function testAddNode()
264254
$crawler = new Crawler();
265255
$crawler->addNode($this->createNodeList()->item(0));
266256

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

270260
public function testClear()
@@ -527,7 +517,7 @@ public function testFilterXPathWithAttributeAxis()
527517

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

533523
public function testFilterXPathWithChildAxis()
@@ -745,6 +735,26 @@ public function testLink()
745735
}
746736
}
747737

738+
/**
739+
* @expectedException \InvalidArgumentException
740+
* @expectedExceptionMessage The selected node should be instance of DOMElement
741+
*/
742+
public function testInvalidLink()
743+
{
744+
$crawler = $this->createTestCrawler('http://example.com/bar/');
745+
$crawler->filterXPath('//li/text()')->link();
746+
}
747+
748+
/**
749+
* @expectedException \InvalidArgumentException
750+
* @expectedExceptionMessage The selected node should be instance of DOMElement
751+
*/
752+
public function testInvalidLinks()
753+
{
754+
$crawler = $this->createTestCrawler('http://example.com/bar/');
755+
$crawler->filterXPath('//li/text()')->link();
756+
}
757+
748758
public function testSelectLinkAndLinkFiltered()
749759
{
750760
$html = <<<HTML
@@ -817,6 +827,16 @@ public function testForm()
817827
}
818828
}
819829

830+
/**
831+
* @expectedException \InvalidArgumentException
832+
* @expectedExceptionMessage The selected node should be instance of DOMElement
833+
*/
834+
public function testInvalidForm()
835+
{
836+
$crawler = $this->createTestCrawler('http://example.com/bar/');
837+
$crawler->filterXPath('//li/text()')->form();
838+
}
839+
820840
public function testLast()
821841
{
822842
$crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.