Skip to content

Commit 8342592

Browse files
Merge branch '6.4' into 7.0
* 6.4: [FrameworkBundle][Validator] Replace annotation by attribute [Validator] Add annotation in Constraint [Lock] Add some missing return types in tests [Clock] Throw `DateMalformedStringException`/`DateInvalidTimeZoneException` when appropriate [VarExporter] Remove unused test files [FrameworkBundle] Remove unused test file [DependencyInjection] Remove unused test file [DomCrawler] Added argument `$default` to method `Crawler::attr()` [HttpKernel] Fix missing Request in RequestStack for StreamedResponse Psalm: Ignore UnusedClass errors fix(console): avoid multiple new line when message already ends with a new line
2 parents 714675d + 3137148 commit 8342592

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ CHANGELOG
99
6.4
1010
---
1111

12-
* Add `CrawlerAnySelectorTextContains` test constraint
13-
* Add `CrawlerAnySelectorTextSame` test constraint
12+
* Add `CrawlerAnySelectorTextContains` test constraint
13+
* Add `CrawlerAnySelectorTextSame` test constraint
14+
* Add argument `$default` to `Crawler::attr()`
1415

1516
6.3
1617
---

Crawler.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,17 +500,24 @@ public function children(string $selector = null): static
500500
/**
501501
* Returns the attribute value of the first node of the list.
502502
*
503+
* @param string|null $default When not null: the value to return when the node or attribute is empty
504+
*
503505
* @throws \InvalidArgumentException When current node is empty
504506
*/
505-
public function attr(string $attribute): ?string
507+
public function attr(string $attribute/* , string $default = null */): ?string
506508
{
509+
$default = \func_num_args() > 1 ? func_get_arg(1) : null;
507510
if (!$this->nodes) {
511+
if (null !== $default) {
512+
return $default;
513+
}
514+
508515
throw new \InvalidArgumentException('The current node list is empty.');
509516
}
510517

511518
$node = $this->getNode(0);
512519

513-
return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : null;
520+
return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : $default;
514521
}
515522

516523
/**

Tests/AbstractCrawlerTestCase.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,9 @@ public function testAttr()
305305
} catch (\InvalidArgumentException $e) {
306306
$this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty');
307307
}
308+
309+
$this->assertSame('my value', $this->createTestCrawler()->filterXPath('//notexists')->attr('class', 'my value'));
310+
$this->assertSame('my value', $this->createTestCrawler()->filterXPath('//li')->attr('attr-not-exists', 'my value'));
308311
}
309312

310313
public function testMissingAttrValueIsNull()

0 commit comments

Comments
 (0)