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)
114
114
return $ this ->outerHtml ();
115
115
case 'innerhtml ' :
116
116
return $ this ->innerHtml ();
117
+ case 'innertext ' :
118
+ return $ this ->innerText ();
117
119
case 'text ' :
118
120
return $ this ->text ();
119
121
case 'tag ' :
Original file line number Diff line number Diff line change @@ -27,6 +27,13 @@ class HtmlNode extends InnerNode
27
27
*/
28
28
protected $ outerHtml ;
29
29
30
+ /**
31
+ * Remembers what the innerText was if it was scanned previously.
32
+ *
33
+ * @var ?string
34
+ */
35
+ protected $ innerText = null ;
36
+
30
37
/**
31
38
* Remembers what the text was if it was scanned previously.
32
39
*
@@ -111,6 +118,21 @@ public function innerHtml(): string
111
118
return $ string ;
112
119
}
113
120
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
+
114
136
/**
115
137
* Gets the html of this node, including it's own
116
138
* tag.
Original file line number Diff line number Diff line change @@ -330,6 +330,16 @@ public function testEmptyAttribute()
330
330
$ this ->assertEquals (1 , \count ($ items ));
331
331
}
332
332
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
+
333
343
public function testMultipleSquareSelector ()
334
344
{
335
345
$ dom = new Dom ();
Original file line number Diff line number Diff line change @@ -323,6 +323,20 @@ public function testTextLookInChildren()
323
323
$ this ->assertEquals ('Please click me! ' , $ node ->text (true ));
324
324
}
325
325
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
+
326
340
public function testTextLookInChildrenAndNoChildren ()
327
341
{
328
342
$ p = new HtmlNode ('p ' );
You can’t perform that action at this time.
0 commit comments