[TASK] Replace JavaScriptEncoder with json_encode() 45/26545/12
authorMichiel Roos <michiel@maxserv.nl>
Sun, 22 Dec 2013 18:51:21 +0000 (19:51 +0100)
committerStefan Neufeind <typo3.neufeind@speedpartner.de>
Tue, 28 Jan 2014 23:38:22 +0000 (00:38 +0100)
The JavaScriptEncoder class is used a lot in the TYPO3 backend. It does
expensive character conversions, character by character.

It would improve performance a lot if we use PHP native json_encode to do
the encoding for us.

Change-Id: I3cb3bd6d887d49565fda496f9b13852cbeb9dfa1
Resolves: #54560
Releases: 6.2
Reviewed-on: https://review.typo3.org/26545
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Michiel Roos
Tested-by: Michiel Roos
Reviewed-by: Marcin Sągol
Tested-by: Marcin Sągol
Reviewed-by: Stefan Neufeind
Tested-by: Stefan Neufeind
typo3/sysext/core/Classes/Utility/DebugUtility.php
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index 0b4c13e..b3541ae 100644 (file)
@@ -51,11 +51,6 @@ class DebugUtility {
        </table>
        ';
 
-       /**
-        * @var \TYPO3\CMS\Core\Encoder\JavaScriptEncoder
-        */
-       static protected $javaScriptEncoder;
-
        /**
         * Debug
         *
@@ -77,9 +72,9 @@ class DebugUtility {
                        $tabHeader = $header ?: 'Debug';
                        $script = '
                                (function debug() {
-                                       var debugMessage = "' . static::getJavaScriptEncoder()->encode($debug) . '";
-                                       var header = "' . static::getJavaScriptEncoder()->encode($tabHeader) . '";
-                                       var group = "' . static::getJavaScriptEncoder()->encode($group) . '";
+                                       var debugMessage = ' . GeneralUtility::quoteJSvalue($debug) . ';
+                                       var header = ' . GeneralUtility::quoteJSvalue($tabHeader) . ';
+                                       var group = ' . GeneralUtility::quoteJSvalue($group) . ';
 
                                        if (typeof Ext !== "object" && (top && typeof top.Ext !== "object")) {
                                                document.write(debugMessage);
@@ -133,7 +128,6 @@ class DebugUtility {
         * @return string
         */
        static public function convertVariableToString($variable) {
-               $string = '';
                if (is_array($variable)) {
                        $string = self::viewArray($variable);
                } elseif (is_object($variable)) {
@@ -159,9 +153,9 @@ class DebugUtility {
                $debugString = self::convertVariableToString($debugVariable);
                $script = '
                        (function debug() {
-                               var debugMessage = "' . static::getJavaScriptEncoder()->encode($debugString) . '",
-                                       header = "' . static::getJavaScriptEncoder()->encode($header) . '",
-                                       group = "' . static::getJavaScriptEncoder()->encode($group) . '",
+                               var debugMessage = ' . GeneralUtility::quoteJSvalue($debugString) . ',
+                                       header = ' . GeneralUtility::quoteJSvalue($header) . ',
+                                       group = ' . GeneralUtility::quoteJSvalue($group) . ',
 
                                        browserWindow = function(debug, header, group) {
                                                var newWindow = window.open("", "TYPO3DebugWindow_" + group,
@@ -350,14 +344,4 @@ class DebugUtility {
                echo self::viewArray($array_in);
        }
 
-       /**
-        * @return \TYPO3\CMS\Core\Encoder\JavaScriptEncoder
-        */
-       static protected function getJavaScriptEncoder() {
-               if (empty(self::$javaScriptEncoder)) {
-                       self::$javaScriptEncoder = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Encoder\\JavaScriptEncoder');
-               }
-
-               return self::$javaScriptEncoder;
-       }
 }
index 4de6a74..4f0e878 100644 (file)
@@ -5122,8 +5122,19 @@ Connection: close
         * @return string the encoded value already quoted (with single quotes),
         */
        static public function quoteJSvalue($value) {
-               $escapedValue = static::makeInstance('TYPO3\\CMS\\Core\\Encoder\\JavaScriptEncoder')->encode($value);
-               return '\'' . $escapedValue . '\'';
+               return strtr(
+                       json_encode((string)$value, JSON_HEX_AMP|JSON_HEX_APOS|JSON_HEX_QUOT|JSON_HEX_TAG),
+                       array(
+                               '"' => '\'',
+                               '/' => '\\u002F',
+                               '\\\\\\' => '\u005C',
+                               ' ' => '\\u0020',
+                               '!' => '\\u0021',
+                               '\\t' => '\\u0009',
+                               '\\n' => '\\u000A',
+                               '\\r' => '\\u000D'
+                       )
+               );
        }
 
        /**
index 525ade9..56c6431 100644 (file)
@@ -2335,13 +2335,13 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                                'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789',
                                'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
                        ),
-                       'Angel brackets and ampersand are encoded' => array(
+                       'Angle brackets and ampersand are encoded' => array(
                                '<>&',
-                               '\\x3C\\x3E\\x26'
+                               '\\u003C\\u003E\\u0026'
                        ),
                        'Quotes and slashes are encoded' => array(
                                '"\'\\/',
-                               '\\x22\\x27\\x5C\\x2F'
+                               '\\u0022\\u0027\\u005C\\u002F'
                        ),
                        'Empty string stays empty' => array(
                                '',
@@ -2349,19 +2349,19 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                        ),
                        'Exclamation mark and space are properly encoded' => array(
                                'Hello World!',
-                               'Hello\\x20World\\x21'
+                               'Hello\\u0020World\\u0021'
                        ),
                        'Whitespaces are properly encoded' => array(
                                TAB . LF . CR . ' ',
-                               '\\x09\\x0A\\x0D\\x20'
+                               '\\u0009\\u000A\\u000D\\u0020'
                        ),
                        'Null byte is properly encoded' => array(
                                chr(0),
-                               '\\x00'
+                               '\\u0000'
                        ),
                        'Umlauts are properly encoded' => array(
                                'ÜüÖöÄä',
-                               '\\xDC\\xFC\\xD6\\xF6\\xC4\\xE4'
+                               '\\u00dc\\u00fc\\u00d6\\u00f6\\u00c4\\u00e4'
                        )
                );
        }