Deprecate loading multiple documents in the same crawler

This commit is contained in:
Christophe Coevoet 2015-10-02 00:41:47 +02:00
parent 99745e12a7
commit 0d1cb3bd1b
2 changed files with 32 additions and 2 deletions

View File

@ -40,6 +40,11 @@ class Crawler extends \SplObjectStorage
*/ */
private $baseHref; private $baseHref;
/**
* @var \DOMDocument|null
*/
private $document;
/** /**
* Whether the Crawler contains HTML or XML content (used when converting CSS to XPath). * 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() public function clear()
{ {
parent::removeAll($this); parent::removeAll($this);
$this->document = null;
} }
/** /**
@ -307,6 +313,14 @@ class Crawler extends \SplObjectStorage
*/ */
public function addNode(\DOMNode $node) 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) { if ($node instanceof \DOMDocument) {
parent::attach($node->documentElement); parent::attach($node->documentElement);
} else { } else {
@ -1152,6 +1166,7 @@ class Crawler extends \SplObjectStorage
{ {
$crawler = new static($nodes, $this->uri, $this->baseHref); $crawler = new static($nodes, $this->uri, $this->baseHref);
$crawler->isHtml = $this->isHtml; $crawler->isHtml = $this->isHtml;
$crawler->document = $this->document;
return $crawler; return $crawler;
} }

View File

@ -20,7 +20,10 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
$crawler = new Crawler(); $crawler = new Crawler();
$this->assertCount(0, $crawler, '__construct() returns an empty 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'); $this->assertCount(1, $crawler, '__construct() takes a node as a first argument');
} }
@ -71,6 +74,14 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
$crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8'); $crawler->addHtmlContent('<html><div class="foo"></html>', 'UTF-8');
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addHtmlContent() adds nodes from an HTML string'); $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('<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>', 'UTF-8'); $crawler->addHtmlContent('<html><head><base href="http://symfony.com"></head><a href="/contact"></a></html>', 'UTF-8');
@ -267,6 +278,7 @@ EOF
*/ */
public function testAddNodes() public function testAddNodes()
{ {
$list = array();
foreach ($this->createNodeList() as $node) { foreach ($this->createNodeList() as $node) {
$list[] = $node; $list[] = $node;
} }
@ -290,7 +302,10 @@ EOF
public function testClear() public function testClear()
{ {
$crawler = new Crawler(new \DOMNode()); $doc = new \DOMDocument();
$node = $doc->createElement('test');
$crawler = new Crawler($node);
$crawler->clear(); $crawler->clear();
$this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler'); $this->assertCount(0, $crawler, '->clear() removes all the nodes from the crawler');
} }