mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-19 02:56:15 +00:00
Allowing HtmlHelper::script() to take an array of options, allows for custom attributes to be added to script include tags. Test cases added. Fixes #2955
This commit is contained in:
parent
c963d5cc43
commit
ead3b0ecf6
2 changed files with 31 additions and 13 deletions
|
@ -88,7 +88,7 @@ class HtmlHelper extends AppHelper {
|
|||
'error' => '<div%s>%s</div>',
|
||||
'javascriptblock' => '<script type="text/javascript">%s</script>',
|
||||
'javascriptstart' => '<script type="text/javascript">',
|
||||
'javascriptlink' => '<script type="text/javascript" src="%s"></script>',
|
||||
'javascriptlink' => '<script type="text/javascript" src="%s"%s></script>',
|
||||
'javascriptend' => '</script>'
|
||||
);
|
||||
|
||||
|
@ -416,26 +416,34 @@ class HtmlHelper extends AppHelper {
|
|||
*
|
||||
* Can include one or many Javascript files.
|
||||
*
|
||||
* @param mixed $url String or array of javascript files to include
|
||||
* @param boolean $inline Whether script should be output inline or into scripts_for_layout.
|
||||
* @param boolean $once Whether or not the script should be checked for uniqueness. If true scripts will only be
|
||||
* #### Options
|
||||
*
|
||||
* - `inline` - Whether script should be output inline or into scripts_for_layout.
|
||||
* - `once` - Whether or not the script should be checked for uniqueness. If true scripts will only be
|
||||
* included once, use false to allow the same script to be included more than once per request.
|
||||
*
|
||||
* @param mixed $url String or array of javascript files to include
|
||||
* @param mixed $options Array of options, and html attributes see above. If boolean sets $options['inline'] = value
|
||||
* @return mixed String of <script /> tags or null if $inline is false or if $once is true and the file has been
|
||||
* included before.
|
||||
**/
|
||||
function script($url, $inline = true, $once = true) {
|
||||
function script($url, $options = array()) {
|
||||
if (is_bool($options)) {
|
||||
list($inline, $options) = array($options, array());
|
||||
$options['inline'] = $inline;
|
||||
}
|
||||
$options = array_merge(array('inline' => true, 'once' => true), $options);
|
||||
if (is_array($url)) {
|
||||
$out = '';
|
||||
foreach ($url as $i) {
|
||||
$out .= "\n\t" . $this->script($i, $inline, $once);
|
||||
$out .= "\n\t" . $this->script($i, $options);
|
||||
}
|
||||
if ($inline) {
|
||||
if ($options['inline']) {
|
||||
return $out . "\n";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
if ($once && isset($this->__includedScripts[$url])) {
|
||||
if ($options['once'] && isset($this->__includedScripts[$url])) {
|
||||
return null;
|
||||
}
|
||||
$this->__includedScripts[$url] = true;
|
||||
|
@ -464,7 +472,10 @@ class HtmlHelper extends AppHelper {
|
|||
$url = str_replace(JS_URL, 'cjs/', $url);
|
||||
}
|
||||
}
|
||||
$out = $this->output(sprintf($this->tags['javascriptlink'], $url));
|
||||
$inline = $options['inline'];
|
||||
unset($options['inline'], $options['once']);
|
||||
$attributes = $this->_parseAttributes($options, ' ', ' ');
|
||||
$out = $this->output(sprintf($this->tags['javascriptlink'], $url, $attributes));
|
||||
|
||||
if ($inline) {
|
||||
return $out;
|
||||
|
|
|
@ -448,16 +448,17 @@ class HtmlHelperTest extends CakeTestCase {
|
|||
touch(WWW_ROOT . 'js' . DS. '__cake_js_test.js');
|
||||
$timestamp = substr(strtotime('now'), 0, 8);
|
||||
|
||||
$result = $this->Html->script('__cake_js_test', true, false);
|
||||
$result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
|
||||
$this->assertPattern('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
|
||||
|
||||
Configure::write('debug', 0);
|
||||
Configure::write('Asset.timestamp', 'force');
|
||||
$result = $this->Html->script('__cake_js_test', true, false);
|
||||
$result = $this->Html->script('__cake_js_test', array('inline' => true, 'once' => false));
|
||||
$this->assertPattern('/__cake_js_test.js\?' . $timestamp . '[0-9]{2}"/', $result, 'Timestamp value not found %s');
|
||||
unlink(WWW_ROOT . 'js' . DS. '__cake_js_test.js');
|
||||
Configure::write('Asset.timestamp', false);
|
||||
}
|
||||
|
||||
/**
|
||||
* test that scripts added with uses() are only ever included once.
|
||||
* test script tag generation
|
||||
|
@ -498,8 +499,14 @@ class HtmlHelperTest extends CakeTestCase {
|
|||
$result = $this->Html->script(array('foo', 'bar', 'baz'));
|
||||
$this->assertNoPattern('/foo.js/', $result);
|
||||
|
||||
$result = $this->Html->script('foo', true, false);
|
||||
$result = $this->Html->script('foo', array('inline' => true, 'once' => false));
|
||||
$this->assertNotNull($result);
|
||||
|
||||
$result = $this->Html->script('jquery-1.3.2', array('defer' => true, 'encoding' => 'utf-8'));
|
||||
$expected = array(
|
||||
'script' => array('type' => 'text/javascript', 'src' => 'js/jquery-1.3.2.js', 'defer' => 'defer', 'encoding' => 'utf-8')
|
||||
);
|
||||
$this->assertTags($result, $expected);
|
||||
}
|
||||
/**
|
||||
* test Script block generation
|
||||
|
|
Loading…
Add table
Reference in a new issue