-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RFC] Script options without inline JavaScript #11671
Changes from all commits
47f5c97
6f6c6f7
52df008
14151a3
fdcb48e
1fc35e5
be7c703
f48a573
dae97f2
0b02d96
cfe2bed
b528028
a97c69d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
|
@@ -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">'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not? <script type="application/json" id="joomla-script-options" data-loaded="0"> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it require more action 😉 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok them, didn't notice you needed multiple elements |
||
|
||
$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 | ||
|
@@ -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) | ||
{ | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is going to be considered this load statement should be before the compile event otherwise you've made it impossible for those who insist on unsetting core media to unset this call's dependencies.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you will be laugh, but on start I made exactly as you just said,
but then thought, if
ScriptOptions
added while 'onBeforeCompileHead', but was empty before, then we got a problem 😉There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just placing the if part between lines 57-58 should be enough?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already problems when people insist on unsetting the core loaded media (JCaption, MooTools, and core.js are the common ones I see already). We give people too powerful of a toolkit, not our fault when they abuse them. The only way to "fix" this is to make JDocument's assets APIs immutable (once it's in it's in unless you're getting your hands dirty with Reflection).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem if someone unset core.js, but
ScriptOptions
not empty, then the codeJoomla.getOptions()
will be broken.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that is what is going on here 😉
behavior.core
will be called only if$document->getScriptOptions()
not emptyThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so for unset
core.js
you need to make empty$doc->scriptOptions
that will lead to broken site 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem here isn't unique to the ScriptOptions. So I don't agree with having a media load statement at a point after we allow the user to manipulate what would be loaded because it's essentially giving preferential treatment to that specific file.
Either way this issue is bigger than this PR. For now I would say move it before the event and just like every other broken JavaScript statement that lives on sites because people are unloading dependencies, live with it or stop unloading crap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i agree with mbabker. if people unload core.js well ... it's their choice ... so they have to deal with the consequences.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, no problem, I will change it