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

This commit is contained in:
EdgarPE 2015-12-16 17:46:28 +01:00 committed by Jakub Zalas
parent 352049c505
commit d2872a3189
2 changed files with 54 additions and 27 deletions

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\DomCrawler;
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 @@ class Crawler extends \SplObjectStorage
$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 @@ class Crawler extends \SplObjectStorage
*
* @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')
{
@ -709,6 +705,10 @@ class Crawler extends \SplObjectStorage
$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);
}
@ -716,11 +716,17 @@ class Crawler extends \SplObjectStorage
* 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');
}
@ -735,7 +741,7 @@ class Crawler extends \SplObjectStorage
*
* @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)
{
@ -743,7 +749,13 @@ class Crawler extends \SplObjectStorage
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);
@ -965,12 +977,7 @@ class Crawler extends \SplObjectStorage
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;

View File

@ -47,7 +47,7 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
$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>');
@ -63,16 +63,6 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
$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();
@ -264,7 +254,7 @@ EOF
$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()
@ -527,7 +517,7 @@ EOF
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 +735,26 @@ HTML;
}
}
/**
* @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
@ -817,6 +827,16 @@ HTML;
}
}
/**
* @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');