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 e0d0842

Browse filesBrowse files
committed
[DomCrawler] Added ability to return empty string on Crawler::text() and Crawler::html() instead of an exception
1 parent 5dadd95 commit e0d0842
Copy full SHA for e0d0842

File tree

3 files changed

+31
-9
lines changed
Filter options

3 files changed

+31
-9
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
@@ -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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Crawler.php
+24-7Lines changed: 24 additions & 7 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
61+
*/
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
6169
*/
62-
public function __construct($node = null, string $uri = null, string $baseHref = null)
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
}
@@ -572,11 +581,15 @@ public function nodeName()
572581
*
573582
* @return string The node value
574583
*
575-
* @throws \InvalidArgumentException When current node is empty
584+
* @throws \InvalidArgumentException When current node is empty unless the emptyValue option is set
576585
*/
577586
public function text()
578587
{
579588
if (!$this->nodes) {
589+
if ($this->emptyValue) {
590+
return '';
591+
}
592+
580593
throw new \InvalidArgumentException('The current node list is empty.');
581594
}
582595

@@ -588,11 +601,15 @@ public function text()
588601
*
589602
* @return string The node html
590603
*
591-
* @throws \InvalidArgumentException When current node is empty
604+
* @throws \InvalidArgumentException When current node is empty unless the emptyValue option is set
592605
*/
593606
public function html()
594607
{
595608
if (!$this->nodes) {
609+
if ($this->emptyValue) {
610+
return '';
611+
}
612+
596613
throw new \InvalidArgumentException('The current node list is empty.');
597614
}
598615

@@ -1152,7 +1169,7 @@ private function findNamespacePrefixes(string $xpath): array
11521169
*/
11531170
private function createSubCrawler($nodes)
11541171
{
1155-
$crawler = new static($nodes, $this->uri, $this->baseHref);
1172+
$crawler = new static($nodes, $this->uri, $this->baseHref, $this->emptyValue);
11561173
$crawler->isHtml = $this->isHtml;
11571174
$crawler->document = $this->document;
11581175
$crawler->namespaces = $this->namespaces;

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
+6-2Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.