From 78cff96230e7b9f14354bc067c698a7fe923c458 Mon Sep 17 00:00:00 2001 From: Christophe Coevoet Date: Sat, 17 May 2014 12:49:12 +0200 Subject: [PATCH] [DomCrawler] Fixed the initial state for options without value attribute --- .../Component/DomCrawler/Field/ChoiceFormField.php | 12 ++++++------ .../DomCrawler/Tests/Field/ChoiceFormFieldTest.php | 5 +++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php b/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php index efdcf5b922..0cbd0d5fcc 100644 --- a/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php +++ b/src/Symfony/Component/DomCrawler/Field/ChoiceFormField.php @@ -232,22 +232,22 @@ class ChoiceFormField extends FormField $found = false; foreach ($this->xpath->query('descendant::option', $this->node) as $option) { - $this->options[] = $this->buildOptionValue($option); + $optionValue = $this->buildOptionValue($option); + $this->options[] = $optionValue; if ($option->getAttribute('selected')) { $found = true; if ($this->multiple) { - $this->value[] = $option->getAttribute('value'); + $this->value[] = $optionValue['value']; } else { - $this->value = $option->getAttribute('value'); + $this->value = $optionValue['value']; } } } // if no option is selected and if it is a simple select box, take the first option as the value - $option = $this->xpath->query('descendant::option', $this->node)->item(0); - if (!$found && !$this->multiple && $option instanceof \DOMElement) { - $this->value = $option->getAttribute('value'); + if (!$found && !$this->multiple && !empty($this->options)) { + $this->value = $this->options[0]['value']; } } } diff --git a/src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php b/src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php index cf7cd5a2f2..926778f6fc 100644 --- a/src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/Field/ChoiceFormFieldTest.php @@ -280,6 +280,11 @@ class ChoiceFormFieldTest extends FormFieldTestCase { $node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => false)); $field = new ChoiceFormField($node); + $this->assertEquals('foo', $field->getValue()); + + $node = $this->createSelectNodeWithEmptyOption(array('foo' => false, 'bar' => true)); + $field = new ChoiceFormField($node); + $this->assertEquals('bar', $field->getValue()); $field->select('foo'); $this->assertEquals('foo', $field->getValue(), '->select() changes the selected option'); }