mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Moving script() method into HtmlHelper.
Moving tests.
This commit is contained in:
parent
8025743830
commit
7ea7bd90c2
2 changed files with 149 additions and 1 deletions
|
@ -89,7 +89,11 @@ class HtmlHelper extends AppHelper {
|
|||
'ul' => '<ul%s>%s</ul>',
|
||||
'ol' => '<ol%s>%s</ol>',
|
||||
'li' => '<li%s>%s</li>',
|
||||
'error' => '<div%s>%s</div>'
|
||||
'error' => '<div%s>%s</div>',
|
||||
'javascriptblock' => '<script type="text/javascript">%s</script>',
|
||||
'javascriptstart' => '<script type="text/javascript">',
|
||||
'javascriptlink' => '<script type="text/javascript" src="%s"></script>',
|
||||
'javascriptend' => '</script>'
|
||||
);
|
||||
/**
|
||||
* Base URL
|
||||
|
@ -133,6 +137,13 @@ class HtmlHelper extends AppHelper {
|
|||
* @access private
|
||||
*/
|
||||
var $_crumbs = array();
|
||||
/**
|
||||
* Names of script files that have been included once
|
||||
*
|
||||
* @var array
|
||||
* @access public
|
||||
**/
|
||||
var $__includedScripts = array();
|
||||
/**
|
||||
* Document type definitions
|
||||
*
|
||||
|
@ -377,6 +388,72 @@ class HtmlHelper extends AppHelper {
|
|||
}
|
||||
}
|
||||
/**
|
||||
* Returns one or many <script> tags depending on the number of scripts given.
|
||||
*
|
||||
* If the filename is prefixed with "/", the path will be relative to the base path of your
|
||||
* application. Otherwise, the path will be relative to your JavaScript path, usually webroot/js.
|
||||
*
|
||||
* Can include one or many Javascript files.
|
||||
*
|
||||
* @param mixed $url String or array of javascript files to include
|
||||
* @param boolean $inline Whether script should be output inline or into scripts_for_layout.
|
||||
* @param boolean $once Whether or not the script should be checked for uniqueness. If true scripts will only be
|
||||
* included once, use false to allow the same script to be included more than once per request.
|
||||
* @return mixed String of <script /> tags or null if $inline is false or if $once is true and the file has been
|
||||
* included before.
|
||||
**/
|
||||
function script($url, $inline = true, $once = true) {
|
||||
if (is_array($url)) {
|
||||
$out = '';
|
||||
foreach ($url as $i) {
|
||||
$out .= "\n\t" . $this->script($i, $inline, $once);
|
||||
}
|
||||
if ($inline) {
|
||||
return $out . "\n";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($once && isset($this->__includedScripts[$url])) {
|
||||
return null;
|
||||
}
|
||||
$this->__includedScripts[$url] = true;
|
||||
|
||||
if (strpos($url, '://') === false) {
|
||||
if ($url[0] !== '/') {
|
||||
$url = JS_URL . $url;
|
||||
}
|
||||
$url = $this->webroot($url);
|
||||
|
||||
if (strpos($url, '?') === false) {
|
||||
if (strpos($url, '.js') === false) {
|
||||
$url .= '.js';
|
||||
}
|
||||
}
|
||||
|
||||
$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 (Configure::read('Asset.filter.js')) {
|
||||
$url = str_replace(JS_URL, 'cjs/', $url);
|
||||
}
|
||||
}
|
||||
$out = $this->output(sprintf($this->tags['javascriptlink'], $url));
|
||||
|
||||
if ($inline) {
|
||||
return $out;
|
||||
} else {
|
||||
$view =& ClassRegistry::getObject('view');
|
||||
$view->addScript($out);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Builds CSS style data from an array of CSS properties
|
||||
*
|
||||
* @param array $data
|
||||
|
|
|
@ -318,6 +318,77 @@ class HtmlHelperTest extends CakeTestCase {
|
|||
|
||||
Configure::write('debug', $debug);
|
||||
}
|
||||
/**
|
||||
* test timestamp enforcement for script tags.
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testScriptTimestamping() {
|
||||
if ($this->skipIf(!is_writable(JS), 'webroot/js is not Writable, timestamp testing has been skipped')) {
|
||||
return;
|
||||
}
|
||||
Configure::write('Asset.timestamp', true);
|
||||
touch(WWW_ROOT . 'js' . DS. '__cake_js_test.js');
|
||||
$timestamp = substr(strtotime('now'), 0, 8);
|
||||
|
||||
$result = $this->Html->script('__cake_js_test', true, false);
|
||||
$this->assertPattern('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
|
||||
|
||||
Configure::write('debug', 0);
|
||||
$result = $this->Html->script('__cake_js_test', true, false);
|
||||
$this->assertPattern('/__cake_js_test.js"/', $result);
|
||||
|
||||
Configure::write('Asset.timestamp', 'force');
|
||||
$result = $this->Html->script('__cake_js_test', true, false);
|
||||
$this->assertPattern('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
|
||||
|
||||
unlink(WWW_ROOT . 'js' . DS. '__cake_js_test.js');
|
||||
Configure::write('debug', 2);
|
||||
Configure::write('Asset.timestamp', false);
|
||||
}
|
||||
/**
|
||||
* test that scripts added with uses() are only ever included once.
|
||||
* test script tag generation
|
||||
*
|
||||
* @return void
|
||||
**/
|
||||
function testScript() {
|
||||
$result = $this->Html->script('foo');
|
||||
$expected = array(
|
||||
'script' => array('type' => 'text/javascript', 'src' => 'js/foo.js')
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Html->script(array('foobar', 'bar'));
|
||||
$expected = array(
|
||||
array('script' => array('type' => 'text/javascript', 'src' => 'js/foobar.js')),
|
||||
'/script',
|
||||
array('script' => array('type' => 'text/javascript', 'src' => 'js/bar.js')),
|
||||
'/script',
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Html->script('jquery-1.3');
|
||||
$expected = array(
|
||||
'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.js')
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Html->script('/plugin/js/jquery-1.3.2.js?someparam=foo');
|
||||
$expected = array(
|
||||
'script' => array('type' => 'text/javascript', 'src' => '/plugin/js/jquery-1.3.2.js?someparam=foo')
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
|
||||
$result = $this->Html->script('foo');
|
||||
$this->assertNull($result, 'Script returned upon duplicate inclusion %s');
|
||||
|
||||
$result = $this->Html->script(array('foo', 'bar', 'baz'));
|
||||
$this->assertNoPattern('/foo.js/', $result);
|
||||
|
||||
$result = $this->Html->script('foo', true, false);
|
||||
$this->assertNotNull($result);
|
||||
}
|
||||
/**
|
||||
* testCharsetTag method
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue