Browse Source

Merge branch 'domcrawler-disabled-fields'

* domcrawler-disabled-fields:
  [DomCrawler] fixed disabled fields in forms (they are available in the DOM, but their values are not submitted -- whereas before, they were simply removed from the DOM)
  $node->hasAttribute('disabled') sf2 should not create disagreement between implementation and practice for a crawler. If sahi real browser can find an element that is disabled, then sf2 should too. https://github.com/Behat/Mink/pull/58#issuecomment-1712459
Fabien Potencier 13 years ago
parent
commit
0c6f47fcf3

+ 6 - 0
src/Symfony/Component/DomCrawler/Field/FormField.php

@@ -23,6 +23,7 @@ abstract class FormField
     protected $value;
     protected $document;
     protected $xpath;
+    protected $disabled;
 
     /**
      * Constructor.
@@ -86,6 +87,11 @@ abstract class FormField
         return true;
     }
 
+    public function isDisabled()
+    {
+        return $this->node->hasAttribute('disabled');
+    }
+
     /**
      * Initializes the form field.
      */

+ 9 - 1
src/Symfony/Component/DomCrawler/Form.php

@@ -82,6 +82,10 @@ class Form extends Link implements \ArrayAccess
     {
         $values = array();
         foreach ($this->fields as $name => $field) {
+            if ($field->isDisabled()) {
+                continue;
+            }
+
             if (!$field instanceof Field\FileFormField && $field->hasValue()) {
                 $values[$name] = $field->getValue();
             }
@@ -105,6 +109,10 @@ class Form extends Link implements \ArrayAccess
 
         $files = array();
         foreach ($this->fields as $name => $field) {
+            if ($field->isDisabled()) {
+                continue;
+            }
+
             if ($field instanceof Field\FileFormField) {
                 $files[$name] = $field->getValue();
             }
@@ -280,7 +288,7 @@ class Form extends Link implements \ArrayAccess
         $xpath = new \DOMXPath($document);
 
         foreach ($xpath->query('descendant::input | descendant::textarea | descendant::select', $root) as $node) {
-            if ($node->hasAttribute('disabled') || !$node->hasAttribute('name')) {
+            if (!$node->hasAttribute('name')) {
                 continue;
             }
 

+ 8 - 2
tests/Symfony/Tests/Component/DomCrawler/FormTest.php

@@ -73,10 +73,10 @@ class FormTest extends \PHPUnit_Framework_TestCase
                 array(),
             ),
             array(
-                'does not take into account disabled input fields',
+                'takes into account disabled input fields',
                 '<input type="text" name="foo" value="foo" disabled="disabled" />
                  <input type="submit" />',
-                array(),
+                array('foo' => array('Symfony\\Component\\DomCrawler\\Field\\InputFormField', 'foo')),
             ),
             array(
                 'appends the submitted button value',
@@ -201,6 +201,9 @@ class FormTest extends \PHPUnit_Framework_TestCase
 
         $form = $this->createForm('<form><input type="file" name="foo" value="foo" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
         $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include file input fields');
+
+        $form = $this->createForm('<form><input type="text" name="foo" value="foo" disabled="disabled" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
+        $this->assertEquals(array('bar' => 'bar'), $form->getValues(), '->getValues() does not include disabled fields');
     }
 
     public function testSetValues()
@@ -223,6 +226,9 @@ class FormTest extends \PHPUnit_Framework_TestCase
 
         $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" /><input type="text" name="bar" value="bar" /><input type="submit" /></form>');
         $this->assertEquals(array('foo[bar]' => array('name' => '', 'type' => '', 'tmp_name' => '', 'error' => 4, 'size' => 0)), $form->getFiles(), '->getFiles() only returns file fields');
+
+        $form = $this->createForm('<form method="post"><input type="file" name="foo[bar]" disabled="disabled" /><input type="submit" /></form>');
+        $this->assertEquals(array(), $form->getFiles(), '->getFiles() does not include disabled file fields');
     }
 
     public function testGetPhpFiles()