浏览代码

Merge pull request #3701 from dakota/3.0-fix-once

3.0 Fix CSS and Script once option
José Lorenzo Rodríguez 11 年之前
父节点
当前提交
cd77c358a8
共有 2 个文件被更改,包括 32 次插入11 次删除
  1. 12 10
      src/View/Helper/HtmlHelper.php
  2. 20 1
      tests/TestCase/View/Helper/HtmlHelperTest.php

+ 12 - 10
src/View/Helper/HtmlHelper.php

@@ -405,12 +405,6 @@ class HtmlHelper extends Helper {
 			return;
 		}
 
-		if ($options['once'] && isset($this->_includedAssets[$path])) {
-			return '';
-		}
-		unset($options['once']);
-		$this->_includedAssets[$path] = true;
-
 		if (strpos($path, '//') !== false) {
 			$url = $path;
 		} else {
@@ -418,6 +412,12 @@ class HtmlHelper extends Helper {
 			$options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
 		}
 
+		if ($options['once'] && isset($this->_includedAssets[$url])) {
+			return '';
+		}
+		unset($options['once']);
+		$this->_includedAssets[$url] = true;
+
 		if ($options['rel'] === 'import') {
 			$out = $this->formatTemplate('style', [
 				'attrs' => $this->templater()->formatAttributes($options, ['rel', 'block']),
@@ -489,15 +489,17 @@ class HtmlHelper extends Helper {
 			}
 			return null;
 		}
-		if ($options['once'] && isset($this->_includedAssets[$url])) {
-			return null;
-		}
-		$this->_includedAssets[$url] = true;
 
 		if (strpos($url, '//') === false) {
 			$url = $this->assetUrl($url, $options + array('pathPrefix' => Configure::read('App.jsBaseUrl'), 'ext' => '.js'));
 			$options = array_diff_key($options, array('fullBase' => null, 'pathPrefix' => null));
 		}
+
+		if ($options['once'] && isset($this->_includedAssets[$url])) {
+			return null;
+		}
+		$this->_includedAssets[$url] = true;
+
 		$out = $this->formatTemplate('javascriptlink', [
 			'url' => $url,
 			'attrs' => $this->templater()->formatAttributes($options, ['block', 'once']),

+ 20 - 1
tests/TestCase/View/Helper/HtmlHelperTest.php

@@ -515,7 +515,7 @@ class HtmlHelperTest extends TestCase {
 		);
 		$this->assertTags($result, $expected);
 
-		$result = $this->Html->css('screen.css');
+		$result = $this->Html->css('screen.css', array('once' => false));
 		$this->assertTags($result, $expected);
 
 		Plugin::load('TestPlugin');
@@ -1979,4 +1979,23 @@ class HtmlHelperTest extends TestCase {
 		);
 	}
 
+/**
+ * Tests that CSS and Javascript files of the same name don't conflict with the 'once' test
+ *
+ * @return void
+ */
+	public function testCssAndScriptWithSameName() {
+		$result = $this->Html->css('foo');
+		$expected = array(
+			'link' => array('rel' => 'stylesheet', 'href' => 'preg:/.*css\/foo\.css/')
+		);
+		$this->assertTags($result, $expected);
+
+		$result = $this->Html->script('foo');
+		$expected = array(
+			'script' => array('src' => 'js/foo.js')
+		);
+		$this->assertTags($result, $expected);
+	}
+
 }