Skip to content

Commit 3245731

Browse files
authored
Merge pull request paquettg#101 from mallardduck/patch-1
Fix issues with HTML cache and updates
2 parents 063ca52 + f557f95 commit 3245731

File tree

6 files changed

+78
-4
lines changed

6 files changed

+78
-4
lines changed

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ public function delete()
169169
if ( ! is_null($this->parent)) {
170170
$this->parent->removeChild($this->id);
171171
}
172-
173-
$this->parent = null;
172+
$this->parent->clear();
173+
$this->clear();
174174
}
175175

176176
/**
@@ -305,6 +305,9 @@ public function setAttribute($key, $value)
305305
{
306306
$this->tag->setAttribute($key, $value);
307307

308+
//clear any cache
309+
$this->clear();
310+
308311
return $this;
309312
}
310313

@@ -318,6 +321,9 @@ public function setAttribute($key, $value)
318321
public function removeAttribute($key)
319322
{
320323
$this->tag->removeAttribute($key);
324+
325+
//clear any cache
326+
$this->clear();
321327
}
322328

323329
/**
@@ -329,8 +335,10 @@ public function removeAttribute($key)
329335
public function removeAllAttributes()
330336
{
331337
$this->tag->removeAllAttributes();
332-
}
333338

339+
//clear any cache
340+
$this->clear();
341+
}
334342
/**
335343
* Function to locate a specific ancestor tag in the path to the root.
336344
*

src/PHPHtmlParser/Dom/HtmlNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ protected function clear()
186186
$this->innerHtml = null;
187187
$this->outerHtml = null;
188188
$this->text = null;
189+
if (is_null($this->parent) === false) {
190+
$this->parent->clear();
191+
}
189192
}
190193

191194
/**

src/PHPHtmlParser/Dom/InnerNode.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ public function replaceChild($childId, AbstractNode $newChild)
242242
$this->children = array_combine($keys, $this->children);
243243
$this->children[$newChild->id()] = $newChild;
244244
unset($oldChild);
245+
246+
//clear any cache
247+
$this->clear();
245248
}
246249

247250
/**
@@ -314,4 +317,4 @@ public function setParent(InnerNode $parent)
314317

315318
return parent::setParent($parent);
316319
}
317-
}
320+
}

src/PHPHtmlParser/Dom/MockNode.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ protected function clear()
4040
$this->innerHtml = null;
4141
$this->outerHtml = null;
4242
$this->text = null;
43+
if (is_null($this->parent) === false) {
44+
$this->parent->clear();
45+
}
4346
}
4447

4548
/**

tests/Node/HtmlTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,39 @@ public function testOuterHtmlNoValueAttribute()
246246
$this->assertEquals('<div class="all"><a href=\'http://google.com\' ui-view>link</a><br /></div>', $parent->outerHtml);
247247
}
248248

249+
public function testOuterHtmlWithChanges()
250+
{
251+
$div = new Tag('div');
252+
$div->setAttributes([
253+
'class' => [
254+
'value' => 'all',
255+
'doubleQuote' => true,
256+
],
257+
]);
258+
$a = new Tag('a');
259+
$a->setAttributes([
260+
'href' => [
261+
'value' => 'http://google.com',
262+
'doubleQuote' => false,
263+
],
264+
]);
265+
$br = new Tag('br');
266+
$br->selfClosing();
267+
268+
$parent = new HtmlNode($div);
269+
$childa = new HtmlNode($a);
270+
$childbr = new HtmlNode($br);
271+
$parent->addChild($childa);
272+
$parent->addChild($childbr);
273+
$childa->addChild(new TextNode('link'));
274+
275+
$this->assertEquals('<div class="all"><a href=\'http://google.com\'>link</a><br /></div>', $parent->outerHtml());
276+
277+
$childa->setAttribute('href', 'https://www.google.com');
278+
279+
$this->assertEquals('<a href="https://www.google.com">link</a>', $childa->outerHtml());
280+
}
281+
249282
public function testText()
250283
{
251284
$a = new Tag('a');

tests/Node/TagTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ public function testSetAttributesNoDoubleArray()
6868
$this->assertEquals('funtimes', $tag->class['value']);
6969
}
7070

71+
public function testUpdateAttributes()
72+
{
73+
$tag = new Tag('a');
74+
$tag->setAttributes([
75+
'href' => [
76+
'value' => 'http://google.com',
77+
'doubleQuote' => false,
78+
],
79+
]);
80+
81+
$this->assertEquals(null, $tag->class['value']);
82+
$this->assertEquals('http://google.com', $tag->href['value']);
83+
84+
85+
$attr = [
86+
'href' => 'https://www.google.com',
87+
'class' => 'funtimes',
88+
];
89+
90+
$tag->setAttributes($attr);
91+
$this->assertEquals('funtimes', $tag->class['value']);
92+
$this->assertEquals('https://www.google.com', $tag->href['value']);
93+
}
94+
7195
public function testNoise()
7296
{
7397
$tag = new Tag('a');

0 commit comments

Comments
 (0)