From 5b67bc18222a251b37de8b2c51072d5ba00c4889 Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Mon, 30 Nov 2015 19:32:41 -0600 Subject: [PATCH 1/6] use ArrayHelper::remove() --- framework/helpers/BaseHtml.php | 39 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 40f888435a..ae0756237f 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -884,9 +884,12 @@ class BaseHtml $name .= '[]'; } - $formatter = isset($options['item']) ? $options['item'] : null; - $itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : []; - $encode = !isset($options['encode']) || $options['encode']; + $formatter = ArrayHelper::remove($options, 'item', null); + $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); + $encode = ArrayHelper::remove($options, 'encode', true); + $separator = ArrayHelper::remove($options, 'separator', PHP_EOL); + $tag = ArrayHelper::remove($options, 'tag', 'div'); + $lines = []; $index = 0; foreach ($items as $value => $label) { @@ -911,10 +914,8 @@ class BaseHtml } else { $hidden = ''; } - $separator = isset($options['separator']) ? $options['separator'] : "\n"; - $tag = isset($options['tag']) ? $options['tag'] : 'div'; - unset($options['tag'], $options['unselect'], $options['encode'], $options['separator'], $options['item'], $options['itemOptions']); + unset($options['unselect']); return $hidden . static::tag($tag, implode($separator, $lines), $options); } @@ -953,9 +954,12 @@ class BaseHtml */ public static function radioList($name, $selection = null, $items = [], $options = []) { - $encode = !isset($options['encode']) || $options['encode']; - $formatter = isset($options['item']) ? $options['item'] : null; - $itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : []; + $formatter = ArrayHelper::remove($options, 'item', null); + $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); + $encode = ArrayHelper::remove($options, 'encode', true); + $separator = ArrayHelper::remove($options, 'separator', PHP_EOL); + $tag = ArrayHelper::remove($options, 'tag', 'div'); + $lines = []; $index = 0; foreach ($items as $value => $label) { @@ -973,7 +977,6 @@ class BaseHtml $index++; } - $separator = isset($options['separator']) ? $options['separator'] : "\n"; if (isset($options['unselect'])) { // add a hidden field so that if the list box has no option being selected, it still submits a value $hidden = static::hiddenInput($name, $options['unselect']); @@ -981,8 +984,7 @@ class BaseHtml $hidden = ''; } - $tag = isset($options['tag']) ? $options['tag'] : 'div'; - unset($options['tag'], $options['unselect'], $options['encode'], $options['separator'], $options['item'], $options['itemOptions']); + unset($options['unselect']); return $hidden . static::tag($tag, implode($separator, $lines), $options); } @@ -1012,11 +1014,10 @@ class BaseHtml */ public static function ul($items, $options = []) { - $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']); + $tag = ArrayHelper::remove($options, 'tag', 'ul'); + $encode = ArrayHelper::remove($options, 'encode', true); + $formatter = ArrayHelper::remove($options, 'item', null); + $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); if (empty($items)) { return static::tag($tag, '', $options); @@ -1085,8 +1086,8 @@ class BaseHtml { $for = array_key_exists('for', $options) ? $options['for'] : static::getInputId($model, $attribute); $attribute = static::getAttributeName($attribute); - $label = isset($options['label']) ? $options['label'] : static::encode($model->getAttributeLabel($attribute)); - unset($options['label'], $options['for']); + $label = ArrayHelper::remove($options, 'label', static::encode($model->getAttributeLabel($attribute))); + unset($options['for']); return static::label($label, $for, $options); } From 7618e4ee1376c166166b4d3e1eab50d7d7e8b17d Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Tue, 1 Dec 2015 00:18:50 -0600 Subject: [PATCH 2/6] changes suggested by review. --- framework/helpers/BaseHtml.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index ae0756237f..21202ef113 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -884,7 +884,7 @@ class BaseHtml $name .= '[]'; } - $formatter = ArrayHelper::remove($options, 'item', null); + $formatter = ArrayHelper::remove($options, 'item'); $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); $encode = ArrayHelper::remove($options, 'encode', true); $separator = ArrayHelper::remove($options, 'separator', PHP_EOL); @@ -911,12 +911,11 @@ class BaseHtml // add a hidden field so that if the list box has no option being selected, it still submits a value $name2 = substr($name, -2) === '[]' ? substr($name, 0, -2) : $name; $hidden = static::hiddenInput($name2, $options['unselect']); + unset($options['unselect']); } else { $hidden = ''; } - unset($options['unselect']); - return $hidden . static::tag($tag, implode($separator, $lines), $options); } @@ -954,7 +953,7 @@ class BaseHtml */ public static function radioList($name, $selection = null, $items = [], $options = []) { - $formatter = ArrayHelper::remove($options, 'item', null); + $formatter = ArrayHelper::remove($options, 'item'); $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); $encode = ArrayHelper::remove($options, 'encode', true); $separator = ArrayHelper::remove($options, 'separator', PHP_EOL); @@ -980,12 +979,11 @@ class BaseHtml if (isset($options['unselect'])) { // add a hidden field so that if the list box has no option being selected, it still submits a value $hidden = static::hiddenInput($name, $options['unselect']); + unset($options['unselect']); } else { $hidden = ''; } - unset($options['unselect']); - return $hidden . static::tag($tag, implode($separator, $lines), $options); } From 7a2bbabbbdd716ec90ca7655db5abbf3aec75c5f Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Tue, 1 Dec 2015 07:28:09 -0600 Subject: [PATCH 3/6] add separator special option to `ul` and `listBox` --- framework/helpers/BaseHtml.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 21202ef113..132a09228d 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -995,6 +995,7 @@ class BaseHtml * * - encode: boolean, whether to HTML-encode the items. Defaults to true. * This option is ignored if the `item` option is specified. + * - separator: string, since 2.0.7 the HTML code that separates items. * - itemOptions: array, the HTML attributes for the `li` tags. This option is ignored if the `item` option is specified. * - item: callable, a callback that is used to generate each individual list item. * The signature of this callback must be: @@ -1014,7 +1015,8 @@ class BaseHtml { $tag = ArrayHelper::remove($options, 'tag', 'ul'); $encode = ArrayHelper::remove($options, 'encode', true); - $formatter = ArrayHelper::remove($options, 'item', null); + $formatter = ArrayHelper::remove($options, 'item'); + $separator = ArrayHelper::remove($options, 'separator', PHP_EOL); $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); if (empty($items)) { @@ -1029,7 +1031,7 @@ class BaseHtml $results[] = static::tag('li', $encode ? static::encode($item) : $item, $itemOptions); } } - return static::tag($tag, "\n" . implode("\n", $results) . "\n", $options); + return static::tag($tag, implode($separator, $results), $options); } /** @@ -1082,10 +1084,9 @@ class BaseHtml */ public static function activeLabel($model, $attribute, $options = []) { - $for = array_key_exists('for', $options) ? $options['for'] : static::getInputId($model, $attribute); + $for = ArrayHelper::remove($options, 'for', static::getInputId($model, $attribute)); $attribute = static::getAttributeName($attribute); $label = ArrayHelper::remove($options, 'label', static::encode($model->getAttributeLabel($attribute))); - unset($options['for']); return static::label($label, $for, $options); } @@ -1113,7 +1114,7 @@ class BaseHtml public static function activeHint($model, $attribute, $options = []) { $attribute = static::getAttributeName($attribute); - $hint = isset($options['hint']) ? $options['hint'] : $model->getAttributeHint($attribute); + $hint = $options['hint']) ? $options['hint'] : $model->getAttributeHint($attribute); if (empty($hint)) { return ''; } @@ -1139,9 +1140,9 @@ class BaseHtml public static function errorSummary($models, $options = []) { $header = isset($options['header']) ? $options['header'] : '

' . Yii::t('yii', 'Please fix the following errors:') . '

'; - $footer = isset($options['footer']) ? $options['footer'] : ''; - $encode = !isset($options['encode']) || $options['encode'] !== false; - unset($options['header'], $options['footer'], $options['encode']); + $footer = ArrayHelper::remove($options, 'footer', ''); + $encode = ArrayHelper::remove($options, 'encode', true); + unset($options['header']); $lines = []; if (!is_array($models)) { @@ -1186,9 +1187,8 @@ class BaseHtml { $attribute = static::getAttributeName($attribute); $error = $model->getFirstError($attribute); - $tag = isset($options['tag']) ? $options['tag'] : 'div'; - $encode = !isset($options['encode']) || $options['encode'] !== false; - unset($options['tag'], $options['encode']); + $tag = ArrayHelper::remove($options, 'tag', 'div'); + $encode = ArrayHelper::remove($options, 'encode', true); return Html::tag($tag, $encode ? Html::encode($error) : $error, $options); } From d0ffb58efbf7a6ca3d8d2d02e3cbeb05104eafe8 Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Tue, 1 Dec 2015 07:34:40 -0600 Subject: [PATCH 4/6] syntax error --- framework/helpers/BaseHtml.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 132a09228d..29292559aa 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -1114,7 +1114,7 @@ class BaseHtml public static function activeHint($model, $attribute, $options = []) { $attribute = static::getAttributeName($attribute); - $hint = $options['hint']) ? $options['hint'] : $model->getAttributeHint($attribute); + $hint = isset($options['hint']) ? $options['hint'] : $model->getAttributeHint($attribute); if (empty($hint)) { return ''; } From ec43f4c370a2387c2b281b3435f32d5492d3b638 Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Tue, 1 Dec 2015 08:00:39 -0600 Subject: [PATCH 5/6] add separator around the items in `ul()` --- framework/helpers/BaseHtml.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 29292559aa..bb9bd83d6e 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -1031,7 +1031,12 @@ class BaseHtml $results[] = static::tag('li', $encode ? static::encode($item) : $item, $itemOptions); } } - return static::tag($tag, implode($separator, $results), $options); + + return static::tag( + $tag, + $separator . implode($separator, $results) . $separator, + $options + ); } /** From 2d05b11f68b20b9decbf375a51ebc173cb2eac09 Mon Sep 17 00:00:00 2001 From: Angel Guevara Date: Wed, 2 Dec 2015 04:21:45 -0600 Subject: [PATCH 6/6] remove PHP_EOL --- framework/helpers/BaseHtml.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index bb9bd83d6e..0460472ea7 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -887,7 +887,7 @@ class BaseHtml $formatter = ArrayHelper::remove($options, 'item'); $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); $encode = ArrayHelper::remove($options, 'encode', true); - $separator = ArrayHelper::remove($options, 'separator', PHP_EOL); + $separator = ArrayHelper::remove($options, 'separator', "\n"); $tag = ArrayHelper::remove($options, 'tag', 'div'); $lines = []; @@ -956,7 +956,7 @@ class BaseHtml $formatter = ArrayHelper::remove($options, 'item'); $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); $encode = ArrayHelper::remove($options, 'encode', true); - $separator = ArrayHelper::remove($options, 'separator', PHP_EOL); + $separator = ArrayHelper::remove($options, 'separator', "\n"); $tag = ArrayHelper::remove($options, 'tag', 'div'); $lines = []; @@ -1016,7 +1016,7 @@ class BaseHtml $tag = ArrayHelper::remove($options, 'tag', 'ul'); $encode = ArrayHelper::remove($options, 'encode', true); $formatter = ArrayHelper::remove($options, 'item'); - $separator = ArrayHelper::remove($options, 'separator', PHP_EOL); + $separator = ArrayHelper::remove($options, 'separator', "\n"); $itemOptions = ArrayHelper::remove($options, 'itemOptions', []); if (empty($items)) {