| 1 |
<?php |
| 2 |
/** |
| 3 |
* PHP port of CSSJanus. https://www.mediawiki.org/wiki/CSSJanus |
| 4 |
* |
| 5 |
* Copyright 2020 Timo Tijhof |
| 6 |
* Copyright 2014 Trevor Parscal |
| 7 |
* Copyright 2010 Roan Kattouw |
| 8 |
* Copyright 2008 Google Inc. |
| 9 |
* |
| 10 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 |
* you may not use this file except in compliance with the License. |
| 12 |
* You may obtain a copy of the License at |
| 13 |
* |
| 14 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 15 |
* |
| 16 |
* Unless required by applicable law or agreed to in writing, software |
| 17 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 18 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 |
* See the License for the specific language governing permissions and |
| 20 |
* limitations under the License. |
| 21 |
* |
| 22 |
* @file |
| 23 |
*/ |
| 24 |
|
| 25 |
/** |
| 26 |
* CSSJanus is a utility that converts CSS stylesheets |
| 27 |
* from left-to-right (LTR) to right-to-left (RTL). |
| 28 |
*/ |
| 29 |
class CSSJanus { |
| 30 |
private const TOKEN_TMP = '`TMP`'; |
| 31 |
private const TOKEN_LTR_TMP = '`TMPLTR`'; |
| 32 |
private const TOKEN_RTL_TMP = '`TMPRTL`'; |
| 33 |
private const TOKEN_COMMENT = '`COMMENT`'; |
| 34 |
|
| 35 |
private static $patterns = null; |
| 36 |
|
| 37 |
private static function buildPatterns() { |
| 38 |
if ( self::$patterns !== null ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
// Patterns defined as null are built dynamically |
| 42 |
$patterns = [ |
| 43 |
'nonAscii' => '[\200-\377]', |
| 44 |
'unicode' => '(?:(?:\\\\[0-9a-f]{1,6})(?:\r\n|\s)?)', |
| 45 |
'num' => '(?:[0-9]*\.[0-9]+|[0-9]+)', |
| 46 |
'unit' => '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)', |
| 47 |
'body_selector' => 'body\s*{\s*', |
| 48 |
'direction' => 'direction\s*:\s*', |
| 49 |
'escape' => null, |
| 50 |
'nmstart' => null, |
| 51 |
'nmchar' => null, |
| 52 |
'ident' => null, |
| 53 |
'quantity' => null, |
| 54 |
'possibly_negative_quantity' => null, |
| 55 |
'color' => null, |
| 56 |
'url_special_chars' => '[!#$%&*-~]', |
| 57 |
'valid_after_uri_chars' => '[\'\"]?\s*', |
| 58 |
'url_chars' => null, |
| 59 |
'lookahead_not_open_brace' => null, |
| 60 |
'lookahead_not_closing_paren' => null, |
| 61 |
'lookahead_for_closing_paren' => null, |
| 62 |
'lookahead_not_letter' => '(?![a-zA-Z])', |
| 63 |
'lookbehind_not_letter' => '(?<![a-zA-Z])', |
| 64 |
'chars_within_selector' => '[^\}]*?', |
| 65 |
'noflip_annotation' => '\/\*\!?\s*@noflip\s*\*\/', |
| 66 |
'noflip_single' => null, |
| 67 |
'noflip_class' => null, |
| 68 |
'comment' => '/\/\*[^*]*\*+([^\/*][^*]*\*+)*\//', |
| 69 |
'direction_ltr' => null, |
| 70 |
'direction_rtl' => null, |
| 71 |
'left' => null, |
| 72 |
'right' => null, |
| 73 |
'left_in_url' => null, |
| 74 |
'right_in_url' => null, |
| 75 |
'ltr_dir_selector' => '/(:dir\( *)ltr( *\))/', |
| 76 |
'rtl_dir_selector' => '/(:dir\( *)rtl( *\))/', |
| 77 |
'ltr_in_url' => null, |
| 78 |
'rtl_in_url' => null, |
| 79 |
'cursor_east' => null, |
| 80 |
'cursor_west' => null, |
| 81 |
'four_notation_quantity' => null, |
| 82 |
'four_notation_color' => null, |
| 83 |
'border_radius' => null, |
| 84 |
'box_shadow' => null, |
| 85 |
'text_shadow1' => null, |
| 86 |
'text_shadow2' => null, |
| 87 |
'bg_horizontal_percentage' => null, |
| 88 |
'bg_horizontal_percentage_x' => null, |
| 89 |
'suffix' => '(\s*(?:!important\s*)?[;}])' |
| 90 |
]; |
| 91 |
|
| 92 |
// @codingStandardsIgnoreStart Generic.Files.LineLength.TooLong |
| 93 |
$patterns['escape'] = "(?:{$patterns['unicode']}|\\\\[^\\r\\n\\f0-9a-f])"; |
| 94 |
$patterns['nmstart'] = "(?:[_a-z]|{$patterns['nonAscii']}|{$patterns['escape']})"; |
| 95 |
$patterns['nmchar'] = "(?:[_a-z0-9-]|{$patterns['nonAscii']}|{$patterns['escape']})"; |
| 96 |
$patterns['ident'] = "-?{$patterns['nmstart']}{$patterns['nmchar']}*"; |
| 97 |
$patterns['quantity'] = "{$patterns['num']}(?:\s*{$patterns['unit']}|{$patterns['ident']})?"; |
| 98 |
$patterns['possibly_negative_quantity'] = "((?:-?{$patterns['quantity']})|(?:inherit|auto))"; |
| 99 |
$patterns['possibly_negative_simple_quantity'] = "(?:-?{$patterns['num']}(?:\s*{$patterns['unit']})?)"; |
| 100 |
$patterns['math_operator'] = '(?:\+|\-|\*|\/)'; |
| 101 |
$patterns['allowed_chars'] = '(?:\(|\)|\t| )'; |
| 102 |
$patterns['calc_equation'] = "(?:{$patterns['allowed_chars']}|{$patterns['possibly_negative_simple_quantity']}|{$patterns['math_operator']}){3,}"; |
| 103 |
$patterns['calc'] = "(?:calc\((?:{$patterns['calc_equation']})\))"; |
| 104 |
$patterns['possibly_negative_quantity_calc'] = "((?:-?{$patterns['quantity']})|(?:inherit|auto)|{$patterns['calc']})"; |
| 105 |
$patterns['color'] = "(#?{$patterns['nmchar']}+|(?:rgba?|hsla?)\([ \d.,%-]+\))"; |
| 106 |
// Use "*+" instead of "*?" to avoid reaching the backtracking limit. |
| 107 |
// <https://phabricator.wikimedia.org/T326481>, <https://phabricator.wikimedia.org/T215746#4944830>. |
| 108 |
$patterns['url_chars'] = "(?:{$patterns['url_special_chars']}|{$patterns['nonAscii']}|{$patterns['escape']})*+"; |
| 109 |
$patterns['lookahead_not_open_brace'] = "(?!({$patterns['nmchar']}|\\r?\\n|\s|#|\:|\.|\,|\+|>|~|\(|\)|\[|\]|=|\*=|~=|\^=|'[^']*'|\"[^\"]*\"|" . self::TOKEN_COMMENT . ")*+{)"; |
| 110 |
$patterns['lookahead_not_closing_paren'] = "(?!{$patterns['url_chars']}{$patterns['valid_after_uri_chars']}\))"; |
| 111 |
$patterns['lookahead_for_closing_paren'] = "(?={$patterns['url_chars']}{$patterns['valid_after_uri_chars']}\))"; |
| 112 |
$patterns['noflip_single'] = "/({$patterns['noflip_annotation']}{$patterns['lookahead_not_open_brace']}[^;}]+;?)/i"; |
| 113 |
$patterns['noflip_class'] = "/({$patterns['noflip_annotation']}{$patterns['chars_within_selector']}})/i"; |
| 114 |
$patterns['direction_ltr'] = "/({$patterns['direction']})ltr/i"; |
| 115 |
$patterns['direction_rtl'] = "/({$patterns['direction']})rtl/i"; |
| 116 |
$patterns['left'] = "/{$patterns['lookbehind_not_letter']}(left){$patterns['lookahead_not_letter']}{$patterns['lookahead_not_closing_paren']}{$patterns['lookahead_not_open_brace']}/i"; |
| 117 |
$patterns['right'] = "/{$patterns['lookbehind_not_letter']}(right){$patterns['lookahead_not_letter']}{$patterns['lookahead_not_closing_paren']}{$patterns['lookahead_not_open_brace']}/i"; |
| 118 |
$patterns['left_in_url'] = "/{$patterns['lookbehind_not_letter']}(left){$patterns['lookahead_for_closing_paren']}/i"; |
| 119 |
$patterns['right_in_url'] = "/{$patterns['lookbehind_not_letter']}(right){$patterns['lookahead_for_closing_paren']}/i"; |
| 120 |
$patterns['ltr_in_url'] = "/{$patterns['lookbehind_not_letter']}(ltr){$patterns['lookahead_for_closing_paren']}/i"; |
| 121 |
$patterns['rtl_in_url'] = "/{$patterns['lookbehind_not_letter']}(rtl){$patterns['lookahead_for_closing_paren']}/i"; |
| 122 |
$patterns['cursor_east'] = "/{$patterns['lookbehind_not_letter']}([ns]?)e-resize/"; |
| 123 |
$patterns['cursor_west'] = "/{$patterns['lookbehind_not_letter']}([ns]?)w-resize/"; |
| 124 |
$patterns['four_notation_quantity_props'] = "((?:margin|padding|border-width)\s*:\s*)"; |
| 125 |
$patterns['four_notation_quantity'] = "/{$patterns['four_notation_quantity_props']}{$patterns['possibly_negative_quantity_calc']}(\s+){$patterns['possibly_negative_quantity_calc']}(\s+){$patterns['possibly_negative_quantity_calc']}(\s+){$patterns['possibly_negative_quantity_calc']}{$patterns['suffix']}/i"; |
| 126 |
$patterns['four_notation_color'] = "/((?:-color|border-style)\s*:\s*){$patterns['color']}(\s+){$patterns['color']}(\s+){$patterns['color']}(\s+){$patterns['color']}{$patterns['suffix']}/i"; |
| 127 |
// border-radius: <length or percentage>{1,4} [optional: / <length or percentage>{1,4} ] |
| 128 |
$patterns['border_radius'] = '/(border-radius\s*:\s*)' . $patterns['possibly_negative_quantity'] |
| 129 |
. '(?:(?:\s+' . $patterns['possibly_negative_quantity'] . ')(?:\s+' . $patterns['possibly_negative_quantity'] . ')?(?:\s+' . $patterns['possibly_negative_quantity'] . ')?)?' |
| 130 |
. '(?:(?:(?:\s*\/\s*)' . $patterns['possibly_negative_quantity'] . ')(?:\s+' . $patterns['possibly_negative_quantity'] . ')?(?:\s+' . $patterns['possibly_negative_quantity'] . ')?(?:\s+' . $patterns['possibly_negative_quantity'] . ')?)?' . $patterns['suffix'] |
| 131 |
. '/i'; |
| 132 |
$patterns['box_shadow'] = "/(box-shadow\s*:\s*(?:inset\s*)?){$patterns['possibly_negative_quantity']}/i"; |
| 133 |
$patterns['text_shadow1'] = "/(text-shadow\s*:\s*){$patterns['possibly_negative_quantity']}(\s*){$patterns['color']}/i"; |
| 134 |
$patterns['text_shadow2'] = "/(text-shadow\s*:\s*){$patterns['color']}(\s*){$patterns['possibly_negative_quantity']}/i"; |
| 135 |
$patterns['text_shadow3'] = "/(text-shadow\s*:\s*){$patterns['possibly_negative_quantity']}/i"; |
| 136 |
$patterns['bg_horizontal_percentage'] = "/(background(?:-position)?\s*:\s*(?:[^:;}\s]+\s+)*?)({$patterns['quantity']})/i"; |
| 137 |
$patterns['bg_horizontal_percentage_x'] = "/(background-position-x\s*:\s*)(-?{$patterns['num']}%)/i"; |
| 138 |
$patterns['translate_x'] = "/(transform\s*:[^;}]*)(translateX\s*\(\s*){$patterns['possibly_negative_quantity']}(\s*\))/i"; |
| 139 |
$patterns['translate'] = "/(transform\s*:[^;}]*)(translate\s*\(\s*){$patterns['possibly_negative_quantity']}((?:\s*,\s*{$patterns['possibly_negative_quantity']}){0,2}\s*\))/i"; |
| 140 |
// @codingStandardsIgnoreEnd |
| 141 |
|
| 142 |
self::$patterns = $patterns; |
| 143 |
} |
| 144 |
|
| 145 |
/** |
| 146 |
* Transform an LTR stylesheet to RTL |
| 147 |
* |
| 148 |
* @param string $css Stylesheet to transform |
| 149 |
* @param bool|array{transformDirInUrl?:bool,transformEdgeInUrl?:bool} $options Options array, |
| 150 |
* or value of transformDirInUrl option (back-compat) |
| 151 |
* - transformDirInUrl: Transform directions in URLs (ltr/rtl). Default: false. |
| 152 |
* - transformEdgeInUrl: Transform edges in URLs (left/right). Default: false. |
| 153 |
* @param bool $transformEdgeInUrl [optional] For back-compat |
| 154 |
* @return string Transformed stylesheet |
| 155 |
*/ |
| 156 |
public static function transform( $css, $options = [], $transformEdgeInUrl = false ) { |
| 157 |
if ( !is_array( $options ) ) { |
| 158 |
$options = [ |
| 159 |
'transformDirInUrl' => (bool)$options, |
| 160 |
'transformEdgeInUrl' => (bool)$transformEdgeInUrl, |
| 161 |
]; |
| 162 |
} |
| 163 |
|
| 164 |
// Defaults |
| 165 |
$options += [ |
| 166 |
'transformDirInUrl' => false, |
| 167 |
'transformEdgeInUrl' => false, |
| 168 |
]; |
| 169 |
|
| 170 |
self::buildPatterns(); |
| 171 |
|
| 172 |
// We wrap tokens in ` , not ~ like the original implementation does. |
| 173 |
// This was done because ` is not a legal character in CSS and can only |
| 174 |
// occur in URLs, where we escape it to %60 before inserting our tokens. |
| 175 |
$css = str_replace( '`', '%60', $css ); |
| 176 |
|
| 177 |
// Tokenize single line rules with /* @noflip */ |
| 178 |
$noFlipSingle = new CSSJanusTokenizer( self::$patterns['noflip_single'], '`NOFLIP_SINGLE`' ); |
| 179 |
$css = $noFlipSingle->tokenize( $css ); |
| 180 |
|
| 181 |
// Tokenize class rules with /* @noflip */ |
| 182 |
$noFlipClass = new CSSJanusTokenizer( self::$patterns['noflip_class'], '`NOFLIP_CLASS`' ); |
| 183 |
$css = $noFlipClass->tokenize( $css ); |
| 184 |
|
| 185 |
// Tokenize comments |
| 186 |
$comments = new CSSJanusTokenizer( self::$patterns['comment'], self::TOKEN_COMMENT ); |
| 187 |
$css = $comments->tokenize( $css ); |
| 188 |
|
| 189 |
// LTR->RTL fixes start here |
| 190 |
$css = self::fixDirection( $css ); |
| 191 |
if ( $options['transformDirInUrl'] ) { |
| 192 |
$css = self::fixLtrRtlInURL( $css ); |
| 193 |
} |
| 194 |
|
| 195 |
if ( $options['transformEdgeInUrl'] ) { |
| 196 |
$css = self::fixLeftRightInURL( $css ); |
| 197 |
} |
| 198 |
$css = self::fixLeftAndRight( $css ); |
| 199 |
$css = self::fixCursorProperties( $css ); |
| 200 |
$css = self::fixFourPartNotation( $css ); |
| 201 |
$css = self::fixBorderRadius( $css ); |
| 202 |
$css = self::fixBackgroundPosition( $css ); |
| 203 |
$css = self::fixShadows( $css ); |
| 204 |
$css = self::fixTranslate( $css ); |
| 205 |
|
| 206 |
// Detokenize stuff we tokenized before |
| 207 |
$css = $comments->detokenize( $css ); |
| 208 |
$css = $noFlipClass->detokenize( $css ); |
| 209 |
$css = $noFlipSingle->detokenize( $css ); |
| 210 |
|
| 211 |
return $css; |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Replace direction: ltr; with direction: rtl; and vice versa. |
| 216 |
* |
| 217 |
* The original implementation only does this inside body selectors |
| 218 |
* and misses "body\n{\ndirection:ltr;\n}". This function does not have |
| 219 |
* these problems. |
| 220 |
* |
| 221 |
* See https://code.google.com/p/cssjanus/issues/detail?id=15 |
| 222 |
* |
| 223 |
* @param string $css |
| 224 |
* @return string |
| 225 |
*/ |
| 226 |
private static function fixDirection( $css ) { |
| 227 |
$css = preg_replace( |
| 228 |
self::$patterns['direction_ltr'], |
| 229 |
'$1' . self::TOKEN_TMP, |
| 230 |
$css |
| 231 |
); |
| 232 |
$css = preg_replace( self::$patterns['direction_rtl'], '$1ltr', $css ); |
| 233 |
$css = str_replace( self::TOKEN_TMP, 'rtl', $css ); |
| 234 |
|
| 235 |
return $css; |
| 236 |
} |
| 237 |
|
| 238 |
/** |
| 239 |
* Replace 'ltr' with 'rtl' and vice versa in background URLs |
| 240 |
* @param string $css |
| 241 |
* @return string |
| 242 |
*/ |
| 243 |
private static function fixLtrRtlInURL( $css ) { |
| 244 |
$css = preg_replace( self::$patterns['ltr_dir_selector'], '$1' . self::TOKEN_LTR_TMP . '$2', $css ); |
| 245 |
$css = preg_replace( self::$patterns['rtl_dir_selector'], '$1' . self::TOKEN_RTL_TMP . '$2', $css ); |
| 246 |
$css = preg_replace( self::$patterns['ltr_in_url'], self::TOKEN_TMP, $css ); |
| 247 |
$css = preg_replace( self::$patterns['rtl_in_url'], 'ltr', $css ); |
| 248 |
$css = str_replace( self::TOKEN_TMP, 'rtl', $css ); |
| 249 |
$css = str_replace( self::TOKEN_LTR_TMP, 'ltr', $css ); |
| 250 |
$css = str_replace( self::TOKEN_RTL_TMP, 'rtl', $css ); |
| 251 |
|
| 252 |
return $css; |
| 253 |
} |
| 254 |
|
| 255 |
/** |
| 256 |
* Replace 'left' with 'right' and vice versa in background URLs |
| 257 |
* @param string $css |
| 258 |
* @return string |
| 259 |
*/ |
| 260 |
private static function fixLeftRightInURL( $css ) { |
| 261 |
$css = preg_replace( self::$patterns['left_in_url'], self::TOKEN_TMP, $css ); |
| 262 |
$css = preg_replace( self::$patterns['right_in_url'], 'left', $css ); |
| 263 |
$css = str_replace( self::TOKEN_TMP, 'right', $css ); |
| 264 |
|
| 265 |
return $css; |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Flip rules like left: , padding-right: , etc. |
| 270 |
* @param string $css |
| 271 |
* @return string |
| 272 |
*/ |
| 273 |
private static function fixLeftAndRight( $css ) { |
| 274 |
$css = preg_replace( self::$patterns['left'], self::TOKEN_TMP, $css ); |
| 275 |
$css = preg_replace( self::$patterns['right'], 'left', $css ); |
| 276 |
$css = str_replace( self::TOKEN_TMP, 'right', $css ); |
| 277 |
|
| 278 |
return $css; |
| 279 |
} |
| 280 |
|
| 281 |
/** |
| 282 |
* Flip East and West in rules like cursor: nw-resize; |
| 283 |
* @param string $css |
| 284 |
* @return string |
| 285 |
*/ |
| 286 |
private static function fixCursorProperties( $css ) { |
| 287 |
$css = preg_replace( |
| 288 |
self::$patterns['cursor_east'], |
| 289 |
'$1' . self::TOKEN_TMP, |
| 290 |
$css |
| 291 |
); |
| 292 |
$css = preg_replace( self::$patterns['cursor_west'], '$1e-resize', $css ); |
| 293 |
$css = str_replace( self::TOKEN_TMP, 'w-resize', $css ); |
| 294 |
|
| 295 |
return $css; |
| 296 |
} |
| 297 |
|
| 298 |
/** |
| 299 |
* Swap the second and fourth parts in four-part notation rules like |
| 300 |
* padding: 1px 2px 3px 4px; |
| 301 |
* |
| 302 |
* Unlike the original implementation, this function doesn't suffer from |
| 303 |
* the bug where whitespace is not preserved when flipping four-part rules |
| 304 |
* and four-part color rules with multiple whitespace characters between |
| 305 |
* colors are not recognized. |
| 306 |
* See https://code.google.com/p/cssjanus/issues/detail?id=16 |
| 307 |
* @param string $css |
| 308 |
* @return string |
| 309 |
*/ |
| 310 |
private static function fixFourPartNotation( $css ) { |
| 311 |
$css = preg_replace( self::$patterns['four_notation_quantity'], '$1$2$3$8$5$6$7$4$9', $css ); |
| 312 |
$css = preg_replace( self::$patterns['four_notation_color'], '$1$2$3$8$5$6$7$4$9', $css ); |
| 313 |
return $css; |
| 314 |
} |
| 315 |
|
| 316 |
/** |
| 317 |
* Swaps appropriate corners in border-radius values. |
| 318 |
* |
| 319 |
* @param string $css |
| 320 |
* @return string |
| 321 |
*/ |
| 322 |
private static function fixBorderRadius( $css ) { |
| 323 |
return preg_replace_callback( |
| 324 |
self::$patterns['border_radius'], |
| 325 |
[ self::class, 'calculateBorderRadius' ], |
| 326 |
$css |
| 327 |
); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Callback for fixBorderRadius() |
| 332 |
* @param array $matches |
| 333 |
* @return string |
| 334 |
*/ |
| 335 |
private static function calculateBorderRadius( $matches ) { |
| 336 |
$pre = $matches[1]; |
| 337 |
$firstGroup = array_filter( array_slice( $matches, 2, 4 ), 'strlen' ); |
| 338 |
$secondGroup = array_filter( array_slice( $matches, 6, 4 ), 'strlen' ); |
| 339 |
$post = $matches[10] ?: ''; |
| 340 |
|
| 341 |
if ( $secondGroup ) { |
| 342 |
$values = self::flipBorderRadiusValues( $firstGroup ) |
| 343 |
. ' / ' . self::flipBorderRadiusValues( $secondGroup ); |
| 344 |
} else { |
| 345 |
$values = self::flipBorderRadiusValues( $firstGroup ); |
| 346 |
} |
| 347 |
|
| 348 |
return $pre . $values . $post; |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Callback for fixBorderRadius() |
| 353 |
* @param array $values Matched values |
| 354 |
* @return string Flipped values |
| 355 |
*/ |
| 356 |
private static function flipBorderRadiusValues( $values ) { |
| 357 |
switch ( count( $values ) ) { |
| 358 |
case 4: |
| 359 |
$values = [ $values[1], $values[0], $values[3], $values[2] ]; |
| 360 |
break; |
| 361 |
case 3: |
| 362 |
$values = [ $values[1], $values[0], $values[1], $values[2] ]; |
| 363 |
break; |
| 364 |
case 2: |
| 365 |
$values = [ $values[1], $values[0] ]; |
| 366 |
break; |
| 367 |
case 1: |
| 368 |
$values = [ $values[0] ]; |
| 369 |
break; |
| 370 |
} |
| 371 |
return implode( ' ', $values ); |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Flips the sign of a CSS value, possibly with a unit. |
| 376 |
* |
| 377 |
* We can't just negate the value with unary minus due to the units. |
| 378 |
* |
| 379 |
* @param string $cssValue |
| 380 |
* @return string |
| 381 |
*/ |
| 382 |
private static function flipSign( $cssValue ) { |
| 383 |
// Don't mangle zeroes |
| 384 |
if ( floatval( $cssValue ) === 0.0 ) { |
| 385 |
return $cssValue; |
| 386 |
} elseif ( $cssValue[0] === '-' ) { |
| 387 |
return substr( $cssValue, 1 ); |
| 388 |
} else { |
| 389 |
return "-" . $cssValue; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Negates horizontal offset in box-shadow and text-shadow rules. |
| 395 |
* |
| 396 |
* @param string $css |
| 397 |
* @return string |
| 398 |
*/ |
| 399 |
private static function fixShadows( $css ) { |
| 400 |
$css = preg_replace_callback( self::$patterns['box_shadow'], function ( $matches ) { |
| 401 |
return $matches[1] . self::flipSign( $matches[2] ); |
| 402 |
}, $css ); |
| 403 |
|
| 404 |
$css = preg_replace_callback( self::$patterns['text_shadow1'], function ( $matches ) { |
| 405 |
return $matches[1] . $matches[2] . $matches[3] . self::flipSign( $matches[4] ); |
| 406 |
}, $css ); |
| 407 |
|
| 408 |
$css = preg_replace_callback( self::$patterns['text_shadow2'], function ( $matches ) { |
| 409 |
return $matches[1] . $matches[2] . $matches[3] . self::flipSign( $matches[4] ); |
| 410 |
}, $css ); |
| 411 |
|
| 412 |
$css = preg_replace_callback( self::$patterns['text_shadow3'], function ( $matches ) { |
| 413 |
return $matches[1] . self::flipSign( $matches[2] ); |
| 414 |
}, $css ); |
| 415 |
|
| 416 |
return $css; |
| 417 |
} |
| 418 |
|
| 419 |
/** |
| 420 |
* Negates horizontal offset in tranform: translate() |
| 421 |
* |
| 422 |
* @param string $css |
| 423 |
* @return string |
| 424 |
*/ |
| 425 |
private static function fixTranslate( $css ) { |
| 426 |
$css = preg_replace_callback( self::$patterns['translate'], function ( $matches ) { |
| 427 |
return $matches[1] . $matches[2] . self::flipSign( $matches[3] ) . $matches[4]; |
| 428 |
}, $css ); |
| 429 |
|
| 430 |
$css = preg_replace_callback( self::$patterns['translate_x'], function ( $matches ) { |
| 431 |
return $matches[1] . $matches[2] . self::flipSign( $matches[3] ) . $matches[4]; |
| 432 |
}, $css ); |
| 433 |
|
| 434 |
return $css; |
| 435 |
} |
| 436 |
|
| 437 |
/** |
| 438 |
* Flip horizontal background percentages. |
| 439 |
* @param string $css |
| 440 |
* @return string |
| 441 |
*/ |
| 442 |
private static function fixBackgroundPosition( $css ) { |
| 443 |
$replaced = preg_replace_callback( |
| 444 |
self::$patterns['bg_horizontal_percentage'], |
| 445 |
[ self::class, 'calculateNewBackgroundPosition' ], |
| 446 |
$css |
| 447 |
); |
| 448 |
if ( $replaced !== null ) { |
| 449 |
// preg_replace_callback() sometimes returns null |
| 450 |
$css = $replaced; |
| 451 |
} |
| 452 |
$replaced = preg_replace_callback( |
| 453 |
self::$patterns['bg_horizontal_percentage_x'], |
| 454 |
[ self::class, 'calculateNewBackgroundPosition' ], |
| 455 |
$css |
| 456 |
); |
| 457 |
if ( $replaced !== null ) { |
| 458 |
$css = $replaced; |
| 459 |
} |
| 460 |
|
| 461 |
return $css; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Callback for fixBackgroundPosition() |
| 466 |
* @param array $matches |
| 467 |
* @return string |
| 468 |
*/ |
| 469 |
private static function calculateNewBackgroundPosition( $matches ) { |
| 470 |
$value = $matches[2]; |
| 471 |
if ( substr( $value, -1 ) === '%' ) { |
| 472 |
$idx = strpos( $value, '.' ); |
| 473 |
if ( $idx !== false ) { |
| 474 |
$len = strlen( $value ) - $idx - 2; |
| 475 |
$value = number_format( 100 - (float)$value, $len ) . '%'; |
| 476 |
} else { |
| 477 |
$value = ( 100 - (float)$value ) . '%'; |
| 478 |
} |
| 479 |
} |
| 480 |
return $matches[1] . $value; |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
/** |
| 485 |
* Utility class used by CSSJanus that tokenizes and untokenizes things we want |
| 486 |
* to protect from being janused. |
| 487 |
*/ |
| 488 |
class CSSJanusTokenizer { |
| 489 |
private $regex; |
| 490 |
private $token; |
| 491 |
private $originals; |
| 492 |
|
| 493 |
/** |
| 494 |
* Constructor |
| 495 |
* @param string $regex Regular expression whose matches to replace by a token. |
| 496 |
* @param string $token Token |
| 497 |
*/ |
| 498 |
public function __construct( $regex, $token ) { |
| 499 |
$this->regex = $regex; |
| 500 |
$this->token = $token; |
| 501 |
$this->originals = []; |
| 502 |
} |
| 503 |
|
| 504 |
/** |
| 505 |
* Replace all occurrences of $regex in $str with a token and remember |
| 506 |
* the original strings. |
| 507 |
* @param string $str to tokenize |
| 508 |
* @return string Tokenized string |
| 509 |
*/ |
| 510 |
public function tokenize( $str ) { |
| 511 |
return preg_replace_callback( $this->regex, [ $this, 'tokenizeCallback' ], $str ); |
| 512 |
} |
| 513 |
|
| 514 |
/** |
| 515 |
* @param array $matches |
| 516 |
* @return string |
| 517 |
*/ |
| 518 |
private function tokenizeCallback( $matches ) { |
| 519 |
$this->originals[] = $matches[0]; |
| 520 |
return $this->token; |
| 521 |
} |
| 522 |
|
| 523 |
/** |
| 524 |
* Replace tokens with their originals. If multiple strings were tokenized, it's important they be |
| 525 |
* detokenized in exactly the SAME ORDER. |
| 526 |
* @param string $str previously run through tokenize() |
| 527 |
* @return string Original string |
| 528 |
*/ |
| 529 |
public function detokenize( $str ) { |
| 530 |
// PHP has no function to replace only the first occurrence or to |
| 531 |
// replace occurrences of the same string with different values, |
| 532 |
// so we use preg_replace_callback() even though we don't really need a regex |
| 533 |
return preg_replace_callback( |
| 534 |
'/' . preg_quote( $this->token, '/' ) . '/', |
| 535 |
[ $this, 'detokenizeCallback' ], |
| 536 |
$str |
| 537 |
); |
| 538 |
} |
| 539 |
|
| 540 |
/** |
| 541 |
* @param array $matches |
| 542 |
* @return mixed |
| 543 |
*/ |
| 544 |
private function detokenizeCallback( $matches ) { |
| 545 |
$retval = current( $this->originals ); |
| 546 |
next( $this->originals ); |
| 547 |
|
| 548 |
return $retval; |
| 549 |
} |
| 550 |
} |