From 61b627a6fc67705f93f2b95a9e1d06713c1331eb Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Wed, 16 Jul 2014 21:49:35 -0400 Subject: [PATCH] Fixes #4318: `yii\helpers\Html::ul()` and `ol()` will return an empty list tag if an empty item array is given --- framework/CHANGELOG.md | 1 + framework/helpers/BaseHtml.php | 10 ++++++---- tests/unit/framework/helpers/HtmlTest.php | 4 ++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 836528b190..e20df1cfe1 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -177,6 +177,7 @@ Yii Framework 2 Change Log - Chg #4147: `BaseMailer::compose()` will not overwrite the `message` parameter if it is explicitly provided (qiangxue) - Chg #4201: change default value of `SyslogTarget::facility` from LOG_SYSLOG to LOG_USER (dizews) - Chg #4227: `\yii\widgets\LinkPager::$hideOnSinglePage` is now `true` by default (samdark) +- Chg #4318: `yii\helpers\Html::ul()` and `ol()` will return an empty list tag if an empty item array is given (qiangxue) - Chg: Replaced `clearAll()` and `clearAllAssignments()` in `yii\rbac\ManagerInterface` with `removeAll()`, `removeAllRoles()`, `removeAllPermissions()`, `removeAllRules()` and `removeAllAssignments()` (qiangxue) - Chg: Added `$user` as the first parameter of `yii\rbac\Rule::execute()` (qiangxue) - Chg: `yii\grid\DataColumn::getDataCellValue()` visibility is now `public` to allow accessing the value from a GridView directly (cebe) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 54178d4141..07c3bfa1ba 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -976,18 +976,20 @@ class BaseHtml * * See [[renderTagAttributes()]] for details on how attributes are being rendered. * - * @return string the generated unordered list. An empty string is returned if `$items` is empty. + * @return string the generated unordered list. An empty list tag will be returned if `$items` is empty. */ public static function ul($items, $options = []) { - if (empty($items)) { - return ''; - } $tag = isset($options['tag']) ? $options['tag'] : 'ul'; $encode = !isset($options['encode']) || $options['encode']; $formatter = isset($options['item']) ? $options['item'] : null; $itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : []; unset($options['tag'], $options['encode'], $options['item'], $options['itemOptions']); + + if (empty($items)) { + return static::tag($tag, '', $options); + } + $results = []; foreach ($items as $index => $item) { if ($formatter !== null) { diff --git a/tests/unit/framework/helpers/HtmlTest.php b/tests/unit/framework/helpers/HtmlTest.php index 395390248a..7d0a1cc34e 100644 --- a/tests/unit/framework/helpers/HtmlTest.php +++ b/tests/unit/framework/helpers/HtmlTest.php @@ -439,6 +439,8 @@ EOD; return "
  • $item
  • "; } ])); + + $this->assertEquals('', Html::ul([], ['class' => 'test'])); } public function testOl() @@ -469,6 +471,8 @@ EOD; return "
  • $item
  • "; } ])); + + $this->assertEquals('
      ', Html::ol([], ['class' => 'test'])); } public function testRenderOptions()