From 0d1cb3bd1bc4f9726ad987cba8c2fa298187e9d0 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Fri, 2 Oct 2015 00:41:47 +0200 Subject: [PATCH] Deprecate loading multiple documents in the same crawler --- src/Symfony/Component/DomCrawler/Crawler.php | 15 +++++++++++++++ .../DomCrawler/Tests/CrawlerTest.php | 19 +++++++++++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index d4e452f6f0..fae8cc6249 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -40,6 +40,11 @@ class Crawler extends \SplObjectStorage */ private $baseHref; + /** + * @var \DOMDocument|null + */ + private $document; + /** * Whether the Crawler contains HTML or XML content (used when converting CSS to XPath). * @@ -68,6 +73,7 @@ class Crawler extends \SplObjectStorage public function clear() { parent::removeAll($this); + $this->document = null; } /** @@ -307,6 +313,14 @@ class Crawler extends \SplObjectStorage */ 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); + } + + if (null === $this->document) { + $this->document = $node->ownerDocument; + } + if ($node instanceof \DOMDocument) { parent::attach($node->documentElement); } else { @@ -1152,6 +1166,7 @@ class Crawler extends \SplObjectStorage { $crawler = new static($nodes, $this->uri, $this->baseHref); $crawler->isHtml = $this->isHtml; + $crawler->document = $this->document; return $crawler; } diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index cdd87dd7a8..f3a5b40b5a 100755 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -20,7 +20,10 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase $crawler = new Crawler(); $this->assertCount(0, $crawler, '__construct() returns an empty crawler'); - $crawler = new Crawler(new \DOMNode()); + $doc = new \DOMDocument(); + $node = $doc->createElement('test'); + + $crawler = new Crawler($node); $this->assertCount(1, $crawler, '__construct() takes a node as a first argument'); } @@ -71,6 +74,14 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase $crawler->addHtmlContent('
', 'UTF-8'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string'); + } + + /** + * @covers Symfony\Component\DomCrawler\Crawler::addHtmlContent + */ + public function testAddHtmlContentWithBaseTag() + { + $crawler = new Crawler(); $crawler->addHtmlContent('', 'UTF-8'); @@ -267,6 +278,7 @@ EOF */ public function testAddNodes() { + $list = array(); foreach ($this->createNodeList() as $node) { $list[] = $node; } @@ -290,7 +302,10 @@ EOF public function testClear() { - $crawler = new Crawler(new \DOMNode()); + $doc = new \DOMDocument(); + $node = $doc->createElement('test'); + + $crawler = new Crawler($node); $crawler->clear(); $this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler'); }