Skip to content
Permalink
Browse files

Adding scriptBlock() to HtmlHelper.

Adding tests.
  • Loading branch information...
markstory committed Mar 13, 2009
1 parent 7ea7bd9 commit 91e47a6379b2c4ca8f353b0fa1ac0329bc38840e
Showing with 81 additions and 3 deletions.
  1. +21 −0 cake/libs/view/helpers/html.php
  2. +60 −3 cake/tests/cases/libs/view/helpers/html.test.php
@@ -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
*
@@ -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
*
@@ -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);
@@ -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
*
@@ -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);
}
}

0 comments on commit 91e47a6

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