From 96c9174ee21a866584de2ed4fb0dc243929bed55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Renan=20Gon=C3=A7alves?= Date: Fri, 29 Mar 2013 15:24:08 +0100 Subject: [PATCH] Fixing tag generation for Html::script() and Html::css() when using 'fullBase' => true. Changing values on array_diff_key() from empty ('') to null as they have no consequences and offer better readability. --- .../Test/Case/View/Helper/HtmlHelperTest.php | 40 +++++++++++++++++++ lib/Cake/View/Helper/FormHelper.php | 14 +++---- lib/Cake/View/Helper/HtmlHelper.php | 14 ++++--- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php index b54e4d5ee..316c61838 100644 --- a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php @@ -604,6 +604,22 @@ class HtmlHelperTest extends CakeTestCase { $this->assertNull($result); } +/** + * testCssWithFullBase method + * + * @return void + */ + public function testCssWithFullBase() { + Configure::write('Asset.filter.css', false); + $here = $this->Html->url('/', true); + + $result = $this->Html->css('screen', null, array('fullBase' => true)); + $expected = array( + 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => $here . 'css/screen.css') + ); + $this->assertTags($result, $expected); + } + /** * testPluginCssLink method * @@ -976,6 +992,30 @@ class HtmlHelperTest extends CakeTestCase { $this->assertTags($result, $expected); } +/** + * testScriptWithFullBase method + * + * @return void + */ + public function testScriptWithFullBase() { + $here = $this->Html->url('/', true); + + $result = $this->Html->script('foo', array('fullBase' => true)); + $expected = array( + 'script' => array('type' => 'text/javascript', 'src' => $here . 'js/foo.js') + ); + $this->assertTags($result, $expected); + + $result = $this->Html->script(array('foobar', 'bar'), array('fullBase' => true)); + $expected = array( + array('script' => array('type' => 'text/javascript', 'src' => $here . 'js/foobar.js')), + '/script', + array('script' => array('type' => 'text/javascript', 'src' => $here . 'js/bar.js')), + '/script', + ); + $this->assertTags($result, $expected); + } + /** * test a script file in the webroot/theme dir. * diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index e7374a40f..852eb8816 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -1421,7 +1421,7 @@ class FormHelper extends AppHelper { } unset($options['hiddenField']); - return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => ''))); + return $output . $this->Html->useTag('checkbox', $options['name'], array_diff_key($options, array('name' => null))); } /** @@ -1527,7 +1527,7 @@ class FormHelper extends AppHelper { } $allOptions = array_merge($attributes, $optionsHere); $out[] = $this->Html->useTag('radio', $attributes['name'], $tagName, - array_diff_key($allOptions, array('name' => '', 'type' => '', 'id' => '')), + array_diff_key($allOptions, array('name' => null, 'type' => null, 'id' => null)), $optTitle ); } @@ -1584,7 +1584,7 @@ class FormHelper extends AppHelper { $options['type'] = $method; } $options = $this->_initInputField($params[0], $options); - return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => ''))); + return $this->Html->useTag('input', $options['name'], array_diff_key($options, array('name' => null))); } /** @@ -1610,7 +1610,7 @@ class FormHelper extends AppHelper { } unset($options['value']); } - return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => '', 'name' => '')), $value); + return $this->Html->useTag('textarea', $options['name'], array_diff_key($options, array('type' => null, 'name' => null)), $value); } /** @@ -1636,7 +1636,7 @@ class FormHelper extends AppHelper { $this->_secure(true, null, '' . $options['value']); } - return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => ''))); + return $this->Html->useTag('hidden', $options['name'], array_diff_key($options, array('name' => null))); } /** @@ -1659,7 +1659,7 @@ class FormHelper extends AppHelper { $this->_secure($secure, array_merge($field, array($suffix))); } - $exclude = array('name' => '', 'value' => ''); + $exclude = array('name' => null, 'value' => null); return $this->Html->useTag('file', $options['name'], array_diff_key($options, $exclude)); } @@ -2020,7 +2020,7 @@ class FormHelper extends AppHelper { ) { $this->_secure(true); } - $select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, array('name' => '', 'value' => ''))); + $select[] = $this->Html->useTag($tag, $attributes['name'], array_diff_key($attributes, array('name' => null, 'value' => null))); } $emptyMulti = ( $showEmpty !== null && $showEmpty !== false && !( diff --git a/lib/Cake/View/Helper/HtmlHelper.php b/lib/Cake/View/Helper/HtmlHelper.php index 18587a372..5b100a776 100644 --- a/lib/Cake/View/Helper/HtmlHelper.php +++ b/lib/Cake/View/Helper/HtmlHelper.php @@ -398,6 +398,7 @@ class HtmlHelper extends AppHelper { * - `block` Set the name of the block link/style tag will be appended to. This overrides the `inline` * option. * - `plugin` False value will prevent parsing path as a plugin + * - `fullBase` If true the url will get a full address for the css file. * * @param string|array $path The name of a CSS style sheet or an array containing names of * CSS stylesheets. If `$path` is prefixed with '/', the path will be relative to the webroot @@ -429,6 +430,7 @@ class HtmlHelper extends AppHelper { $url = $path; } else { $url = $this->assetUrl($path, $options + array('pathPrefix' => CSS_URL, 'ext' => '.css')); + $options = array_diff_key($options, array('fullBase' => null)); if (Configure::read('Asset.filter.css')) { $pos = strpos($url, CSS_URL); @@ -488,6 +490,7 @@ class HtmlHelper extends AppHelper { * - `once` Whether or not the script should be checked for uniqueness. If true scripts will only be * included once, use false to allow the same script to be included more than once per request. * - `plugin` False value will prevent parsing path as a plugin + * - `fullBase` If true the url will get a full address for the script file. * * @param string|array $url String or array of javascript files to include * @param array|boolean $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value @@ -523,6 +526,7 @@ class HtmlHelper extends AppHelper { if (strpos($url, '//') === false) { $url = $this->assetUrl($url, $options + array('pathPrefix' => JS_URL, 'ext' => '.js')); + $options = array_diff_key($options, array('fullBase' => null)); if (Configure::read('Asset.filter.js')) { $url = str_replace(JS_URL, 'cjs/', $url); @@ -781,7 +785,7 @@ class HtmlHelper extends AppHelper { */ public function image($path, $options = array()) { $path = $this->assetUrl($path, $options + array('pathPrefix' => IMAGES_URL)); - $options = array_diff_key($options, array('fullBase' => '', 'pathPrefix' => '')); + $options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null)); if (!isset($options['alt'])) { $options['alt'] = ''; @@ -1085,10 +1089,10 @@ class HtmlHelper extends AppHelper { $text = $options['text']; $options = array_diff_key($options, array( - 'tag' => '', - 'fullBase' => '', - 'pathPrefix' => '', - 'text' => '' + 'tag' => null, + 'fullBase' => null, + 'pathPrefix' => null, + 'text' => null )); return $this->tag($tag, $text, $options); }