Permalink
Browse files

Allowing HtmlHelper::script() to take an array of options, allows for…

… custom attributes to be added to script include tags. Test cases added. Fixes #2955
  • Loading branch information...
1 parent c963d5c commit ead3b0ecf622b2182f5024c87b9df06242ce5eff @markstory markstory committed Sep 3, 2009
Showing with 31 additions and 13 deletions.
  1. +21 −10 cake/libs/view/helpers/html.php
  2. +10 −3 cake/tests/cases/libs/view/helpers/html.test.php
View
31 cake/libs/view/helpers/html.php
@@ -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>'
);
@@ -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;
@@ -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;
View
13 cake/tests/cases/libs/view/helpers/html.test.php
@@ -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
@@ -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

0 comments on commit ead3b0e

Please sign in to comment.