Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions libraries/src/Document/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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)]))
{
Expand Down
124 changes: 124 additions & 0 deletions plugins/system/debug/JavascriptRenderer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php
/**
* @package Joomla.Plugin
* @subpackage System.Debug
*
* @copyright Copyright (C) 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

namespace Joomla\Plugin\System\Debug;

use DebugBar\DebugBar;
use DebugBar\JavascriptRenderer as DebugBarJavascriptRenderer;

/**
* Custom JavascriptRenderer for DebugBar
*
* @since __DEPLOY_VERSION__
*/
class JavascriptRenderer extends DebugBarJavascriptRenderer
{
/**
* Class constructor.
*
* @param \DebugBar\DebugBar $debugBar DebugBar instance
* @param string $baseUrl The base URL from which assets will be served
* @param string $basePath The path which assets are relative to
*
* @since __DEPLOY_VERSION__
*/
public function __construct(DebugBar $debugBar, $baseUrl = null, $basePath = null)
{
parent::__construct($debugBar, $baseUrl, $basePath);

// Disable features that loaded by Joomla! API
$this->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 = '';
Copy link
Copy Markdown
Contributor

@dgrammatiko dgrammatiko Jun 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Fedik you do realize that this debugbar is not compatible with the csp (eg the rest of the assets)?
I will suggest that the document nonce (whatever the function name ) becomes a trait to the HTMLDocument so it can be used here as well...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it another issue for another PR, I just made it work, I not tried to fix everything here 😉

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, I’m just mentioning so maybe someone can eventually fix it


foreach ($cssFiles as $file)
{
$html .= sprintf('<link rel="stylesheet" type="text/css" href="%s">' . "\n", $file);
}

foreach ($inlineCss as $content)
{
$html .= sprintf('<style type="text/css">%s</style>' . "\n", $content);
}

foreach ($jsFiles as $file)
{
$html .= sprintf('<script type="text/javascript" src="%s" defer></script>' . "\n", $file);
}

foreach ($inlineJs as $content)
{
$html .= sprintf('<script type="module">%s</script>' . "\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 "<script type=\"module\">\nrequire(['debugbar'], function(PhpDebugBar){ $js });\n</script>\n";
}
else
{
return "<script type=\"module\">\n$js\n</script>\n";
}
}
}
10 changes: 3 additions & 7 deletions plugins/system/debug/debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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
Expand Down