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