bug #11219 [DomCrawler] properly handle buttons with single and double quotes insid... (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[DomCrawler] properly handle buttons with single and double quotes insid...

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #11151
| License       | MIT
| Doc PR        |

Commits
-------

cbbdbe4 [DomCrawler] properly handle buttons with single and double quotes inside the name attribute
This commit is contained in:
Fabien Potencier 2014-06-27 08:55:46 +02:00
commit 1045adfd56
2 changed files with 44 additions and 2 deletions

View File

@ -660,8 +660,8 @@ class Crawler extends \SplObjectStorage
{
$translate = 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")';
$xpath = sprintf('descendant-or-self::input[((contains(%s, "submit") or contains(%s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %s)) ', $translate, $translate, static::xpathLiteral(' '.$value.' ')).
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).
sprintf('| descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %s) or @id="%s" or @name="%s"]', static::xpathLiteral(' '.$value.' '), $value, $value);
sprintf('or (contains(%s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %s)) or @id=%s or @name=%s] ', $translate, static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value), static::xpathLiteral($value)).
sprintf('| descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %s) or @id=%s or @name=%s]', static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value), static::xpathLiteral($value));
return $this->filterRelativeXPath($xpath);
}

View File

@ -466,6 +466,48 @@ EOF
$this->assertEquals(1, $crawler->selectButton('FooBarName')->count(), '->selectButton() selects buttons with form attribute too');
}
public function testSelectButtonWithSingleQuotesInNameAttribute()
{
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<div id="action">
<a href="/index.php?r=site/login">Login</a>
</div>
<form id="login-form" action="/index.php?r=site/login" method="post">
<button type="submit" name="Click 'Here'">Submit</button>
</form>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$this->assertCount(1, $crawler->selectButton('Click \'Here\''));
}
public function testSelectButtonWithDoubleQuotesInNameAttribute()
{
$html = <<<HTML
<!DOCTYPE html>
<html lang="en">
<body>
<div id="action">
<a href="/index.php?r=site/login">Login</a>
</div>
<form id="login-form" action="/index.php?r=site/login" method="post">
<button type="submit" name='Click "Here"'>Submit</button>
</form>
</body>
</html>
HTML;
$crawler = new Crawler($html);
$this->assertCount(1, $crawler->selectButton('Click "Here"'));
}
public function testLink()
{
$crawler = $this->createTestCrawler('http://example.com/bar/')->selectLink('Foo');