Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DomCrawler] return empty string on Crawler::text() and Crawler::html() instead of an exception #28581

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
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
1 change: 1 addition & 0 deletions 1 src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CHANGELOG
-----

* Added return of element name (`_name`) in `extract()` method.
* Added ability to return a default value in `text()` and `html()` instead of throwing an exception when node is empty.

4.2.0
-----
Expand Down
16 changes: 14 additions & 2 deletions 16 src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -570,13 +570,19 @@ public function nodeName()
/**
* Returns the node value of the first node of the list.
*
* @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
*
* @return string The node value
*
* @throws \InvalidArgumentException When current node is empty
*/
public function text()
public function text(/* $default = null */)
{
if (!$this->nodes) {
if (0 < \func_num_args()) {
return \func_get_arg(0);
}

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

Expand All @@ -586,13 +592,19 @@ public function text()
/**
* Returns the first node of the list as HTML.
*
* @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
*
* @return string The node html
*
* @throws \InvalidArgumentException When current node is empty
*/
public function html()
public function html(/* $default = null */)
{
if (!$this->nodes) {
if (0 < \func_num_args()) {
return \func_get_arg(0);
}

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

Expand Down
4 changes: 4 additions & 0 deletions 4 src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,8 @@ public function testText()
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty');
}

$this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->text('my value'));
}

public function testHtml()
Expand All @@ -405,6 +407,8 @@ public function testHtml()
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->html() throws an \InvalidArgumentException if the node list is empty');
}

$this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->html('my value'));
}

public function testExtract()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.