Skip to content

Commit c47796c

Browse files
committed
Added setStyleAttributeValue, getStyleAttributeArray methods to Tag class
1 parent b3f326b commit c47796c

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/PHPHtmlParser/Dom/AbstractNode.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,13 @@ public function isTextNode() {
453453

454454
/**
455455
* Get attribute values in array
456-
*
456+
* @deprecated
457+
* @todo remove this method
457458
* @param $attributeValue
458459
* @param $delimiter
459460
* @return array
460461
*/
461-
public function getAttributeArray($attributeValue, $delimiter)
462+
public function getAttributeArray($attributeValue, $delimiter = ';')
462463
{
463464
if ($attributeValue == null) {
464465
return null;

src/PHPHtmlParser/Dom/Tag.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,51 @@ public function setAttribute($key, $value)
153153
return $this;
154154
}
155155

156+
/**
157+
* Set inline style attribute value.
158+
*
159+
* @param $attr_key
160+
* @param $attr_value
161+
*/
162+
public function setStyleAttributeValue($attr_key, $attr_value)
163+
{
164+
165+
$style_array = $this->getStyleAttributeArray();
166+
$style_array[$attr_key] = $attr_value;
167+
168+
$style_string = '';
169+
foreach ($style_array as $key => $value) {
170+
$style_string .= $key . ':' . $value . ';';
171+
}
172+
173+
$this->setAttribute('style', $style_string);
174+
}
175+
176+
/**
177+
* Get style attribute in array
178+
*
179+
* @return array|null
180+
*/
181+
public function getStyleAttributeArray()
182+
{
183+
$value = $this->getAttribute('style');
184+
185+
if ($value === null) {
186+
return null;
187+
}
188+
189+
$value = explode(';', substr(trim($value), 0, -1));
190+
$result = [];
191+
foreach ($value as $attr) {
192+
$attr = explode(':', $attr);
193+
$result[$attr[0]] = $attr[1];
194+
}
195+
196+
return $result;
197+
}
198+
199+
200+
156201
/**
157202
* Removes an attribute from this tag.
158203
*

0 commit comments

Comments
 (0)