Skip to content

Commit

Permalink
Added removeAttribute and removeAllAttributes tag methods
Browse files Browse the repository at this point in the history
fixes #57
  • Loading branch information
paquettg committed Apr 6, 2016
1 parent 5ed2e83 commit 45d18c3
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 34 deletions.
55 changes: 28 additions & 27 deletions src/PHPHtmlParser/Dom/AbstractNode.php
Expand Up @@ -12,6 +12,8 @@
* @property string outerhtml
* @property string innerhtml
* @property string text
* @property \PHPHtmlParser\Dom\Tag tag
* @property InnerNode parent
*/
abstract class AbstractNode
{
Expand Down Expand Up @@ -78,6 +80,9 @@ public function __get($key)
return $this->innerHtml();
case 'text':
return $this->text();
case 'tag':
return $this->getTag();
case 'parent': $this->getParent();
}

return null;
Expand Down Expand Up @@ -214,33 +219,6 @@ public function getAncestor($id)
return null;
}

/**
* Shortcut to return the first child.
*
* @return AbstractNode
* @uses $this->getChild()
*/
public function firstChild()
{
reset($this->children);
$key = key($this->children);

return $this->getChild($key);
}

/**
* Attempts to get the last child.
*
* @return AbstractNode
*/
public function lastChild()
{
end($this->children);
$key = key($this->children);

return $this->getChild($key);
}

/**
* Attempts to get the next sibling.
*
Expand Down Expand Up @@ -329,6 +307,29 @@ public function setAttribute($key, $value)
return $this;
}

/**
* A wrapper method that simply calls the removeAttribute method
* on the tag of this node.
*
* @param string $key
* @return void
*/
public function removeAttribute($key)
{
$this->tag->removeAttribute($key);
}

/**
* A wrapper method that simply calls the removeAllAttributes
* method on the tag of this node.
*
* @return void
*/
public function removeAllAttributes()
{
$this->tag->removeAllAttributes();
}

/**
* Function to locate a specific ancestor tag in the path to the root.
*
Expand Down
27 changes: 27 additions & 0 deletions src/PHPHtmlParser/Dom/InnerNode.php
Expand Up @@ -244,6 +244,33 @@ public function replaceChild($childId, AbstractNode $newChild)
unset($oldChild);
}

/**
* Shortcut to return the first child.
*
* @return AbstractNode
* @uses $this->getChild()
*/
public function firstChild()
{
reset($this->children);
$key = key($this->children);

return $this->getChild($key);
}

/**
* Attempts to get the last child.
*
* @return AbstractNode
*/
public function lastChild()
{
end($this->children);
$key = key($this->children);

return $this->getChild($key);
}

/**
* Checks if the given node id is a descendant of the
* current node.
Expand Down
24 changes: 23 additions & 1 deletion src/PHPHtmlParser/Dom/Tag.php
Expand Up @@ -136,7 +136,7 @@ public function noise($noise)
* Set an attribute for this tag.
*
* @param string $key
* @param mixed $value
* @param string|array $value
* @return $this
*/
public function setAttribute($key, $value)
Expand All @@ -153,6 +153,28 @@ public function setAttribute($key, $value)
return $this;
}

/**
* Removes an attribute from this tag.
*
* @param $key
* @return void
*/
public function removeAttribute($key)
{
$key = strtolower($key);
unset($this->attr[$key]);
}

/**
* Removes all attributes on this tag.
*
* @return void
*/
public function removeAllAttributes()
{
$this->attr = [];
}

/**
* Sets the attributes for this tag
*
Expand Down
27 changes: 21 additions & 6 deletions tests/Node/HtmlTest.php
Expand Up @@ -47,17 +47,15 @@ public function testInnerHtmlTwice()
],
]);
$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);
$childa->setAttribute('href', [
'value' => 'http://google.com',
'doubleQuote' => false,
]);
$childbr = new HtmlNode($br);
$parent->addChild($childa);
$parent->addChild($childbr);
Expand Down Expand Up @@ -370,6 +368,23 @@ public function testSetAttribute()
$this->assertEquals('foo', $node->getAttribute('class'));
}

public function testRemoveAttribute()
{
$node = new HtmlNode('a');
$node->setAttribute('class', 'foo');
$node->removeAttribute('class');
$this->assertnull($node->getAttribute('class'));
}

public function testRemoveAllAttributes()
{
$node = new HtmlNode('a');
$node->setAttribute('class', 'foo');
$node->setAttribute('href', 'http://google.com');
$node->removeAllAttributes();
$this->assertEquals(0, count($node->getAttributes()));
}

public function testCountable()
{
$div = new Tag('div');
Expand Down
24 changes: 24 additions & 0 deletions tests/Node/TagTest.php
Expand Up @@ -25,6 +25,30 @@ public function testSetAttributes()
$this->assertEquals('http://google.com', $tag->getAttribute('href')['value']);
}

public function testRemoveAttribute()
{
$tag = new Tag('a');
$tag->setAttribute('href', 'http://google.com');
$tag->removeAttribute('href');
$this->assertNull($tag->getAttribute('href')['value']);
}

public function testRemoveAllAttributes()
{
$attr = [
'class' => [
'value' => 'clear-fix',
'doubleQuote' => true,
],
];

$tag = new Tag('a');
$tag->setAttribute('href', 'http://google.com');
$tag->setAttribute('class', $attr);
$tag->removeAllAttributes();
$this->assertEquals(0, count($tag->getAttributes()));
}

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

0 comments on commit 45d18c3

Please sign in to comment.