Skip to content
This repository has been archived by the owner on Nov 3, 2023. It is now read-only.

Commit

Permalink
Add the |async flag to $GLOBALS['TL_JAVASCRIPT'] (see #7172)
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed Sep 5, 2014
1 parent 5c3f0b0 commit 014a334
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 42 deletions.
7 changes: 4 additions & 3 deletions contao/classes/BackendTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public function output()

foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet)
{
list($stylesheet, $media) = explode('|', $stylesheet);
$strStyleSheets .= '<link rel="stylesheet" href="' . $this->addStaticUrlTo($stylesheet) . '"' . (($media != '' && $media != 'all') ? ' media="' . $media . '"' : '') . '>' . "\n";
$options = \String::resolveFlaggedUrl($stylesheet);
$strStyleSheets .= \Template::generateStyleTag($this->addStaticUrlTo($stylesheet), $options->media);
}

$this->stylesheets = $strStyleSheets;
Expand All @@ -86,7 +86,8 @@ public function output()

foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript)
{
$strJavaScripts .= '<script src="' . $this->addStaticUrlTo($javascript) . '"></script>' . "\n";
$options = \String::resolveFlaggedUrl($javascript);
$strJavaScripts .= \Template::generateScriptTag($this->addStaticUrlTo($javascript), false, $options->async) . "\n";
}

$this->javascripts = $strJavaScripts;
Expand Down
2 changes: 1 addition & 1 deletion contao/elements/ContentCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function compile()
}

// Add the style sheet
$GLOBALS['TL_CSS'][] = 'assets/highlighter/' . $GLOBALS['TL_ASSETS']['HIGHLIGHTER'] . '/shCore.css||static';
$GLOBALS['TL_CSS'][] = 'assets/highlighter/' . $GLOBALS['TL_ASSETS']['HIGHLIGHTER'] . '/shCore.css|static';

// Add the JavaScripts
$GLOBALS['TL_HIGHLIGHTER'][] = 'assets/highlighter/' . $GLOBALS['TL_ASSETS']['HIGHLIGHTER'] . '/XRegExp.js';
Expand Down
28 changes: 14 additions & 14 deletions contao/library/Contao/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1897,15 +1897,15 @@ public static function replaceDynamicScriptTags($strBuffer)
{
foreach (array_unique($GLOBALS['TL_CSS']) as $stylesheet)
{
list($stylesheet, $media, $mode) = explode('|', $stylesheet);
$options = \String::resolveFlaggedUrl($stylesheet);

if ($mode == 'static')
if ($options->static)
{
$objCombiner->add($stylesheet, filemtime(TL_ROOT . '/' . $stylesheet), $media);
$objCombiner->add($stylesheet, filemtime(TL_ROOT . '/' . $stylesheet), $options->media);
}
else
{
$strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $media, $blnXhtml) . "\n";
$strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media, $blnXhtml) . "\n";
}
}
}
Expand All @@ -1915,20 +1915,20 @@ public static function replaceDynamicScriptTags($strBuffer)
{
foreach (array_unique($GLOBALS['TL_USER_CSS']) as $stylesheet)
{
list($stylesheet, $media, $mode, $version) = explode('|', $stylesheet);
$options = \String::resolveFlaggedUrl($stylesheet);

if (!$version)
if ($options->static)
{
$version = filemtime(TL_ROOT . '/' . $stylesheet);
}
if ($options->mtime === null)
{
$options->mtime = filemtime(TL_ROOT . '/' . $stylesheet);
}

if ($mode == 'static')
{
$objCombiner->add($stylesheet, $version, $media);
$objCombiner->add($stylesheet, $options->mtime, $options->media);
}
else
{
$strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $media, $blnXhtml) . "\n";
$strScripts .= \Template::generateStyleTag(static::addStaticUrlTo($stylesheet), $options->media, $blnXhtml) . "\n";
}
}
}
Expand All @@ -1949,9 +1949,9 @@ public static function replaceDynamicScriptTags($strBuffer)

foreach (array_unique($GLOBALS['TL_JAVASCRIPT']) as $javascript)
{
list($javascript, $mode) = explode('|', $javascript);
$options = \String::resolveFlaggedUrl($javascript);

if ($mode == 'static')
if ($options->static)
{
$objCombiner->add($javascript, filemtime(TL_ROOT . '/' . $javascript));
}
Expand Down
52 changes: 52 additions & 0 deletions contao/library/Contao/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,58 @@ public static function insertTagToSrc($data)
}


/**
* Resolve a flagged URL such as assets/js/core.js|static|10184084
*
* @param string $url The URL
*
* @return \stdClass The options object
*/
public static function resolveFlaggedUrl(&$url)
{
$options = new \stdClass();

// Defaults
$options->static = false;
$options->media = null;
$options->mtime = null;
$options->async = false;

$chunks = explode('|', $url);

// Remove the flags from the URL
$url = $chunks[0];

for ($i=1; $i<count($chunks); $i++)
{
switch ($chunks[$i])
{
case 'static':
$options->static = true;
break;

case 'async':
$options->async = true;
break;

case empty($chunks[$i]):
// Ignore
break;

case is_numeric($chunks[$i]):
$options->mtime = $chunks[$i];
break;

default:
$options->media = $chunks[$i];
break;
}
}

return $options;
}


/**
* Prevent direct instantiation (Singleton)
*
Expand Down
9 changes: 5 additions & 4 deletions contao/library/Contao/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,9 @@ public function minifyHtml($strHtml)
*
* @return string The markup string
*/
public static function generateStyleTag($href, $media, $xhtml=false)
public static function generateStyleTag($href, $media=null, $xhtml=false)
{
return '<link' . ($xhtml ? ' type="text/css"' : '') . ' rel="stylesheet" href="' . $href . '"' . (($media != '' && $media != 'all') ? ' media="' . $media . '"' : '') . ($xhtml ? ' />' : '>');
return '<link' . ($xhtml ? ' type="text/css"' : '') . ' rel="stylesheet" href="' . $href . '"' . (($media && $media != 'all') ? ' media="' . $media . '"' : '') . ($xhtml ? ' />' : '>');
}


Expand Down Expand Up @@ -473,12 +473,13 @@ public static function generateInlineStyle($script, $xhtml=false)
*
* @param string $src The script path
* @param boolean $xhtml True if the output shall be XHTML compliant
* @param boolean $async True to add the async attribute
*
* @return string The markup string
*/
public static function generateScriptTag($src, $xhtml=false)
public static function generateScriptTag($src, $xhtml=false, $async=false)
{
return '<script' . ($xhtml ? ' type="text/javascript"' : '') . ' src="' . $src . '"></script>';
return '<script' . ($xhtml ? ' type="text/javascript"' : '') . ' src="' . $src . '"' . ($async && !$xhtml ? ' async' : '') . '></script>';
}


Expand Down
2 changes: 1 addition & 1 deletion contao/templates/jquery/j_colorbox.html5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the colorbox style sheet
$GLOBALS['TL_CSS'][] = 'assets/jquery/colorbox/'. $GLOBALS['TL_ASSETS']['COLORBOX'] .'/css/colorbox.min.css||static';
$GLOBALS['TL_CSS'][] = 'assets/jquery/colorbox/'. $GLOBALS['TL_ASSETS']['COLORBOX'] .'/css/colorbox.min.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/jquery/j_colorbox.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the colorbox style sheet
$GLOBALS['TL_CSS'][] = 'assets/jquery/colorbox/'. $GLOBALS['TL_ASSETS']['COLORBOX'] .'/css/colorbox.min.css||static';
$GLOBALS['TL_CSS'][] = 'assets/jquery/colorbox/'. $GLOBALS['TL_ASSETS']['COLORBOX'] .'/css/colorbox.min.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/jquery/j_mediaelement.html5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the mediaelement style sheet
$GLOBALS['TL_CSS'][] = 'assets/jquery/mediaelement/'. $GLOBALS['TL_ASSETS']['MEDIAELEMENT'] .'/css/mediaelementplayer.min.css||static';
$GLOBALS['TL_CSS'][] = 'assets/jquery/mediaelement/'. $GLOBALS['TL_ASSETS']['MEDIAELEMENT'] .'/css/mediaelementplayer.min.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/jquery/j_mediaelement.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the mediaelement style sheet
$GLOBALS['TL_CSS'][] = 'assets/jquery/mediaelement/'. $GLOBALS['TL_ASSETS']['MEDIAELEMENT'] .'/css/mediaelementplayer.min.css||static';
$GLOBALS['TL_CSS'][] = 'assets/jquery/mediaelement/'. $GLOBALS['TL_ASSETS']['MEDIAELEMENT'] .'/css/mediaelementplayer.min.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/jquery/j_slider.html5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the swipe style sheet
$GLOBALS['TL_CSS'][] = 'assets/swipe/'. $GLOBALS['TL_ASSETS']['SWIPE'] .'/css/swipe.min.css||static';
$GLOBALS['TL_CSS'][] = 'assets/swipe/'. $GLOBALS['TL_ASSETS']['SWIPE'] .'/css/swipe.min.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/jquery/j_slider.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the swipe style sheet
$GLOBALS['TL_CSS'][] = 'assets/swipe/'. $GLOBALS['TL_ASSETS']['SWIPE'] .'/css/swipe.min.css||static';
$GLOBALS['TL_CSS'][] = 'assets/swipe/'. $GLOBALS['TL_ASSETS']['SWIPE'] .'/css/swipe.min.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/jquery/j_tablesort.html5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the tablesorter style sheet
$GLOBALS['TL_CSS'][] = 'assets/jquery/tablesorter/' . $GLOBALS['TL_ASSETS']['TABLESORTER'] . '/css/tablesorter.css||static';
$GLOBALS['TL_CSS'][] = 'assets/jquery/tablesorter/' . $GLOBALS['TL_ASSETS']['TABLESORTER'] . '/css/tablesorter.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/jquery/j_tablesort.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the tablesorter style sheet
$GLOBALS['TL_CSS'][] = 'assets/jquery/tablesorter/' . $GLOBALS['TL_ASSETS']['TABLESORTER'] . '/css/tablesorter.css||static';
$GLOBALS['TL_CSS'][] = 'assets/jquery/tablesorter/' . $GLOBALS['TL_ASSETS']['TABLESORTER'] . '/css/tablesorter.css|static';

?>

Expand Down
4 changes: 2 additions & 2 deletions contao/templates/mootools/moo_chosen.html5
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

// CSS files
$GLOBALS['TL_CSS'][] = 'assets/mootools/chosen/chosen.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/stylect/css/stylect.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/chosen/chosen.css|static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/stylect/css/stylect.css|static';

// JavaScript files
$objCombiner = new Combiner();
Expand Down
4 changes: 2 additions & 2 deletions contao/templates/mootools/moo_chosen.xhtml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

// CSS files
$GLOBALS['TL_CSS'][] = 'assets/mootools/chosen/chosen.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/stylect/css/stylect.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/chosen/chosen.css|static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/stylect/css/stylect.css|static';

// JavaScript files
$objCombiner = new Combiner();
Expand Down
2 changes: 1 addition & 1 deletion contao/templates/mootools/moo_mediabox.html5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the mediabox style sheet
$GLOBALS['TL_CSS'][] = 'assets/mootools/mediabox/'. $GLOBALS['TL_ASSETS']['MEDIABOX'] .'/css/mediaboxAdvBlack21.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/mediabox/'. $GLOBALS['TL_ASSETS']['MEDIABOX'] .'/css/mediaboxAdvBlack21.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/mootools/moo_mediabox.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the mediabox style sheet
$GLOBALS['TL_CSS'][] = 'assets/mootools/mediabox/'. $GLOBALS['TL_ASSETS']['MEDIABOX'] .'/css/mediaboxAdvBlack21.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/mediabox/'. $GLOBALS['TL_ASSETS']['MEDIABOX'] .'/css/mediaboxAdvBlack21.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/mootools/moo_slider.html5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the swipe style sheet
$GLOBALS['TL_CSS'][] = 'assets/swipe/'. $GLOBALS['TL_ASSETS']['SWIPE'] .'/css/swipe.min.css||static';
$GLOBALS['TL_CSS'][] = 'assets/swipe/'. $GLOBALS['TL_ASSETS']['SWIPE'] .'/css/swipe.min.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/mootools/moo_slider.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the swipe style sheet
$GLOBALS['TL_CSS'][] = 'assets/swipe/'. $GLOBALS['TL_ASSETS']['SWIPE'] .'/css/swipe.min.css||static';
$GLOBALS['TL_CSS'][] = 'assets/swipe/'. $GLOBALS['TL_ASSETS']['SWIPE'] .'/css/swipe.min.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/mootools/moo_slimbox.html5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add slimbox style sheet
$GLOBALS['TL_CSS'][] = 'assets/mootools/slimbox/' . $GLOBALS['TL_ASSETS']['SLIMBOX'] . '/css/slimbox.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/slimbox/' . $GLOBALS['TL_ASSETS']['SLIMBOX'] . '/css/slimbox.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/mootools/moo_slimbox.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add slimbox style sheet
$GLOBALS['TL_CSS'][] = 'assets/mootools/slimbox/' . $GLOBALS['TL_ASSETS']['SLIMBOX'] . '/css/slimbox.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/slimbox/' . $GLOBALS['TL_ASSETS']['SLIMBOX'] . '/css/slimbox.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/mootools/moo_tablesort.html5
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the tablesorter style sheet
$GLOBALS['TL_CSS'][] = 'assets/mootools/tablesort/css/tablesort.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/tablesort/css/tablesort.css|static';

?>

Expand Down
2 changes: 1 addition & 1 deletion contao/templates/mootools/moo_tablesort.xhtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

// Add the tablesorter style sheet
$GLOBALS['TL_CSS'][] = 'assets/mootools/tablesort/css/tablesort.css||static';
$GLOBALS['TL_CSS'][] = 'assets/mootools/tablesort/css/tablesort.css|static';

?>

Expand Down

0 comments on commit 014a334

Please sign in to comment.