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('