From de4737323f9650be3d07af0191cb9f61ea8bf1c2 Mon Sep 17 00:00:00 2001 From: ADmad Date: Tue, 22 Sep 2009 02:30:52 +0530 Subject: [PATCH] Updated Html and Javascript helpers to suffix asset urls with timestamp even when the app is run off a subfolder on the domain Signed-off-by: Mark Story --- cake/libs/view/helpers/html.php | 21 ++++++++----------- cake/libs/view/helpers/javascript.php | 12 ++++------- .../cases/libs/view/helpers/html.test.php | 14 +++++++++++-- .../libs/view/helpers/javascript.test.php | 4 ++-- 4 files changed, 27 insertions(+), 24 deletions(-) diff --git a/cake/libs/view/helpers/html.php b/cake/libs/view/helpers/html.php index 6be580f89..331b1ad8d 100644 --- a/cake/libs/view/helpers/html.php +++ b/cake/libs/view/helpers/html.php @@ -374,14 +374,8 @@ class HtmlHelper extends AppHelper { } } - $path = $this->webroot($path); - - $url = $path; - $timestampEnabled = ( - (Configure::read('Asset.timestamp') === true && Configure::read() > 0) || - Configure::read('Asset.timestamp') === 'force' - ); - if (strpos($path, '?') === false && $timestampEnabled) { + $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)); } @@ -607,12 +601,15 @@ class HtmlHelper extends AppHelper { 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); } } diff --git a/cake/libs/view/helpers/javascript.php b/cake/libs/view/helpers/javascript.php index 3460f1c07..b46fc14a4 100644 --- a/cake/libs/view/helpers/javascript.php +++ b/cake/libs/view/helpers/javascript.php @@ -271,14 +271,10 @@ class JavascriptHelper extends AppHelper { } } - $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')) { diff --git a/cake/tests/cases/libs/view/helpers/html.test.php b/cake/tests/cases/libs/view/helpers/html.test.php index 18c998b30..0a5b2f1f2 100644 --- a/cake/tests/cases/libs/view/helpers/html.test.php +++ b/cake/tests/cases/libs/view/helpers/html.test.php @@ -332,6 +332,16 @@ class HtmlHelperTest extends CakeTestCase { '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; } /** @@ -420,14 +430,14 @@ class HtmlHelperTest extends CakeTestCase { $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; } diff --git a/cake/tests/cases/libs/view/helpers/javascript.test.php b/cake/tests/cases/libs/view/helpers/javascript.test.php index eb64fec96..0ceea78f0 100644 --- a/cake/tests/cases/libs/view/helpers/javascript.test.php +++ b/cake/tests/cases/libs/view/helpers/javascript.test.php @@ -278,11 +278,11 @@ class JavascriptTest extends CakeTestCase { $this->Javascript->webroot = '/testing/'; $result = $this->Javascript->link('__cake_js_test'); - $this->assertPattern('/^]+src="\/testing\/js\/__cake_js_test\.js\?"[^<>]*>/', $result); + $this->assertPattern('/^]+src="\/testing\/js\/__cake_js_test\.js\?\d+"[^<>]*>/', $result); $this->Javascript->webroot = '/testing/longer/'; $result = $this->Javascript->link('__cake_js_test'); - $this->assertPattern('/^]+src="\/testing\/longer\/js\/__cake_js_test\.js\?"[^<>]*>/', $result); + $this->assertPattern('/^]+src="\/testing\/longer\/js\/__cake_js_test\.js\?\d+"[^<>]*>/', $result); $this->Javascript->webroot = $webroot; Configure::write('debug', $debug);