[DomCrawler] properly handle buttons with single and double quotes inside the name attribute

This commit is contained in:
Christian Flothmann 2014-06-24 23:06:26 +02:00
parent eeeae94196
commit cbbdbe4c15
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');