8000 feature #51368 [DomCrawler] Added argument `$default` to method `Craw… · symfony/symfony@c2ac73c · GitHub
[go: up one dir, main page]

Skip to content

Commit c2ac73c

Browse files
feature #51368 [DomCrawler] Added argument $default to method Crawler::attr() (Rastishka)
This PR was merged into the 6.4 branch. Discussion ---------- [DomCrawler] Added argument `$default` to method `Crawler::attr()` | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | no | License | MIT | Doc PR | symfony/symfony-docs#... Added argument `$default` to method `->attr()` like `->text()` and `->html()` have. Commits ------- 85f84d6 [DomCrawler] Added argument `$default` to method `Crawler::attr()`
2 parents ff654da + 85f84d6 commit c2ac73c

File tree

4 files changed

+20
-5
lines changed

4 files changed

+20
-5
lines changed

UPGRADE-6.4.md

+5
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ DoctrineBridge
6161
* Deprecate `DoctrineDataCollector::addLogger()`, use a `DebugDataHolder` instead
6262
* Deprecate `ContainerAwareLoader`, use dependency injection in your fixtures instead
6363

64+
DomCrawler
65+
----------
66+
67+
* Add argument `$default` to `Crawler::attr()`
68+
6469
ErrorHandler
6570
------------
6671

src/Symfony/Component/DomCrawler/CHANGELOG.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ CHANGELOG
44
6.4
55
---
66

7-
* Add `CrawlerAnySelectorTextContains` test constraint
8-
* Add `CrawlerAnySelectorTextSame` test constraint
7+
* Add `CrawlerAnySelectorTextContains` test constraint
8+
* Add `CrawlerAnySelectorTextSame` test constraint
9+
* Add argument `$default` to `Crawler::attr()`
910

1011
6.3
1112
---

src/Symfony/Component/DomCrawler/Crawler.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ class Crawler implements \Countable, \IteratorAggregate
5858
*/
5959
private bool $isHtml = true;
6060

61-
6261
private ?HTML5 $html5Parser = null;
6362

6463
/**
@@ -522,17 +521,24 @@ public function children(string $selector = null): static
522521
/**
523522
* Returns the attribute value of the first node of the list.
524523
*
524+
* @param string|null $default When not null: the value to return when the node or attribute is empty
525+
*
525526
* @throws \InvalidArgumentException When current node is empty
526527
*/
527-
public function attr(string $attribute): ?string
528+
public function attr(string $attribute/* , string $default = null */): ?string
528529
{
530+
$default = \func_num_args() > 1 ? func_get_arg(1) : null;
529531
if (!$this->nodes) {
532+
if (null !== $default) {
533+
return $default;
534+
}
535+
530536
throw new \InvalidArgumentException('The current node list is empty.');
531537
}
532538

533539
$node = $this->getNode(0);
534540

535-
return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : null;
541+
return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : $default;
536542
}
537543

538544
/**

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

+3
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)
0