From 889883b731d9c5676fa698453301367109bd9caa Mon Sep 17 00:00:00 2001 From: Ori Livneh Date: Fri, 25 Sep 2015 09:43:35 -0700 Subject: [PATCH] =?utf8?q?ResourceLoaderEditToolbarModule::cssSerializeStr?= =?utf8?q?ing()=20=E2=86=92=20CSSMin::serializeStringValue()?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 19 ++++++++++++++++++ .../ResourceLoaderEditToolbarModule.php | 20 +------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/includes/libs/CSSMin.php b/includes/libs/CSSMin.php index 5a8c4c7bc1..c93206ced9 100644 --- a/includes/libs/CSSMin.php +++ b/includes/libs/CSSMin.php @@ -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 diff --git a/includes/resourceloader/ResourceLoaderEditToolbarModule.php b/includes/resourceloader/ResourceLoaderEditToolbarModule.php index da729fdc67..ef51e0cbd3 100644 --- a/includes/resourceloader/ResourceLoaderEditToolbarModule.php +++ b/includes/resourceloader/ResourceLoaderEditToolbarModule.php @@ -26,24 +26,6 @@ * @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; -- 2.20.1