Skip to content

Commit 858ec46

Browse files
committed
Added setAttribute to the node
fixes paquettg#7
1 parent 2cf6f25 commit 858ec46

File tree

4 files changed

+55
-2
lines changed

4 files changed

+55
-2
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ The whitespaceTextNode, by default true, option tells the parser to save textnod
117117
The enforceEncoding, by default null, option will enforce an charater set to be used for reading the content and returning the content in that encoding. Setting it to null will trigger an attempt to figure out the encoding from within the content of the string given instead.
118118

119119
Static Facade
120-
------------
120+
-------------
121121

122122
You can also mount a static facade for the Dom object.
123123

@@ -130,3 +130,27 @@ $objects = Dom::find('.content-border');
130130
```
131131

132132
The above php block does the same find and load as the first example but it is done using the static facade, which supports all public methods found in the Dom object.
133+
134+
Modifying The Dom
135+
-----------------
136+
137+
You can always modify the dom that was created from any loading method. To change the attribute of any node you can just call the `setAttribute` method.
138+
139+
```php
140+
$dom = new Dom;
141+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
142+
$a = $dom->find('a')[0];
143+
$a->setAttribute('class', 'foo');
144+
echo $a->getAttribute('class'); // "foo"
145+
```
146+
147+
You may also get the `PHPHtmlParser\Dom\Tag` class directly and manipulate it as you see fit.
148+
149+
```php
150+
$dom = new Dom;
151+
$dom->load('<div class="all"><p>Hey bro, <a href="google.com">click here</a><br /> :)</p></div>');
152+
$a = $dom->find('a')[0];
153+
$tag = $a->getTag();
154+
$tag->setAttribute('class', 'foo');
155+
echo $a->getAttribute('class'); // "foo"
156+
```

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ public function getAttributes()
475475
}
476476

477477
/**
478-
* A wrapper method that simply calls the getAttributes method
478+
* A wrapper method that simply calls the getAttribute method
479479
* on the tag of this node.
480480
*
481481
* @param string $key
@@ -492,6 +492,20 @@ public function getAttribute($key)
492492
return $attribute;
493493
}
494494

495+
/**
496+
* A wrapper method that simply calls the setAttribute method
497+
* on the tag of this node.
498+
*
499+
* @param string $key
500+
* @param string $value
501+
* @chainable
502+
*/
503+
public function setAttribute($key, $value)
504+
{
505+
$this->tag->setAttribute($key, $value);
506+
return $this;
507+
}
508+
495509
/**
496510
* Function to locate a specific ancestor tag in the path to the root.
497511
*

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,15 @@ public function noise($noise)
111111
public function setAttribute($key, $value)
112112
{
113113
$key = strtolower($key);
114+
if ( ! is_array($value))
115+
{
116+
$value = [
117+
'value' => $value,
118+
'doubleQuote' => true,
119+
];
120+
}
114121
$this->attr[$key] = $value;
122+
115123
return $this;
116124
}
117125

tests/Node/HtmlTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,4 +338,11 @@ public function testGetAttributes()
338338

339339
$this->assertEquals('outerlink rounded', $node->getAttributes()['class']);
340340
}
341+
342+
public function testSetAttribute()
343+
{
344+
$node = new HtmlNode('a');
345+
$node->setAttribute('class', 'foo');
346+
$this->assertEquals('foo', $node->getAttribute('class'));
347+
}
341348
}

0 commit comments

Comments
 (0)