Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[DomCrawler] Added argument $default to method Crawler::attr() #51368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions UPGRADE-6.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ DoctrineBridge
* Deprecate `DoctrineDataCollector::addLogger()`, use a `DebugDataHolder` instead
* Deprecate `ContainerAwareLoader`, use dependency injection in your fixtures instead

DomCrawler
----------

* Add argument `$default` to `Crawler::attr()`

ErrorHandler
------------

Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ CHANGELOG
6.4
---

* Add `CrawlerAnySelectorTextContains` test constraint
* Add `CrawlerAnySelectorTextSame` test constraint
* Add `CrawlerAnySelectorTextContains` test constraint
* Add `CrawlerAnySelectorTextSame` test constraint
* Add argument `$default` to `Crawler::attr()`

6.3
---
Expand Down
12 changes: 9 additions & 3 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class Crawler implements \Countable, \IteratorAggregate
*/
private bool $isHtml = true;


private ?HTML5 $html5Parser = null;

/**
Expand Down Expand Up @@ -522,17 +521,24 @@ public function children(string $selector = null): static
/**
* Returns the attribute value of the first node of the list.
*
* @param string|null $default When not null: the value to return when the node or attribute is empty
*
* @throws \InvalidArgumentException When current node is empty
*/
public function attr(string $attribute): ?string
public function attr(string $attribute/* , string $default = null */): ?string
{
$default = \func_num_args() > 1 ? func_get_arg(1) : null;
if (!$this->nodes) {
if (null !== $default) {
return $default;
}

throw new \InvalidArgumentException('The current node list is empty.');
}

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

return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : null;
return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : $default;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,9 @@ public function testAttr()
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty');
}

$this->assertSame('my value', $this->createTestCrawler()->filterXPath('//notexists')->attr('class', 'my value'));
$this->assertSame('my value', $this->createTestCrawler()->filterXPath('//li')->attr('attr-not-exists', 'my value'));
}

public function testMissingAttrValueIsNull()
Expand Down