Skip to content

Commit bc86237

Browse files
authored
Merge pull request #430 from FriendsOfCake/feature/icon-optional-namespace-prefix
Allow icon() to work with empty namespace and/or prefix
2 parents 731d71a + 84afc4c commit bc86237

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

src/View/Helper/HtmlHelper.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,20 @@ public function icon(string $name, array $options = []): string
9191
'class' => null,
9292
];
9393

94-
$classes = [$options['namespace'], $options['prefix'] . '-' . $name];
95-
if (!empty($options['size'])) {
96-
$classes[] = $options['prefix'] . '-' . $options['size'];
94+
$classes = [];
95+
if ($options['namespace']) {
96+
$classes[] = $options['namespace'];
9797
}
98+
99+
if ($options['prefix']) {
100+
$classes[] = $options['prefix'] . '-' . $name;
101+
if (!empty($options['size'])) {
102+
$classes[] = $options['prefix'] . '-' . $options['size'];
103+
}
104+
} else {
105+
$classes[] = $name;
106+
}
107+
98108
$options = $this->injectClasses($classes, $options);
99109

100110
return $this->formatTemplate('tag', [

tests/TestCase/View/Helper/HtmlHelperTest.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,66 @@ public function testIcon()
7575
];
7676
$this->assertHtml($expected, $result);
7777
}
78+
79+
public function testIconWithoutNamespace(): void
80+
{
81+
// Remixicon style: ri-home-line (no namespace, just prefix)
82+
$result = $this->Html->icon('home-line', ['namespace' => null, 'prefix' => 'ri']);
83+
$expected = [
84+
'i' => ['class' => 'ri-home-line'],
85+
'/i',
86+
];
87+
$this->assertHtml($expected, $result);
88+
89+
// With empty string namespace
90+
$result = $this->Html->icon('home-line', ['namespace' => '', 'prefix' => 'ri']);
91+
$expected = [
92+
'i' => ['class' => 'ri-home-line'],
93+
'/i',
94+
];
95+
$this->assertHtml($expected, $result);
96+
97+
// With size
98+
$result = $this->Html->icon('home-line', ['namespace' => null, 'prefix' => 'ri', 'size' => 'lg']);
99+
$expected = [
100+
'i' => ['class' => 'ri-home-line ri-lg'],
101+
'/i',
102+
];
103+
$this->assertHtml($expected, $result);
104+
}
105+
106+
public function testIconWithoutPrefix(): void
107+
{
108+
// Icon set with namespace but no prefix
109+
$result = $this->Html->icon('home', ['namespace' => 'icons', 'prefix' => null]);
110+
$expected = [
111+
'i' => ['class' => 'icons home'],
112+
'/i',
113+
];
114+
$this->assertHtml($expected, $result);
115+
}
116+
117+
public function testIconWithoutNamespaceAndPrefix(): void
118+
{
119+
// Hypothetical icon set using custom tag and just the icon name
120+
$result = $this->Html->icon('home', ['tag' => 'icon', 'namespace' => null, 'prefix' => null]);
121+
$expected = [
122+
'icon' => ['class' => 'home'],
123+
'/icon',
124+
];
125+
$this->assertHtml($expected, $result);
126+
127+
// With additional class
128+
$result = $this->Html->icon('home', [
129+
'tag' => 'icon',
130+
'namespace' => null,
131+
'prefix' => null,
132+
'class' => 'fs-5',
133+
]);
134+
$expected = [
135+
'icon' => ['class' => 'fs-5 home'],
136+
'/icon',
137+
];
138+
$this->assertHtml($expected, $result);
139+
}
78140
}

0 commit comments

Comments
 (0)