Skip to content

Commit 22210aa

Browse files
committed
[DomCrawler] Allow selecting buttons by their value
1 parent 19073e3 commit 22210aa

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

Crawler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -770,12 +770,12 @@ public function selectImage(string $value): static
770770
}
771771

772772
/**
773-
* Selects a button by name or alt value for images.
773+
* Selects a button by its text content, id, value, name or alt attribute.
774774
*/
775775
public function selectButton(string $value): static
776776
{
777777
return $this->filterRelativeXPath(
778-
sprintf('descendant-or-self::input[((contains(%1$s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)) or (contains(%1$s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)) or @id=%3$s or @name=%3$s] | descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s) or @id=%3$s or @name=%3$s]', 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")', static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value))
778+
sprintf('descendant-or-self::input[((contains(%1$s, "submit") or contains(%1$s, "button")) and contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s)) or (contains(%1$s, "image") and contains(concat(\' \', normalize-space(string(@alt)), \' \'), %2$s)) or @id=%3$s or @name=%3$s] | descendant-or-self::button[contains(concat(\' \', normalize-space(string(.)), \' \'), %2$s) or contains(concat(\' \', normalize-space(string(@value)), \' \'), %2$s) or @id=%3$s or @name=%3$s]', 'translate(@type, "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "abcdefghijklmnopqrstuvwxyz")', static::xpathLiteral(' '.$value.' '), static::xpathLiteral($value))
779779
);
780780
}
781781

Tests/AbstractCrawlerTestCase.php

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,10 @@ public function testFilterXpathComplexQueries()
452452
$this->assertCount(0, $crawler->filterXPath('/body'));
453453
$this->assertCount(1, $crawler->filterXPath('./body'));
454454
$this->assertCount(1, $crawler->filterXPath('.//body'));
455-
$this->assertCount(5, $crawler->filterXPath('.//input'));
455+
$this->assertCount(6, $crawler->filterXPath('.//input'));
456456
$this->assertCount(4, $crawler->filterXPath('//form')->filterXPath('//button | //input'));
457457
$this->assertCount(1, $crawler->filterXPath('body'));
458-
$this->assertCount(6, $crawler->filterXPath('//button | //input'));
458+
$this->assertCount(8, $crawler->filterXPath('//button | //input'));
459459
$this->assertCount(1, $crawler->filterXPath('//body'));
460460
$this->assertCount(1, $crawler->filterXPath('descendant-or-self::body'));
461461
$this->assertCount(1, $crawler->filterXPath('//div[@id="parent"]')->filterXPath('./div'), 'A child selection finds only the current div');
@@ -723,16 +723,23 @@ public function testSelectButton()
723723
$this->assertNotSame($crawler, $crawler->selectButton('FooValue'), '->selectButton() returns a new instance of a crawler');
724724
$this->assertInstanceOf(Crawler::class, $crawler->selectButton('FooValue'), '->selectButton() returns a new instance of a crawler');
725725

726-
$this->assertEquals(1, $crawler->selectButton('FooValue')->count(), '->selectButton() selects buttons');
727-
$this->assertEquals(1, $crawler->selectButton('FooName')->count(), '->selectButton() selects buttons');
728-
$this->assertEquals(1, $crawler->selectButton('FooId')->count(), '->selectButton() selects buttons');
726+
$this->assertCount(1, $crawler->selectButton('FooValue'), '->selectButton() selects type-submit inputs by value');
727+
$this->assertCount(1, $crawler->selectButton('FooName'), '->selectButton() selects type-submit inputs by name');
728+
$this->assertCount(1, $crawler->selectButton('FooId'), '->selectButton() selects type-submit inputs by id');
729729

730-
$this->assertEquals(1, $crawler->selectButton('BarValue')->count(), '->selectButton() selects buttons');
731-
$this->assertEquals(1, $crawler->selectButton('BarName')->count(), '->selectButton() selects buttons');
732-
$this->assertEquals(1, $crawler->selectButton('BarId')->count(), '->selectButton() selects buttons');
730+
$this->assertCount(1, $crawler->selectButton('BarValue'), '->selectButton() selects type-button inputs by value');
731+
$this->assertCount(1, $crawler->selectButton('BarName'), '->selectButton() selects type-button inputs by name');
732+
$this->assertCount(1, $crawler->selectButton('BarId'), '->selectButton() selects type-button inputs by id');
733733

734-
$this->assertEquals(1, $crawler->selectButton('FooBarValue')->count(), '->selectButton() selects buttons with form attribute too');
735-
$this->assertEquals(1, $crawler->selectButton('FooBarName')->count(), '->selectButton() selects buttons with form attribute too');
734+
$this->assertCount(1, $crawler->selectButton('ImageAlt'), '->selectButton() selects type-image inputs by alt');
735+
736+
$this->assertCount(1, $crawler->selectButton('ButtonValue'), '->selectButton() selects buttons by value');
737+
$this->assertCount(1, $crawler->selectButton('ButtonName'), '->selectButton() selects buttons by name');
738+
$this->assertCount(1, $crawler->selectButton('ButtonId'), '->selectButton() selects buttons by id');
739+
$this->assertCount(1, $crawler->selectButton('ButtonText'), '->selectButton() selects buttons by text content');
740+
741+
$this->assertCount(1, $crawler->selectButton('FooBarValue'), '->selectButton() selects buttons with form attribute too');
742+
$this->assertCount(1, $crawler->selectButton('FooBarName'), '->selectButton() selects buttons with form attribute too');
736743
}
737744

738745
public function testSelectButtonWithSingleQuotesInNameAttribute()
@@ -1322,6 +1329,9 @@ public function createTestCrawler($uri = null)
13221329
<input type="submit" value="FooBarValue" name="FooBarName" form="FooFormId" />
13231330
<input type="text" value="FooTextValue" name="FooTextName" form="FooFormId" />
13241331
1332+
<input type="image" alt="ImageAlt" form="FooFormId">
1333+
<button form="FooFormId">ButtonText</button>
1334+
13251335
<ul class="first">
13261336
<li class="first">One</li>
13271337
<li>Two</li>

0 commit comments

Comments
 (0)