Skip to content
Permalink
Browse files

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

…. Test case added.
  • Loading branch information...
markstory committed Sep 3, 2009
1 parent ead3b0e commit ce7864920f7e2ef73a1544d5ee8244fe952b88ea
Showing with 20 additions and 9 deletions.
  1. +7 −4 cake/libs/view/helpers/html.php
  2. +13 −5 cake/tests/cases/libs/view/helpers/html.test.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;
}
}
@@ -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();

0 comments on commit ce78649

Please sign in to comment.
You can’t perform that action at this time.