[DomCrawler] Changed typehints form DomNode to DomElement

Closes #10924
This commit is contained in:
Christophe Coevoet 2014-05-17 15:22:00 +02:00
parent 75ccdb0e37
commit f416e7044c
8 changed files with 37 additions and 30 deletions

View File

@ -1,6 +1,13 @@
CHANGELOG CHANGELOG
========= =========
2.5.0
-----
* [BC BREAK] The typehints on the `Link`, `Form` and `FormField` classes have been changed from
`\DOMNode` to `DOMElement`. Using any other type of `DOMNode` was triggering fatal errors in previous
versions. Code extending these classes will need to update the typehints when overwriting these methods.
2.4.0 2.4.0
----- -----

View File

@ -14,7 +14,7 @@ namespace Symfony\Component\DomCrawler;
use Symfony\Component\CssSelector\CssSelector; use Symfony\Component\CssSelector\CssSelector;
/** /**
* Crawler eases navigation of a list of \DOMNode objects. * Crawler eases navigation of a list of \DOMElement objects.
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
* *

View File

@ -161,11 +161,11 @@ class ChoiceFormField extends FormField
* *
* This method should only be used internally. * This method should only be used internally.
* *
* @param \DOMNode $node A \DOMNode * @param \DOMElement $node
* *
* @throws \LogicException When choice provided is not multiple nor radio * @throws \LogicException When choice provided is not multiple nor radio
*/ */
public function addChoice(\DOMNode $node) public function addChoice(\DOMElement $node)
{ {
if (!$this->multiple && 'radio' != $this->type) { if (!$this->multiple && 'radio' != $this->type) {
throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name)); throw new \LogicException(sprintf('Unable to add a choice for "%s" as it is not multiple or is not a radio button.', $this->name));
@ -259,11 +259,11 @@ class ChoiceFormField extends FormField
/** /**
* Returns option value with associated disabled flag * Returns option value with associated disabled flag
* *
* @param \DOMNode $node * @param \DOMElement $node
* *
* @return array * @return array
*/ */
private function buildOptionValue($node) private function buildOptionValue(\DOMElement $node)
{ {
$option = array(); $option = array();

View File

@ -19,7 +19,7 @@ namespace Symfony\Component\DomCrawler\Field;
abstract class FormField abstract class FormField
{ {
/** /**
* @var \DOMNode * @var \DOMElement
*/ */
protected $node; protected $node;
/** /**
@ -46,9 +46,9 @@ abstract class FormField
/** /**
* Constructor. * Constructor.
* *
* @param \DOMNode $node The node associated with this field * @param \DOMElement $node The node associated with this field
*/ */
public function __construct(\DOMNode $node) public function __construct(\DOMElement $node)
{ {
$this->node = $node; $this->node = $node;
$this->name = $node->getAttribute('name'); $this->name = $node->getAttribute('name');

View File

@ -23,7 +23,7 @@ use Symfony\Component\DomCrawler\Field\FormField;
class Form extends Link implements \ArrayAccess class Form extends Link implements \ArrayAccess
{ {
/** /**
* @var \DOMNode * @var \DOMElement
*/ */
private $button; private $button;
@ -35,15 +35,15 @@ class Form extends Link implements \ArrayAccess
/** /**
* Constructor. * Constructor.
* *
* @param \DOMNode $node A \DOMNode instance * @param \DOMElement $node A \DOMElement instance
* @param string $currentUri The URI of the page where the form is embedded * @param string $currentUri The URI of the page where the form is embedded
* @param string $method The method to use for the link (if null, it defaults to the method defined by the form) * @param string $method The method to use for the link (if null, it defaults to the method defined by the form)
* *
* @throws \LogicException if the node is not a button inside a form tag * @throws \LogicException if the node is not a button inside a form tag
* *
* @api * @api
*/ */
public function __construct(\DOMNode $node, $currentUri, $method = null) public function __construct(\DOMElement $node, $currentUri, $method = null)
{ {
parent::__construct($node, $currentUri, $method); parent::__construct($node, $currentUri, $method);
@ -53,7 +53,7 @@ class Form extends Link implements \ArrayAccess
/** /**
* Gets the form node associated with this form. * Gets the form node associated with this form.
* *
* @return \DOMNode A \DOMNode instance * @return \DOMElement A \DOMElement instance
*/ */
public function getFormNode() public function getFormNode()
{ {
@ -359,13 +359,13 @@ class Form extends Link implements \ArrayAccess
/** /**
* Sets the node for the form. * Sets the node for the form.
* *
* Expects a 'submit' button \DOMNode and finds the corresponding form element. * Expects a 'submit' button \DOMElement and finds the corresponding form element, or the form element itself.
* *
* @param \DOMNode $node A \DOMNode instance * @param \DOMElement $node A \DOMElement instance
* *
* @throws \LogicException If given node is not a button or input or does not have a form ancestor * @throws \LogicException If given node is not a button or input or does not have a form ancestor
*/ */
protected function setNode(\DOMNode $node) protected function setNode(\DOMElement $node)
{ {
$this->button = $node; $this->button = $node;
if ('button' == $node->nodeName || ('input' == $node->nodeName && in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image')))) { if ('button' == $node->nodeName || ('input' == $node->nodeName && in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image')))) {
@ -454,7 +454,7 @@ class Form extends Link implements \ArrayAccess
} }
} }
private function addField(\DOMNode $node) private function addField(\DOMElement $node)
{ {
if (!$node->hasAttribute('name') || !$node->getAttribute('name')) { if (!$node->hasAttribute('name') || !$node->getAttribute('name')) {
return; return;

View File

@ -21,7 +21,7 @@ namespace Symfony\Component\DomCrawler;
class Link class Link
{ {
/** /**
* @var \DOMNode A \DOMNode instance * @var \DOMElement
*/ */
protected $node; protected $node;
/** /**
@ -36,15 +36,15 @@ class Link
/** /**
* Constructor. * Constructor.
* *
* @param \DOMNode $node A \DOMNode instance * @param \DOMElement $node A \DOMElement instance
* @param string $currentUri The URI of the page where the link is embedded (or the base href) * @param string $currentUri The URI of the page where the link is embedded (or the base href)
* @param string $method The method to use for the link (get by default) * @param string $method The method to use for the link (get by default)
* *
* @throws \InvalidArgumentException if the node is not a link * @throws \InvalidArgumentException if the node is not a link
* *
* @api * @api
*/ */
public function __construct(\DOMNode $node, $currentUri, $method = 'GET') public function __construct(\DOMElement $node, $currentUri, $method = 'GET')
{ {
if (!in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) { if (!in_array(strtolower(substr($currentUri, 0, 4)), array('http', 'file'))) {
throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri)); throw new \InvalidArgumentException(sprintf('Current URI must be an absolute URL ("%s").', $currentUri));
@ -58,7 +58,7 @@ class Link
/** /**
* Gets the node associated with this link. * Gets the node associated with this link.
* *
* @return \DOMNode A \DOMNode instance * @return \DOMElement A \DOMElement instance
*/ */
public function getNode() public function getNode()
{ {
@ -180,13 +180,13 @@ class Link
} }
/** /**
* Sets current \DOMNode instance. * Sets current \DOMElement instance.
* *
* @param \DOMNode $node A \DOMNode instance * @param \DOMElement $node A \DOMElement instance
* *
* @throws \LogicException If given node is not an anchor * @throws \LogicException If given node is not an anchor
*/ */
protected function setNode(\DOMNode $node) protected function setNode(\DOMElement $node)
{ {
if ('a' != $node->nodeName && 'area' != $node->nodeName) { if ('a' != $node->nodeName && 'area' != $node->nodeName) {
throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName)); throw new \LogicException(sprintf('Unable to click on a "%s" tag.', $node->nodeName));

View File

@ -47,7 +47,7 @@ class CrawlerTest extends \PHPUnit_Framework_TestCase
$crawler = new Crawler(); $crawler = new Crawler();
$crawler->add($this->createNodeList()->item(0)); $crawler->add($this->createNodeList()->item(0));
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from an \DOMNode'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->add() adds nodes from a \DOMElement');
$crawler = new Crawler(); $crawler = new Crawler();
$crawler->add('<html><body>Foo</body></html>'); $crawler->add('<html><body>Foo</body></html>');
@ -280,7 +280,7 @@ EOF
$crawler = new Crawler(); $crawler = new Crawler();
$crawler->addNode($this->createNodeList()->item(0)); $crawler->addNode($this->createNodeList()->item(0));
$this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from an \DOMNode'); $this->assertEquals('foo', $crawler->filterXPath('//div')->attr('class'), '->addNode() adds nodes from a \DOMElement');
} }
public function testClear() public function testClear()

View File

@ -191,7 +191,7 @@ class ChoiceFormFieldTest extends FormFieldTestCase
$this->assertNull($field->getValue(), '->getValue() returns null if the checkbox is not checked'); $this->assertNull($field->getValue(), '->getValue() returns null if the checkbox is not checked');
$this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes'); $this->assertFalse($field->isMultiple(), '->hasValue() returns false for checkboxes');
try { try {
$field->addChoice(new \DOMNode()); $field->addChoice(new \DOMElement('input'));
$this->fail('->addChoice() throws a \LogicException for checkboxes'); $this->fail('->addChoice() throws a \LogicException for checkboxes');
} catch (\LogicException $e) { } catch (\LogicException $e) {
$this->assertTrue(true, '->initialize() throws a \LogicException for checkboxes'); $this->assertTrue(true, '->initialize() throws a \LogicException for checkboxes');