Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
16 changes: 13 additions & 3 deletions src/View/Helper/HtmlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,20 @@ public function icon(string $name, array $options = []): string
'class' => null,
];

$classes = [$options['namespace'], $options['prefix'] . '-' . $name];
if (!empty($options['size'])) {
$classes[] = $options['prefix'] . '-' . $options['size'];
$classes = [];
if ($options['namespace']) {
$classes[] = $options['namespace'];
}

if ($options['prefix']) {
$classes[] = $options['prefix'] . '-' . $name;
if (!empty($options['size'])) {
$classes[] = $options['prefix'] . '-' . $options['size'];
}
} else {
$classes[] = $name;
}

$options = $this->injectClasses($classes, $options);

return $this->formatTemplate('tag', [
Expand Down
62 changes: 62 additions & 0 deletions tests/TestCase/View/Helper/HtmlHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,66 @@ public function testIcon()
];
$this->assertHtml($expected, $result);
}

public function testIconWithoutNamespace(): void
{
// Remixicon style: ri-home-line (no namespace, just prefix)
$result = $this->Html->icon('home-line', ['namespace' => null, 'prefix' => 'ri']);
$expected = [
'i' => ['class' => 'ri-home-line'],
'/i',
];
$this->assertHtml($expected, $result);

// With empty string namespace
$result = $this->Html->icon('home-line', ['namespace' => '', 'prefix' => 'ri']);
$expected = [
'i' => ['class' => 'ri-home-line'],
'/i',
];
$this->assertHtml($expected, $result);

// With size
$result = $this->Html->icon('home-line', ['namespace' => null, 'prefix' => 'ri', 'size' => 'lg']);
$expected = [
'i' => ['class' => 'ri-home-line ri-lg'],
'/i',
];
$this->assertHtml($expected, $result);
}

public function testIconWithoutPrefix(): void
{
// Icon set with namespace but no prefix
$result = $this->Html->icon('home', ['namespace' => 'icons', 'prefix' => null]);
$expected = [
'i' => ['class' => 'icons home'],
'/i',
];
$this->assertHtml($expected, $result);
}

public function testIconWithoutNamespaceAndPrefix(): void
{
// Hypothetical icon set using custom tag and just the icon name
$result = $this->Html->icon('home', ['tag' => 'icon', 'namespace' => null, 'prefix' => null]);
$expected = [
'icon' => ['class' => 'home'],
'/icon',
];
$this->assertHtml($expected, $result);

// With additional class
$result = $this->Html->icon('home', [
'tag' => 'icon',
'namespace' => null,
'prefix' => null,
'class' => 'fs-5',
]);
$expected = [
'icon' => ['class' => 'fs-5 home'],
'/icon',
];
$this->assertHtml($expected, $result);
}
}