Skip to content

Commit 76d2253

Browse files
committed
Fix icon color
1 parent 02d3274 commit 76d2253

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

src/Icons/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 2.30
44

55
- Ensure compatibility with PHP 8.5
6+
- Fix icon color for fill and stroke attributes
67

78
## 2.29.0
89

src/Icons/src/Icon.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public static function fromFile(string $filename): self
131131
}
132132

133133
public function __construct(
134-
private readonly string $innerSvg,
134+
private string $innerSvg,
135135
private readonly array $attributes = [],
136136
) {
137137
}
@@ -155,6 +155,14 @@ public function toHtml(): string
155155
continue;
156156
}
157157

158+
if ($name === 'fill') {
159+
$this->innerSvg = str_replace('fill="currentColor"', "fill=\"{$value}\"", $this->innerSvg);
160+
}
161+
162+
if ($name === 'stroke') {
163+
$this->innerSvg = str_replace('stroke="currentColor"', "stroke=\"{$value}\"", $this->innerSvg);
164+
}
165+
158166
$value = htmlspecialchars($value, \ENT_QUOTES | \ENT_SUBSTITUTE, 'UTF-8');
159167
$htmlAttributes .= '="'.$value.'"';
160168
}

src/Icons/tests/Unit/IconTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ public function testWithAttributes(array $attributes, array $withAttributes, arr
124124
$this->assertSame($expected, $icon->getAttributes());
125125
}
126126

127+
/**
128+
* @dataProvider provideRenderAttributeColorModifiers
129+
*/
130+
public function testColorModifierWithFillAttributeSet(array $attributes,string $innerSvg, string $expected)
131+
{
132+
$icon = new Icon($innerSvg, $attributes);
133+
$this->assertStringStartsWith($expected, $icon->toHtml());
134+
}
135+
127136
public static function provideIdToName(): iterable
128137
{
129138
yield from [
@@ -290,4 +299,23 @@ public function testSerialize()
290299

291300
$this->assertEquals($icon, unserialize(serialize($icon)));
292301
}
302+
303+
public static function provideRenderAttributeColorModifiers(): iterable
304+
{
305+
yield 'it_renders_with_attribute_fill_set' => [
306+
['fill' => 'red'],
307+
'<path fill="currentColor" d="M8 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6m2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0m4 8c0 1-1 1-1 1H3s-1 0-1-1 1-4 6-4 6 3 6 4m-1-.004c-.001-.246-.154-.986-.832-1.664C11.516 10.68 10.289 10 8 10s-3.516.68-4.168 1.332c-.678.678-.83 1.418-.832 1.664z"/>',
308+
'<svg fill="red"><path fill="red" d="M8 8a3 3 0 1 0 0-6 3 3 0 0 0 0 6m2-3a2 2 0 1 1-4 0 2 2 0 0 1 4 0m4 8c0 1-1 1-1 1H3s-1 0-1-1 1-4 6-4 6 3 6 4m-1-.004c-.001-.246-.154-.986-.832-1.664C11.516 10.68 10.289 10 8 10s-3.516.68-4.168 1.332c-.678.678-.83 1.418-.832 1.664z"/></svg',
309+
];
310+
yield 'it_renders_with_attribute_stroke_set' => [
311+
['stroke' => 'red'],
312+
'<path fill="currentColor" stroke="currentColor" stroke-linecap="round" stroke-width="2" d="M6 4v10m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v2m6-16v2m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v10m6-16v10m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v2"/>',
313+
'<svg stroke="red"><path fill="currentColor" stroke="red" stroke-linecap="round" stroke-width="2" d="M6 4v10m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v2m6-16v2m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v10m6-16v10m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v2"/></svg>',
314+
];
315+
yield 'it_renders_with_attribute_stroke_and_fill_set' => [
316+
['fill' => 'red', 'stroke' => 'blue'],
317+
'<path fill="currentColor" stroke="currentColor" stroke-linecap="round" stroke-width="2" d="M6 4v10m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v2m6-16v2m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v10m6-16v10m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v2"/>',
318+
'<svg fill="red" stroke="blue"><path fill="red" stroke="blue" stroke-linecap="round" stroke-width="2" d="M6 4v10m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v2m6-16v2m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v10m6-16v10m0 0a2 2 0 1 0 0 4m0-4a2 2 0 1 1 0 4m0 0v2"/></svg>',
319+
];
320+
}
293321
}

0 commit comments

Comments
 (0)