ResourceLoaderEditToolbarModule::cssSerializeString() → CSSMin::serializeStringValue()
authorOri Livneh <ori@wikimedia.org>
Fri, 25 Sep 2015 16:43:35 +0000 (09:43 -0700)
committerOri Livneh <ori@wikimedia.org>
Fri, 25 Sep 2015 16:48:51 +0000 (09:48 -0700)
ResourceLoaderEditToolbarModule is clearly the wrong place for something so
generic, so this method needs a new home. We can either introduce a new class
or find a suitable existing home. I think CSSMin is suitable. It has public
methods that do things that are closely related, like `encodeStringAsDataURI`
and `buildUrlValue`.

Change-Id: Icc6dfb8f47199e6188dd71948f4645baee085e51

includes/libs/CSSMin.php
includes/resourceloader/ResourceLoaderEditToolbarModule.php

index 5a8c4c7..c93206c 100644 (file)
@@ -180,6 +180,25 @@ class CSSMin {
                return false;
        }
 
+       /**
+        * Serialize a string (escape and quote) for use as a CSS string value.
+        * http://www.w3.org/TR/2013/WD-cssom-20131205/#serialize-a-string
+        *
+        * @param string $value
+        * @return string
+        * @throws Exception
+        */
+       public static function serializeStringValue( $value ) {
+               if ( strstr( $value, "\0" ) ) {
+                       throw new Exception( "Invalid character in CSS string" );
+               }
+               $value = strtr( $value, array( '\\' => '\\\\', '"' => '\\"' ) );
+               $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
+                       return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
+               }, $value );
+               return '"' . $value . '"';
+       }
+
        /**
         * @param $file string
         * @return bool|string
index da729fd..ef51e0c 100644 (file)
  * @since 1.24
  */
 class ResourceLoaderEditToolbarModule extends ResourceLoaderFileModule {
-       /**
-        * Serialize a string (escape and quote) for use as a CSS string value.
-        * http://www.w3.org/TR/2013/WD-cssom-20131205/#serialize-a-string
-        *
-        * @param string $value
-        * @return string
-        * @throws Exception
-        */
-       private static function cssSerializeString( $value ) {
-               if ( strstr( $value, "\0" ) ) {
-                       throw new Exception( "Invalid character in CSS string" );
-               }
-               $value = strtr( $value, array( '\\' => '\\\\', '"' => '\\"' ) );
-               $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) {
-                       return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' ';
-               }, $value );
-               return '"' . $value . '"';
-       }
 
        /**
         * Get language-specific LESS variables for this module.
@@ -58,7 +40,7 @@ class ResourceLoaderEditToolbarModule extends ResourceLoaderFileModule {
 
                // less.php tries to be helpful and parse our variables as LESS source code
                foreach ( $vars as $key => &$value ) {
-                       $value = self::cssSerializeString( $value );
+                       $value = CSSMin::serializeStringValue( $value );
                }
 
                return $vars;