Permalink
Browse files

[DomCrawler] added support for HTML5 'form' attribute

  • Loading branch information...
1 parent ea79360 commit f8178dd1bb53f192b8e20fd002ec507ac3711199 @kepten kepten committed Mar 27, 2013
@@ -5,6 +5,7 @@ CHANGELOG
-----
* added schema relative URL support to links
+ * added support for HTML5 'form' attribute
2.2.0
-----
@@ -331,7 +331,9 @@ public function offsetUnset($name)
}
/**
- * Sets current \DOMNode instance.
+ * Sets the node for the form.
+ *
+ * Expects a 'submit' button \DOMNode and finds the corresponding form element.
*
* @param \DOMNode $node A \DOMNode instance
*
@@ -341,8 +343,18 @@ protected function setNode(\DOMNode $node)
{
$this->button = $node;
if ('button' == $node->nodeName || ('input' == $node->nodeName && in_array($node->getAttribute('type'), array('submit', 'button', 'image')))) {
+ if ($node->hasAttribute('form')) {
+ // if the node has the HTML5-compliant 'form' attribute, use it
+ $formId = $node->getAttribute('form');
+ $form = $node->ownerDocument->getElementById($formId);
+ if (null === $form) {
+ throw new \LogicException(sprintf('The selected node has an invalid form attribute (%s).', $formId));
+ }
+ $this->node = $form;
+ return;
+ }
+ // we loop until we find a form ancestor
do {
- // use the ancestor form element
if (null === $node = $node->parentNode) {
throw new \LogicException('The selected node does not have a form ancestor.');
}
@@ -366,30 +378,47 @@ private function initialize()
$root->appendChild($button);
$xpath = new \DOMXPath($document);
- foreach ($xpath->query('descendant::input | descendant::button | descendant::textarea | descendant::select', $root) as $node) {
- if (!$node->hasAttribute('name') || !$node->getAttribute('name')) {
- continue;
+ // add descendant elements to the form
+ $fieldNodes = $xpath->query('descendant::input | descendant::button | descendant::textarea | descendant::select', $root);
+ foreach ($fieldNodes as $node) {
+ $this->addField($node, $button);
+ }
+
+ // find form elements corresponding to the current form by the HTML5 form attribute
+ if ($this->node->hasAttribute('id')) {
+ $formId = Crawler::xpathLiteral($this->node->getAttribute('id'));
+ $xpath = new \DOMXPath($this->node->ownerDocument);
+ $fieldNodes = $xpath->query(sprintf('descendant::input[@form=%s] | descendant::button[@form=%s] | descendant::textarea[@form=%s] | descendant::select[@form=%s]', $formId, $formId, $formId, $formId));
+ foreach ($fieldNodes as $node) {
+ $this->addField($node, $button);
}
+ }
+ }
+
+ private function addField(\DOMNode $node, \DOMNode $button)
+ {
+ if (!$node->hasAttribute('name') || !$node->getAttribute('name')) {
+ return;
+ }
- $nodeName = $node->nodeName;
+ $nodeName = $node->nodeName;
- if ($node === $button) {
- $this->set(new Field\InputFormField($node));
- } elseif ('select' == $nodeName || 'input' == $nodeName && 'checkbox' == $node->getAttribute('type')) {
+ if ($node === $button) {
+ $this->set(new Field\InputFormField($node));
+ } elseif ('select' == $nodeName || 'input' == $nodeName && 'checkbox' == $node->getAttribute('type')) {
+ $this->set(new Field\ChoiceFormField($node));
+ } elseif ('input' == $nodeName && 'radio' == $node->getAttribute('type')) {
+ if ($this->has($node->getAttribute('name'))) {
+ $this->get($node->getAttribute('name'))->addChoice($node);
+ } else {
$this->set(new Field\ChoiceFormField($node));
- } elseif ('input' == $nodeName && 'radio' == $node->getAttribute('type')) {
- if ($this->has($node->getAttribute('name'))) {
- $this->get($node->getAttribute('name'))->addChoice($node);
- } else {
- $this->set(new Field\ChoiceFormField($node));
- }
- } elseif ('input' == $nodeName && 'file' == $node->getAttribute('type')) {
- $this->set(new Field\FileFormField($node));
- } elseif ('input' == $nodeName && !in_array($node->getAttribute('type'), array('submit', 'button', 'image'))) {
- $this->set(new Field\InputFormField($node));
- } elseif ('textarea' == $nodeName) {
- $this->set(new Field\TextareaFormField($node));
}
+ } elseif ('input' == $nodeName && 'file' == $node->getAttribute('type')) {
+ $this->set(new Field\FileFormField($node));
+ } elseif ('input' == $nodeName && !in_array($node->getAttribute('type'), array('submit', 'button', 'image'))) {
+ $this->set(new Field\InputFormField($node));
+ } elseif ('textarea' == $nodeName) {
+ $this->set(new Field\TextareaFormField($node));
}
}
}
@@ -393,6 +393,9 @@ public function testSelectButton()
$this->assertEquals(1, $crawler->selectButton('BarValue')->count(), '->selectButton() selects buttons');
$this->assertEquals(1, $crawler->selectButton('BarName')->count(), '->selectButton() selects buttons');
$this->assertEquals(1, $crawler->selectButton('BarId')->count(), '->selectButton() selects buttons');
+
+ $this->assertEquals(1, $crawler->selectButton('FooBarValue')->count(), '->selectButton() selects buttons with form attribute too');
+ $this->assertEquals(1, $crawler->selectButton('FooBarName')->count(), '->selectButton() selects buttons with form attribute too');
}
public function testLink()
@@ -427,10 +430,17 @@ public function testLinks()
public function testForm()
{
- $crawler = $this->createTestCrawler('http://example.com/bar/')->selectButton('FooValue');
+ $testCrawler = $this->createTestCrawler('http://example.com/bar/');
+ $crawler = $testCrawler->selectButton('FooValue');
+ $crawler2 = $testCrawler->selectButton('FooBarValue');
$this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler->form(), '->form() returns a Form instance');
+ $this->assertInstanceOf('Symfony\\Component\\DomCrawler\\Form', $crawler2->form(), '->form() returns a Form instance');
+
+ $this->assertEquals($crawler->form()->getFormNode()->getAttribute('id'), $crawler2->form()->getFormNode()->getAttribute('id'), '->form() works on elements with form attribute');
- $this->assertEquals(array('FooName' => 'FooBar'), $crawler->form(array('FooName' => 'FooBar'))->getValues(), '->form() takes an array of values to submit as its first argument');
+ $this->assertEquals(array('FooName' => 'FooBar', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form(array('FooName' => 'FooBar'))->getValues(), '->form() takes an array of values to submit as its first argument');
+ $this->assertEquals(array('FooName' => 'FooValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler->form()->getValues(), '->form() takes an array of values to submit as its first argument');
+ $this->assertEquals(array('FooBarName' => 'FooBarValue', 'TextName' => 'TextValue', 'FooTextName' => 'FooTextValue'), $crawler2->form()->getValues(), '->form() takes an array of values to submit as its first argument');
try {
$this->createTestCrawler()->filterXPath('//ol')->form();
@@ -576,12 +586,16 @@ public function createTestCrawler($uri = null)
<a href="?get=param">GetLink</a>
- <form action="foo">
+ <form action="foo" id="FooFormId">
+ <input type="text" value="TextValue" name="TextName" />
<input type="submit" value="FooValue" name="FooName" id="FooId" />
<input type="button" value="BarValue" name="BarName" id="BarId" />
<button value="ButtonValue" name="ButtonName" id="ButtonId" />
</form>
+ <input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" />
+ <input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" />
+
<ul class="first">
<li class="first">One</li>
<li>Two</li>
@@ -62,6 +62,51 @@ public function testConstructorThrowsExceptionIfTheNodeHasNoFormAncestor()
}
}
+ /**
+ * __construct() should throw \\LogicException if the form attribute is invalid
+ * @expectedException \LogicException
+ */
+ public function testConstructorThrowsExceptionIfNoRelatedForm()
+ {
+ $dom = new \DOMDocument();
+ $dom->loadHTML('
+ <html>
+ <form id="bar">
+ <input type="submit" form="nonexistent" />
+ </form>
+ <input type="text" form="nonexistent" />
+ <button />
+ </html>
+ ');
+
+ $nodes = $dom->getElementsByTagName('input');
+
+ $form = new Form($nodes->item(0), 'http://example.com');
+ $form = new Form($nodes->item(1), 'http://example.com');
+ }
+
+ public function testConstructorHandlesFormAttribute()
+ {
+ $dom = new \DOMDocument();
+ $dom->loadHTML('
+ <html>
+ <form id="bar">
+ <input type="submit" form="bar" />
+ </form>
+ <input type="submit" form="bar" />
+ <button />
+ </html>
+ ');
+
+ $nodes = $dom->getElementsByTagName('input');
+
+ $form = new Form($nodes->item(0), 'http://example.com');
+ $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), 'HTML5-compliant form attribute handled incorrectly');
+
+ $form = new Form($nodes->item(1), 'http://example.com');
+ $this->assertSame($dom->getElementsByTagName('form')->item(0), $form->getFormNode(), 'HTML5-compliant form attribute handled incorrectly');
+ }
+
public function testMultiValuedFields()
{
$form = $this->createForm('<form>

0 comments on commit f8178dd

Please sign in to comment.