From 0ed190e995a65972d44a889328898badf0ef63c5 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Mon, 19 May 2014 21:32:13 +0530 Subject: [PATCH 1/8] Fix #3472: Configurable option to encode spaces in dropdowns --- framework/helpers/BaseHtml.php | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 80389b426a..d5e448f25c 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -729,6 +729,8 @@ class BaseHtml * * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', * except that the array keys represent the optgroup labels specified in $items. + * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. + * Defaults to `true`. * * The rest of the options will be rendered as the attributes of the resulting tag. The values will * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. @@ -742,7 +744,8 @@ class BaseHtml return static::listBox($name, $selection, $items, $options); } $options['name'] = $name; - $selectOptions = static::renderSelectOptions($selection, $items, $options); + $encodeSpaces = ArrayHelper::remove($options, 'encodeSpaces', true); + $selectOptions = static::renderSelectOptions($selection, $items, $options, $encodeSpaces); return static::tag('select', "\n" . $selectOptions . "\n", $options); } @@ -777,6 +780,8 @@ class BaseHtml * - unselect: string, the value that will be submitted when no option is selected. * When this attribute is set, a hidden field will be generated so that if no option is selected in multiple * mode, we can still obtain the posted unselect value. + * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. + * Defaults to `true`. * * The rest of the options will be rendered as the attributes of the resulting tag. The values will * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. @@ -803,7 +808,8 @@ class BaseHtml } else { $hidden = ''; } - $selectOptions = static::renderSelectOptions($selection, $items, $options); + $encodeSpaces = ArrayHelper::remove($options, 'encodeSpaces', true); + $selectOptions = static::renderSelectOptions($selection, $items, $options, $encodeSpaces); return $hidden . static::tag('select', "\n" . $selectOptions . "\n", $options); } @@ -1534,14 +1540,16 @@ class BaseHtml * @param array $tagOptions the $options parameter that is passed to the [[dropDownList()]] or [[listBox()]] call. * This method will take out these elements, if any: "prompt", "options" and "groups". See more details * in [[dropDownList()]] for the explanation of these elements. + * @param bool $encodeSpaces whether to encode spaces in option prompt and option value with ` ` character. + * Defaults to `true`. * * @return string the generated list options */ - public static function renderSelectOptions($selection, $items, &$tagOptions = []) + public static function renderSelectOptions($selection, $items, &$tagOptions = [], $encodeSpaces = true) { $lines = []; if (isset($tagOptions['prompt'])) { - $prompt = str_replace(' ', ' ', static::encode($tagOptions['prompt'])); + $prompt = $encodeSpaces ? str_replace(' ', ' ', static::encode($tagOptions['prompt'])) : static::encode($tagOptions['prompt']); $lines[] = static::tag('option', $prompt, ['value' => '']); } @@ -1554,7 +1562,7 @@ class BaseHtml $groupAttrs = isset($groups[$key]) ? $groups[$key] : []; $groupAttrs['label'] = $key; $attrs = ['options' => $options, 'groups' => $groups]; - $content = static::renderSelectOptions($selection, $value, $attrs); + $content = static::renderSelectOptions($selection, $value, $attrs, $encodeSpaces); $lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs); } else { $attrs = isset($options[$key]) ? $options[$key] : []; @@ -1562,7 +1570,7 @@ class BaseHtml $attrs['selected'] = $selection !== null && (!is_array($selection) && !strcmp($key, $selection) || is_array($selection) && in_array($key, $selection)); - $lines[] = static::tag('option', str_replace(' ', ' ', static::encode($value)), $attrs); + $lines[] = static::tag('option', ($encodeSpaces ? str_replace(' ', ' ', static::encode($value)) : static::encode($value)), $attrs); } } From 5c778b429771a6609bca2f04a7308a95ea385985 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Mon, 19 May 2014 21:35:43 +0530 Subject: [PATCH 2/8] Fix #3472: Configurable option to encode spaces in dropdowns --- framework/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 669a0b44ee..ee826af868 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.0-rc under development -------------------------- +- Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes - Bug #1263: Fixed the issue that Gii and Debug modules might be affected by incompatible asset manager configuration (qiangxue) - Bug #2563: Theming is not working if the path map of the theme contains ".." or "." in the paths (qiangxue) - Bug #2801: Fixed the issue that GridView gets footer content before data cells content (ElisDN) From e1934ccf6c5384a38d62b005b74b11c7c2841e79 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Mon, 19 May 2014 21:41:31 +0530 Subject: [PATCH 3/8] Fix #3472: Configurable option to encode spaces in dropdowns --- framework/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index ee826af868..1aa1dc38c6 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 Change Log 2.0.0-rc under development -------------------------- -- Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes +- Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes (kartik-v) - Bug #1263: Fixed the issue that Gii and Debug modules might be affected by incompatible asset manager configuration (qiangxue) - Bug #2563: Theming is not working if the path map of the theme contains ".." or "." in the paths (qiangxue) - Bug #2801: Fixed the issue that GridView gets footer content before data cells content (ElisDN) From 0e40dfdd6e401af5d5b1356678a1b7f9793dd7eb Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Tue, 20 May 2014 23:42:49 +0530 Subject: [PATCH 4/8] Fix #3472: Configurable option to encode spaces in dropdowns --- framework/helpers/BaseHtml.php | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index d5e448f25c..0017415fb0 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -730,7 +730,7 @@ class BaseHtml * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', * except that the array keys represent the optgroup labels specified in $items. * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. - * Defaults to `true`. + * Defaults to `false`. * * The rest of the options will be rendered as the attributes of the resulting tag. The values will * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. @@ -744,9 +744,8 @@ class BaseHtml return static::listBox($name, $selection, $items, $options); } $options['name'] = $name; - $encodeSpaces = ArrayHelper::remove($options, 'encodeSpaces', true); - $selectOptions = static::renderSelectOptions($selection, $items, $options, $encodeSpaces); - + $selectOptions = static::renderSelectOptions($selection, $items, $options); + unset($options['encodeSpaces']); return static::tag('select', "\n" . $selectOptions . "\n", $options); } @@ -781,7 +780,7 @@ class BaseHtml * When this attribute is set, a hidden field will be generated so that if no option is selected in multiple * mode, we can still obtain the posted unselect value. * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. - * Defaults to `true`. + * Defaults to `false`. * * The rest of the options will be rendered as the attributes of the resulting tag. The values will * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. @@ -808,9 +807,8 @@ class BaseHtml } else { $hidden = ''; } - $encodeSpaces = ArrayHelper::remove($options, 'encodeSpaces', true); - $selectOptions = static::renderSelectOptions($selection, $items, $options, $encodeSpaces); - + $selectOptions = static::renderSelectOptions($selection, $items, $options); + unset($options['encodeSpaces']); return $hidden . static::tag('select', "\n" . $selectOptions . "\n", $options); } @@ -1540,14 +1538,13 @@ class BaseHtml * @param array $tagOptions the $options parameter that is passed to the [[dropDownList()]] or [[listBox()]] call. * This method will take out these elements, if any: "prompt", "options" and "groups". See more details * in [[dropDownList()]] for the explanation of these elements. - * @param bool $encodeSpaces whether to encode spaces in option prompt and option value with ` ` character. - * Defaults to `true`. * * @return string the generated list options */ - public static function renderSelectOptions($selection, $items, &$tagOptions = [], $encodeSpaces = true) + public static function renderSelectOptions($selection, $items, &$tagOptions = []) { $lines = []; + $encodeSpaces = ArrayHelper::remove($tagOptions, 'encodeSpaces', false); if (isset($tagOptions['prompt'])) { $prompt = $encodeSpaces ? str_replace(' ', ' ', static::encode($tagOptions['prompt'])) : static::encode($tagOptions['prompt']); $lines[] = static::tag('option', $prompt, ['value' => '']); @@ -1562,7 +1559,7 @@ class BaseHtml $groupAttrs = isset($groups[$key]) ? $groups[$key] : []; $groupAttrs['label'] = $key; $attrs = ['options' => $options, 'groups' => $groups]; - $content = static::renderSelectOptions($selection, $value, $attrs, $encodeSpaces); + $content = static::renderSelectOptions($selection, $value, $attrs); $lines[] = static::tag('optgroup', "\n" . $content . "\n", $groupAttrs); } else { $attrs = isset($options[$key]) ? $options[$key] : []; From 1abd0ab48607b023cacaa05c09db18b59a4f46a0 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Tue, 20 May 2014 23:53:37 +0530 Subject: [PATCH 5/8] Fix #3472: Configurable option to encode spaces in dropdowns --- framework/helpers/BaseHtml.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index 0017415fb0..b742d37143 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -1553,7 +1553,8 @@ class BaseHtml $options = isset($tagOptions['options']) ? $tagOptions['options'] : []; $groups = isset($tagOptions['groups']) ? $tagOptions['groups'] : []; unset($tagOptions['prompt'], $tagOptions['options'], $tagOptions['groups']); - + $options['encodeSpaces'] = ArrayHelper::getValue($options, 'encodeSpaces', $encodeSpaces); + foreach ($items as $key => $value) { if (is_array($value)) { $groupAttrs = isset($groups[$key]) ? $groups[$key] : []; From ed5fe9643202d53cf889d8c11f6076f24169564f Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Wed, 21 May 2014 12:25:03 +0530 Subject: [PATCH 6/8] Fix #3472: Configurable option to encode spaces in dropdowns --- framework/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 1aa1dc38c6..9df68b6f9f 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,7 +4,6 @@ Yii Framework 2 Change Log 2.0.0-rc under development -------------------------- -- Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes (kartik-v) - Bug #1263: Fixed the issue that Gii and Debug modules might be affected by incompatible asset manager configuration (qiangxue) - Bug #2563: Theming is not working if the path map of the theme contains ".." or "." in the paths (qiangxue) - Bug #2801: Fixed the issue that GridView gets footer content before data cells content (ElisDN) @@ -46,6 +45,7 @@ Yii Framework 2 Change Log - Enh #3252: Added support for case insensitive matching using ILIKE to PostgreSQL QueryBuilder (cebe) - Enh #3298: Supported configuring `View::theme` using a class name (netyum, qiangxue) - Enh #3328: `BaseMailer` generates better text body from html body (armab) +- Enh #3472: Added configurable option to encode spaces in dropDownLists and listBoxes (kartik-v) - Enh: Added support for using sub-queries when building a DB query with `IN` condition (qiangxue) - Enh: Supported adding a new response formatter without the need to reconfigure existing formatters (qiangxue) - Enh: Added `yii\web\UrlManager::addRules()` to simplify adding new URL rules (qiangxue) From 16f279ebe306bf1c9a8ceb63ce85cc2af01fdc54 Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Thu, 22 May 2014 00:06:06 +0530 Subject: [PATCH 7/8] Fix #3472: Configurable option to encode spaces in dropdowns --- framework/helpers/BaseHtml.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/framework/helpers/BaseHtml.php b/framework/helpers/BaseHtml.php index b742d37143..2dfce54d62 100644 --- a/framework/helpers/BaseHtml.php +++ b/framework/helpers/BaseHtml.php @@ -745,7 +745,6 @@ class BaseHtml } $options['name'] = $name; $selectOptions = static::renderSelectOptions($selection, $items, $options); - unset($options['encodeSpaces']); return static::tag('select', "\n" . $selectOptions . "\n", $options); } @@ -808,7 +807,6 @@ class BaseHtml $hidden = ''; } $selectOptions = static::renderSelectOptions($selection, $items, $options); - unset($options['encodeSpaces']); return $hidden . static::tag('select', "\n" . $selectOptions . "\n", $options); } @@ -1358,6 +1356,8 @@ class BaseHtml * * - groups: array, the attributes for the optgroup tags. The structure of this is similar to that of 'options', * except that the array keys represent the optgroup labels specified in $items. + * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. + * Defaults to `false`. * * The rest of the options will be rendered as the attributes of the resulting tag. The values will * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. @@ -1411,6 +1411,8 @@ class BaseHtml * - unselect: string, the value that will be submitted when no option is selected. * When this attribute is set, a hidden field will be generated so that if no option is selected in multiple * mode, we can still obtain the posted unselect value. + * - encodeSpaces: bool, whether to encode spaces in option prompt and option value with ` ` character. + * Defaults to `false`. * * The rest of the options will be rendered as the attributes of the resulting tag. The values will * be HTML-encoded using [[encode()]]. If a value is null, the corresponding attribute will not be rendered. From 45e355a50c08fb04d1ed601426b022a60a6765ca Mon Sep 17 00:00:00 2001 From: Kartik Visweswaran Date: Thu, 22 May 2014 00:12:21 +0530 Subject: [PATCH 8/8] Fix #3472: Configurable option to encode spaces in dropdowns --- framework/UPGRADE.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/framework/UPGRADE.md b/framework/UPGRADE.md index 71daf5543a..e952d037a3 100644 --- a/framework/UPGRADE.md +++ b/framework/UPGRADE.md @@ -33,3 +33,8 @@ Upgrade from Yii 2.0 Beta * If you are sharing the same cache across different applications, you should configure the `keyPrefix` property of the cache component to use some unique string. Previously, this property was automatically assigned with a unique string. + +* If you are using `yii\helpers\Html::dropDownList` or `yii\helpers\Html::listBox`, a new parameter + `encodeSpaces` is now available within the `options` array. This defaults to `false`. If + set to `true` all spaces in the rendered option value and prompt will be replaced with ` ` + the HTML non breaking space character.