Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/PHPHtmlParser/Dom/AbstractNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public function delete()
if ( ! is_null($this->parent)) {
$this->parent->removeChild($this->id);
}

$this->parent = null;
$this->parent->clear();
$this->clear();
}

/**
Expand Down Expand Up @@ -305,6 +305,9 @@ public function setAttribute($key, $value)
{
$this->tag->setAttribute($key, $value);

//clear any cache
$this->clear();

return $this;
}

Expand All @@ -318,6 +321,9 @@ public function setAttribute($key, $value)
public function removeAttribute($key)
{
$this->tag->removeAttribute($key);

//clear any cache
$this->clear();
}

/**
Expand All @@ -329,8 +335,10 @@ public function removeAttribute($key)
public function removeAllAttributes()
{
$this->tag->removeAllAttributes();
}

//clear any cache
$this->clear();
}
/**
* Function to locate a specific ancestor tag in the path to the root.
*
Expand Down
3 changes: 3 additions & 0 deletions src/PHPHtmlParser/Dom/HtmlNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ protected function clear()
$this->innerHtml = null;
$this->outerHtml = null;
$this->text = null;
if (is_null($this->parent) === false) {
$this->parent->clear();
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion src/PHPHtmlParser/Dom/InnerNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ public function replaceChild($childId, AbstractNode $newChild)
$this->children = array_combine($keys, $this->children);
$this->children[$newChild->id()] = $newChild;
unset($oldChild);

//clear any cache
$this->clear();
}

/**
Expand Down Expand Up @@ -314,4 +317,4 @@ public function setParent(InnerNode $parent)

return parent::setParent($parent);
}
}
}
3 changes: 3 additions & 0 deletions src/PHPHtmlParser/Dom/MockNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ protected function clear()
$this->innerHtml = null;
$this->outerHtml = null;
$this->text = null;
if (is_null($this->parent) === false) {
$this->parent->clear();
}
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/Node/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,39 @@ public function testOuterHtmlNoValueAttribute()
$this->assertEquals('<div class="all"><a href=\'http://google.com\' ui-view>link</a><br /></div>', $parent->outerHtml);
}

public function testOuterHtmlWithChanges()
{
$div = new Tag('div');
$div->setAttributes([
'class' => [
'value' => 'all',
'doubleQuote' => true,
],
]);
$a = new Tag('a');
$a->setAttributes([
'href' => [
'value' => 'http://google.com',
'doubleQuote' => false,
],
]);
$br = new Tag('br');
$br->selfClosing();

$parent = new HtmlNode($div);
$childa = new HtmlNode($a);
$childbr = new HtmlNode($br);
$parent->addChild($childa);
$parent->addChild($childbr);
$childa->addChild(new TextNode('link'));

$this->assertEquals('<div class="all"><a href=\'http://google.com\'>link</a><br /></div>', $parent->outerHtml());

$childa->setAttribute('href', 'https://www.google.com');

$this->assertEquals('<a href="https://www.google.com">link</a>', $childa->outerHtml());
}

public function testText()
{
$a = new Tag('a');
Expand Down
24 changes: 24 additions & 0 deletions tests/Node/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ public function testSetAttributesNoDoubleArray()
$this->assertEquals('funtimes', $tag->class['value']);
}

public function testUpdateAttributes()
{
$tag = new Tag('a');
$tag->setAttributes([
'href' => [
'value' => 'http://google.com',
'doubleQuote' => false,
],
]);

$this->assertEquals(null, $tag->class['value']);
$this->assertEquals('http://google.com', $tag->href['value']);


$attr = [
'href' => 'https://www.google.com',
'class' => 'funtimes',
];

$tag->setAttributes($attr);
$this->assertEquals('funtimes', $tag->class['value']);
$this->assertEquals('https://www.google.com', $tag->href['value']);
}

public function testNoise()
{
$tag = new Tag('a');
Expand Down