Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 4.2.1 under development

- no changes in this release.
- New #279: Add support for conditional CSS classes in `Html::addCssClass()` (@Mister-42)

## 4.2.0 June 05, 2026

Expand Down
8 changes: 8 additions & 0 deletions src/Html.php
Original file line number Diff line number Diff line change
Expand Up @@ -1649,11 +1649,11 @@
/** @psalm-var array<array-key, scalar[]|string|Stringable|null> $value */
foreach ($value as $n => $v) {
if (!isset($v)) {
continue;

Check warning on line 1652 in src/Html.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Continue_": @@ @@ /** @psalm-var array<array-key, scalar[]|string|Stringable|null> $value */ foreach ($value as $n => $v) { if (!isset($v)) { - continue; + break; } $fullName = "$name-$n"; if (in_array($fullName, self::ATTRIBUTES_WITH_CONCATENATED_VALUES, true)) {
}
$fullName = "$name-$n";
if (in_array($fullName, self::ATTRIBUTES_WITH_CONCATENATED_VALUES, true)) {
$html .= self::renderAttribute(

Check warning on line 1656 in src/Html.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Assignment": @@ @@ } $fullName = "$name-$n"; if (in_array($fullName, self::ATTRIBUTES_WITH_CONCATENATED_VALUES, true)) { - $html .= self::renderAttribute( + $html = self::renderAttribute( $fullName, self::encodeAttribute( is_array($v) ? implode(' ', $v) : $v,
$fullName,
self::encodeAttribute(
is_array($v) ? implode(' ', $v) : $v,
Expand Down Expand Up @@ -1731,6 +1731,14 @@
$value = is_string($value->value) ? $value->value : null;
}

/** @psalm-suppress DocblockTypeContradiction */
Comment thread
Mister-42 marked this conversation as resolved.
Outdated
if (is_bool($value)) {
Comment thread
Mister-42 marked this conversation as resolved.
Comment thread
Mister-42 marked this conversation as resolved.
if ($value && is_string($key)) {
$filteredClass[] = $key;
Comment thread
Mister-42 marked this conversation as resolved.
}
continue;

Check warning on line 1739 in src/Html.php

View workflow job for this annotation

GitHub Actions / mutation / PHP 8.5-ubuntu-latest

Escaped Mutant for Mutator "Continue_": @@ @@ if ($value && is_string($key)) { $filteredClass[] = $key; } - continue; + break; } if ($value !== null) {
}

if ($value !== null) {
$filteredClass[$key] = $value;
}
Expand Down
20 changes: 20 additions & 0 deletions tests/HtmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,12 @@ public static function dataAddCssClass(): array
16 => [['id' => 'w2', 'class' => 't1'], ['id' => 'w2'], 't1'],
17 => [['id' => 'w2', 'class' => ['t3', 2 => 't4']], ['id' => 'w2'], ['t3', null, 't4']],
18 => [['id' => 'w2', 'class' => ['t3', 2 => 't4']], ['id' => 'w2'], ['t3', null, 't4', null]],
19 => [['class' => ['btn-active']], [], ['btn-active' => true]],
20 => [[], [], ['btn-active' => false]],
21 => [['class' => ['btn', 'btn-active']], [], ['btn', 'btn-active' => true]],
22 => [['class' => ['btn']], [], ['btn', 'btn-active' => false]],
23 => [['class' => ['btn', 'btn-active']], ['class' => 'btn'], ['btn-active' => true]],
24 => [['class' => 'btn'], ['class' => 'btn'], ['btn-active' => false]],
];
}

Expand Down Expand Up @@ -1091,6 +1097,20 @@ public function testMergeCssClass(): void
$this->assertSame(['persistent' => 'test1', 'additional' => 'test2'], $attributes['class']);
}

public function testAddCssClassConditional(): void
Comment thread
Mister-42 marked this conversation as resolved.
Outdated
{
$attributes = ['class' => 'btn'];
Html::addCssClass($attributes, ['btn-active' => true, 'hidden' => false]);
$this->assertSame(['class' => ['btn', 'btn-active']], $attributes);

Html::addCssClass($attributes, ['btn' => true]);
$this->assertSame(['class' => ['btn', 'btn-active']], $attributes);

$attributes = ['class' => ['persistent' => 'widget']];
Html::addCssClass($attributes, ['btn', 'btn-active' => true]);
$this->assertSame(['class' => ['persistent' => 'widget', 'btn', 'btn-active']], $attributes);
}

public function testAddCssClassArrayToString(): void
{
$attributes = ['class' => 'test'];
Expand Down
Loading