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..0753fe602fcc2 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->assertNull($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->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()))); - - 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->assertNull($this->createTestCrawler()->filterXPath('//ol')->html(), '->html() returns null if the node list is empty'); } public function testExtract()