From e0db122a61edaf71d84b4530cdd78a8ceb41ee0b Mon Sep 17 00:00:00 2001 From: Raja Amer Khan Date: Thu, 27 Sep 2018 00:35:09 +0100 Subject: [PATCH 1/2] [DomCrawler] Change ->text() , ->html() and ->attr() methods to return null if node list is empty --- src/Symfony/Component/DomCrawler/Crawler.php | 16 ++++--------- .../DomCrawler/Tests/CrawlerTest.php | 24 +++---------------- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 77c392daf5310..0d75a40add674 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -537,13 +537,11 @@ public function children(/* string $selector = null */) * @param string $attribute The attribute name * * @return string|null The attribute value or null if the attribute does not exist - * - * @throws \InvalidArgumentException When current node is empty */ public function attr($attribute) { if (!$this->nodes) { - throw new \InvalidArgumentException('The current node list is empty.'); + return null; } $node = $this->getNode(0); @@ -570,14 +568,12 @@ public function nodeName() /** * Returns the node value of the first node of the list. * - * @return string The node value - * - * @throws \InvalidArgumentException When current node is empty + * @return string|null The node value */ public function text() { if (!$this->nodes) { - throw new \InvalidArgumentException('The current node list is empty.'); + return null; } return $this->getNode(0)->nodeValue; @@ -586,14 +582,12 @@ public function text() /** * Returns the first node of the list as HTML. * - * @return string The node html - * - * @throws \InvalidArgumentException When current node is empty + * @return string|null The node html */ public function html() { if (!$this->nodes) { - throw new \InvalidArgumentException('The current node list is empty.'); + return null; } $html = ''; diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 9e73eaae1d9c2..0f22243cac805 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -350,13 +350,7 @@ public function testReduce() public function testAttr() { $this->assertEquals('first', $this->createTestCrawler()->filterXPath('//li')->attr('class'), '->attr() returns the attribute of the first element of the node list'); - - try { - $this->createTestCrawler()->filterXPath('//ol')->attr('class'); - $this->fail('->attr() throws an \InvalidArgumentException if the node list is empty'); - } catch (\InvalidArgumentException $e) { - $this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty'); - } + $this->assertEquals(null, $this->createTestCrawler()->filterXPath('//ol')->attr('class'), '->attr() returns null if the node list is empty'); } public function testMissingAttrValueIsNull() @@ -385,26 +379,14 @@ public function testNodeName() public function testText() { $this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list'); - - try { - $this->createTestCrawler()->filterXPath('//ol')->text(); - $this->fail('->text() throws an \InvalidArgumentException if the node list is empty'); - } catch (\InvalidArgumentException $e) { - $this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty'); - } + $this->assertEquals(null, $this->createTestCrawler()->filterXPath('//ol')->text(), '->text() returns null if the node list is empty'); } public function testHtml() { $this->assertEquals('Bar', $this->createTestCrawler()->filterXPath('//a[5]')->html()); $this->assertEquals('', trim(preg_replace('~>\s+<~', '><', $this->createTestCrawler()->filterXPath('//form[@id="FooFormId"]')->html()))); - - try { - $this->createTestCrawler()->filterXPath('//ol')->html(); - $this->fail('->html() throws an \InvalidArgumentException if the node list is empty'); - } catch (\InvalidArgumentException $e) { - $this->assertTrue(true, '->html() throws an \InvalidArgumentException if the node list is empty'); - } + $this->assertEquals(null, $this->createTestCrawler()->filterXPath('//ol')->html(), '->html() returns null if the node list is empty'); } public function testExtract() From b0e20bcc2b7b63b6c654d0253f6f7004280c973e Mon Sep 17 00:00:00 2001 From: Raja Amer Khan Date: Thu, 27 Sep 2018 00:51:07 +0100 Subject: [PATCH 2/2] [DomCrawler] Change ->text() , ->html() and ->attr() methods to return null if node list is empty --- src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 0f22243cac805..0753fe602fcc2 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -350,7 +350,7 @@ public function testReduce() public function testAttr() { $this->assertEquals('first', $this->createTestCrawler()->filterXPath('//li')->attr('class'), '->attr() returns the attribute of the first element of the node list'); - $this->assertEquals(null, $this->createTestCrawler()->filterXPath('//ol')->attr('class'), '->attr() returns null if the node list is empty'); + $this->assertNull($this->createTestCrawler()->filterXPath('//ol')->attr('class'), '->attr() returns null if the node list is empty'); } public function testMissingAttrValueIsNull() @@ -379,14 +379,14 @@ public function testNodeName() public function testText() { $this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list'); - $this->assertEquals(null, $this->createTestCrawler()->filterXPath('//ol')->text(), '->text() returns null if the node list is empty'); + $this->assertNull($this->createTestCrawler()->filterXPath('//ol')->text(), '->text() returns null if the node list is empty'); } public function testHtml() { $this->assertEquals('Bar', $this->createTestCrawler()->filterXPath('//a[5]')->html()); $this->assertEquals('', trim(preg_replace('~>\s+<~', '><', $this->createTestCrawler()->filterXPath('//form[@id="FooFormId"]')->html()))); - $this->assertEquals(null, $this->createTestCrawler()->filterXPath('//ol')->html(), '->html() returns null if the node list is empty'); + $this->assertNull($this->createTestCrawler()->filterXPath('//ol')->html(), '->html() returns null if the node list is empty'); } public function testExtract()