Skip to content

Commit e4a28dd

Browse files
committed
bug symfony#10925 [DomCrawler] Fixed the handling of boolean attributes in ChoiceFormField (stof)
This PR was merged into the 2.3 branch. Discussion ---------- [DomCrawler] Fixed the handling of boolean attributes in ChoiceFormField | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | n/a An option is marked as selected by the presence of the selected attribute, not by the presence of a non-empty selected attribute. The same is true for checked radio buttons or checkboxes. Commits ------- b505708 Fixed the handling of boolean attributes in ChoiceFormField
2 parents 0762bae + b505708 commit e4a28dd

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function addChoice(\DOMNode $node)
170170
$option = $this->buildOptionValue($node);
171171
$this->options[] = $option;
172172

173-
if ($node->getAttribute('checked')) {
173+
if ($node->hasAttribute('checked')) {
174174
$this->value = $option['value'];
175175
}
176176
}
@@ -219,7 +219,7 @@ protected function initialize()
219219
$optionValue = $this->buildOptionValue($this->node);
220220
$this->options[] = $optionValue;
221221

222-
if ($this->node->getAttribute('checked')) {
222+
if ($this->node->hasAttribute('checked')) {
223223
$this->value = $optionValue['value'];
224224
}
225225
} else {
@@ -234,7 +234,7 @@ protected function initialize()
234234
foreach ($this->xpath->query('descendant::option', $this->node) as $option) {
235235
$this->options[] = $this->buildOptionValue($option);
236236

237-
if ($option->getAttribute('selected')) {
237+
if ($option->hasAttribute('selected')) {
238238
$found = true;
239239
if ($this->multiple) {
240240
$this->value[] = $option->getAttribute('value');
@@ -265,7 +265,7 @@ private function buildOptionValue($node)
265265

266266
$defaultValue = (isset($node->nodeValue) && !empty($node->nodeValue)) ? $node->nodeValue : '1';
267267
$option['value'] = $node->hasAttribute('value') ? $node->getAttribute('value') : $defaultValue;
268-
$option['disabled'] = ($node->hasAttribute('disabled') && $node->getAttribute('disabled') == 'disabled');
268+
$option['disabled'] = $node->hasAttribute('disabled');
269269

270270
return $option;
271271
}

src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ public function testIsMultiple()
7373
$field = new ChoiceFormField($node);
7474

7575
$this->assertTrue($field->isMultiple(), '->isMultiple() returns true for selects with the multiple attribute');
76+
77+
$node = $this->createNode('select', '', array('multiple' => ''));
78+
$field = new ChoiceFormField($node);
79+
80+
$this->assertTrue($field->isMultiple(), '->isMultiple() returns true for selects with an empty multiple attribute');
7681
}
7782

7883
public function testSelects()
@@ -107,6 +112,14 @@ public function testSelects()
107112
}
108113
}
109114

115+
public function testSelectWithEmptyBooleanAttribute()
116+
{
117+
$node = $this->createSelectNode(array('foo' => false, 'bar' => true), array(), '');
118+
$field = new ChoiceFormField($node);
119+
120+
$this->assertEquals('bar', $field->getValue());
121+
}
122+
110123
public function testMultipleSelects()
111124
{
112125
$node = $this->createSelectNode(array('foo' => false, 'bar' => false), array('multiple' => 'multiple'));
@@ -166,12 +179,25 @@ public function testRadioButtons()
166179
}
167180
}
168181

182+
public function testRadioButtonsWithEmptyBooleanAttribute()
183+
{
184+
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo'));
185+
$field = new ChoiceFormField($node);
186+
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar', 'checked' => ''));
187+
$field->addChoice($node);
188+
189+
$this->assertTrue($field->hasValue(), '->hasValue() returns true when a radio button is selected');
190+
$this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
191+
}
192+
169193
public function testRadioButtonIsDisabled()
170194
{
171195
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'foo', 'disabled' => 'disabled'));
172196
$field = new ChoiceFormField($node);
173197
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'bar'));
174198
$field->addChoice($node);
199+
$node = $this->createNode('input', '', array('type' => 'radio', 'name' => 'name', 'value' => 'baz', 'disabled' => ''));
200+
$field->addChoice($node);
175201

176202
$field->select('foo');
177203
$this->assertEquals('foo', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
@@ -180,6 +206,10 @@ public function testRadioButtonIsDisabled()
180206
$field->select('bar');
181207
$this->assertEquals('bar', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
182208
$this->assertFalse($field->isDisabled());
209+
210+
$field->select('baz');
211+
$this->assertEquals('baz', $field->getValue(), '->getValue() returns the value attribute of the selected radio button');
212+
$this->assertTrue($field->isDisabled());
183213
}
184214

185215
public function testCheckboxes()
@@ -225,6 +255,15 @@ public function testCheckboxes()
225255
}
226256
}
227257

258+
public function testCheckboxWithEmptyBooleanAttribute()
259+
{
260+
$node = $this->createNode('input', '', array('type' => 'checkbox', 'name' => 'name', 'value' => 'foo', 'checked' => ''));
261+
$field = new ChoiceFormField($node);
262+
263+
$this->assertTrue($field->hasValue(), '->hasValue() returns true when the checkbox is checked');
264+
$this->assertEquals('foo', $field->getValue());
265+
}
266+
228267
public function testTick()
229268
{
230269
$node = $this->createSelectNode(array('foo' => false, 'bar' => false));
@@ -284,7 +323,7 @@ public function testOptionWithNoValue()
284323
$this->assertEquals('foo', $field->getValue(), '->select() changes the selected option');
285324
}
286325

287-
protected function createSelectNode($options, $attributes = array())
326+
protected function createSelectNode($options, $attributes = array(), $selectedAttrText = 'selected')
288327
{
289328
$document = new \DOMDocument();
290329
$node = $document->createElement('select');
@@ -298,7 +337,7 @@ protected function createSelectNode($options, $attributes = array())
298337
$option = $document->createElement('option', $value);
299338
$option->setAttribute('value', $value);
300339
if ($selected) {
301-
$option->setAttribute('selected', 'selected');
340+
$option->setAttribute('selected', $selectedAttrText);
302341
}
303342
$node->appendChild($option);
304343
}

0 commit comments

Comments
 (0)