Skip to content

Commit a06a0ae

Browse files
committed
Added a delete method.
fixes paquettg#43
1 parent 160bf74 commit a06a0ae

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PHP Html Parser
22
==========================
33

4-
Version 1.6.9
4+
Version 1.6.9 - DEV
55

66
[![Build Status](https://travis-ci.org/paquettg/php-html-parser.png)](https://travis-ci.org/paquettg/php-html-parser)
77
[![Coverage Status](https://coveralls.io/repos/paquettg/php-html-parser/badge.png)](https://coveralls.io/r/paquettg/php-html-parser)
@@ -206,3 +206,14 @@ $tag = $a->getTag();
206206
$tag->setAttribute('class', 'foo');
207207
echo $a->getAttribute('class'); // "foo"
208208
```
209+
210+
It is also possible to remove a node from the tree. Simply call the `delete` method on any node to remove it from the tree. It is important to note that you should unset the node after removing it from the `DOM``, it will still take memory as long as it is not unset.
211+
212+
```php
213+
$dom = new Dom;
214+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
215+
$a = $dom->find('a')[0];
216+
$a->delete();
217+
unset($a);
218+
echo $dom; // '<div class="all"><p>Hey bro, <br /> :)</p></div>');
219+
```

src/PHPHtmlParser/Dom.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace PHPHtmlParser;
33

4-
use PHPHtmlPArser\Dom\AbstractNode;
4+
use PHPHtmlParser\Dom\AbstractNode;
55
use PHPHtmlParser\Dom\HtmlNode;
66
use PHPHtmlParser\Dom\TextNode;
77
use PHPHtmlParser\Exceptions\NotLoadedException;

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ public function setParent(InnerNode $parent)
152152
return $this;
153153
}
154154

155+
/**
156+
* Removes this node and all its children from the
157+
* DOM tree.
158+
*
159+
* @return void
160+
*/
161+
public function delete()
162+
{
163+
if ( ! is_null($this->parent)) {
164+
$this->parent->removeChild($this->id);
165+
}
166+
167+
$this->parent = null;
168+
}
169+
155170
/**
156171
* Sets the encoding class to this node.
157172
*

tests/DomTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,4 +309,14 @@ public function testCodeTag()
309309
$dom->load('<strong>hello</strong><code class="language-php">$foo = "bar";</code>');
310310
$this->assertEquals('<strong>hello</strong><code class="language-php">$foo = "bar";</code>', (string) $dom);
311311
}
312+
313+
public function testDeleteNode()
314+
{
315+
$dom = new Dom;
316+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
317+
$a = $dom->find('a')[0];
318+
$a->delete();
319+
unset($a);
320+
$this->assertEquals('<div class="all"><p>Hey bro, <br /> :)</p></div>', (string) $dom);
321+
}
312322
}

0 commit comments

Comments
 (0)