bug #16094 Fix the crawler refactoring (stof)

This PR was merged into the 3.0-dev branch.

Discussion
----------

Fix the crawler refactoring

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | n/a
| License       | MIT
| Doc PR        | n/a

This fixes a few mistakes I spotted in #16075 for the DomCrawler component.

Regression tests are added separately in https://github.com/symfony/symfony/pull/16093 to be included in older branches too.

Commits
-------

d128735 Fix the crawler refactoring
This commit is contained in:
Fabien Potencier 2015-10-03 08:28:13 +02:00
commit 38c059fc03
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
*/