From e0020207b775a9605f8e6c925545e7a4cddf6d67 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 22 Feb 2021 14:48:02 +0300 Subject: [PATCH] Adopt to yiisoft/html changes --- src/Breadcrumbs.php | 18 +++++-------- src/Dropdown.php | 40 ++++++++++++++-------------- src/Menu.php | 26 +++++++++--------- src/Message.php | 29 +++++++++----------- src/Modal.php | 12 ++++----- src/ModalCard.php | 24 ++++++++--------- src/Nav.php | 20 +++++++------- src/NavBar.php | 63 ++++++++++++++++++++++++++------------------ src/Panel.php | 29 ++++++++------------ src/ProgressBar.php | 2 +- src/Tabs.php | 48 ++++++++++++++++----------------- tests/NavBarTest.php | 27 ++++++++++++++----- tests/NavTest.php | 5 ++-- 13 files changed, 177 insertions(+), 166 deletions(-) diff --git a/src/Breadcrumbs.php b/src/Breadcrumbs.php index f4bf872..cd1c5ed 100644 --- a/src/Breadcrumbs.php +++ b/src/Breadcrumbs.php @@ -47,11 +47,11 @@ protected function run(): string $this->buildOptions(); return - Html::beginTag('nav', $this->options) . "\n" . - Html::beginTag('ul', $this->itemsOptions) . "\n" . + Html::openTag('nav', $this->options) . "\n" . + Html::openTag('ul', $this->itemsOptions) . "\n" . implode('', $this->renderItems()) . - Html::endTag('ul') . "\n" . - Html::endTag('nav'); + Html::closeTag('ul') . "\n" . + Html::closeTag('nav'); } /** @@ -199,9 +199,9 @@ private function renderIcon(string $icon, array $iconOptions): string $html = ''; if ($icon !== '') { - $html = Html::beginTag('span', $iconOptions) . + $html = Html::openTag('span', $iconOptions) . Html::tag('i', '', ['class' => $icon]) . - Html::endTag('span'); + Html::closeTag('span'); } return $html; @@ -245,14 +245,10 @@ private function renderItem(array $link, string $template): string $template = $link['template']; } - if ($this->encodeLinks === false) { - $link['encode'] = false; - } - if (isset($link['url'])) { $options = $link; unset($options['template'], $options['label'], $options['url'], $options['icon']); - $linkHtml = Html::a($label, $link['url'], $options); + $linkHtml = Html::a($label, $link['url'], $options)->encode($this->encodeLinks)->render(); } else { $linkHtml = $label; } diff --git a/src/Dropdown.php b/src/Dropdown.php index 9a4b80d..ae2f3e8 100644 --- a/src/Dropdown.php +++ b/src/Dropdown.php @@ -234,10 +234,10 @@ public function triggerOptions(array $value): self private function buildDropdown(): string { if ($this->encloseByContainer) { - $html = Html::beginTag('div', $this->options) . "\n"; + $html = Html::openTag('div', $this->options) . "\n"; $html .= $this->buildDropdownTrigger(); $html .= $this->renderItems($this->items, $this->itemsOptions) . "\n"; - $html .= Html::endTag('div'); + $html .= Html::closeTag('div'); } else { $html = $this->renderItems($this->items, $this->itemsOptions); } @@ -248,14 +248,14 @@ private function buildDropdown(): string private function buildDropdownTrigger(): string { return - Html::beginTag('div', $this->triggerOptions) . "\n" . - Html::beginTag('button', $this->buttonOptions) . "\n" . + Html::openTag('div', $this->triggerOptions) . "\n" . + Html::openTag('button', $this->buttonOptions) . "\n" . Html::tag('span', $this->buttonLabel, $this->buttonLabelOptions) . "\n" . - Html::beginTag('span', $this->buttonIconOptions) . "\n" . + Html::openTag('span', $this->buttonIconOptions) . "\n" . Html::tag('i', '', $this->buttonIcon) . "\n" . - Html::endTag('span') . "\n" . - Html::endTag('button') . "\n" . - Html::endTag('div') . "\n"; + Html::closeTag('span') . "\n" . + Html::closeTag('button') . "\n" . + Html::closeTag('div') . "\n"; } private function buildOptions(): void @@ -326,10 +326,6 @@ private function renderItems(array $items, array $itemsOptions = []): string $active = ArrayHelper::getValue($item, 'active', false); $disabled = ArrayHelper::getValue($item, 'disabled', false); - if ($this->encodeLinks === false) { - $linkOptions['encode'] = false; - } - Html::addCssClass($linkOptions, $this->itemClass); if ($disabled) { @@ -344,9 +340,13 @@ private function renderItems(array $items, array $itemsOptions = []): string $url = $item['url'] ?? null; if (empty($item['items'])) { - $lines[] = Html::a($label, $url, $linkOptions); + $lines[] = Html::a($label, $url, $linkOptions) + ->encode($this->encodeLinks) + ->render(); } else { - $lines[] = Html::a($label, $url, array_merge($this->linkOptions, $linkOptions)); + $lines[] = Html::a($label, $url, array_merge($this->linkOptions, $linkOptions)) + ->encode($this->encodeLinks) + ->render(); $dropdownWidget = self::widget() ->dividerClass($this->dividerClass) @@ -367,18 +367,18 @@ private function renderItems(array $items, array $itemsOptions = []): string } return - Html::beginTag('div', $itemsOptions) . "\n" . + Html::openTag('div', $itemsOptions) . "\n" . implode("\n", $lines) . "\n" . - Html::endTag('div'); + Html::closeTag('div'); } private function renderIcon(string $label, string $icon, array $iconOptions): string { if ($icon !== '') { - $label = Html::beginTag('span', $iconOptions) . - Html::tag('i', '', ['class' => $icon, 'encode' => false]) . - Html::endTag('span') . - Html::tag('span', $label, ['encode' => false]); + $label = Html::openTag('span', $iconOptions) . + Html::tag('i', '', ['class' => $icon])->encode(false) . + Html::closeTag('span') . + Html::span($label)->encode(false); } return $label; diff --git a/src/Menu.php b/src/Menu.php index 774e8e1..e24d169 100644 --- a/src/Menu.php +++ b/src/Menu.php @@ -356,7 +356,9 @@ private function renderItems(array $items): string $lines[] = $item['label']; } } else { - $lines[] = Html::tag($tag, $menu, $options); + $lines[] = $tag === false + ? $menu + : Html::tag($tag, $menu, $options)->encode($this->encodeLinks)->render(); } } @@ -442,9 +444,9 @@ private function renderIcon(string $icon, array $iconOptions): string $html = ''; if ($icon !== '') { - $html = Html::beginTag('span', $iconOptions) . + $html = Html::openTag('span', $iconOptions) . Html::tag('i', '', ['class' => $icon]) . - Html::endTag('span'); + Html::closeTag('span'); } return $html; @@ -454,25 +456,25 @@ private function buildOptions(): void { $this->options = $this->addOptions($this->options, 'menu'); $this->itemsOptions = $this->addOptions($this->itemsOptions, 'menu-list'); - - if ($this->encodeLinks === false) { - $this->itemOptions['encode'] = false; - } } private function buildMenu(): string { $tag = ArrayHelper::remove($this->options, 'tag', 'ul'); - $html = Html::beginTag('aside', $this->options) . "\n"; + $html = Html::openTag('aside', $this->options) . "\n"; if ($this->brand !== '') { $html .= $this->brand . "\n"; } - $html .= Html::beginTag($tag, $this->itemsOptions) . "\n"; - $html .= $this->renderItems($this->items) . "\n"; - $html .= Html::endTag($tag) . "\n"; - $html .= Html::endTag('aside'); + if ($tag) { + $html .= Html::openTag($tag, $this->itemsOptions); + } + $html .= "\n" . $this->renderItems($this->items) . "\n"; + if ($tag) { + $html .= Html::closeTag($tag); + } + $html .= "\n" . Html::closeTag('aside'); return $html; } diff --git a/src/Message.php b/src/Message.php index eb4f603..ff157f7 100644 --- a/src/Message.php +++ b/src/Message.php @@ -37,12 +37,12 @@ protected function run(): string $this->buildOptions(); return - Html::beginTag('div', $this->options) . "\n" . - $this->renderHeader() . - Html::beginTag('div', $this->bodyOptions) . "\n" . - $this->renderBodyEnd() . "\n" . - Html::endTag('div') . "\n" . - Html::endTag('div'); + Html::openTag('div', $this->options) . "\n" . + $this->renderHeader() . + Html::openTag('div', $this->bodyOptions) . "\n" . + $this->renderBodyEnd() . "\n" . + Html::closeTag('div') . "\n" . + Html::closeTag('div'); } /** @@ -210,11 +210,6 @@ private function buildOptions(): void $this->bodyOptions = $this->addOptions($this->bodyOptions, 'message-body'); $this->closeButtonOptions = $this->addOptions($this->closeButtonOptions, 'delete'); $this->headerOptions = $this->addOptions($this->headerOptions, 'message-header'); - - if ($this->encodeTags === false) { - $this->closeButtonOptions['encode'] = false; - $this->options['encode'] = false; - } } private function renderHeader(): string @@ -222,8 +217,8 @@ private function renderHeader(): string $html = ''; if ($this->withoutHeader) { - $html = Html::beginTag('div', $this->headerOptions) . "\n" . $this->renderHeaderMessage() . "\n" . - Html::endTag('div') . "\n"; + $html = Html::openTag('div', $this->headerOptions) . "\n" . $this->renderHeaderMessage() . "\n" . + Html::closeTag('div') . "\n"; } return $html; @@ -251,12 +246,12 @@ private function renderCloseButton(): ?string return null; } - $spanOptions = ['aria-hidden' => 'true', 'encode' => false]; + $spanOptions = ['aria-hidden' => 'true']; $tag = ArrayHelper::remove($this->closeButtonOptions, 'tag', 'button'); $label = ArrayHelper::remove( $this->closeButtonOptions, 'label', - Html::tag('span', '×', $spanOptions) + Html::tag('span', '×', $spanOptions)->encode(false)->render() ); if ($tag === 'button') { @@ -267,6 +262,8 @@ private function renderCloseButton(): ?string Html::addCssClass($this->closeButtonOptions, $this->size); } - return Html::tag($tag, $label, $this->closeButtonOptions); + return Html::tag($tag, $label ?? '', $this->closeButtonOptions) + ->encode($this->encodeTags) + ->render(); } } diff --git a/src/Modal.php b/src/Modal.php index 769e403..3a1f887 100644 --- a/src/Modal.php +++ b/src/Modal.php @@ -69,10 +69,10 @@ public function begin(): ?string $html = ''; $html .= $this->renderToggleButton() . "\n"; - $html .= Html::beginTag('div', $this->options) . "\n"; // .modal + $html .= Html::openTag('div', $this->options) . "\n"; // .modal $html .= Html::tag('div', '', ['class' => 'modal-background']) . "\n"; $html .= $this->renderCloseButton() . "\n"; - $html .= Html::beginTag('div', $this->contentOptions) . "\n"; // .modal-content + $html .= Html::openTag('div', $this->contentOptions) . "\n"; // .modal-content return $html; } @@ -80,8 +80,8 @@ public function begin(): ?string protected function run(): string { $html = ''; - $html .= Html::endTag('div') . "\n"; // .modal-content - $html .= Html::endTag('div'); // .modal + $html .= Html::closeTag('div') . "\n"; // .modal-content + $html .= Html::closeTag('div'); // .modal return $html; } @@ -267,7 +267,7 @@ public function contentOptions(array $value): self private function renderToggleButton(): string { if ($this->withoutToggleButton) { - return Html::button($this->toggleButtonLabel, $this->toggleButtonOptions); + return Html::button($this->toggleButtonLabel, $this->toggleButtonOptions)->render(); } return ''; @@ -283,7 +283,7 @@ private function renderToggleButton(): string private function renderCloseButton(): string { if ($this->withoutCloseButton) { - return Html::button('', $this->closeButtonOptions); + return Html::button('', $this->closeButtonOptions)->render(); } return ''; diff --git a/src/ModalCard.php b/src/ModalCard.php index ab3729b..740622f 100644 --- a/src/ModalCard.php +++ b/src/ModalCard.php @@ -80,9 +80,9 @@ public function begin(): ?string $html = ''; $html .= $this->renderToggleButton() . "\n"; - $html .= Html::beginTag('div', $this->options) . "\n"; // .modal + $html .= Html::openTag('div', $this->options) . "\n"; // .modal $html .= $this->renderBackgroundTransparentOverlay() . "\n"; // .modal-background - $html .= Html::beginTag('div', $this->contentOptions) . "\n"; // .modal-card + $html .= Html::openTag('div', $this->contentOptions) . "\n"; // .modal-card $html .= $this->renderHeader(); $html .= $this->renderBodyBegin() . "\n"; @@ -94,8 +94,8 @@ protected function run(): string $html = ''; $html .= $this->renderBodyEnd() . "\n"; $html .= $this->renderFooter() . "\n"; - $html .= Html::endTag('div') . "\n"; // .modal-card - $html .= Html::endTag('div'); // .modal + $html .= Html::closeTag('div') . "\n"; // .modal-card + $html .= Html::closeTag('div'); // .modal return $html; } @@ -381,7 +381,7 @@ public function title(string $value): self private function renderToggleButton(): string { if ($this->withoutToggleButton) { - return Html::button($this->toggleButtonLabel, $this->toggleButtonOptions); + return Html::button($this->toggleButtonLabel, $this->toggleButtonOptions)->render(); } return ''; @@ -397,7 +397,7 @@ private function renderToggleButton(): string private function renderCloseButton(): string { if ($this->withoutCloseButton) { - return Html::button('', $this->closeButtonOptions); + return Html::button('', $this->closeButtonOptions)->render(); } return ''; @@ -413,10 +413,10 @@ private function renderCloseButton(): string private function renderHeader(): string { $html = ''; - $html .= Html::beginTag('header', $this->headerOptions) . "\n"; + $html .= Html::openTag('header', $this->headerOptions) . "\n"; $html .= Html::tag('p', $this->title, $this->titleOptions) . "\n"; $html .= $this->renderCloseButton() . "\n"; - $html .= Html::endTag('header') . "\n"; + $html .= Html::closeTag('header') . "\n"; return $html; } @@ -430,7 +430,7 @@ private function renderHeader(): string */ private function renderBodyBegin(): string { - return Html::beginTag('section', $this->bodyOptions); + return Html::openTag('section', $this->bodyOptions); } /** @@ -442,7 +442,7 @@ private function renderBodyBegin(): string */ private function renderBodyEnd(): string { - return Html::endTag('section'); + return Html::closeTag('section'); } /** @@ -454,7 +454,7 @@ private function renderBodyEnd(): string */ private function renderFooter(): string { - return Html::tag('footer', $this->footer, $this->footerOptions); + return Html::tag('footer', $this->footer, $this->footerOptions)->render(); } /** @@ -466,7 +466,7 @@ private function renderFooter(): string */ private function renderBackgroundTransparentOverlay(): string { - return Html::tag('div', '', ['class' => 'modal-background']); + return Html::tag('div', '', ['class' => 'modal-background'])->render(); } private function buildOptions(): void diff --git a/src/Nav.php b/src/Nav.php index baff244..47b1cb9 100644 --- a/src/Nav.php +++ b/src/Nav.php @@ -209,9 +209,9 @@ private function isItemActive($item): bool private function renderIcon(string $label, string $icon, array $iconOptions): string { if ($icon !== '') { - $label = Html::beginTag('span', $iconOptions) . + $label = Html::openTag('span', $iconOptions) . Html::tag('i', '', ['class' => $icon]) . - Html::endTag('span') . + Html::closeTag('span') . Html::tag('span', $label); } @@ -283,19 +283,17 @@ private function renderItem(array $item): string } if ($dropdown) { - $dropdownOptions = ['class' => 'navbar-link', 'encode' => false]; + $dropdownOptions = ['class' => 'navbar-link']; return - Html::beginTag('div', $options) . "\n" . - Html::a($label, $url, $dropdownOptions) . "\n" . + Html::openTag('div', $options) . "\n" . + Html::a($label, $url, $dropdownOptions)->encode(false) . "\n" . $items . - Html::endTag('div'); + Html::closeTag('div'); } - if ($this->encodeTags === false) { - $linkOptions['encode'] = false; - } - - return Html::a($label, $url, $linkOptions); + return Html::a($label, $url, $linkOptions) + ->encode($this->encodeTags) + ->render(); } } diff --git a/src/NavBar.php b/src/NavBar.php index adfb966..611c2c6 100644 --- a/src/NavBar.php +++ b/src/NavBar.php @@ -46,29 +46,38 @@ public function begin(): ?string $navOptions = $this->options; $navTag = ArrayHelper::remove($navOptions, 'tag', 'nav'); - - if (!is_string($navTag) && !is_bool($navTag) && $navTag !== null) { - throw new InvalidArgumentException('Tag should be either string, bool or null.'); - } + $this->checkNavTag($navTag); return - Html::beginTag($navTag, $navOptions) . "\n" . + (is_string($navTag) ? Html::openTag($navTag, $navOptions) : '') . "\n" . $this->brand . "\n" . - Html::beginTag('div', $this->menuOptions) . - Html::beginTag('div', $this->itemsOptions); + Html::openTag('div', $this->menuOptions) . + Html::openTag('div', $this->itemsOptions); } protected function run(): string { $tag = ArrayHelper::remove($this->options, 'tag', 'nav'); - if (!is_string($tag) && !is_bool($tag) && $tag !== null) { - throw new InvalidArgumentException('Tag should be either string, bool or null.'); - } + $this->checkNavTag($tag); return - Html::endTag('div') . "\n" . - Html::endTag('div') . "\n" . - Html::endTag($tag); + Html::closeTag('div') . "\n" . + Html::closeTag('div') . "\n" . + (is_string($tag) ? Html::closeTag($tag) : ''); + } + + /** + * @param mixed $navTag + */ + private function checkNavTag($navTag): void + { + if ( + (!is_string($navTag) || $navTag === '') && + !is_bool($navTag) && + $navTag !== null + ) { + throw new InvalidArgumentException('Tag should be either non empty string, bool or null.'); + } } /** @@ -131,7 +140,7 @@ public function brandUrl(string $value): self /** * Set toggle icon. * - * @param string $value. + * @param string $value . * * @return self */ @@ -269,10 +278,6 @@ private function buildOptions(): void $this->brandImageOptions = $this->addOptions($this->brandImageOptions, 'navbar-item'); $this->menuOptions = $this->addOptions($this->menuOptions, 'navbar-menu'); - if ($this->encodeTags === false) { - $this->brandImageOptions['encode'] = false; - } - $this->menuOptions['id'] = "{$id}-navbar-Menu"; $this->initItemsOptions(); @@ -304,14 +309,22 @@ private function initItemsOptions(): void private function renderBrand(): void { if ($this->brand === '') { - $this->brand = Html::beginTag('div', $this->brandOptions); + $this->brand = Html::openTag('div', $this->brandOptions); if ($this->brandImage !== '' && $this->brandLabel !== '') { - $this->brand .= Html::tag('span', Html::img($this->brandImage), $this->brandImageOptions); + $this->brand .= Html::tag( + 'span', + Html::img($this->brandImage)->render(), + $this->brandImageOptions + )->encode($this->encodeTags); } if ($this->brandImage !== '' && $this->brandLabel === '') { - $this->brand .= Html::a(Html::img($this->brandImage), $this->brandUrl, $this->brandImageOptions); + $this->brand .= Html::a( + Html::img($this->brandImage)->render(), + $this->brandUrl, + $this->brandImageOptions + )->encode($this->encodeTags); } if ($this->brandLabel !== '') { @@ -319,7 +332,7 @@ private function renderBrand(): void } $this->brand .= $this->renderToggleButton(); - $this->brand .= Html::endTag('div'); + $this->brand .= Html::closeTag('div'); } } @@ -333,10 +346,10 @@ private function renderBrand(): void private function renderToggleButton(): string { return - Html::beginTag('a', $this->toggleOptions) . - $this->renderToggleIcon() . + Html::openTag('a', $this->toggleOptions) . + $this->renderToggleIcon() . - Html::endTag('a'); + Html::closeTag('a'); } /** diff --git a/src/Panel.php b/src/Panel.php index b0dd7e8..5a52376 100644 --- a/src/Panel.php +++ b/src/Panel.php @@ -43,11 +43,11 @@ protected function run(): string $tag = ArrayHelper::getValue($this->options, 'tag', 'nav'); return strtr($this->template, [ - '{panelBegin}' => Html::beginTag($tag, $this->options), + '{panelBegin}' => Html::openTag($tag, $this->options), '{panelHeading}' => $this->renderHeading(), '{panelTabs}' => $this->renderTabs(), '{panelItems}' => implode("\n", $this->tabItems), - '{panelEnd}' => Html::endTag($tag), + '{panelEnd}' => Html::closeTag($tag), ]); } @@ -187,7 +187,7 @@ private function renderTabs(): string Html::addCssClass($this->tabsOptions, 'panel-tabs'); - return "\n" . Html::tag('p', "\n" . $tabs, $this->tabsOptions) . "\n"; + return "\n" . Html::tag('p', "\n" . $tabs, $this->tabsOptions)->encode($this->encodeTags) . "\n"; } return ''; @@ -199,10 +199,6 @@ private function buildOptions(): void $this->options['id'] ??= $this->getId() . '-panel'; - if ($this->encodeTags === false) { - $this->tabsOptions = array_merge($this->tabsOptions, ['encode' => false]); - } - if ($this->color !== '') { Html::addCssClass($this->options, $this->color); } @@ -246,18 +242,18 @@ private function renderTab(int $index, array $item): string $linkOptions['href'] ??= '#' . $id; $tabItemsContainerOptions['id'] ??= $id; - $tabItemsContainer = Html::beginTag('div', $tabItemsContainerOptions) . "\n"; + $tabItemsContainer = Html::openTag('div', $tabItemsContainerOptions) . "\n"; foreach ($tabItems as $tabItem) { $tabItemsContainer .= $this->renderItem($tabItem) . "\n"; } - $tabItemsContainer .= Html::endTag('div'); + $tabItemsContainer .= Html::closeTag('div'); $this->tabItems[$index] = $tabItemsContainer; } - return Html::tag('a', $label, $linkOptions); + return Html::tag('a', $label, $linkOptions)->render(); } private function renderItem(array $item): string @@ -283,17 +279,14 @@ private function renderItem(array $item): string $labelOptions = ['class' => 'panel-icon']; - if ($this->encodeTags === false) { - $labelOptions['encode'] = false; - $options['encode'] = false; - } - if ($icon !== '') { $icon = "\n" . Html::tag('i', '', ['class' => $icon, 'aria-hidden' => 'true']) . "\n"; - $label = "\n" . Html::tag('span', $icon, $labelOptions) . "\n" . $label . "\n"; + $label = "\n" . Html::tag('span', $icon, $labelOptions)->encode($this->encodeTags) . "\n" . $label . "\n"; } - return Html::tag('a', $label, $options); + return Html::tag('a', $label, $options) + ->encode($this->encodeTags) + ->render(); } /** @@ -305,6 +298,6 @@ private function renderItem(array $item): string */ private function isActive(array $item): bool { - return (bool) ArrayHelper::getValue($item, 'active', false); + return (bool)ArrayHelper::getValue($item, 'active', false); } } diff --git a/src/ProgressBar.php b/src/ProgressBar.php index c3b7887..2da255c 100644 --- a/src/ProgressBar.php +++ b/src/ProgressBar.php @@ -55,7 +55,7 @@ protected function run(): string $content = $this->value > 0 ? $this->value . '%' : ''; - return Html::tag('progress', $content, $this->options); + return Html::tag('progress', $content, $this->options)->render(); } /** diff --git a/src/Tabs.php b/src/Tabs.php index 2ef60c5..15a7ac5 100644 --- a/src/Tabs.php +++ b/src/Tabs.php @@ -88,7 +88,7 @@ protected function run(): string { $this->buildOptions(); - return Html::tag('div', "\n" . $this->renderItems() . "\n", $this->options) + return Html::tag('div', "\n" . $this->renderItems() . "\n", $this->options)->encode($this->encodeTags) . $this->renderTabsContent(); } @@ -271,11 +271,6 @@ private function buildOptions(): void if ($this->style !== '') { Html::addCssClass($this->options, $this->style); } - - if ($this->encodeTags === false) { - $this->options['encode'] = false; - $this->tabsContentOptions['encode'] = false; - } } /** @@ -297,18 +292,16 @@ private function renderItems(): string $itemOptions = []; - if ($this->encodeTags === false) { - $itemOptions['encode'] = false; - } - - return Html::tag('ul', $items . "\n", $itemOptions); + return Html::tag('ul', $items . "\n", $itemOptions) + ->encode($this->encodeTags) + ->render(); } /** * @param int $index * @param array $item * - *@throws InvalidArgumentException|JsonException + * @throws InvalidArgumentException|JsonException * * @return string */ @@ -317,7 +310,7 @@ private function renderItem(int $index, array $item): string $id = $this->getId() . '-tabs-c' . $index; $url = ArrayHelper::getValue($item, 'url', ''); $icon = ArrayHelper::getValue($item, 'icon', ''); - $label = ArrayHelper::getValue($item, 'label', ''); + $label = (string)ArrayHelper::getValue($item, 'label', ''); $encode = ArrayHelper::getValue($item, 'encode', $this->encodeLabels); $options = ArrayHelper::getValue($item, 'options', []); $linkOptions = ArrayHelper::getValue($item, 'linkOptions', []); @@ -326,13 +319,6 @@ private function renderItem(int $index, array $item): string $contentOptions = ArrayHelper::getValue($item, 'contentOptions', []); $active = $this->isItemActive($item); - if ($this->encodeTags === false) { - $contentOptions['encode'] = false; - $iconOptions['encode'] = false; - $linkOptions['encode'] = false; - $options['encode'] = false; - } - if ($label === '') { throw new InvalidArgumentException("The 'label' option is required."); } @@ -361,10 +347,14 @@ private function renderItem(int $index, array $item): string $contentOptions['id'] = ArrayHelper::getValue($contentOptions, 'id', $id); - $this->tabsContent[] = Html::tag('div', $content, $contentOptions); + $this->tabsContent[] = Html::tag('div', $content, $contentOptions)->encode($this->encodeTags); } - return Html::tag('li', Html::tag('a', $label, $linkOptions), $options); + return Html::tag( + 'li', + Html::tag('a', $label, $linkOptions)->encode($this->encodeTags)->render(), + $options + )->encode($this->encodeTags)->render(); } /** @@ -382,8 +372,12 @@ private function renderIcon(string $label, string $icon, array $iconOptions): st unset($iconOptions['rightSide']); $elements = [ - Html::tag('span', Html::tag('i', '', ['class' => $icon, 'aria-hidden' => 'true']), $iconOptions), - Html::tag('span', $label), + Html::tag( + 'span', + Html::tag('i', '', ['class' => $icon, 'aria-hidden' => 'true'])->render(), + $iconOptions + )->encode($this->encodeTags)->render(), + Html::tag('span', $label)->render(), ]; if ($rightSide === true) { @@ -405,7 +399,11 @@ private function renderTabsContent(): string $html = ''; if (!empty($this->tabsContent)) { - $html .= "\n" . Html::tag('div', "\n" . implode("\n", $this->tabsContent) . "\n", $this->tabsContentOptions); + $html .= "\n" . Html::tag( + 'div', + "\n" . implode("\n", $this->tabsContent) . "\n", + $this->tabsContentOptions + )->encode($this->encodeTags); } return $html; diff --git a/tests/NavBarTest.php b/tests/NavBarTest.php index 48ba94a..ce1ec96 100644 --- a/tests/NavBarTest.php +++ b/tests/NavBarTest.php @@ -342,18 +342,33 @@ public function testNavBarIconToggle(): void $this->assertEqualsWithoutLE($expected, $html); } - public function testNavBarBeginExceptionTag(): void + public function dataExceptionTag(): array + { + return [ + [['test']], + [''], + [42], + ]; + } + + /** + * @dataProvider dataExceptionTag + */ + public function testNavBarBeginExceptionTag($tag): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Tag should be either string, bool or null.'); - NavBar::widget()->options(['tag' => ['testMe']])->begin(); + $this->expectExceptionMessage('Tag should be either non empty string, bool or null.'); + NavBar::widget()->options(['tag' => $tag])->begin(); } - public function testNavBarRunExceptionTag(): void + /** + * @dataProvider dataExceptionTag + */ + public function testNavBarRunExceptionTag($tag): void { $this->expectException(InvalidArgumentException::class); - $this->expectExceptionMessage('Tag should be either string, bool or null.'); - NavBar::widget()->options(['tag' => ['testMe']])->render(); + $this->expectExceptionMessage('Tag should be either non empty string, bool or null.'); + NavBar::widget()->options(['tag' => $tag])->render(); } public function testNavBarOptionsClassArray(): void diff --git a/tests/NavTest.php b/tests/NavTest.php index daf1960..6ab9987 100644 --- a/tests/NavTest.php +++ b/tests/NavTest.php @@ -490,9 +490,8 @@ public function testNavIcon(): void ], [ 'label' => 'Admin' . Html::img( - '../../docs/images/icon-avatar.png', - ['class' => 'img-rounded', 'aria-expanded' => 'false'] - ), + '../../docs/images/icon-avatar.png' + )->attributes(['class' => 'img-rounded', 'aria-expanded' => 'false']), 'items' => [ ['label' => 'Logout', 'url' => '/auth/logout'], ],