File tree 3 files changed +19
-2
lines changed
src/Symfony/Component/DomCrawler
3 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ CHANGELOG
5
5
-----
6
6
7
7
* Added return of element name (` _name ` ) in ` extract() ` method.
8
+ * Added ability to return a default value in ` text() ` and ` html() ` instead of throwing an exception when node is empty.
8
9
9
10
4.2.0
10
11
-----
Original file line number Diff line number Diff line change @@ -570,13 +570,19 @@ public function nodeName()
570
570
/**
571
571
* Returns the node value of the first node of the list.
572
572
*
573
+ * @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
574
+ *
573
575
* @return string The node value
574
576
*
575
577
* @throws \InvalidArgumentException When current node is empty
576
578
*/
577
- public function text ()
579
+ public function text (/* $default = null */ )
578
580
{
579
581
if (!$ this ->nodes ) {
582
+ if (0 < \func_num_args ()) {
583
+ return \func_get_arg (0 );
584
+ }
585
+
580
586
throw new \InvalidArgumentException ('The current node list is empty. ' );
581
587
}
582
588
@@ -586,13 +592,19 @@ public function text()
586
592
/**
587
593
* Returns the first node of the list as HTML.
588
594
*
595
+ * @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
596
+ *
589
597
* @return string The node html
590
598
*
591
599
* @throws \InvalidArgumentException When current node is empty
592
600
*/
593
- public function html ()
601
+ public function html (/* $default = null */ )
594
602
{
595
603
if (!$ this ->nodes ) {
604
+ if (0 < \func_num_args ()) {
605
+ return \func_get_arg (0 );
606
+ }
607
+
596
608
throw new \InvalidArgumentException ('The current node list is empty. ' );
597
609
}
598
610
Original file line number Diff line number Diff line change @@ -392,6 +392,8 @@ public function testText()
392
392
} catch (\InvalidArgumentException $ e ) {
393
393
$ this ->assertTrue (true , '->text() throws an \InvalidArgumentException if the node list is empty ' );
394
394
}
395
+
396
+ $ this ->assertSame ('my value ' , $ this ->createTestCrawler (null )->filterXPath ('//ol ' )->text ('my value ' ));
395
397
}
396
398
397
399
public function testHtml ()
@@ -405,6 +407,8 @@ public function testHtml()
405
407
} catch (\InvalidArgumentException $ e ) {
406
408
$ this ->assertTrue (true , '->html() throws an \InvalidArgumentException if the node list is empty ' );
407
409
}
410
+
411
+ $ this ->assertSame ('my value ' , $ this ->createTestCrawler (null )->filterXPath ('//ol ' )->html ('my value ' ));
408
412
}
409
413
410
414
public function testExtract ()
You can’t perform that action at this time.
0 commit comments