Skip to content

Commit

Permalink
Allow adding script options without inline JavaScript. Fixes #11671
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik authored and wilsonge committed Sep 3, 2016
1 parent a3808b3 commit 611cb2e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 32 deletions.
50 changes: 20 additions & 30 deletions libraries/joomla/document/renderer/html/head.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public function fetchHead($document)
$document->_metaTags['name']['tags'] = implode(', ', $tagsHelper->getTagNames($document->_metaTags['name']['tags']));
}

if ($document->getScriptOptions())
{
JHtml::_('behavior.core');
}

// Trigger the onBeforeCompileHead event
$app = JFactory::getApplication();
$app->triggerEvent('onBeforeCompileHead');
Expand Down Expand Up @@ -184,6 +189,21 @@ public function fetchHead($document)
$buffer .= $tab . '</style>' . $lnEnd;
}

// Generate scripts options
$scriptOptions = $document->getScriptOptions();

if (!empty($scriptOptions))
{
$buffer .= $tab . '<script type="application/json" class="joomla-script-options new">';

$prettyPrint = (JDEBUG && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);
$jsonOptions = json_encode($scriptOptions, $prettyPrint);
$jsonOptions = $jsonOptions ? $jsonOptions : '{}';

$buffer .= $jsonOptions;
$buffer .= '</script>' . $lnEnd;
}

$defaultJsMimes = array('text/javascript', 'application/javascript', 'text/x-javascript', 'application/x-javascript');

// Generate script file links
Expand Down Expand Up @@ -219,36 +239,6 @@ public function fetchHead($document)
$buffer .= '></script>' . $lnEnd;
}

// Generate scripts options
$scriptOptions = $document->getScriptOptions();

if (!empty($scriptOptions))
{
$buffer .= $tab . '<script type="text/javascript">' . $lnEnd;

// This is for full XHTML support.
if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '//<![CDATA[' . $lnEnd;
}

$pretyPrint = (JDEBUG && defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);
$jsonOptions = json_encode($scriptOptions, $pretyPrint);
$jsonOptions = $jsonOptions ? $jsonOptions : '{}';

// TODO: use .extend(Joomla.optionsStorage, options) when it will be safe
$buffer .= $tab . 'var Joomla = Joomla || {};' . $lnEnd;
$buffer .= $tab . 'Joomla.optionsStorage = ' . $jsonOptions . ';' . $lnEnd;

// See above note
if ($document->_mime != 'text/html')
{
$buffer .= $tab . $tab . '//]]>' . $lnEnd;
}

$buffer .= $tab . '</script>' . $lnEnd;
}

// Generate script declarations
foreach ($document->_script as $type => $content)
{
Expand Down
70 changes: 69 additions & 1 deletion media/system/js/core-uncompressed.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,74 @@ Joomla.editors.instances = Joomla.editors.instances || {};
}
};

/**
* Joomla options storage
*
* @type {{}}
*
* @since __DEPLOY_VERSION__
*/
Joomla.optionsStorage = Joomla.optionsStorage || null;

/**
* Get script(s) options
*
* @param {String} key Name in Storage
* @param mixed def Default value if nothing found
*
* @return mixed
*
* @since __DEPLOY_VERSION__
*/
Joomla.getOptions = function( key, def ) {
// Load options if they not exists
if (!Joomla.optionsStorage) {
Joomla.loadOptions();
}

return Joomla.optionsStorage[key] !== undefined ? Joomla.optionsStorage[key] : def;
};

/**
* Load new options from given options object or from Element
*
* @param {Object|undefined} options The options object to load. Eg {"com_foobar" : {"option1": 1, "option2": 2}}
*
* @since __DEPLOY_VERSION__
*/
Joomla.loadOptions = function( options ) {
// Load form the script container
if (!options) {
var elements = document.querySelectorAll('.joomla-script-options.new'),
str, element, option;

for (var i = 0, l = elements.length; i < l; i++) {
element = elements[i];
str = element.text || element.textContent;
option = JSON.parse(str);

option ? Joomla.loadOptions(option) : null;

element.className = element.className.replace(' new', ' loaded');
}

return;
}

// Initial loading
if (!Joomla.optionsStorage) {
Joomla.optionsStorage = options;
}
// Merge with existing
else {
for (var p in options) {
if (options.hasOwnProperty(p)) {
Joomla.optionsStorage[p] = options[p];
}
}
}
};

/**
* Method to replace all request tokens on the page with a new one.
* Used in Joomla Installation
Expand Down Expand Up @@ -614,7 +682,7 @@ Joomla.editors.instances = Joomla.editors.instances || {};
parentElement.appendChild(loadingDiv);
}
// Show or hide the layer.
else
else
{
if (!document.getElementById('loading-logo'))
{
Expand Down
2 changes: 1 addition & 1 deletion media/system/js/core.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions tests/javascript/core/fixtures/fixture.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,11 @@
<form id="table-ordering-test-form">
</form>
</div>
<div id="get-options">
<script type="application/json" class="joomla-script-options new">{
"com_foobar": ["my options"],
"com_foobar2": "Alert message!",
"com_foobar3": false
}</script>
</div>
</div>
Loading

0 comments on commit 611cb2e

Please sign in to comment.