From 758599e6f4d184c1b8024cbb69db200ebb1d0957 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 4 Jun 2014 22:15:56 -0400 Subject: [PATCH] Add once option to css(). The default value is false for backwards compatibility. In 3.0, the default will be made consistent with script(). Refs #1973 Refs #3628 --- .../Test/Case/View/Helper/HtmlHelperTest.php | 25 +++++++++++++++++++ lib/Cake/View/Helper/HtmlHelper.php | 24 ++++++++++++++---- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php index 35edd4841..5e58b2950 100644 --- a/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php @@ -695,6 +695,31 @@ class HtmlHelperTest extends CakeTestCase { $this->assertTags($result, $expected); } +/** + * Test css() with once option. + * + * @return void + */ + public function testCssLinkOnce() { + Configure::write('Asset.filter.css', false); + + $result = $this->Html->css('screen', array('once' => true)); + $expected = array( + 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*css\/screen\.css/') + ); + $this->assertTags($result, $expected); + + $result = $this->Html->css('screen', array('once' => true)); + $this->assertEquals('', $result); + + // Default is once=false + $result = $this->Html->css('screen'); + $expected = array( + 'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/.*css\/screen\.css/') + ); + $this->assertTags($result, $expected); + } + /** * Test css link BC usage * diff --git a/lib/Cake/View/Helper/HtmlHelper.php b/lib/Cake/View/Helper/HtmlHelper.php index 6a8640bc8..00fa8faed 100644 --- a/lib/Cake/View/Helper/HtmlHelper.php +++ b/lib/Cake/View/Helper/HtmlHelper.php @@ -112,11 +112,11 @@ class HtmlHelper extends AppHelper { protected $_crumbs = array(); /** - * Names of script files that have been included once + * Names of script & css files that have been included once * * @var array */ - protected $_includedScripts = array(); + protected $_includedAssets = array(); /** * Options for the currently opened script block buffer if any. @@ -397,6 +397,9 @@ class HtmlHelper extends AppHelper { * * - `inline` If set to false, the generated tag will be appended to the 'css' block, * and included in the `$scripts_for_layout` layout variable. Defaults to true. + * - `once` Whether or not the css file should be checked for uniqueness. If true css + * files will only be included once, use false to allow the same + * css to be included more than once per request. * - `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 @@ -423,7 +426,12 @@ class HtmlHelper extends AppHelper { unset($rel); } - $options += array('block' => null, 'inline' => true, 'rel' => 'stylesheet'); + $options += array( + 'block' => null, + 'inline' => true, + 'once' => false, + 'rel' => 'stylesheet' + ); if (!$options['inline'] && empty($options['block'])) { $options['block'] = __FUNCTION__; } @@ -440,6 +448,12 @@ class HtmlHelper extends AppHelper { return; } + if ($options['once'] && isset($this->_includedAssets[$path])) { + return ''; + } + unset($options['once']); + $this->_includedAssets[$path] = true; + if (strpos($path, '//') !== false) { $url = $path; } else { @@ -538,10 +552,10 @@ class HtmlHelper extends AppHelper { } return null; } - if ($options['once'] && isset($this->_includedScripts[$url])) { + if ($options['once'] && isset($this->_includedAssets[$url])) { return null; } - $this->_includedScripts[$url] = true; + $this->_includedAssets[$url] = true; if (strpos($url, '//') === false) { $url = $this->assetUrl($url, $options + array('pathPrefix' => Configure::read('App.jsBaseUrl'), 'ext' => '.js'));