File tree Expand file tree Collapse file tree 4 files changed +48
-0
lines changed
src/PHPHtmlParser/Dom/Node Expand file tree Collapse file tree 4 files changed +48
-0
lines changed Original file line number Diff line number Diff line change @@ -114,6 +114,8 @@ public function __get(string $key)
114114 return $ this ->outerHtml ();
115115 case 'innerhtml ' :
116116 return $ this ->innerHtml ();
117+ case 'innertext ' :
118+ return $ this ->innerText ();
117119 case 'text ' :
118120 return $ this ->text ();
119121 case 'tag ' :
Original file line number Diff line number Diff line change @@ -27,6 +27,13 @@ class HtmlNode extends InnerNode
2727 */
2828 protected $ outerHtml ;
2929
30+ /**
31+ * Remembers what the innerText was if it was scanned previously.
32+ *
33+ * @var ?string
34+ */
35+ protected $ innerText = null ;
36+
3037 /**
3138 * Remembers what the text was if it was scanned previously.
3239 *
@@ -111,6 +118,21 @@ public function innerHtml(): string
111118 return $ string ;
112119 }
113120
121+ /**
122+ * Gets the inner text of this node.
123+ * @return string
124+ * @throws ChildNotFoundException
125+ * @throws UnknownChildTypeException
126+ */
127+ public function innerText (): string
128+ {
129+ if (is_null ($ this ->innerText )) {
130+ $ this ->innerText = strip_tags ($ this ->innerHtml ());
131+ }
132+
133+ return $ this ->innerText ;
134+ }
135+
114136 /**
115137 * Gets the html of this node, including it's own
116138 * tag.
Original file line number Diff line number Diff line change @@ -330,6 +330,16 @@ public function testEmptyAttribute()
330330 $ this ->assertEquals (1 , \count ($ items ));
331331 }
332332
333+ public function testInnerText ()
334+ {
335+ $ html = <<<EOF
336+ <body class="" style="" data-gr-c-s-loaded="true">123<a>456789</a><span>101112</span></body>
337+ EOF ;
338+ $ dom = new Dom ();
339+ $ dom ->loadStr ($ html );
340+ $ this ->assertEquals ($ dom ->innerText , "123456789101112 " );
341+ }
342+
333343 public function testMultipleSquareSelector ()
334344 {
335345 $ dom = new Dom ();
Original file line number Diff line number Diff line change @@ -323,6 +323,20 @@ public function testTextLookInChildren()
323323 $ this ->assertEquals ('Please click me! ' , $ node ->text (true ));
324324 }
325325
326+ public function testInnerText ()
327+ {
328+ $ node = new HtmlNode ('div ' );
329+ $ node ->addChild (new TextNode ('123 ' ));
330+ $ anode = new HtmlNode ('a ' );
331+ $ anode ->addChild (new TextNode ('456789 ' ));
332+ $ span_node = new HtmlNode ('span ' );
333+ $ span_node ->addChild (new TextNode ('101112 ' ));
334+
335+ $ node ->addChild ($ anode );
336+ $ node ->addChild ($ span_node );
337+ $ this ->assertEquals ($ node ->innerText (), '123 456789 101112 ' );
338+ }
339+
326340 public function testTextLookInChildrenAndNoChildren ()
327341 {
328342 $ p = new HtmlNode ('p ' );
You can’t perform that action at this time.
0 commit comments