[FEATURE] TypoScript property escapeJsValue 77/28177/3
authorJigal van Hemert <jigal.van.hemert@typo3.org>
Sat, 8 Mar 2014 08:46:39 +0000 (09:46 +0100)
committerJigal van Hemert <jigal.van.hemert@typo3.org>
Sat, 8 Mar 2014 14:54:05 +0000 (15:54 +0100)
The stdWrap property escapeJsValue allows content to be
included in JavaScript strings.

Resolves: #56642
Documentation: #56665
Releases: 6.2
Change-Id: I43f59889caec1a89970b94c93c93894d589243c9
Reviewed-on: https://review.typo3.org/28177
Reviewed-by: Xavier Perseguers
Tested-by: Xavier Perseguers
Reviewed-by: Markus Klein
Reviewed-by: Wouter Wolters
Tested-by: Wouter Wolters
Reviewed-by: Jigal van Hemert
Tested-by: Jigal van Hemert
typo3/sysext/frontend/Classes/ContentObject/ContentObjectRenderer.php
typo3/sysext/frontend/Tests/Unit/ContentObject/ContentObjectRendererTest.php

index f2bbc8a..750a194 100644 (file)
@@ -154,6 +154,8 @@ class ContentObjectRenderer {
                'rawUrlEncode.' => 'array',
                'htmlSpecialChars' => 'boolean',
                'htmlSpecialChars.' => 'array',
+               'escapeJsValue' => 'boolean',
+               'escapeJsValue.' => 'array',
                'doubleBrTag' => 'string',
                'doubleBrTag.' => 'array',
                'br' => 'boolean',
@@ -2860,6 +2862,19 @@ class ContentObjectRenderer {
        }
 
        /**
+        * escapeJsValue
+        * Escapes content to be used inside JavaScript strings. No quotes are added around the value
+        * as this can easily be done in TypoScript
+        *
+        * @param string $content Input value undergoing processing in this function
+        * @param array $conf stdWrap properties for escapeJsvalue
+        * @return string The processed input value
+        */
+       public function stdWrap_escapeJsValue($content = '', $conf = array()) {
+               return trim(GeneralUtility::quoteJSvalue($content), '\'');
+       }
+
+       /**
         * doubleBrTag
         * Searches for double line breaks and replaces them with the given value
         *
index d0e8a9c..03222fb 100644 (file)
@@ -1464,6 +1464,63 @@ class ContentObjectRendererTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                );
        }
 
+       /**
+        * Data provider for stdWrap_escapeJsValue test
+        *
+        * @return array multi-dimensional array with the second level like this:
+        * @see escapeJsValue
+        */
+       public function stdWrap_escapeJsValueDataProvider() {
+               return array(
+                       'double quote in string' => array(
+                               'double quote"',
+                               array(),
+                               'double\u0020quote\u0022'
+                       ),
+                       'backslash in string' => array(
+                               'backslash \\',
+                               array(),
+                               'backslash\u0020\u005C'
+                       ),
+                       'exclamation mark' => array(
+                               'exclamation!',
+                               array(),
+                               'exclamation\u0021'
+                       ),
+                       'whitespace tab, newline and carriage return' => array(
+                               "white\tspace\ns\r",
+                               array(),
+                               'white\u0009space\u000As\u000D'
+                       ),
+                       'single quote in string' => array(
+                               'single quote \'',
+                               array(),
+                               'single\u0020quote\u0020\u0027'
+                       ),
+                       'tag' => array(
+                               '<tag>',
+                               array(),
+                               '\u003Ctag\u003E'
+                       ),
+                       'ampersand in string' => array(
+                               'amper&sand',
+                               array(),
+                               'amper\u0026sand'
+                       ),
+               );
+       }
+
+       /**
+        * Check if escapeJsValue works properly
+        *
+        * @dataProvider stdWrap_escapeJsValueDataProvider
+        * @test
+        */
+       public function stdWrap_escapeJsValue($input, $conf, $expected) {
+               $result = $this->cObj->stdWrap_escapeJsValue($input, $conf);
+               $this->assertEquals($expected, $result);
+       }
+
 
        /////////////////////////////
        // Tests concerning getData()