Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38c059f

Browse files
committed
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 #16093 to be included in older branches too. Commits ------- d128735 Fix the crawler refactoring
2 parents 91cab2e + d128735 commit 38c059f

File tree

2 files changed

+26
-14
lines changed

2 files changed

+26
-14
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Fabien Potencier <[email protected]>
2020
*/
21-
class Crawler implements \Countable
21+
class Crawler implements \Countable, \IteratorAggregate
2222
{
2323
/**
2424
* @var string The current URI
@@ -46,7 +46,7 @@ class Crawler implements \Countable
4646
private $document;
4747

4848
/**
49-
* @var \DOMNode[]
49+
* @var \DOMElement[]
5050
*/
5151
private $nodes = array();
5252

@@ -327,25 +327,19 @@ public function addNode(\DOMNode $node)
327327
}
328328

329329
if (null !== $this->document && $this->document !== $node->ownerDocument) {
330-
@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);
330+
throw new \InvalidArgumentException('Attaching DOM nodes from multiple documents in the same crawler is forbidden.');
331331
}
332332

333333
if (null === $this->document) {
334334
$this->document = $node->ownerDocument;
335335
}
336336

337-
$this->nodes[] = $node;
338-
}
339-
340-
// Serializing and unserializing a crawler creates DOM objects in a corrupted state. DOM elements are not properly serializable.
341-
public function unserialize($serialized)
342-
{
343-
throw new \BadMethodCallException('A Crawler cannot be serialized.');
344-
}
337+
// Don't add duplicate nodes in the Crawler
338+
if (in_array($node, $this->nodes, true)) {
339+
return;
340+
}
345341

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

351345
/**
@@ -966,6 +960,14 @@ public function count()
966960
return count($this->nodes);
967961
}
968962

963+
/**
964+
* @return \ArrayIterator
965+
*/
966+
public function getIterator()
967+
{
968+
return new \ArrayIterator($this->nodes);
969+
}
970+
969971
/**
970972
* @param \DOMElement $node
971973
* @param string $siblingDir

src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ public function testAddInvalidNode()
7676
$crawler->add(new \DOMNode());
7777
}
7878

79+
/**
80+
* @expectedException \InvalidArgumentException
81+
* @expectedExceptionMessage Attaching DOM nodes from multiple documents in the same crawler is forbidden.
82+
*/
83+
public function testAddMultipleDocumentNode()
84+
{
85+
$crawler = $this->createTestCrawler();
86+
$crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
87+
}
88+
7989
/**
8090
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
8191
*/

0 commit comments

Comments
 (0)