Skip to content

Commit

Permalink
Fixing JsHelper::writeBuffer when inline is set to false. Scripts are…
Browse files Browse the repository at this point in the history
… now enclosed in a script tag. Fixes #130
  • Loading branch information
markstory committed Dec 21, 2009
1 parent 3eaff3e commit 42fa6ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
9 changes: 5 additions & 4 deletions cake/libs/view/helpers/js.php
Expand Up @@ -199,20 +199,21 @@ function writeBuffer($options = array()) {
if ($options['onDomReady']) {
$script = $this->{$this->__engineName}->domReady($script);
}
$opts = $options;
unset($opts['onDomReady'], $opts['cache'], $opts['clear']);

if (!$options['cache'] && $options['inline']) {
$opts = $options;
unset($opts['onDomReady'], $opts['cache'], $opts['clear']);
return $this->Html->scriptBlock($script, $opts);
}

if ($options['cache'] && $options['inline']) {
$filename = md5($script);
if (!file_exists(JS . $filename . '.js')) {
cache(str_replace(WWW_ROOT, '', JS) . $filename . '.js', $script, '+999 days', 'public');
}
return $this->Html->script($filename);
}
$view =& ClassRegistry::getObject('view');
$view->addScript($script);
$this->Html->scriptBlock($script, $opts);
return null;
}

Expand Down
22 changes: 22 additions & 0 deletions cake/tests/cases/libs/view/helpers/js.test.php
Expand Up @@ -231,6 +231,28 @@ function testWriteScriptsNoFile() {
$view->expectAt(0, 'addScript', array(new PatternExpectation('/one\s=\s1;\ntwo\=\2;/')));
$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'cache' => false));
}
function getTests() {
return array('start', 'startCase', 'testWriteBufferNotInline', 'endCase', 'end');
}

/**
* test that writing the buffer with inline = false includes a script tag.
*
* @return void
*/
function testWriteBufferNotInline() {
$this->Js->set('foo', 1);

$view =& new JsHelperMockView();
ClassRegistry::removeObject('view');
ClassRegistry::addObject('view', $view);
$view->expectCallCount('addScript', 1);

$pattern = new PatternExpectation('#<script type="text\/javascript">window.app \= \{"foo"\:1\}\;<\/script>#');
$view->expectAt(0, 'addScript', array($pattern));

$result = $this->Js->writeBuffer(array('onDomReady' => false, 'inline' => false, 'safe' => false));
}

/**
* test that writeScripts makes files, and puts the events into them.
Expand Down

0 comments on commit 42fa6ff

Please sign in to comment.