Skip to content

Commit 85b21b7

Browse files
committed
Added an optional paramter to enable recursive text
1 parent 84b3440 commit 85b21b7

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,14 @@ class HtmlNode extends AbstractNode {
2525
*/
2626
protected $text = null;
2727

28+
/**
29+
* Remembers what the text was when we looked into all our
30+
* children nodes.
31+
*
32+
* @var string
33+
*/
34+
protected $textWithChildren = null;
35+
2836
/**
2937
* Sets up the tag of this node.
3038
*/
@@ -134,37 +142,54 @@ public function outerHtml()
134142
}
135143

136144
/**
137-
* Gets the text of this node (if there is any text).
145+
* Gets the text of this node (if there is any text). Or get all the text
146+
* in this node, including children.
138147
*
148+
* @param bool $lookInChildren
139149
* @return string
140150
*/
141-
public function text()
151+
public function text($lookInChildren = false)
142152
{
143-
if ( ! is_null($this->text))
153+
if ($lookInChildren)
154+
{
155+
if ( ! is_null($this->textWithChildren))
156+
{
157+
// we already know the results.
158+
return $this->textWithChildren;
159+
}
160+
}
161+
elseif( ! is_null($this->text))
144162
{
145163
// we already know the results.
146164
return $this->text;
147165
}
148166

149167
// find out if this node has any text children
168+
$text = '';
150169
foreach ($this->children as $child)
151170
{
152171
if ($child['node'] instanceof TextNode)
153172
{
154-
// we found a text node
155-
$text = $child['node']->text();
156-
157-
// remember the results
158-
$this->text = $text;
159-
160-
return $text;
173+
$text .= $child['node']->text;
174+
}
175+
elseif($lookInChildren and
176+
$child['node'] instanceof HtmlNode)
177+
{
178+
$text .= $child['node']->text($lookInChildren);
161179
}
162180
}
163181

164-
// no text found in this node
165-
$this->text = '';
182+
// remember our result
183+
if ($lookInChildren)
184+
{
185+
$this->textWithChildren = $text;
186+
}
187+
else
188+
{
189+
$this->text = $text;
190+
}
166191

167-
return '';
192+
return $text;
168193
}
169194

170195
/**

tests/Node/HtmlTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,35 @@ public function testTextMagic()
155155
$this->assertEquals('link', $node->text);
156156
}
157157

158+
public function testTextLookInChildren()
159+
{
160+
$p = new HtmlNode('p');
161+
$a = new HtmlNode('a');
162+
$a->addChild(new TextNode('click me'));
163+
$p->addChild(new TextNode('Please '));
164+
$p->addChild($a);
165+
$p->addChild(new TextNode('!'));
166+
$node = new HtmlNode('div');
167+
$node->addChild($p);
168+
169+
$this->assertEquals('Please click me!', $node->text(true));
170+
}
171+
172+
public function testTextLookInChildrenAndNoChildren()
173+
{
174+
$p = new HtmlNode('p');
175+
$a = new HtmlNode('a');
176+
$a->addChild(new TextNode('click me'));
177+
$p->addChild(new TextNode('Please '));
178+
$p->addChild($a);
179+
$p->addChild(new TextNode('!'));
180+
181+
$p->text;
182+
$p->text(true);
183+
184+
$this->assertEquals('Please click me!', $p->text(true));
185+
}
186+
158187
public function testGetAttribute()
159188
{
160189
$node = new HtmlNode('a');

0 commit comments

Comments
 (0)