-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Changes from 1 commit
4ee2799
9dacd9a
8543eb1
60db47e
26b50cc
fb481f3
58de1fa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…lection of every DOMNode object #17021
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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> | ||
*/ | ||
|
@@ -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); | ||
} | ||
|
@@ -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') | ||
{ | ||
|
@@ -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))); | ||
} | ||
|
||
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))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
} | ||
|
||
|
@@ -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))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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>'); | ||
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you adding a test without any assertion here ? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
|
@@ -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() | ||
|
@@ -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() | ||
|
@@ -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 | ||
|
@@ -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'); | ||
|
There was a problem hiding this comment.
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.