Permalink
Browse files

Adding scriptBlock() to HtmlHelper.

Adding tests.
  • Loading branch information...
1 parent 7ea7bd9 commit 91e47a6379b2c4ca8f353b0fa1ac0329bc38840e @markstory markstory committed Mar 13, 2009
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
@@ -454,6 +454,27 @@ function script($url, $inline = true, $once = true) {
}
}
/**
+ * 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
*
* @param array $data
@@ -48,6 +48,9 @@ class TheHtmlTestController extends Controller {
*/
var $uses = null;
}
+
+Mock::generate('View', 'HtmlHelperMockView');
+
/**
* HtmlHelperTest class
*
@@ -56,6 +59,18 @@ class TheHtmlTestController extends Controller {
*/
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
*
* @var mixed null
@@ -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);
@@ -390,6 +405,47 @@ function testScript() {
$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
*
* @access public
@@ -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.