From 6a4ce386fef2f0013af694cc46e077cb41a3d46e Mon Sep 17 00:00:00 2001 From: Roberto Espinoza Date: Mon, 24 Sep 2018 20:20:15 +0900 Subject: [PATCH] [DomCrawler] Added ability to return a default value in `text()` and `html()` instead of throwing an exception when node is empty. --- src/Symfony/Component/DomCrawler/CHANGELOG.md | 1 + src/Symfony/Component/DomCrawler/Crawler.php | 16 ++++++++++++++-- .../Component/DomCrawler/Tests/CrawlerTest.php | 4 ++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/CHANGELOG.md b/src/Symfony/Component/DomCrawler/CHANGELOG.md index 3accde4bf573..fae5bd3f1d91 100644 --- a/src/Symfony/Component/DomCrawler/CHANGELOG.md +++ b/src/Symfony/Component/DomCrawler/CHANGELOG.md @@ -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 ----- diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index a70c6466e068..3526a1ebc611 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -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.'); } @@ -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.'); } diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 254b1ba7f320..fe2cf669b445 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -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() @@ -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()