Skip to content

Commit 45d18c3

Browse files
committed
Added removeAttribute and removeAllAttributes tag methods
fixes paquettg#57
1 parent 5ed2e83 commit 45d18c3

File tree

5 files changed

+123
-34
lines changed

5 files changed

+123
-34
lines changed

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* @property string outerhtml
1313
* @property string innerhtml
1414
* @property string text
15+
* @property \PHPHtmlParser\Dom\Tag tag
16+
* @property InnerNode parent
1517
*/
1618
abstract class AbstractNode
1719
{
@@ -78,6 +80,9 @@ public function __get($key)
7880
return $this->innerHtml();
7981
case 'text':
8082
return $this->text();
83+
case 'tag':
84+
return $this->getTag();
85+
case 'parent': $this->getParent();
8186
}
8287

8388
return null;
@@ -214,33 +219,6 @@ public function getAncestor($id)
214219
return null;
215220
}
216221

217-
/**
218-
* Shortcut to return the first child.
219-
*
220-
* @return AbstractNode
221-
* @uses $this->getChild()
222-
*/
223-
public function firstChild()
224-
{
225-
reset($this->children);
226-
$key = key($this->children);
227-
228-
return $this->getChild($key);
229-
}
230-
231-
/**
232-
* Attempts to get the last child.
233-
*
234-
* @return AbstractNode
235-
*/
236-
public function lastChild()
237-
{
238-
end($this->children);
239-
$key = key($this->children);
240-
241-
return $this->getChild($key);
242-
}
243-
244222
/**
245223
* Attempts to get the next sibling.
246224
*
@@ -329,6 +307,29 @@ public function setAttribute($key, $value)
329307
return $this;
330308
}
331309

310+
/**
311+
* A wrapper method that simply calls the removeAttribute method
312+
* on the tag of this node.
313+
*
314+
* @param string $key
315+
* @return void
316+
*/
317+
public function removeAttribute($key)
318+
{
319+
$this->tag->removeAttribute($key);
320+
}
321+
322+
/**
323+
* A wrapper method that simply calls the removeAllAttributes
324+
* method on the tag of this node.
325+
*
326+
* @return void
327+
*/
328+
public function removeAllAttributes()
329+
{
330+
$this->tag->removeAllAttributes();
331+
}
332+
332333
/**
333334
* Function to locate a specific ancestor tag in the path to the root.
334335
*

src/PHPHtmlParser/Dom/InnerNode.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,33 @@ public function replaceChild($childId, AbstractNode $newChild)
244244
unset($oldChild);
245245
}
246246

247+
/**
248+
* Shortcut to return the first child.
249+
*
250+
* @return AbstractNode
251+
* @uses $this->getChild()
252+
*/
253+
public function firstChild()
254+
{
255+
reset($this->children);
256+
$key = key($this->children);
257+
258+
return $this->getChild($key);
259+
}
260+
261+
/**
262+
* Attempts to get the last child.
263+
*
264+
* @return AbstractNode
265+
*/
266+
public function lastChild()
267+
{
268+
end($this->children);
269+
$key = key($this->children);
270+
271+
return $this->getChild($key);
272+
}
273+
247274
/**
248275
* Checks if the given node id is a descendant of the
249276
* current node.

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public function noise($noise)
136136
* Set an attribute for this tag.
137137
*
138138
* @param string $key
139-
* @param mixed $value
139+
* @param string|array $value
140140
* @return $this
141141
*/
142142
public function setAttribute($key, $value)
@@ -153,6 +153,28 @@ public function setAttribute($key, $value)
153153
return $this;
154154
}
155155

156+
/**
157+
* Removes an attribute from this tag.
158+
*
159+
* @param $key
160+
* @return void
161+
*/
162+
public function removeAttribute($key)
163+
{
164+
$key = strtolower($key);
165+
unset($this->attr[$key]);
166+
}
167+
168+
/**
169+
* Removes all attributes on this tag.
170+
*
171+
* @return void
172+
*/
173+
public function removeAllAttributes()
174+
{
175+
$this->attr = [];
176+
}
177+
156178
/**
157179
* Sets the attributes for this tag
158180
*

tests/Node/HtmlTest.php

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,15 @@ public function testInnerHtmlTwice()
4747
],
4848
]);
4949
$a = new Tag('a');
50-
$a->setAttributes([
51-
'href' => [
52-
'value' => 'http://google.com',
53-
'doubleQuote' => false,
54-
],
55-
]);
5650
$br = new Tag('br');
5751
$br->selfClosing();
5852

5953
$parent = new HtmlNode($div);
6054
$childa = new HtmlNode($a);
55+
$childa->setAttribute('href', [
56+
'value' => 'http://google.com',
57+
'doubleQuote' => false,
58+
]);
6159
$childbr = new HtmlNode($br);
6260
$parent->addChild($childa);
6361
$parent->addChild($childbr);
@@ -370,6 +368,23 @@ public function testSetAttribute()
370368
$this->assertEquals('foo', $node->getAttribute('class'));
371369
}
372370

371+
public function testRemoveAttribute()
372+
{
373+
$node = new HtmlNode('a');
374+
$node->setAttribute('class', 'foo');
375+
$node->removeAttribute('class');
376+
$this->assertnull($node->getAttribute('class'));
377+
}
378+
379+
public function testRemoveAllAttributes()
380+
{
381+
$node = new HtmlNode('a');
382+
$node->setAttribute('class', 'foo');
383+
$node->setAttribute('href', 'http://google.com');
384+
$node->removeAllAttributes();
385+
$this->assertEquals(0, count($node->getAttributes()));
386+
}
387+
373388
public function testCountable()
374389
{
375390
$div = new Tag('div');

tests/Node/TagTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ public function testSetAttributes()
2525
$this->assertEquals('http://google.com', $tag->getAttribute('href')['value']);
2626
}
2727

28+
public function testRemoveAttribute()
29+
{
30+
$tag = new Tag('a');
31+
$tag->setAttribute('href', 'http://google.com');
32+
$tag->removeAttribute('href');
33+
$this->assertNull($tag->getAttribute('href')['value']);
34+
}
35+
36+
public function testRemoveAllAttributes()
37+
{
38+
$attr = [
39+
'class' => [
40+
'value' => 'clear-fix',
41+
'doubleQuote' => true,
42+
],
43+
];
44+
45+
$tag = new Tag('a');
46+
$tag->setAttribute('href', 'http://google.com');
47+
$tag->setAttribute('class', $attr);
48+
$tag->removeAllAttributes();
49+
$this->assertEquals(0, count($tag->getAttributes()));
50+
}
51+
2852
public function testSetAttributeNoArray()
2953
{
3054
$tag = new Tag('a');

0 commit comments

Comments
 (0)