Skip to content

Commit 30c3a2e

Browse files
committed
bug #3042 [Icons] Fallback to default dimensions 16/16 when Icon's dimensions on Iconify are missing (stlgaits)
This PR was merged into the 2.x branch. Discussion ---------- [Icons] Fallback to default dimensions 16/16 when Icon's dimensions on Iconify are missing | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | Docs? | no | Issues | Fix #3016 | License | MIT Set default viewBox size to 0 0 16 16 when not specified in Iconify icon set as per their documentation specifies : https://iconify.design/docs/types/iconify-json.html#icon to avoid exception being thrown when rendering via ux_icon() or <twig:ux:icon /> Commits ------- 5945959 [Icons] fix set default viewBox when not defined in Iconify icon set
2 parents 06a5d89 + 5945959 commit 30c3a2e

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/Icons/src/Iconify.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ final class Iconify
3434
// -safe margin
3535
private const MAX_ICONS_QUERY_LENGTH = 400;
3636

37+
// https://github.com/iconify/iconify/blob/00cc144b040b838bd86474ab83f0e50e6c6a12a1/packages/utils/src/icon/defaults.ts#L23-L30
38+
private const DEFAULT_ICON_WIDTH = 16;
39+
private const DEFAULT_ICON_HEIGHT = 16;
40+
3741
private HttpClientInterface $http;
3842
private \ArrayObject $sets;
3943
private int $maxIconsQueryLength;
@@ -81,13 +85,10 @@ public function fetchIcon(string $prefix, string $name): Icon
8185

8286
$height = $data['icons'][$name]['height'] ?? $data['height'] ?? $this->sets()[$prefix]['height'] ?? null;
8387
$width = $data['icons'][$name]['width'] ?? $data['width'] ?? $this->sets()[$prefix]['width'] ?? null;
84-
if (null === $width && null === $height) {
85-
throw new \RuntimeException(\sprintf('The icon "%s:%s" does not have a width or height.', $prefix, $nameArg));
86-
}
8788

8889
return new Icon($data['icons'][$name]['body'], [
8990
'xmlns' => self::ATTR_XMLNS_URL,
90-
'viewBox' => \sprintf('0 0 %s %s', $width ?? $height, $height ?? $width),
91+
'viewBox' => \sprintf('0 0 %s %s', $width ?? $height ?? self::DEFAULT_ICON_WIDTH, $height ?? $width ?? self::DEFAULT_ICON_HEIGHT),
9192
]);
9293
}
9394

@@ -135,7 +136,7 @@ public function fetchIcons(string $prefix, array $names): array
135136

136137
$icons[$iconName] = new Icon($iconData['body'], [
137138
'xmlns' => self::ATTR_XMLNS_URL,
138-
'viewBox' => \sprintf('0 0 %d %d', $width ?? $height, $height ?? $width),
139+
'viewBox' => \sprintf('0 0 %d %d', $width ?? $height ?? self::DEFAULT_ICON_WIDTH, $height ?? $width ?? self::DEFAULT_ICON_HEIGHT),
139140
]);
140141
}
141142

src/Icons/tests/Unit/IconifyTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function testFetchIconUsesIconsetViewBoxHeight()
119119
$this->assertEquals('0 0 17 17', $icon->getAttributes()['viewBox']);
120120
}
121121

122-
public function testFetchIconThrowsWhenViewBoxCannotBeComputed()
122+
public function testFetchIconSetsDefaultViewBoxTo16()
123123
{
124124
$iconify = new Iconify(
125125
cache: new NullAdapter(),
@@ -138,10 +138,11 @@ public function testFetchIconThrowsWhenViewBoxCannotBeComputed()
138138
]),
139139
);
140140

141-
$this->expectException(\RuntimeException::class);
142-
$this->expectExceptionMessage('The icon "bi:heart" does not have a width or height.');
141+
$icon = $iconify->fetchIcon('bi', 'heart');
143142

144-
$iconify->fetchIcon('bi', 'heart');
143+
$this->assertIsArray($icon->getAttributes());
144+
$this->assertArrayHasKey('viewBox', $icon->getAttributes());
145+
$this->assertEquals('0 0 16 16', $icon->getAttributes()['viewBox']);
145146
}
146147

147148
public function testFetchIconThrowsWhenStatusCodeNot200()

0 commit comments

Comments
 (0)