Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 571d6bc

Browse files
committed
[DomCrawler] Added ability to return empty string on Crawler::text() and Crawler::html() instead of an exception
1 parent 5dadd95 commit 571d6bc

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
`Image` classes is now optional.
99
* The `Crawler::children()` method will have a new `$selector` argument in version 5.0,
1010
not defining it is deprecated since version 4.2.
11+
* Added ability to return empty string on `Crawler::text()` and `Crawler::html()` instead of an exception
1112

1213
3.1.0
1314
-----

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,23 @@ class Crawler implements \Countable, \IteratorAggregate
5555
private $isHtml = true;
5656

5757
/**
58-
* @param mixed $node A Node to use as the base for the crawling
59-
* @param string $uri The current URI
60-
* @param string $baseHref The base href value
58+
* Whether the text() and html() methods will return an empty string
59+
*
60+
* @var bool
6161
*/
62-
public function __construct($node = null, string $uri = null, string $baseHref = null)
62+
private $emptyValue;
63+
64+
/**
65+
* @param mixed $node A Node to use as the base for the crawling
66+
* @param string $uri The current URI
67+
* @param string $baseHref The base href value
68+
* @param bool $emptyValue Return empty string on text and html methods instead of an exception
69+
*/
70+
public function __construct($node = null, string $uri = null, string $baseHref = null, bool $emptyValue = false)
6371
{
6472
$this->uri = $uri;
6573
$this->baseHref = $baseHref ?: $uri;
74+
$this->emptyValue = $emptyValue;
6675

6776
$this->add($node);
6877
}
@@ -571,12 +580,15 @@ public function nodeName()
571580
* Returns the node value of the first node of the list.
572581
*
573582
* @return string The node value
574-
*
575-
* @throws \InvalidArgumentException When current node is empty
583+
* @throws \InvalidArgumentException When current node is empty unless the emptyValue option is set
576584
*/
577585
public function text()
578586
{
579587
if (!$this->nodes) {
588+
if ($this->emptyValue) {
589+
return '';
590+
}
591+
580592
throw new \InvalidArgumentException('The current node list is empty.');
581593
}
582594

@@ -587,12 +599,15 @@ public function text()
587599
* Returns the first node of the list as HTML.
588600
*
589601
* @return string The node html
590-
*
591-
* @throws \InvalidArgumentException When current node is empty
602+
* @throws \InvalidArgumentException When current node is empty unless the emptyValue option is set
592603
*/
593604
public function html()
594605
{
595606
if (!$this->nodes) {
607+
if ($this->emptyValue) {
608+
return '';
609+
}
610+
596611
throw new \InvalidArgumentException('The current node list is empty.');
597612
}
598613

@@ -1152,7 +1167,7 @@ private function findNamespacePrefixes(string $xpath): array
11521167
*/
11531168
private function createSubCrawler($nodes)
11541169
{
1155-
$crawler = new static($nodes, $this->uri, $this->baseHref);
1170+
$crawler = new static($nodes, $this->uri, $this->baseHref, $this->emptyValue);
11561171
$crawler->isHtml = $this->isHtml;
11571172
$crawler->document = $this->document;
11581173
$crawler->namespaces = $this->namespaces;

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

Lines changed: 6 additions & 2 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('', $this->createTestCrawler(null, true)->filterXPath('//ol')->text());
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('', $this->createTestCrawler(null, true)->filterXPath('//ol')->html());
408412
}
409413

410414
public function testExtract()
@@ -1204,7 +1208,7 @@ public function testInheritedClassCallChildrenWithoutArgument()
12041208
$crawlerChild->children();
12051209
}
12061210

1207-
public function createTestCrawler($uri = null)
1211+
public function createTestCrawler($uri = null, $emptyValue = false)
12081212
{
12091213
$dom = new \DOMDocument();
12101214
$dom->loadHTML('
@@ -1253,7 +1257,7 @@ public function createTestCrawler($uri = null)
12531257
</html>
12541258
');
12551259

1256-
return new Crawler($dom, $uri);
1260+
return new Crawler($dom, $uri, null, $emptyValue);
12571261
}
12581262

12591263
protected function createTestXmlCrawler($uri = null)

0 commit comments

Comments
 (0)