Skip to content

Commit

Permalink
feature #28581 [DomCrawler] return empty string on Crawler::text()
Browse files Browse the repository at this point in the history
…and `Crawler::html()` instead of an exception (respinoza)

This PR was merged into the 4.3-dev branch.

Discussion
----------

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

…stead of an exception

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #28313
| License       | MIT
| Doc PR        | symfony/symfony-docs#... <!-- required for new features -->

This changes the behavior when using text and html methods from the Crawler by returning null instead of throwing an exception.

Commits
-------

6a4ce38 [DomCrawler] Added ability to return a default value in `text()` and `html()` instead of throwing an exception when node is empty.
  • Loading branch information
nicolas-grekas committed Dec 17, 2018
2 parents ef555a9 + 6a4ce38 commit 562448a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
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 src/Symfony/Component/DomCrawler/Crawler.php
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 src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
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

0 comments on commit 562448a

Please sign in to comment.