Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding ability to add arbitrary attributes to HtmlHelper::scriptBlock…

…. Test case added.
  • Loading branch information...
commit ce7864920f7e2ef73a1544d5ee8244fe952b88ea 1 parent ead3b0e
@markstory markstory authored
View
11 cake/libs/view/helpers/html.php
@@ -86,7 +86,7 @@ class HtmlHelper extends AppHelper {
'ol' => '<ol%s>%s</ol>',
'li' => '<li%s>%s</li>',
'error' => '<div%s>%s</div>',
- 'javascriptblock' => '<script type="text/javascript">%s</script>',
+ 'javascriptblock' => '<script type="text/javascript"%s>%s</script>',
'javascriptstart' => '<script type="text/javascript">',
'javascriptlink' => '<script type="text/javascript" src="%s"%s></script>',
'javascriptend' => '</script>'
@@ -502,11 +502,14 @@ function scriptBlock($script, $options = array()) {
if ($options['safe']) {
$script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
}
- if ($options['inline']) {
- return sprintf($this->tags['javascriptblock'], $script);
+ $inline = $options['inline'];
+ unset($options['inline'], $options['safe']);
+ $attributes = $this->_parseAttributes($options, ' ', ' ');
+ if ($inline) {
+ return sprintf($this->tags['javascriptblock'], $attributes, $script);
} else {
$view =& ClassRegistry::getObject('view');
- $view->addScript(sprintf($this->tags['javascriptblock'], $script));
+ $view->addScript(sprintf($this->tags['javascriptblock'], $attributes, $script));
return null;
}
}
View
18 cake/tests/cases/libs/view/helpers/html.test.php
@@ -391,7 +391,7 @@ function testCssLink() {
$expected['link']['href'] = 'preg:/.*css\/vendor\.generic\.css/';
$this->assertTags($result[1], $expected);
$this->assertEqual(count($result), 2);
-
+
Configure::write('debug', 2);
Configure::write('Asset.timestamp', true);
@@ -492,7 +492,7 @@ function testScript() {
'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');
@@ -523,7 +523,7 @@ function testScriptBlock() {
'/script',
);
$this->assertTags($result, $expected);
-
+
$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
@@ -541,13 +541,21 @@ function testScriptBlock() {
'/script',
);
$this->assertTags($result, $expected);
-
+
$view =& ClassRegistry::getObject('view');
$view =& new HtmlHelperMockView();
$view->expectAt(0, 'addScript', array(new PatternExpectation('/window\.foo\s\=\s2;/')));
-
+
$result = $this->Html->scriptBlock('window.foo = 2;', array('inline' => false));
$this->assertNull($result);
+
+ $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
+ $expected = array(
+ 'script' => array('type' => 'text/javascript', 'encoding' => 'utf-8'),
+ 'window.foo = 2;',
+ '/script',
+ );
+ $this->assertTags($result, $expected);
}
/**
* test script tag output buffering when using scriptStart() and scriptEnd();
Please sign in to comment.
Something went wrong with that request. Please try again.