Skip to content

Commit 0d1cb3b

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

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-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.', E_USER_DEPRECATED);
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: 17 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

@@ -71,6 +74,14 @@ public function testAddHtmlContent()
7174
$crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
7275

7376
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string');
77+
}
78+
79+
/**
80+
* @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent
81+
*/
82+
public function testAddHtmlContentWithBaseTag()
83+
{
84+
$crawler = new Crawler();
7485

7586
$crawler->addHtmlContent('<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>', 'UTF-8');
7687

@@ -267,6 +278,7 @@ public function testAddNodeList()
267278
*/
268279
public function testAddNodes()
269280
{
281+
$list = array();
270282
foreach ($this->createNodeList() as $node) {
271283
$list[] = $node;
272284
}
@@ -290,7 +302,10 @@ public function testAddNode()
290302

291303
public function testClear()
292304
{
293-
$crawler = new Crawler(new \DOMNode());
305+
$doc = new \DOMDocument();
306+
$node = $doc->createElement('test');
307+
308+
$crawler = new Crawler($node);
294309
$crawler->clear();
295310
$this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler');
296311
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy