Skip to content
Open
Show file tree
Hide file tree
Changes from 9 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
os: >-
['ubuntu-latest', 'windows-latest']
php: >-
['8.0', '8.1', '8.2']
['8.0', '8.1', '8.2', '8.3']
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"rector/rector": "^2.0.3",
"roave/infection-static-analysis-plugin": "^1.25",
"spatie/phpunit-watcher": "^1.23",
"vimeo/psalm": "^4.30|^5.22",
"vimeo/psalm": "^4.30|^5.22|^6.4.1",
"yiisoft/psr-dummy-provider": "^1.0",
"yiisoft/test-support": "^3.0"
},
Expand Down
3 changes: 3 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?xml version="1.0"?>
<psalm
errorLevel="1"
ensureOverrideAttribute="false"
findUnusedBaselineEntry="true"
findUnusedCode="false"
resolveFromConfigFile="true"
strictBinaryOperands="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
3 changes: 2 additions & 1 deletion src/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public function render(): string
throw new RuntimeException('You must assign the "id" using the "id()" setter.');
}

$block = ob_get_clean();
$content = ob_get_clean();
$block = $content === false ? '' : $content;

if ($this->renderInPlace) {
return $block;
Expand Down
20 changes: 17 additions & 3 deletions src/Helper/Normalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use InvalidArgumentException;
use Yiisoft\Html\Html;
use Yiisoft\Html\Tag\I;
use Yiisoft\Html\Tag\Span;

final class Normalizer
Expand Down Expand Up @@ -106,13 +105,22 @@ public static function renderLabel(
): string {
$html = '';

/** @psalm-var non-empty-string $tagName */
Comment thread
terabytesoftw marked this conversation as resolved.
Outdated
$tagName = self::iconTagName($iconAttributes);

unset($iconAttributes['tagName']);
Comment thread
terabytesoftw marked this conversation as resolved.
Outdated

if ($iconClass !== '') {
Html::addCssClass($iconAttributes, $iconClass);
}

if ($icon !== '' || $iconAttributes !== [] || $iconClass !== '') {
$i = I::tag()->attributes($iconAttributes)->content($icon);
$html = Span::tag()->attributes($iconContainerAttributes)->content($i)->encode(false)->render();
$tag = Html::tag($tagName)->attributes($iconAttributes)->content($icon);

$html = match ($tagName) {
'i' => Span::tag()->attributes($iconContainerAttributes)->content($tag)->encode(false)->render(),
default => $tag->render(),
};
Comment thread
terabytesoftw marked this conversation as resolved.
Outdated
}

if ($label !== '') {
Expand Down Expand Up @@ -170,6 +178,12 @@ private static function iconContainerAttributes(array $item, array $iconContaine
? $item['iconContainerAttributes'] : $iconContainerAttributes;
}

private static function iconTagName(array $item): string
{
return array_key_exists('tagName', $item) && is_string($item['tagName']) && $item['tagName'] !== ''
? $item['tagName'] : 'i';
}

/**
* Checks whether a menu item is active.
*
Expand Down
46 changes: 46 additions & 0 deletions tests/Menu/MenuTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,52 @@ public function testItemsIcon(): void
);
}

/**
* @throws CircularReferenceException
* @throws InvalidConfigException
* @throws NotFoundException
* @throws NotInstantiableException
*/
public function testItemsIconWithTagName(): void
{
Assert::equalsWithoutLE(
<<<HTML
<ul>
<li><a class="me-2" href="/active"><span class="me-2"><i>🏠</i></span>Home</a></li>
<li><a class="me-2" href="#"><span class="me-2"><i>📧</i></span>Contact</a></li>
<li><a class="me-2" href="#"><img src="icon-url" alt="Dahsbaord">Login</a></li>
</ul>
HTML,
Menu::widget()
->iconContainerAttributes(['class' => 'me-2'])
->linkAttributes(['class' => 'me-2'])
->items(
[
[
'label' => 'Home',
'link' => '/active',
'icon' => '🏠',
],
[
'label' => 'Contact',
'link' => '#',
'icon' => '📧',
],
[
'label' => 'Login',
'link' => '#',
'iconAttributes' => [
'tagName' => 'img',
'src' => 'icon-url',
'alt' => 'Dahsbaord',
],
],
],
)
->render(),
);
}

/**
* @throws CircularReferenceException
* @throws InvalidConfigException
Expand Down