Skip to content

Commit

Permalink
Updated Html and Javascript helpers to suffix asset urls with timesta…
Browse files Browse the repository at this point in the history
…mp even when the app is run off a subfolder on the domain

Signed-off-by: Mark Story <mark@mark-story.com>
  • Loading branch information
ADmad authored and markstory committed Sep 22, 2009
1 parent 7116c01 commit 07a89cd
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
14 changes: 7 additions & 7 deletions cake/libs/view/helpers/html.php
Expand Up @@ -349,9 +349,7 @@ function css($path, $rel = null, $htmlAttributes = array(), $inline = true) {
}
}

$path = $this->webroot($path);

$url = $path;
$url = $this->webroot($path);
if (strpos($path, '?') === false && ((Configure::read('Asset.timestamp') === true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force')) {
$url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $path));
}
Expand Down Expand Up @@ -434,13 +432,15 @@ function getCrumbs($separator = '&raquo;', $startText = false) {
function image($path, $options = array()) {
if (is_array($path)) {
$path = $this->url($path);
} elseif ($path[0] === '/') {
$path = $this->webroot($path);
} elseif (strpos($path, '://') === false) {
$path = $this->webroot(IMAGES_URL . $path);
if ($path[0] !== '/') {
$path = IMAGES_URL . $path;
}

if ((Configure::read('Asset.timestamp') == true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force') {
$path .= '?' . @filemtime(str_replace('/', DS, WWW_ROOT . $path));
$path = $this->webroot($path) . '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $path));
} else {
$path = $this->webroot($path);
}
}

Expand Down
16 changes: 6 additions & 10 deletions cake/libs/view/helpers/javascript.php
Expand Up @@ -214,11 +214,11 @@ function blockEnd() {
$options = $this->_blockOptions;
$this->_blockOptions = array();
$this->inBlock = false;

if (empty($script)) {
return null;
}

return $this->codeBlock($script, $options);
}
/**
Expand Down Expand Up @@ -254,14 +254,10 @@ function link($url, $inline = true) {
}
}

$url = $this->webroot($url);
$timestampEnabled = (
(Configure::read('Asset.timestamp') === true && Configure::read() > 0) ||
Configure::read('Asset.timestamp') === 'force'
);

if (strpos($url, '?') === false && $timestampEnabled) {
$url .= '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $url));
if (strpos($url, '?') === false && ((Configure::read('Asset.timestamp') === true && Configure::read() > 0) || Configure::read('Asset.timestamp') === 'force')) {
$url = $this->webroot($url) . '?' . @filemtime(WWW_ROOT . str_replace('/', DS, $url));
} else {
$url = $this->webroot($url);
}

if (Configure::read('Asset.filter.js')) {
Expand Down
14 changes: 12 additions & 2 deletions cake/tests/cases/libs/view/helpers/html.test.php
Expand Up @@ -307,6 +307,16 @@ function testImageTagWithTheme() {
'src' => 'preg:/themed\/default\/img\/cake\.power\.gif\?\d+/',
'alt' => ''
)));

$webroot = $this->Html->webroot;
$this->Html->webroot = '/testing/';
$result = $this->Html->image('cake.power.gif');
$this->assertTags($result, array(
'img' => array(
'src' => 'preg:/\/testing\/themed\/default\/img\/cake\.power\.gif\?\d+/',
'alt' => ''
)));
$this->Html->webroot = $webroot;
}
/**
* testStyle method
Expand Down Expand Up @@ -391,14 +401,14 @@ function testCssLink() {
$webroot = $this->Html->webroot;
$this->Html->webroot = '/testing/';
$result = $this->Html->css('cake.generic');
$expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?/';
$expected['link']['href'] = 'preg:/\/testing\/css\/cake\.generic\.css\?[0-9]+/';
$this->assertTags($result, $expected);
$this->Html->webroot = $webroot;

$webroot = $this->Html->webroot;
$this->Html->webroot = '/testing/longer/';
$result = $this->Html->css('cake.generic');
$expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?/';
$expected['link']['href'] = 'preg:/\/testing\/longer\/css\/cake\.generic\.css\?[0-9]+/';
$this->assertTags($result, $expected);
$this->Html->webroot = $webroot;
}
Expand Down
6 changes: 3 additions & 3 deletions cake/tests/cases/libs/view/helpers/javascript.test.php
Expand Up @@ -262,11 +262,11 @@ function testFilteringAndTimestamping() {

$this->Javascript->webroot = '/testing/';
$result = $this->Javascript->link('__cake_js_test');
$this->assertPattern('/^<script[^<>]+src="\/testing\/js\/__cake_js_test\.js\?"[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+src="\/testing\/js\/__cake_js_test\.js\?\d+"[^<>]*>/', $result);

$this->Javascript->webroot = '/testing/longer/';
$result = $this->Javascript->link('__cake_js_test');
$this->assertPattern('/^<script[^<>]+src="\/testing\/longer\/js\/__cake_js_test\.js\?"[^<>]*>/', $result);
$this->assertPattern('/^<script[^<>]+src="\/testing\/longer\/js\/__cake_js_test\.js\?\d+"[^<>]*>/', $result);

$this->Javascript->webroot = $webroot;
Configure::write('debug', $debug);
Expand Down Expand Up @@ -826,7 +826,7 @@ function testAfterRender() {
ob_start();
$this->Javascript->afterRender();
$result = ob_get_clean();

$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
Expand Down

0 comments on commit 07a89cd

Please sign in to comment.