Skip to content

Commit

Permalink
Updating HtmlHelper::css, JavascriptHelper::link to not replace multi…
Browse files Browse the repository at this point in the history
…ple occurences of CSS_URL or JS_URL when using Asset.filter settings.

Test cases added.
Thanks to 'robustsolution' for the patch.
Fixes #105
  • Loading branch information
markstory committed Sep 26, 2009
1 parent 06cf974 commit 508d737
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 16 deletions.
7 changes: 5 additions & 2 deletions cake/libs/view/helpers/html.php
Expand Up @@ -352,13 +352,16 @@ function css($path, $rel = null, $htmlAttributes = array(), $inline = true) {
(Configure::read('Asset.timestamp') === true && Configure::read() > 0) ||
Configure::read('Asset.timestamp') === 'force'
);

$url = $this->webroot($path);
if (strpos($path, '?') === false && $timestampEnabled) {
$url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $path));
}

if (Configure::read('Asset.filter.css')) {
$url = str_replace(CSS_URL, 'ccss/', $url);
$pos = strpos($url, CSS_URL);
if ($pos !== false) {
$url = substr($url, 0, $pos) . 'ccss/' . substr($url, $pos + strlen(CSS_URL));
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions cake/libs/view/helpers/javascript.php
Expand Up @@ -263,9 +263,12 @@ function link($url, $inline = true) {
} else {
$url = $this->webroot($url);
}

if (Configure::read('Asset.filter.js')) {
$url = str_replace(JS_URL, 'cjs/', $url);
$pos = strpos($url, JS_URL);
if ($pos !== false) {
$url = substr($url, 0, $pos) . 'cjs/' . substr($url, $pos + strlen(JS_URL));
}
}
}
$out = $this->output(sprintf($this->tags['javascriptlink'], $url));
Expand Down
46 changes: 34 additions & 12 deletions cake/tests/cases/libs/view/helpers/html.test.php
Expand Up @@ -370,12 +370,6 @@ function testCssLink() {
$expected['link']['href'] = 'preg:/http:\/\/.*\/screen\.css\?1234/';
$this->assertTags($result, $expected);

Configure::write('Asset.filter.css', 'css.php');
$result = $this->Html->css('cake.generic');
$expected['link']['href'] = 'preg:/.*ccss\/cake\.generic\.css/';
$this->assertTags($result, $expected);
Configure::write('Asset.filter.css', false);

$result = explode("\n", trim($this->Html->css(array('cake.generic', 'vendor.generic'))));
$expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css/';
$this->assertTags($result[0], $expected);
Expand All @@ -385,12 +379,6 @@ function testCssLink() {

Configure::write('Asset.timestamp', true);

Configure::write('Asset.filter.css', 'css.php');
$result = $this->Html->css('cake.generic');
$expected['link']['href'] = 'preg:/.*ccss\/cake\.generic\.css\?[0-9]+/';
$this->assertTags($result, $expected);
Configure::write('Asset.filter.css', false);

$result = $this->Html->css('cake.generic');
$expected['link']['href'] = 'preg:/.*css\/cake\.generic\.css\?[0-9]+/';
$this->assertTags($result, $expected);
Expand Down Expand Up @@ -421,6 +409,40 @@ function testCssLink() {
$this->assertTags($result, $expected);
$this->Html->webroot = $webroot;
}
/**
* test css() with Asset.Css.filter
*
* @return void
**/
function testCssFiltering() {
$this->Html->webroot = '/';

Configure::write('Asset.filter.css', 'css.php');
$result = $this->Html->css('cake.generic');
$expected = array(
'link' => array('rel' => 'stylesheet', 'type' => 'text/css', 'href' => 'preg:/\/ccss\/cake\.generic\.css/')
);
$this->assertTags($result, $expected);

Configure::write('Asset.timestamp', true);
Configure::write('Asset.filter.css', 'css.php');

$result = $this->Html->css('cake.generic');
$expected['link']['href'] = 'preg:/\/ccss\/cake\.generic\.css\?[0-9]+/';
$this->assertTags($result, $expected);

Configure::write('Asset.timestamp', false);
$result = $this->Html->css('myfoldercss/cake.generic');
$expected['link']['href'] = 'preg:/\/ccss\/myfoldercss\/cake\.generic\.css/';
$this->assertTags($result, $expected);

$this->Html->webroot = '/testing/longer/';
$result = $this->Html->css('myfoldercss/cake.generic');
$expected['link']['href'] = 'preg:/\/testing\/longer\/ccss\/myfoldercss\/cake\.generic\.css/';
$this->assertTags($result, $expected);

Configure::write('Asset.filter.css', false);
}
/**
* testCharsetTag method
*
Expand Down
4 changes: 4 additions & 0 deletions cake/tests/cases/libs/view/helpers/javascript.test.php
Expand Up @@ -250,6 +250,10 @@ function testFilteringAndTimestamping() {
$expected = '<script type="text/javascript" src="cjs/jquery-1.1.2.js"></script>';
$this->assertEqual($result, $expected);

$result = $this->Javascript->link('folderjs/jquery-1.1.2');
$expected = '<script type="text/javascript" src="cjs/folderjs/jquery-1.1.2.js"></script>';
$this->assertEqual($result, $expected);

if ($old === null) {
Configure::delete('Asset.filter.js');
}
Expand Down

0 comments on commit 508d737

Please sign in to comment.