Skip to content

Commit

Permalink
Add once option to css().
Browse files Browse the repository at this point in the history
The default value is false for backwards compatibility. In 3.0, the
default will be made consistent with script().

Refs #1973
Refs #3628
  • Loading branch information
markstory committed Jun 5, 2014
1 parent 92eeef8 commit 758599e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
25 changes: 25 additions & 0 deletions lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
Expand Up @@ -695,6 +695,31 @@ public function testCssLink() {
$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
*
Expand Down
24 changes: 19 additions & 5 deletions lib/Cake/View/Helper/HtmlHelper.php
Expand Up @@ -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.
Expand Down Expand Up @@ -397,6 +397,9 @@ public function link($title, $url = null, $options = array(), $confirmMessage =
*
* - `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
Expand All @@ -423,7 +426,12 @@ public function css($path, $options = array()) {
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__;
}
Expand All @@ -440,6 +448,12 @@ public function css($path, $options = array()) {
return;
}

if ($options['once'] && isset($this->_includedAssets[$path])) {
return '';
}
unset($options['once']);
$this->_includedAssets[$path] = true;

if (strpos($path, '//') !== false) {
$url = $path;
} else {
Expand Down Expand Up @@ -538,10 +552,10 @@ public function script($url, $options = array()) {
}
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'));
Expand Down

0 comments on commit 758599e

Please sign in to comment.