Skip to content

Commit e298f36

Browse files
Machy8dg
authored andcommitted
Added Html::removeAttributes() - removing multiple attributes (#161)
1 parent 841fbf3 commit e298f36

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

src/Utils/Html.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,19 @@ public function removeAttribute(string $name)
176176
}
177177

178178

179+
/**
180+
* Unsets element's attributes.
181+
* @return static
182+
*/
183+
public function removeAttributes(array $attributes)
184+
{
185+
foreach ($attributes as $name) {
186+
unset($this->attrs[$name]);
187+
}
188+
return $this;
189+
}
190+
191+
179192
/**
180193
* Overloaded setter for element's attribute.
181194
*/
@@ -564,7 +577,7 @@ final public function attributes(): string
564577
$tmp = null;
565578
foreach ($value as $k => $v) {
566579
if ($v != null) { // intentionally ==, skip nulls & empty string
567-
// composite 'style' vs. 'others'
580+
// composite 'style' vs. 'others'
568581
$tmp[] = $v === true ? $k : (is_string($k) ? $k . ':' . $v : $v);
569582
}
570583
}

tests/Utils/Html.basic.phpt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,14 @@ test(function () { // isset
177177
Assert::true(isset(Html::el('a')->setAttribute('id', '')->id));
178178
Assert::false(isset(Html::el('a')->setAttribute('id', null)->id));
179179
});
180+
181+
182+
test(function () { // removeAttributes
183+
$el = Html::el('a')->addAttributes(['onclick' => '', 'onmouseover' => '']);
184+
Assert::true(isset($el->onclick));
185+
Assert::true(isset($el->onmouseover));
186+
187+
$el->removeAttributes(['onclick', 'onmouseover']);
188+
Assert::false(isset($el->onclick));
189+
Assert::false(isset($el->onmouseover));
190+
});

0 commit comments

Comments
 (0)