Skip to content

Commit 5ab2a4e

Browse files
committed
Added integrity checks for the cached html
1 parent 2bf24d7 commit 5ab2a4e

File tree

3 files changed

+77
-18
lines changed

3 files changed

+77
-18
lines changed

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,15 @@ public function text()
162162

163163
return '';
164164
}
165+
166+
/**
167+
* Call this when something in the node tree has changed. Like a child has been added
168+
* or a parent has been changed.
169+
*/
170+
protected function clear()
171+
{
172+
$this->innerHtml = null;
173+
$this->outerHtml = null;
174+
$this->text = null;
175+
}
165176
}

src/PHPHtmlParser/Dom/Node.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,14 @@ public function __get($key)
8383
}
8484

8585
/**
86-
* @todo Remove the need for this to be called.
86+
* Attempts to clear out any object references.
8787
*/
8888
public function __destruct()
8989
{
90-
$this->clear();
90+
$this->tag = null;
91+
$this->attr = [];
92+
$this->parent = null;
93+
$this->children = [];
9194
}
9295

9396
/**
@@ -150,6 +153,9 @@ public function setParent(Node $parent)
150153
// assign child to parent
151154
$this->parent->addChild($this);
152155

156+
//clear any cache
157+
$this->clear();
158+
153159
return $this;
154160
}
155161

@@ -243,6 +249,9 @@ public function addChild(Node $child)
243249
// tell child I am the new parent
244250
$child->setParent($this);
245251

252+
//clear any cache
253+
$this->clear();
254+
246255
return true;
247256
}
248257

@@ -272,6 +281,9 @@ public function removeChild($id)
272281
// remove the child
273282
unset($this->children[$id]);
274283

284+
//clear any cache
285+
$this->clear();
286+
275287
return $this;
276288
}
277289

@@ -351,6 +363,26 @@ public function isAncestor($id)
351363
return false;
352364
}
353365

366+
/**
367+
* Attempts to get an ancestor node by the given id.
368+
*
369+
* @param int $id
370+
* @return null|Node
371+
*/
372+
public function getAncestor($id)
373+
{
374+
if ( ! is_null($this->parent))
375+
{
376+
if ($this->parent->id() == $id)
377+
{
378+
return $this->parent;
379+
}
380+
return $this->parent->getAncestor($id);
381+
}
382+
383+
return null;
384+
}
385+
354386
/**
355387
* Shortcut to return the first child.
356388
*
@@ -589,16 +621,9 @@ public function get_display_size()
589621
return $result;
590622
}
591623

592-
/**
593-
* clean up memory due to php5 circular references memory leak...
594-
*
595-
* @todo Remove the need for this. (Remove circular references)
596-
*/
597-
protected function clear()
598-
{
599-
$this->nodes = null;
600-
$this->parent = null;
601-
$this->children = null;
602-
}
603-
624+
/**
625+
* Call this when something in the node tree has changed. Like a child has been added
626+
* or a parent has been changed.
627+
*/
628+
protected function clear() {}
604629
}

src/PHPHtmlParser/Dom/TextNode.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ class TextNode extends Node {
1919
*/
2020
protected $text;
2121

22+
/**
23+
* This is the converted version of the text.
24+
*
25+
* @var string
26+
*/
27+
protected $convertedText = null;
28+
2229
/**
2330
* Sets the text for this node.
2431
*
@@ -30,7 +37,7 @@ public function __construct($text)
3037
$text = preg_replace('/\s+/', ' ', $text);
3138

3239
$this->text = $text;
33-
$this->tag = new Tag('text');
40+
$this->tag = new Tag('text');
3441
parent::__construct();
3542
}
3643

@@ -44,14 +51,30 @@ public function text()
4451
// convert charset
4552
if ( ! is_null($this->encode))
4653
{
54+
if ( ! is_null($this->convertedText))
55+
{
56+
// we already know the converted value
57+
return $this->convertedText;
58+
}
4759
$text = $this->encode->convert($this->text);
60+
61+
// remember the conversion
62+
$this->convertedText = $text;
63+
64+
return $text;
4865
}
4966
else
5067
{
51-
$text = $this->text;
68+
return $this->text;
5269
}
53-
54-
return $text;
5570
}
5671

72+
/**
73+
* Call this when something in the node tree has changed. Like a child has been added
74+
* or a parent has been changed.
75+
*/
76+
protected function clear()
77+
{
78+
$this->convertedText = null;
79+
}
5780
}

0 commit comments

Comments
 (0)