Skip to content

Commit

Permalink
Allowing HtmlHelper::script() to take an array of options, allows for…
Browse files Browse the repository at this point in the history
… custom attributes to be added to script include tags. Test cases added. Fixes #2955
  • Loading branch information
markstory committed Sep 3, 2009
1 parent c963d5c commit ead3b0e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
31 changes: 21 additions & 10 deletions cake/libs/view/helpers/html.php
Expand Up @@ -88,7 +88,7 @@ class HtmlHelper extends AppHelper {
'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>',
'javascriptlink' => '<script type="text/javascript" src="%s"%s></script>',
'javascriptend' => '</script>'
);

Expand Down Expand Up @@ -416,26 +416,34 @@ function css($path, $rel = null, $htmlAttributes = array(), $inline = true) {
*
* 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
* #### Options
*
* - `inline` - Whether script should be output inline or into scripts_for_layout.
* - `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.
*
* @param mixed $url String or array of javascript files to include
* @param mixed $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value
* @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) {
function script($url, $options = array()) {
if (is_bool($options)) {
list($inline, $options) = array($options, array());
$options['inline'] = $inline;
}
$options = array_merge(array('inline' => true, 'once' => true), $options);
if (is_array($url)) {
$out = '';
foreach ($url as $i) {
$out .= "\n\t" . $this->script($i, $inline, $once);
$out .= "\n\t" . $this->script($i, $options);
}
if ($inline) {
if ($options['inline']) {
return $out . "\n";
}
return null;
}

if ($once && isset($this->__includedScripts[$url])) {
if ($options['once'] && isset($this->__includedScripts[$url])) {
return null;
}
$this->__includedScripts[$url] = true;
Expand Down Expand Up @@ -464,7 +472,10 @@ function script($url, $inline = true, $once = true) {
$url = str_replace(JS_URL, 'cjs/', $url);
}
}
$out = $this->output(sprintf($this->tags['javascriptlink'], $url));
$inline = $options['inline'];
unset($options['inline'], $options['once']);
$attributes = $this->_parseAttributes($options, ' ', ' ');
$out = $this->output(sprintf($this->tags['javascriptlink'], $url, $attributes));

if ($inline) {
return $out;
Expand Down
13 changes: 10 additions & 3 deletions cake/tests/cases/libs/view/helpers/html.test.php
Expand Up @@ -448,16 +448,17 @@ function testScriptTimestamping() {
touch(WWW_ROOT . 'js' . DS. '__cake_js_test.js');
$timestamp = substr(strtotime('now'), 0, 8);

$result = $this->Html->script('__cake_js_test', true, false);
$result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
$this->assertPattern('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');

Configure::write('debug', 0);
Configure::write('Asset.timestamp', 'force');
$result = $this->Html->script('__cake_js_test', true, false);
$result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => 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('Asset.timestamp', false);
}

/**
* test that scripts added with uses() are only ever included once.
* test script tag generation
Expand Down Expand Up @@ -498,8 +499,14 @@ function testScript() {
$result = $this->Html->script(array('foo', 'bar', 'baz'));
$this->assertNoPattern('/foo.js/', $result);

$result = $this->Html->script('foo', true, false);
$result = $this->Html->script('foo', array('inline' => true, 'once' => false));
$this->assertNotNull($result);

$result = $this->Html->script('jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
$expected = array(
'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
);
$this->assertTags($result, $expected);
}
/**
* test Script block generation
Expand Down

0 comments on commit ead3b0e

Please sign in to comment.