Fix the crawler refactoring

This commit is contained in:
Christophe Coevoet 2015-10-03 01:19:52 +02:00
parent d606e76ecf
commit d128735803
2 changed files with 26 additions and 14 deletions

View File

@ -18,7 +18,7 @@ use Symfony\Component\CssSelector\CssSelectorConverter;
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Crawler implements \Countable
class Crawler implements \Countable, \IteratorAggregate
{
/**
* @var string The current URI
@ -46,7 +46,7 @@ class Crawler implements \Countable
private $document;
/**
* @var \DOMNode[]
* @var \DOMElement[]
*/
private $nodes = array();
@ -327,27 +327,21 @@ class Crawler implements \Countable
}
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);
throw new \InvalidArgumentException('Attaching DOM nodes from multiple documents in the same crawler is forbidden.');
}
if (null === $this->document) {
$this->document = $node->ownerDocument;
}
// Don't add duplicate nodes in the Crawler
if (in_array($node, $this->nodes, true)) {
return;
}
$this->nodes[] = $node;
}
// Serializing and unserializing a crawler creates DOM objects in a corrupted state. DOM elements are not properly serializable.
public function unserialize($serialized)
{
throw new \BadMethodCallException('A Crawler cannot be serialized.');
}
public function serialize()
{
throw new \BadMethodCallException('A Crawler cannot be serialized.');
}
/**
* Returns a node given its position in the node list.
*
@ -966,6 +960,14 @@ class Crawler implements \Countable
return count($this->nodes);
}
/**
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->nodes);
}
/**
* @param \DOMElement $node
* @param string $siblingDir

View File

@ -76,6 +76,16 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
$crawler->add(new \DOMNode());
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Attaching DOM nodes from multiple documents in the same crawler is forbidden.
*/
public function testAddMultipleDocumentNode()
{
$crawler = $this->createTestCrawler();
$crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
}
/**
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
*/