From 59459599dd6e04f0ed770aa93661a3e425fc9dca Mon Sep 17 00:00:00 2001 From: stlgaits Date: Fri, 22 Aug 2025 11:36:43 +0200 Subject: [PATCH] [Icons] fix set default viewBox when not defined in Iconify icon set --- src/Icons/src/Iconify.php | 11 ++++++----- src/Icons/tests/Unit/IconifyTest.php | 9 +++++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Icons/src/Iconify.php b/src/Icons/src/Iconify.php index dbc0cb3d236..e03785bef26 100644 --- a/src/Icons/src/Iconify.php +++ b/src/Icons/src/Iconify.php @@ -34,6 +34,10 @@ final class Iconify // -safe margin private const MAX_ICONS_QUERY_LENGTH = 400; + // https://github.com/iconify/iconify/blob/00cc144b040b838bd86474ab83f0e50e6c6a12a1/packages/utils/src/icon/defaults.ts#L23-L30 + private const DEFAULT_ICON_WIDTH = 16; + private const DEFAULT_ICON_HEIGHT = 16; + private HttpClientInterface $http; private \ArrayObject $sets; private int $maxIconsQueryLength; @@ -81,13 +85,10 @@ public function fetchIcon(string $prefix, string $name): Icon $height = $data['icons'][$name]['height'] ?? $data['height'] ?? $this->sets()[$prefix]['height'] ?? null; $width = $data['icons'][$name]['width'] ?? $data['width'] ?? $this->sets()[$prefix]['width'] ?? null; - if (null === $width && null === $height) { - throw new \RuntimeException(\sprintf('The icon "%s:%s" does not have a width or height.', $prefix, $nameArg)); - } return new Icon($data['icons'][$name]['body'], [ 'xmlns' => self::ATTR_XMLNS_URL, - 'viewBox' => \sprintf('0 0 %s %s', $width ?? $height, $height ?? $width), + 'viewBox' => \sprintf('0 0 %s %s', $width ?? $height ?? self::DEFAULT_ICON_WIDTH, $height ?? $width ?? self::DEFAULT_ICON_HEIGHT), ]); } @@ -135,7 +136,7 @@ public function fetchIcons(string $prefix, array $names): array $icons[$iconName] = new Icon($iconData['body'], [ 'xmlns' => self::ATTR_XMLNS_URL, - 'viewBox' => \sprintf('0 0 %d %d', $width ?? $height, $height ?? $width), + 'viewBox' => \sprintf('0 0 %d %d', $width ?? $height ?? self::DEFAULT_ICON_WIDTH, $height ?? $width ?? self::DEFAULT_ICON_HEIGHT), ]); } diff --git a/src/Icons/tests/Unit/IconifyTest.php b/src/Icons/tests/Unit/IconifyTest.php index 83b31ae8752..b4686378a73 100644 --- a/src/Icons/tests/Unit/IconifyTest.php +++ b/src/Icons/tests/Unit/IconifyTest.php @@ -119,7 +119,7 @@ public function testFetchIconUsesIconsetViewBoxHeight() $this->assertEquals('0 0 17 17', $icon->getAttributes()['viewBox']); } - public function testFetchIconThrowsWhenViewBoxCannotBeComputed() + public function testFetchIconSetsDefaultViewBoxTo16() { $iconify = new Iconify( cache: new NullAdapter(), @@ -138,10 +138,11 @@ public function testFetchIconThrowsWhenViewBoxCannotBeComputed() ]), ); - $this->expectException(\RuntimeException::class); - $this->expectExceptionMessage('The icon "bi:heart" does not have a width or height.'); + $icon = $iconify->fetchIcon('bi', 'heart'); - $iconify->fetchIcon('bi', 'heart'); + $this->assertIsArray($icon->getAttributes()); + $this->assertArrayHasKey('viewBox', $icon->getAttributes()); + $this->assertEquals('0 0 16 16', $icon->getAttributes()['viewBox']); } public function testFetchIconThrowsWhenStatusCodeNot200()