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 19c9818

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

File tree

3 files changed

+31
-13
lines changed
Filter options

3 files changed

+31
-13
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 null 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-11Lines changed: 24 additions & 11 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 null or an exception
59+
*
60+
* @var bool
6161
*/
62-
public function __construct($node = null, string $uri = null, string $baseHref = null)
62+
private $nullOnValue;
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 $nullOnValue Return null on text and html value instead of an exception
69+
*/
70+
public function __construct($node = null, string $uri = null, string $baseHref = null, bool $nullOnValue = false)
6371
{
6472
$this->uri = $uri;
6573
$this->baseHref = $baseHref ?: $uri;
74+
$this->nullOnValue = $nullOnValue;
6675

6776
$this->add($node);
6877
}
@@ -570,13 +579,15 @@ public function nodeName()
570579
/**
571580
* Returns the node value of the first node of the list.
572581
*
573-
* @return string The node value
574-
*
575-
* @throws \InvalidArgumentException When current node is empty
582+
* @return string|null The node value or null when current node is empty
576583
*/
577584
public function text()
578585
{
579586
if (!$this->nodes) {
587+
if ($this->nullOnValue) {
588+
return null;
589+
}
590+
580591
throw new \InvalidArgumentException('The current node list is empty.');
581592
}
582593

@@ -586,13 +597,15 @@ public function text()
586597
/**
587598
* Returns the first node of the list as HTML.
588599
*
589-
* @return string The node html
590-
*
591-
* @throws \InvalidArgumentException When current node is empty
600+
* @return string|null The node html or null when current node is empty
592601
*/
593602
public function html()
594603
{
595604
if (!$this->nodes) {
605+
if ($this->nullOnValue) {
606+
return null;
607+
}
608+
596609
throw new \InvalidArgumentException('The current node list is empty.');
597610
}
598611

@@ -1152,7 +1165,7 @@ private function findNamespacePrefixes(string $xpath): array
11521165
*/
11531166
private function createSubCrawler($nodes)
11541167
{
1155-
$crawler = new static($nodes, $this->uri, $this->baseHref);
1168+
$crawler = new static($nodes, $this->uri, $this->baseHref, $this->nullOnValue);
11561169
$crawler->isHtml = $this->isHtml;
11571170
$crawler->document = $this->document;
11581171
$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->assertNull($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->assertNull($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, $nullOnValue = 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, $nullOnValue);
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.