Skip to content

Commit a2c5eb1

Browse files
authored
Merge pull request #226 from HongjiangHuang/dev/3.0.0
[dev/3.0.0]feature: innerText property support
2 parents c487fce + a430293 commit a2c5eb1

File tree

4 files changed

+48
-0
lines changed

4 files changed

+48
-0
lines changed

src/PHPHtmlParser/Dom/Node/AbstractNode.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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':

src/PHPHtmlParser/Dom/Node/HtmlNode.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

tests/DomTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff 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();

tests/Node/HtmlTest.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff 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');

0 commit comments

Comments
 (0)