File tree Expand file tree Collapse file tree 3 files changed +19
-2
lines changed Expand file tree Collapse file tree 3 files changed +19
-2
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ CHANGELOG
55-----
66
77* 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.
89
9104.2.0
1011-----
Original file line number Diff line number Diff line change @@ -570,13 +570,19 @@ public function nodeName()
570570 /**
571571 * Returns the node value of the first node of the list.
572572 *
573+ * @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
574+ *
573575 * @return string The node value
574576 *
575577 * @throws \InvalidArgumentException When current node is empty
576578 */
577- public function text ()
579+ public function text (/* $default = null */ )
578580 {
579581 if (!$ this ->nodes ) {
582+ if (0 < \func_num_args ()) {
583+ return \func_get_arg (0 );
584+ }
585+
580586 throw new \InvalidArgumentException ('The current node list is empty. ' );
581587 }
582588
@@ -586,13 +592,19 @@ public function text()
586592 /**
587593 * Returns the first node of the list as HTML.
588594 *
595+ * @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
596+ *
589597 * @return string The node html
590598 *
591599 * @throws \InvalidArgumentException When current node is empty
592600 */
593- public function html ()
601+ public function html (/* $default = null */ )
594602 {
595603 if (!$ this ->nodes ) {
604+ if (0 < \func_num_args ()) {
605+ return \func_get_arg (0 );
606+ }
607+
596608 throw new \InvalidArgumentException ('The current node list is empty. ' );
597609 }
598610
Original file line number Diff line number Diff 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 ('my value ' , $ this ->createTestCrawler (null )->filterXPath ('//ol ' )->text ('my value ' ));
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 ('my value ' , $ this ->createTestCrawler (null )->filterXPath ('//ol ' )->html ('my value ' ));
408412 }
409413
410414 public function testExtract ()
You can’t perform that action at this time.
0 commit comments