diff --git a/libraries/src/Document/Document.php b/libraries/src/Document/Document.php index 5afe52ebdb16b..576ea1dba617a 100644 --- a/libraries/src/Document/Document.php +++ b/libraries/src/Document/Document.php @@ -525,6 +525,12 @@ public function addScript($url, $options = array(), $attribs = array()) $attribs['type'] = 'text/javascript'; } + // Defer all text/javascript files by default + if (!isset($attribs['defer']) && $attribs['type'] === 'text/javascript') + { + $attribs['defer'] = true; + } + $this->_scripts[$url] = isset($this->_scripts[$url]) ? array_replace($this->_scripts[$url], $attribs) : $attribs; $this->_scripts[$url]['options'] = isset($this->_scripts[$url]['options']) ? array_replace($this->_scripts[$url]['options'], $options) : $options; @@ -535,13 +541,13 @@ public function addScript($url, $options = array(), $attribs = array()) * Adds a script to the page * * @param string $content Script - * @param string $type Scripting mime (defaults to 'text/javascript') + * @param string $type Scripting mime (defaults to 'module') * * @return Document instance of $this to allow chaining * * @since 1.7.0 */ - public function addScriptDeclaration($content, $type = 'text/javascript') + public function addScriptDeclaration($content, $type = 'module') { if (!isset($this->_script[strtolower($type)])) { diff --git a/plugins/system/debug/JavascriptRenderer.php b/plugins/system/debug/JavascriptRenderer.php new file mode 100644 index 0000000000000..3d21cdfa7bf2b --- /dev/null +++ b/plugins/system/debug/JavascriptRenderer.php @@ -0,0 +1,124 @@ +setEnableJqueryNoConflict(false); + $this->disableVendor('jquery'); + $this->disableVendor('fontawesome'); + } + + /** + * Renders the html to include needed assets + * + * Only useful if Assetic is not used + * + * @return string + * + * @since __DEPLOY_VERSION__ + */ + public function renderHead() + { + list($cssFiles, $jsFiles, $inlineCss, $inlineJs, $inlineHead) = $this->getAssets(null, self::RELATIVE_URL); + $html = ''; + + foreach ($cssFiles as $file) + { + $html .= sprintf('' . "\n", $file); + } + + foreach ($inlineCss as $content) + { + $html .= sprintf('' . "\n", $content); + } + + foreach ($jsFiles as $file) + { + $html .= sprintf('' . "\n", $file); + } + + foreach ($inlineJs as $content) + { + $html .= sprintf('' . "\n", $content); + } + + foreach ($inlineHead as $content) + { + $html .= $content . "\n"; + } + + return $html; + } + + /** + * Returns the code needed to display the debug bar + * + * AJAX request should not render the initialization code. + * + * @param boolean $initialize Whether or not to render the debug bar initialization code + * @param boolean $renderStackedData Whether or not to render the stacked data + * + * @return string + * + * @since __DEPLOY_VERSION__ + */ + public function render($initialize = true, $renderStackedData = true) + { + $js = ''; + + if ($initialize) + { + $js = $this->getJsInitializationCode(); + } + + if ($renderStackedData && $this->debugBar->hasStackedData()) + { + foreach ($this->debugBar->getStackedData() as $id => $data) + { + $js .= $this->getAddDatasetCode($id, $data, '(stacked)'); + } + } + + $suffix = !$initialize ? '(ajax)' : null; + $js .= $this->getAddDatasetCode($this->debugBar->getCurrentRequestId(), $this->debugBar->getData(), $suffix); + + if ($this->useRequireJs) + { + return "\n"; + } + else + { + return "\n"; + } + } +} diff --git a/plugins/system/debug/debug.php b/plugins/system/debug/debug.php index 81612928c85bd..1df6b8f8cd643 100644 --- a/plugins/system/debug/debug.php +++ b/plugins/system/debug/debug.php @@ -32,6 +32,7 @@ use Joomla\Plugin\System\Debug\DataCollector\ProfileCollector; use Joomla\Plugin\System\Debug\DataCollector\QueryCollector; use Joomla\Plugin\System\Debug\DataCollector\SessionCollector; +use Joomla\Plugin\System\Debug\JavascriptRenderer; use Joomla\Plugin\System\Debug\Storage\FileStorage; /** @@ -201,7 +202,7 @@ public function onAfterDispatch() { // Use our own jQuery and fontawesome instead of the debug bar shipped version $assetManager = $this->app->getDocument()->getWebAssetManager(); - $assetManager->enableAsset('jquery-noconflict'); + $assetManager->enableAsset('jquery'); $assetManager->enableAsset('fontawesome-free'); HTMLHelper::_('stylesheet', 'plg_system_debug/debug.css', array('version' => 'auto', 'relative' => true)); @@ -283,16 +284,11 @@ public function onAfterRespond() $this->debugBar->addCollector(new LanguageErrorsCollector($this->params)); } - $debugBarRenderer = $this->debugBar->getJavascriptRenderer(); + $debugBarRenderer = new JavascriptRenderer($this->debugBar, Uri::root(true) . '/media/vendor/debugbar/'); $openHandlerUrl = Uri::base(true) . '/index.php?option=com_ajax&plugin=debug&group=system&format=raw&action=openhandler'; $openHandlerUrl .= '&' . Session::getFormToken() . '=1'; $debugBarRenderer->setOpenHandlerUrl($openHandlerUrl); - $debugBarRenderer->setBaseUrl(Uri::root(true) . '/media/vendor/debugbar/'); - - $debugBarRenderer->disableVendor('jquery'); - $debugBarRenderer->setEnableJqueryNoConflict(false); - $debugBarRenderer->disableVendor('fontawesome'); /** * @todo disable highlightjs from the DebugBar, import it through NPM