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

Commit 6a4ce38

Browse filesBrowse files
respinozanicolas-grekas
authored andcommitted
[DomCrawler] Added ability to return a default value in text() and html() instead of throwing an exception when node is empty.
1 parent f2590d1 commit 6a4ce38
Copy full SHA for 6a4ce38

File tree

Expand file treeCollapse file tree

3 files changed

+19
-2
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+19
-2
lines changed

‎src/Symfony/Component/DomCrawler/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

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

910
4.2.0
1011
-----

‎src/Symfony/Component/DomCrawler/Crawler.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Crawler.php
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -570,13 +570,19 @@ public function nodeName()
570570
/**
571571
* Returns the node value of the first node of the list.
572572
*
573+
* @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
574+
*
573575
* @return string The node value
574576
*
575577
* @throws \InvalidArgumentException When current node is empty
576578
*/
577-
public function text()
579+
public function text(/* $default = null */)
578580
{
579581
if (!$this->nodes) {
582+
if (0 < \func_num_args()) {
583+
return \func_get_arg(0);
584+
}
585+
580586
throw new \InvalidArgumentException('The current node list is empty.');
581587
}
582588

@@ -586,13 +592,19 @@ public function text()
586592
/**
587593
* Returns the first node of the list as HTML.
588594
*
595+
* @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
596+
*
589597
* @return string The node html
590598
*
591599
* @throws \InvalidArgumentException When current node is empty
592600
*/
593-
public function html()
601+
public function html(/* $default = null */)
594602
{
595603
if (!$this->nodes) {
604+
if (0 < \func_num_args()) {
605+
return \func_get_arg(0);
606+
}
607+
596608
throw new \InvalidArgumentException('The current node list is empty.');
597609
}
598610

‎src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,8 @@ public function testText()
392392
} catch (\InvalidArgumentException $e) {
393393
$this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty');
394394
}
395+
396+
$this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->text('my value'));
395397
}
396398

397399
public function testHtml()
@@ -405,6 +407,8 @@ public function testHtml()
405407
} catch (\InvalidArgumentException $e) {
406408
$this->assertTrue(true, '->html() throws an \InvalidArgumentException if the node list is empty');
407409
}
410+
411+
$this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->html('my value'));
408412
}
409413

410414
public function testExtract()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.