Skip to content

Commit ab1198f

Browse files
Geoffrey Brierfabpot
authored andcommitted
[DomCrawler] Fixed image input case sensitive
1 parent d9fec70 commit ab1198f

File tree

6 files changed

+21
-13
lines changed

6 files changed

+21
-13
lines changed

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,9 @@ public function selectLink($value)
651651
*/
652652
public function selectButton($value)
653653
{
654-
$xpath = sprintf('//input[((@type="submit" or @type="button") and contains(concat(\' \', normalize-space(string(@value)), \' \'), %s)) ', static::xpathLiteral(' '.$value.' ')).
655-
sprintf('or (@type="image" and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)) or @id="%s" or @name="%s"] ', static::xpathLiteral(' '.$value.' '), $value, $value).
654+
$translate = 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")';
655+
$xpath = sprintf('//input[((contains(%s, "submit") or contains(%s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %s)) ', $translate, $translate, static::xpathLiteral(' '.$value.' ')).
656+
sprintf('or (contains(%s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)) or @id="%s" or @name="%s"] ', $translate, static::xpathLiteral(' '.$value.' '), $value, $value).
656657
sprintf('| //button[contains(concat(\' \', normalize-space(string(.)), \' \'), %s) or @id="%s" or @name="%s"]', static::xpathLiteral(' '.$value.' '), $value, $value);
657658

658659
return $this->filterXPath($xpath);

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ protected function initialize()
206206
throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input or select tag (%s given).', $this->node->nodeName));
207207
}
208208

209-
if ('input' == $this->node->nodeName && 'checkbox' != $this->node->getAttribute('type') && 'radio' != $this->node->getAttribute('type')) {
209+
if ('input' == $this->node->nodeName && 'checkbox' != strtolower($this->node->getAttribute('type')) && 'radio' != strtolower($this->node->getAttribute('type'))) {
210210
throw new \LogicException(sprintf('A ChoiceFormField can only be created from an input tag with a type of checkbox or radio (given type is %s).', $this->node->getAttribute('type')));
211211
}
212212

@@ -215,7 +215,7 @@ protected function initialize()
215215
$this->multiple = false;
216216

217217
if ('input' == $this->node->nodeName) {
218-
$this->type = $this->node->getAttribute('type');
218+
$this->type = strtolower($this->node->getAttribute('type'));
219219
$optionValue = $this->buildOptionValue($this->node);
220220
$this->options[] = $optionValue;
221221

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ protected function initialize()
103103
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag (%s given).', $this->node->nodeName));
104104
}
105105

106-
if ('file' != $this->node->getAttribute('type')) {
106+
if ('file' != strtolower($this->node->getAttribute('type'))) {
107107
throw new \LogicException(sprintf('A FileFormField can only be created from an input tag with a type of file (given type is %s).', $this->node->getAttribute('type')));
108108
}
109109

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ protected function initialize()
3434
throw new \LogicException(sprintf('An InputFormField can only be created from an input or button tag (%s given).', $this->node->nodeName));
3535
}
3636

37-
if ('checkbox' == $this->node->getAttribute('type')) {
37+
if ('checkbox' == strtolower($this->node->getAttribute('type'))) {
3838
throw new \LogicException('Checkboxes should be instances of ChoiceFormField.');
3939
}
4040

41-
if ('file' == $this->node->getAttribute('type')) {
41+
if ('file' == strtolower($this->node->getAttribute('type'))) {
4242
throw new \LogicException('File inputs should be instances of FileFormField.');
4343
}
4444

src/Symfony/Component/DomCrawler/Form.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public function offsetUnset($name)
352352
protected function setNode(\DOMNode $node)
353353
{
354354
$this->button = $node;
355-
if ('button' == $node->nodeName || ('input' == $node->nodeName && in_array($node->getAttribute('type'), array('submit', 'button', 'image')))) {
355+
if ('button' == $node->nodeName || ('input' == $node->nodeName && in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image')))) {
356356
if ($node->hasAttribute('form')) {
357357
// if the node has the HTML5-compliant 'form' attribute, use it
358358
$formId = $node->getAttribute('form');
@@ -394,7 +394,7 @@ private function initialize()
394394

395395
// add submitted button if it has a valid name
396396
if ('form' !== $this->button->nodeName && $this->button->hasAttribute('name') && $this->button->getAttribute('name')) {
397-
if ('input' == $this->button->nodeName && 'image' == $this->button->getAttribute('type')) {
397+
if ('input' == $this->button->nodeName && 'image' == strtolower($this->button->getAttribute('type'))) {
398398
$name = $this->button->getAttribute('name');
399399
$this->button->setAttribute('value', '0');
400400

@@ -445,17 +445,17 @@ private function addField(\DOMNode $node)
445445
}
446446

447447
$nodeName = $node->nodeName;
448-
if ('select' == $nodeName || 'input' == $nodeName && 'checkbox' == $node->getAttribute('type')) {
448+
if ('select' == $nodeName || 'input' == $nodeName && 'checkbox' == strtolower($node->getAttribute('type'))) {
449449
$this->set(new Field\ChoiceFormField($node));
450-
} elseif ('input' == $nodeName && 'radio' == $node->getAttribute('type')) {
450+
} elseif ('input' == $nodeName && 'radio' == strtolower($node->getAttribute('type'))) {
451451
if ($this->has($node->getAttribute('name'))) {
452452
$this->get($node->getAttribute('name'))->addChoice($node);
453453
} else {
454454
$this->set(new Field\ChoiceFormField($node));
455455
}
456-
} elseif ('input' == $nodeName && 'file' == $node->getAttribute('type')) {
456+
} elseif ('input' == $nodeName && 'file' == strtolower($node->getAttribute('type'))) {
457457
$this->set(new Field\FileFormField($node));
458-
} elseif ('input' == $nodeName && !in_array($node->getAttribute('type'), array('submit', 'button', 'image'))) {
458+
} elseif ('input' == $nodeName && !in_array(strtolower($node->getAttribute('type')), array('submit', 'button', 'image'))) {
459459
$this->set(new Field\InputFormField($node));
460460
} elseif ('textarea' == $nodeName) {
461461
$this->set(new Field\TextareaFormField($node));

src/Symfony/Component/DomCrawler/Tests/FormTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,13 @@ public function testSubmitWithoutAFormButton()
631631
$this->assertSame($nodes->item(0), $form->getFormNode(), '->getFormNode() returns the form node associated with this form');
632632
}
633633

634+
public function testTypeAttributeIsCaseInsensitive()
635+
{
636+
$form = $this->createForm('<form method="post"><input type="IMAGE" name="example" /></form>');
637+
$this->assertTrue($form->has('example.x'), '->has() returns true if the image input was correctly turned into an x and a y fields');
638+
$this->assertTrue($form->has('example.y'), '->has() returns true if the image input was correctly turned into an x and a y fields');
639+
}
640+
634641
/**
635642
* @expectedException \InvalidArgumentException
636643
*/

0 commit comments

Comments
 (0)