Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
Fixed small issue in setting text for escaped HTML.
Added new feature which is the ability to remove a child by ID.
  • Loading branch information
usernane committed Aug 15, 2020
1 parent fc1f43b commit 260f960
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/html/HTMLDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ public function getLanguage() {
/**
* Removes a child node from the document.
*
* @param HTMLNode $node The node that will be removed. If the given
* node name is 'body' or 'head', The node will never be removed.
* @param HTMLNode|string $node The node that will be removed. This also
* can be the value of the attribute ID of the node that will be removed.
*
* @return HTMLNode|null The method will return the node if removed.
* If not removed, the method will return null.
Expand All @@ -315,6 +315,11 @@ public function getLanguage() {
public function removeChild($node) {
if ($node instanceof HTMLNode && $node !== $this->body && $node !== $this->headNode) {
return $this->_removeChild($this->getDocumentRoot(), $node);
} else if (gettype($node) == 'string') {
$toRemove = $this->getDocumentRoot()->getChildByID($node);
if ($toRemove !== $this->body && $toRemove !== $this->headNode) {
return $this->_removeChild($this->getDocumentRoot(), $toRemove);
}
}

return null;
Expand Down
29 changes: 20 additions & 9 deletions src/html/HTMLNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ public function addChild($node, $attrs = [], $chainOnParent = true) {
$lastChild = $this->getLastChild();

if ($lastChild !== null && $lastChild->getNodeName() == '#TEXT') {
$lastChild->setText($lastChild->getText().$toAdd->getText());
$lastChild->setText($lastChild->getText().$toAdd->getText(), $toAdd->getOriginalText() != $toAdd->getText());
} else {
$toAdd->_setParent($this);
$this->childrenList->add($toAdd);
Expand Down Expand Up @@ -1794,21 +1794,24 @@ public function removeAttributes() {
/**
* Removes a direct child node.
*
* @param HTMLNode $node The node that will be removed.
* @param HTMLNode|string $node The node that will be removed. This also can
* be the ID of the child that will be removed.
*
* @return HTMLNode|null The method will return the node if removed.
* If not removed, the method will return null.
*
* @since 1.2
*/
public function removeChild($node) {
if ($this->mustClose() && $node instanceof HTMLNode) {
$child = $this->children()->removeElement($node);

if ($child instanceof HTMLNode) {
$child->_setParent(null);

return $child;
if ($this->mustClose()) {

if ($node instanceof HTMLNode) {
$child = $this->children()->removeElement($node);
return $this->_removeChHelper($child);
} else if (gettype($node) == 'string') {
$toRemove = $this->getChildByID($node);
$child = $this->children()->removeElement($toRemove);
return $this->_removeChHelper($child);
}
}
}
Expand Down Expand Up @@ -2968,6 +2971,14 @@ private function _reduceTab() {
$this->tabCount -= 1;
}
}
private function _removeChHelper($node) {

if ($node instanceof HTMLNode) {
$node->_setParent(null);

return $node;
}
}
/**
* Replace all attributes values in HTML string with a hash.
*
Expand Down
19 changes: 19 additions & 0 deletions tests/html/HTMLDocTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,25 @@ public function testRemoveChild01() {
$this->assertTrue($doc->removeChild($ch01) === $ch01);
$this->assertEquals(2,$doc->getBody()->childrenCount());
}
/**
* @test
*/
public function testRemoveChild02() {
$doc = new HTMLDoc();
$ch00 = new HTMLNode();
$ch01 = new HTMLNode('input');
$ch02 = new HTMLNode();
$ch03 = new HTMLNode('input');
$ch03->setID('my-input');
$doc->addChild($ch00);
$doc->addChild($ch01);
$doc->addChild($ch02);
$doc->addChild($ch03);
$this->assertEquals(4,$doc->getBody()->childrenCount());
$removed = $doc->removeChild('my-input');
$this->assertEquals(3,$doc->getBody()->childrenCount());
$this->assertTrue($removed === $ch03);
}
/**
* @test
*/
Expand Down
19 changes: 19 additions & 0 deletions tests/html/HTMLNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1697,6 +1697,25 @@ public function testRemoveChild08() {
.'<p id="paragraph-2"></p>'
.'</div>',$node->toHTML());
}
/**
* @test
*/
public function testRemoveChild09() {
$node = new HTMLNode();
$node->text('Hello')->paragraph('Super Paragraph', ['id'=>'p-1'])
->div(['id'=>'empty-div'])->section('Hello Sec', 1, [
'id' => 'my-sec'
]);
$this->assertEquals(4, $node->childrenCount());
$node->removeChild('empty-div');
$this->assertEquals(3, $node->childrenCount());
$this->assertEquals('<div>'
. 'Hello'
. '<p id="p-1">'
. 'Super Paragraph</p>'
. '<section id="my-sec"><h1>Hello Sec</h1></section>'
. '</div>', $node->toHTML());
}
/**
* @test
*/
Expand Down

0 comments on commit 260f960

Please sign in to comment.