8000 Fix the crawler refactoring by stof · Pull Request #16094 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fix the crawler refactoring #16094

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

Merged
merged 1 commit into from
Oct 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Crawler implements \Countable
class Crawler implements \Countable, \IteratorAggregate
{
/**
* @var string The current URI
Expand Down Expand Up @@ -46,7 +46,7 @@ class Crawler implements \Countable
private $document;

/**
* @var \DOMNode[]
* @var \DOMElement[]
*/
private $nodes = array();

Expand Down Expand Up @@ -327,25 +327,19 @@ public function addNode(\DOMNode $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);
throw new \InvalidArgumentException('Attaching DOM nodes from multiple documents in the same crawler is forbidden.');
}

if (null === $this->document) {
$this->document = $node->ownerDocument;
}

$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.');
}
// Don't add duplicate nodes in the Crawler
if (in_array($node, $this->nodes, true)) {
return;
}

public function serialize()
{
throw new \BadMethodCallException('A Crawler cannot be serialized.');
$this->nodes[] = $node;
}

/**
Expand Down Expand Up @@ -966,6 +960,14 @@ public function count()
return count($this->nodes);
}

/**
* @return \ArrayIterator
*/
public function getIterator()
{
return new \ArrayIterator($this->nodes);
}

/**
* @param \DOMElement $node
* @param string $siblingDir
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public function testAddInvalidNode()
$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
*/
Expand Down
0