Skip to content

Commit

Permalink
Add block option to HtmlHelper::scriptBlock()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Feb 1, 2012
1 parent edeca2f commit 94778ef
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
9 changes: 8 additions & 1 deletion lib/Cake/Test/Case/View/Helper/HtmlHelperTest.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -790,13 +790,20 @@ public function testScriptBlock() {
$this->assertTags($result, $expected); $this->assertTags($result, $expected);




$this->View->expects($this->any()) $this->View->expects($this->at(0))
->method('append') ->method('append')
->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/')); ->with('script', $this->matchesRegularExpression('/window\.foo\s\=\s2;/'));


$this->View->expects($this->at(1))
->method('append')
->with('scriptTop', $this->stringContains('alert('));

$result = $this->Html->scriptBlock('window.foo = 2;', array('inline' => false)); $result = $this->Html->scriptBlock('window.foo = 2;', array('inline' => false));
$this->assertNull($result); $this->assertNull($result);


$result = $this->Html->scriptBlock('alert("hi")', array('block' => 'scriptTop'));
$this->assertNull($result);

$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8')); $result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false, 'encoding' => 'utf-8'));
$expected = array( $expected = array(
'script' => array('type' => 'text/javascript', 'encoding' => 'utf-8'), 'script' => array('type' => 'text/javascript', 'encoding' => 'utf-8'),
Expand Down
26 changes: 17 additions & 9 deletions lib/Cake/View/Helper/HtmlHelper.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -565,26 +565,34 @@ public function script($url, $options = array()) {
* ### Options * ### Options
* *
* - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]> * - `safe` (boolean) Whether or not the $script should be wrapped in <![CDATA[ ]]>
* - `inline` (boolean) Whether or not the $script should be added to $scripts_for_layout or output inline * - `inline` (boolean) Whether or not the $script should be added to
* `$scripts_for_layout` / `script` block, or output inline. (Deprecated, use `block` instead)
* - `block` Which block you want this script block appended to.
* Defaults to `script`.
* *
* @param string $script The script to wrap * @param string $script The script to wrap
* @param array $options The options to use. * @param array $options The options to use. Options not listed above will be
* @return mixed string or null depending on the value of `$options['inline']` * treated as HTML attributes.
* @return mixed string or null depending on the value of `$options['block']`
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptBlock * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/html.html#HtmlHelper::scriptBlock
*/ */
public function scriptBlock($script, $options = array()) { public function scriptBlock($script, $options = array()) {
$options += array('safe' => true, 'inline' => true); $options += array('safe' => true, 'inline' => true);
if ($options['safe']) { if ($options['safe']) {
$script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n"; $script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
} }
$inline = $options['inline']; if (!$options['inline'] && empty($options['block'])) {
$options['block'] = 'script';
}
unset($options['inline'], $options['safe']); unset($options['inline'], $options['safe']);
$attributes = $this->_parseAttributes($options, ' ', ' ');
if ($inline) { $attributes = $this->_parseAttributes($options, array('block'), ' ');
return sprintf($this->_tags['javascriptblock'], $attributes, $script); $out = sprintf($this->_tags['javascriptblock'], $attributes, $script);

if (empty($options['block'])) {
return $out;
} else { } else {
$this->_View->append('script', sprintf($this->_tags['javascriptblock'], $attributes, $script)); $this->_View->append($options['block'], $out);
return null;
} }
} }


Expand Down

0 comments on commit 94778ef

Please sign in to comment.