From: jenkins-bot Date: Fri, 13 Apr 2018 12:30:30 +0000 (+0000) Subject: Merge "CSSMin::serializeStringValue: Update implementation to new specification" X-Git-Tag: 1.31.0-rc.0~86 X-Git-Url: http://git.cyclocoop.org/%24self?a=commitdiff_plain;h=046250bfc357c3b4f5c5106aa223d608ce83c203;hp=40ad4433ecfc625524f67762c478fb06a806293f;p=lhc%2Fweb%2Fwiklou.git Merge "CSSMin::serializeStringValue: Update implementation to new specification" --- diff --git a/includes/libs/CSSMin.php b/includes/libs/CSSMin.php index 1cbcbde1e0..73825e82e3 100644 --- a/includes/libs/CSSMin.php +++ b/includes/libs/CSSMin.php @@ -173,18 +173,14 @@ class CSSMin { /** * 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 + * https://www.w3.org/TR/2016/WD-cssom-1-20160317/#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, [ '\\' => '\\\\', '"' => '\\"' ] ); - $value = preg_replace_callback( '/[\x01-\x1f\x7f-\x9f]/', function ( $match ) { + $value = strtr( $value, [ "\0" => "\\fffd ", '\\' => '\\\\', '"' => '\\"' ] ); + $value = preg_replace_callback( '/[\x01-\x1f\x7f]/', function ( $match ) { return '\\' . base_convert( ord( $match[0] ), 10, 16 ) . ' '; }, $value ); return '"' . $value . '"'; diff --git a/tests/phpunit/includes/libs/CSSMinTest.php b/tests/phpunit/includes/libs/CSSMinTest.php index f2d5ef379b..59a1c7c9b7 100644 --- a/tests/phpunit/includes/libs/CSSMinTest.php +++ b/tests/phpunit/includes/libs/CSSMinTest.php @@ -22,6 +22,43 @@ class CSSMinTest extends MediaWikiTestCase { ] ); } + /** + * @dataProvider serializeStringValueProvider + * @covers CSSMin::serializeStringValue + */ + public function testSerializeStringValue( $input, $expected ) { + $output = CSSMin::serializeStringValue( $input ); + $this->assertEquals( + $expected, + $output, + 'Serialized output must be in the expected form.' + ); + } + + public function serializeStringValueProvider() { + return [ + [ 'Hello World!', '"Hello World!"' ], + [ "Null\0Null", "\"Null\\fffd Null\"" ], + [ '"', '"\\""' ], + [ "'", '"\'"' ], + [ "\\", '"\\\\"' ], + [ "Tab\tTab", '"Tab\\9 Tab"' ], + [ "Space tab \t space", '"Space tab \\9 space"' ], + [ "Line\nfeed", '"Line\\a feed"' ], + [ "Return\rreturn", '"Return\\d return"' ], + [ "Next\xc2\x85line", "\"Next\xc2\x85line\"" ], + [ "Del\x7fDel", '"Del\\7f Del"' ], + [ "nb\xc2\xa0sp", "\"nb\xc2\xa0sp\"" ], + [ "AMP&AMP", "\"AMP&AMP\"" ], + [ '!"#$%&\'()*+,-./0123456789:;<=>?', '"!\\"#$%&\'()*+,-./0123456789:;<=>?"' ], + [ '@[\\]^_`{|}~', '"@[\\\\]^_`{|}~"' ], + [ 'ä', '"ä"' ], + [ 'Ä', '"Ä"' ], + [ '€', '"€"' ], + [ '𝒞', '"𝒞"' ], // U+1D49E 'MATHEMATICAL SCRIPT CAPITAL C' + ]; + } + /** * @dataProvider mimeTypeProvider * @covers CSSMin::getMimeType