[FEATURE] Add async property to JavaScript files 00/34000/7
authorArnd Messer <info@arndmesser.de>
Tue, 11 Nov 2014 13:40:09 +0000 (14:40 +0100)
committerFrank Nägler <typo3@naegler.net>
Mon, 12 Jan 2015 13:04:11 +0000 (14:04 +0100)
Add a property 'async="async"' to JavaScript files via TypoScript
page.includeJSlibs.<array>.async = 1

This patch affects the TypoScript PAGE properties
* includeJSlibs
* includeJSFooterlibs
* includeJS
* includeJSFooter

Resolves: #28382
Releases: master
Change-Id: I9a86edaba118370d4df9f11216998d808276b634
Reviewed-on: http://review.typo3.org/34000
Reviewed-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Tested-by: Mathias Schreiber <mathias.schreiber@wmdb.de>
Reviewed-by: Frank Nägler <typo3@naegler.net>
Tested-by: Frank Nägler <typo3@naegler.net>
typo3/sysext/core/Classes/Page/PageRenderer.php
typo3/sysext/core/Documentation/Changelog/master/Feature-28382-AddAsyncPropertyToJavaScriptFiles.rst [new file with mode: 0644]
typo3/sysext/frontend/Classes/Page/PageGenerator.php

index 9a0e019..9e8d3b2 100644 (file)
@@ -1192,9 +1192,10 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
         * @param string $allWrap
         * @param bool $excludeFromConcatenation
         * @param string $splitChar The char used to split the allWrap value, default is "|"
+        * @param bool $async Flag if property 'async="async"' should be added to JavaScript tags
         * @return void
         */
-       public function addJsLibrary($name, $file, $type = 'text/javascript', $compress = FALSE, $forceOnTop = FALSE, $allWrap = '', $excludeFromConcatenation = FALSE, $splitChar = '|') {
+       public function addJsLibrary($name, $file, $type = 'text/javascript', $compress = FALSE, $forceOnTop = FALSE, $allWrap = '', $excludeFromConcatenation = FALSE, $splitChar = '|', $async = FALSE) {
                if (!$type) {
                        $type = 'text/javascript';
                }
@@ -1207,7 +1208,8 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
                                'forceOnTop' => $forceOnTop,
                                'allWrap' => $allWrap,
                                'excludeFromConcatenation' => $excludeFromConcatenation,
-                               'splitChar' => $splitChar
+                               'splitChar' => $splitChar,
+                               'async' => $async
                        );
                }
        }
@@ -1223,9 +1225,10 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
         * @param string $allWrap
         * @param bool $excludeFromConcatenation
         * @param string $splitChar The char used to split the allWrap value, default is "|"
+        * @param bool $async Flag if property 'async="async"' should be added to JavaScript tags
         * @return void
         */
-       public function addJsFooterLibrary($name, $file, $type = 'text/javascript', $compress = FALSE, $forceOnTop = FALSE, $allWrap = '', $excludeFromConcatenation = FALSE, $splitChar = '|') {
+       public function addJsFooterLibrary($name, $file, $type = 'text/javascript', $compress = FALSE, $forceOnTop = FALSE, $allWrap = '', $excludeFromConcatenation = FALSE, $splitChar = '|', $async = FALSE) {
                if (!$type) {
                        $type = 'text/javascript';
                }
@@ -1238,7 +1241,8 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
                                'forceOnTop' => $forceOnTop,
                                'allWrap' => $allWrap,
                                'excludeFromConcatenation' => $excludeFromConcatenation,
-                               'splitChar' => $splitChar
+                               'splitChar' => $splitChar,
+                               'async' => $async
                        );
                }
        }
@@ -1253,9 +1257,10 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
         * @param string $allWrap
         * @param bool $excludeFromConcatenation
         * @param string $splitChar The char used to split the allWrap value, default is "|"
+        * @param bool $async Flag if property 'async="async"' should be added to JavaScript tags
         * @return void
         */
-       public function addJsFile($file, $type = 'text/javascript', $compress = TRUE, $forceOnTop = FALSE, $allWrap = '', $excludeFromConcatenation = FALSE, $splitChar = '|') {
+       public function addJsFile($file, $type = 'text/javascript', $compress = TRUE, $forceOnTop = FALSE, $allWrap = '', $excludeFromConcatenation = FALSE, $splitChar = '|', $async = FALSE) {
                if (!$type) {
                        $type = 'text/javascript';
                }
@@ -1271,7 +1276,8 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
                                'forceOnTop' => $forceOnTop,
                                'allWrap' => $allWrap,
                                'excludeFromConcatenation' => $excludeFromConcatenation,
-                               'splitChar' => $splitChar
+                               'splitChar' => $splitChar,
+                               'async' => $async
                        );
                }
        }
@@ -1286,9 +1292,10 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
         * @param string $allWrap
         * @param bool $excludeFromConcatenation
         * @param string $splitChar The char used to split the allWrap value, default is "|"
+        * @param bool $async Flag if property 'async="async"' should be added to JavaScript tags
         * @return void
         */
-       public function addJsFooterFile($file, $type = 'text/javascript', $compress = TRUE, $forceOnTop = FALSE, $allWrap = '', $excludeFromConcatenation = FALSE, $splitChar = '|') {
+       public function addJsFooterFile($file, $type = 'text/javascript', $compress = TRUE, $forceOnTop = FALSE, $allWrap = '', $excludeFromConcatenation = FALSE, $splitChar = '|', $async = FALSE) {
                if (!$type) {
                        $type = 'text/javascript';
                }
@@ -1304,7 +1311,8 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
                                'forceOnTop' => $forceOnTop,
                                'allWrap' => $allWrap,
                                'excludeFromConcatenation' => $excludeFromConcatenation,
-                               'splitChar' => $splitChar
+                               'splitChar' => $splitChar,
+                               'async' => $async
                        );
                }
        }
@@ -2460,7 +2468,8 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
                        foreach ($this->jsLibs as $properties) {
                                $properties['file'] = GeneralUtility::resolveBackPath($properties['file']);
                                $properties['file'] = GeneralUtility::createVersionNumberedFilename($properties['file']);
-                               $tag = '<script src="' . htmlspecialchars($properties['file']) . '" type="' . htmlspecialchars($properties['type']) . '"></script>';
+                               $async = ($properties['async']) ? ' async="async"' : '';
+                               $tag = '<script src="' . htmlspecialchars($properties['file']) . '" type="' . htmlspecialchars($properties['type']) . '"' . $async . '></script>';
                                if ($properties['allWrap']) {
                                        $wrapArr = explode($properties['splitChar'] ?: '|', $properties['allWrap'], 2);
                                        $tag = $wrapArr[0] . $tag . $wrapArr[1];
@@ -2500,7 +2509,8 @@ class PageRenderer implements \TYPO3\CMS\Core\SingletonInterface {
                        foreach ($this->jsFiles as $file => $properties) {
                                $file = GeneralUtility::resolveBackPath($file);
                                $file = GeneralUtility::createVersionNumberedFilename($file);
-                               $tag = '<script src="' . htmlspecialchars($file) . '" type="' . htmlspecialchars($properties['type']) . '"></script>';
+                               $async = ($properties['async']) ? ' async="async"' : '';
+                               $tag = '<script src="' . htmlspecialchars($file) . '" type="' . htmlspecialchars($properties['type']) . '"' . $async . '></script>';
                                if ($properties['allWrap']) {
                                        $wrapArr = explode($properties['splitChar'] ?: '|', $properties['allWrap'], 2);
                                        $tag = $wrapArr[0] . $tag . $wrapArr[1];
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-28382-AddAsyncPropertyToJavaScriptFiles.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-28382-AddAsyncPropertyToJavaScriptFiles.rst
new file mode 100644 (file)
index 0000000..a9c342d
--- /dev/null
@@ -0,0 +1,32 @@
+========================================================
+Feature: #28382 - Add async property to JavaScript files
+========================================================
+
+Description
+===========
+
+Add a property ``async="async"`` to JavaScript files via TypoScript
+
+``page.includeJSlibs.<array>.async = 1``
+
+This patch affects the TypoScript PAGE properties
+
+* includeJSlibs
+* includeJSFooterlibs
+* includeJS
+* includeJSFooter
+
+Usage:
+------
+
+.. code-block:: typoscript
+
+       page {
+               includeJS {
+                       jsFile = /Path/To/jsFile.js
+                       jsFile.async = 1
+               }
+       }
+
+..
+
index 45b15d6..984e498 100644 (file)
@@ -669,7 +669,8 @@ class PageGenerator {
                                                        $jsFileConfig['forceOnTop'] ? TRUE : FALSE,
                                                        $jsFileConfig['allWrap'],
                                                        $jsFileConfig['excludeFromConcatenation'] ? TRUE : FALSE,
-                                                       $jsFileConfig['allWrap.']['splitChar']
+                                                       $jsFileConfig['allWrap.']['splitChar'],
+                                                       $jsFileConfig['async'] ? TRUE : FALSE
                                                );
                                                unset($jsFileConfig);
                                        }
@@ -697,7 +698,8 @@ class PageGenerator {
                                                        $jsFileConfig['forceOnTop'] ? TRUE : FALSE,
                                                        $jsFileConfig['allWrap'],
                                                        $jsFileConfig['excludeFromConcatenation'] ? TRUE : FALSE,
-                                                       $jsFileConfig['allWrap.']['splitChar']
+                                                       $jsFileConfig['allWrap.']['splitChar'],
+                                                       $jsFileConfig['async'] ? TRUE : FALSE
                                                );
                                                unset($jsFileConfig);
                                        }
@@ -725,7 +727,8 @@ class PageGenerator {
                                                        $jsConfig['forceOnTop'] ? TRUE : FALSE,
                                                        $jsConfig['allWrap'],
                                                        $jsConfig['excludeFromConcatenation'] ? TRUE : FALSE,
-                                                       $jsConfig['allWrap.']['splitChar']
+                                                       $jsConfig['allWrap.']['splitChar'],
+                                                       $jsConfig['async'] ? TRUE : FALSE
                                                );
                                                unset($jsConfig);
                                        }
@@ -752,7 +755,8 @@ class PageGenerator {
                                                        $jsConfig['forceOnTop'] ? TRUE : FALSE,
                                                        $jsConfig['allWrap'],
                                                        $jsConfig['excludeFromConcatenation'] ? TRUE : FALSE,
-                                                       $jsConfig['allWrap.']['splitChar']
+                                                       $jsConfig['allWrap.']['splitChar'],
+                                                       $jsConfig['async'] ? TRUE : FALSE
                                                );
                                                unset($jsConfig);
                                        }