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

Skip to content

Commit 5a8e2ad

Browse files
committed
Deprecate loading multiple documents in the same crawler
1 parent 99745e1 commit 5a8e2ad

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ class Crawler extends \SplObjectStorage
4040
*/
4141
private $baseHref;
4242

43+
/**
44+
* @var \DOMDocument|null
45+
*/
46+
private $document;
47+
4348
/**
4449
* Whether the Crawler contains HTML or XML content (used when converting CSS to XPath).
4550
*
@@ -68,6 +73,7 @@ public function __construct($node = null, $currentUri = null, $baseHref = null)
6873
public function clear()
6974
{
7075
parent::removeAll($this);
76+
$this->document = null;
7177
}
7278

7379
/**
@@ -307,6 +313,14 @@ public function addNodes(array $nodes)
307313
*/
308314
public function addNode(\DOMNode $node)
309315
{
316+
if (null !== $this->document && $this->document !== $node->ownerDocument) {
317+
@trigger_error('Attaching DOM nodes from multiple documents in a Crawler is deprecated as of 2.8 and will be forbidden in 3.0.');
318+
}
319+
320+
if (null === $this->document) {
321+
$this->document = $node->ownerDocument;
322+
}
323+
310324
if ($node instanceof \DOMDocument) {
311325
parent::attach($node->documentElement);
312326
} else {
@@ -1152,6 +1166,7 @@ private function createSubCrawler($nodes)
11521166
{
11531167
$crawler = new static($nodes, $this->uri, $this->baseHref);
11541168
$crawler->isHtml = $this->isHtml;
1169+
$crawler->document = $this->document;
11551170

11561171
return $crawler;
11571172
}

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ public function testConstructor()
2020
$crawler = new Crawler();
2121
$this->assertCount(0, $crawler, '__construct() returns an empty crawler');
2222

23-
$crawler = new Crawler(new \DOMNode());
23+
$doc = new \DOMDocument();
24+
$node = $doc->createElement('test');
25+
26+
$crawler = new Crawler($node);
2427
$this->assertCount(1, $crawler, '__construct() takes a node as a first argument');
2528
}
2629

@@ -290,7 +293,10 @@ public function testAddNode()
290293

291294
public function testClear()
292295
{
293-
$crawler = new Crawler(new \DOMNode());
296+
$doc = new \DOMDocument();
297+
$node = $doc->createElement('test');
298+
299+
$crawler = new Crawler($node);
294300
$crawler->clear();
295301
$this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler');
296302
}

0 commit comments

Comments
 (0)