Skip to content

Commit

Permalink
Adding scriptBlock() to HtmlHelper.
Browse files Browse the repository at this point in the history
Adding tests.
  • Loading branch information
markstory committed Mar 13, 2009
1 parent 7ea7bd9 commit 91e47a6
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 3 deletions.
21 changes: 21 additions & 0 deletions cake/libs/view/helpers/html.php
Expand Up @@ -453,6 +453,27 @@ function script($url, $inline = true, $once = true) {
$view->addScript($out);
}
}
/**
* Wrap $script in a script tag.
*
* @param string $script The script to wrap
* @param array $options The options to use.
* @return mixed string or null
**/
function scriptBlock($script, $options = array()) {
$defaultOptions = array('safe' => true, 'inline' => true);
$options = array_merge($defaultOptions, $options);
if ($options['safe']) {
$script = "\n" . '//<![CDATA[' . "\n" . $script . "\n" . '//]]>' . "\n";
}
if ($options['inline']) {
return sprintf($this->tags['javascriptblock'], $script);
} else {
$view =& ClassRegistry::getObject('view');
$view->addScript(sprintf($this->tags['javascriptblock'], $script));
return null;
}
}
/**
* Builds CSS style data from an array of CSS properties
*
Expand Down
63 changes: 60 additions & 3 deletions cake/tests/cases/libs/view/helpers/html.test.php
Expand Up @@ -48,13 +48,28 @@ class TheHtmlTestController extends Controller {
*/
var $uses = null;
}

Mock::generate('View', 'HtmlHelperMockView');

/**
* HtmlHelperTest class
*
* @package cake
* @subpackage cake.tests.cases.libs.view.helpers
*/
class HtmlHelperTest extends CakeTestCase {
/**
* Regexp for CDATA start block
*
* @var string
*/
var $cDataStart = 'preg:/^\/\/<!\[CDATA\[[\n\r]*/';
/**
* Regexp for CDATA end block
*
* @var string
*/
var $cDataEnd = 'preg:/[^\]]*\]\]\>[\s\r\n]*/';
/**
* html property
*
Expand All @@ -68,7 +83,7 @@ class HtmlHelperTest extends CakeTestCase {
* @access public
* @return void
*/
function setUp() {
function startTest() {
$this->Html =& new HtmlHelper();
$view =& new View(new TheHtmlTestController());
ClassRegistry::addObject('view', $view);
Expand Down Expand Up @@ -389,6 +404,47 @@ function testScript() {
$result = $this->Html->script('foo', true, false);
$this->assertNotNull($result);
}
/**
* test Script block generation
*
* @return void
**/
function testScriptBlock() {
$result = $this->Html->scriptBlock('window.foo = 2;');
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'window.foo = 2;',
$this->cDataEnd,
'/script',
);
$this->assertTags($result, $expected);

$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => false));
$expected = array(
'script' => array('type' => 'text/javascript'),
'window.foo = 2;',
'/script',
);
$this->assertTags($result, $expected);

$result = $this->Html->scriptBlock('window.foo = 2;', array('safe' => true));
$expected = array(
'script' => array('type' => 'text/javascript'),
$this->cDataStart,
'window.foo = 2;',
$this->cDataEnd,
'/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);
}
/**
* testCharsetTag method
*
Expand Down Expand Up @@ -936,12 +992,13 @@ function testPara() {
$this->assertTags($result, array('p' => array('class' => 'class-name'), '&lt;text&gt;', '/p'));
}
/**
* tearDown method
* endTest method
*
* @access public
* @return void
*/
function tearDown() {
function endTest() {
ClassRegistry::flush();
unset($this->Html);
}
}
Expand Down

0 comments on commit 91e47a6

Please sign in to comment.