Merge "Replace a few more logical operators"
[lhc/web/wiklou.git] / includes / libs / CSSJanus.php
1 <?php
2 /**
3 * PHP port of CSSJanus.
4 *
5 * Copyright 2008 Google Inc.
6 * Copyright 2010 Roan Kattouw
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 * @file
21 */
22
23 /**
24 * This is a PHP port of CSSJanus, a utility that transforms CSS style sheets
25 * written for LTR to RTL.
26 *
27 * Original code: http://code.google.com/p/cssjanus/source/browse/trunk/cssjanus.py
28 *
29 * @author Lindsey Simon <elsigh@google.com>
30 * @author Roan Kattouw
31 */
32 class CSSJanus {
33 // Patterns defined as null are built dynamically by buildPatterns()
34 private static $patterns = array(
35 'tmpToken' => '`TMP`',
36 'nonAscii' => '[\200-\377]',
37 'unicode' => '(?:(?:\\[0-9a-f]{1,6})(?:\r\n|\s)?)',
38 'num' => '(?:[0-9]*\.[0-9]+|[0-9]+)',
39 'unit' => '(?:em|ex|px|cm|mm|in|pt|pc|deg|rad|grad|ms|s|hz|khz|%)',
40 'body_selector' => 'body\s*{\s*',
41 'direction' => 'direction\s*:\s*',
42 'escape' => null,
43 'nmstart' => null,
44 'nmchar' => null,
45 'ident' => null,
46 'quantity' => null,
47 'possibly_negative_quantity' => null,
48 'color' => null,
49 'url_special_chars' => '[!#$%&*-~]',
50 'valid_after_uri_chars' => '[\'\"]?\s*',
51 'url_chars' => null,
52 'lookahead_not_open_brace' => null,
53 'lookahead_not_closing_paren' => null,
54 'lookahead_for_closing_paren' => null,
55 'lookahead_not_letter' => '(?![a-zA-Z])',
56 'lookbehind_not_letter' => '(?<![a-zA-Z])',
57 'chars_within_selector' => '[^\}]*?',
58 'noflip_annotation' => '\/\*\!?\s*@noflip\s*\*\/',
59 'noflip_single' => null,
60 'noflip_class' => null,
61 'comment' => '/\/\*[^*]*\*+([^\/*][^*]*\*+)*\//',
62 'direction_ltr' => null,
63 'direction_rtl' => null,
64 'left' => null,
65 'right' => null,
66 'left_in_url' => null,
67 'right_in_url' => null,
68 'ltr_in_url' => null,
69 'rtl_in_url' => null,
70 'cursor_east' => null,
71 'cursor_west' => null,
72 'four_notation_quantity' => null,
73 'four_notation_color' => null,
74 'border_radius' => null,
75 'box_shadow' => null,
76 'text_shadow1' => null,
77 'text_shadow2' => null,
78 'bg_horizontal_percentage' => null,
79 'bg_horizontal_percentage_x' => null,
80 );
81
82 /**
83 * Build patterns we can't define above because they depend on other patterns.
84 */
85 private static function buildPatterns() {
86 if (!is_null(self::$patterns['escape'])) {
87 // Patterns have already been built
88 return;
89 }
90
91 // @codingStandardsIgnoreStart Generic.Files.LineLength.TooLong
92 $patterns =& self::$patterns;
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['color'] = "(#?{$patterns['nmchar']}+|(?:rgba?|hsla?)\([ \d.,%-]+\))";
100 $patterns['url_chars'] = "(?:{$patterns['url_special_chars']}|{$patterns['nonAscii']}|{$patterns['escape']})*";
101 $patterns['lookahead_not_open_brace'] = "(?!({$patterns['nmchar']}|\r?\n|\s|#|\:|\.|\,|\+|>|\(|\)|\[|\]|=|\*=|~=|\^=|'[^']*'])*?{)";
102 $patterns['lookahead_not_closing_paren'] = "(?!{$patterns['url_chars']}?{$patterns['valid_after_uri_chars']}\))";
103 $patterns['lookahead_for_closing_paren'] = "(?={$patterns['url_chars']}?{$patterns['valid_after_uri_chars']}\))";
104 $patterns['noflip_single'] = "/({$patterns['noflip_annotation']}{$patterns['lookahead_not_open_brace']}[^;}]+;?)/i";
105 $patterns['noflip_class'] = "/({$patterns['noflip_annotation']}{$patterns['chars_within_selector']}})/i";
106 $patterns['direction_ltr'] = "/({$patterns['direction']})ltr/i";
107 $patterns['direction_rtl'] = "/({$patterns['direction']})rtl/i";
108 $patterns['left'] = "/{$patterns['lookbehind_not_letter']}(left){$patterns['lookahead_not_letter']}{$patterns['lookahead_not_closing_paren']}{$patterns['lookahead_not_open_brace']}/i";
109 $patterns['right'] = "/{$patterns['lookbehind_not_letter']}(right){$patterns['lookahead_not_letter']}{$patterns['lookahead_not_closing_paren']}{$patterns['lookahead_not_open_brace']}/i";
110 $patterns['left_in_url'] = "/{$patterns['lookbehind_not_letter']}(left){$patterns['lookahead_for_closing_paren']}/i";
111 $patterns['right_in_url'] = "/{$patterns['lookbehind_not_letter']}(right){$patterns['lookahead_for_closing_paren']}/i";
112 $patterns['ltr_in_url'] = "/{$patterns['lookbehind_not_letter']}(ltr){$patterns['lookahead_for_closing_paren']}/i";
113 $patterns['rtl_in_url'] = "/{$patterns['lookbehind_not_letter']}(rtl){$patterns['lookahead_for_closing_paren']}/i";
114 $patterns['cursor_east'] = "/{$patterns['lookbehind_not_letter']}([ns]?)e-resize/";
115 $patterns['cursor_west'] = "/{$patterns['lookbehind_not_letter']}([ns]?)w-resize/";
116 $patterns['four_notation_quantity_props'] = "((?:margin|padding|border-width)\s*:\s*)";
117 $patterns['four_notation_quantity'] = "/{$patterns['four_notation_quantity_props']}{$patterns['possibly_negative_quantity']}(\s+){$patterns['possibly_negative_quantity']}(\s+){$patterns['possibly_negative_quantity']}(\s+){$patterns['possibly_negative_quantity']}(\s*[;}])/i";
118 $patterns['four_notation_color'] = "/((?:-color|border-style)\s*:\s*){$patterns['color']}(\s+){$patterns['color']}(\s+){$patterns['color']}(\s+){$patterns['color']}(\s*[;}])/i";
119 $patterns['border_radius'] = "/(border-radius\s*:\s*)([^;}]*)/";
120 $patterns['box_shadow'] = "/(box-shadow\s*:\s*(?:inset\s*)?){$patterns['possibly_negative_quantity']}/i";
121 $patterns['text_shadow1'] = "/(text-shadow\s*:\s*){$patterns['color']}(\s*){$patterns['possibly_negative_quantity']}/i";
122 $patterns['text_shadow2'] = "/(text-shadow\s*:\s*){$patterns['possibly_negative_quantity']}/i";
123 $patterns['bg_horizontal_percentage'] = "/(background(?:-position)?\s*:\s*(?:[^:;}\s]+\s+)*?)({$patterns['quantity']})/i";
124 $patterns['bg_horizontal_percentage_x'] = "/(background-position-x\s*:\s*)(-?{$patterns['num']}%)/i";
125 // @codingStandardsIgnoreEnd
126
127 }
128
129 /**
130 * Transform an LTR stylesheet to RTL
131 * @param string $css stylesheet to transform
132 * @param $swapLtrRtlInURL Boolean: If true, swap 'ltr' and 'rtl' in URLs
133 * @param $swapLeftRightInURL Boolean: If true, swap 'left' and 'right' in URLs
134 * @return string Transformed stylesheet
135 */
136 public static function transform($css, $swapLtrRtlInURL = false, $swapLeftRightInURL = false) {
137 // We wrap tokens in ` , not ~ like the original implementation does.
138 // This was done because ` is not a legal character in CSS and can only
139 // occur in URLs, where we escape it to %60 before inserting our tokens.
140 $css = str_replace('`', '%60', $css);
141
142 self::buildPatterns();
143
144 // Tokenize single line rules with /* @noflip */
145 $noFlipSingle = new CSSJanusTokenizer(self::$patterns['noflip_single'], '`NOFLIP_SINGLE`');
146 $css = $noFlipSingle->tokenize($css);
147
148 // Tokenize class rules with /* @noflip */
149 $noFlipClass = new CSSJanusTokenizer(self::$patterns['noflip_class'], '`NOFLIP_CLASS`');
150 $css = $noFlipClass->tokenize($css);
151
152 // Tokenize comments
153 $comments = new CSSJanusTokenizer(self::$patterns['comment'], '`C`');
154 $css = $comments->tokenize($css);
155
156 // LTR->RTL fixes start here
157 $css = self::fixDirection($css);
158 if ($swapLtrRtlInURL) {
159 $css = self::fixLtrRtlInURL($css);
160 }
161
162 if ($swapLeftRightInURL) {
163 $css = self::fixLeftRightInURL($css);
164 }
165 $css = self::fixLeftAndRight($css);
166 $css = self::fixCursorProperties($css);
167 $css = self::fixFourPartNotation($css);
168 $css = self::fixBorderRadius($css);
169 $css = self::fixBackgroundPosition($css);
170 $css = self::fixShadows($css);
171
172 // Detokenize stuff we tokenized before
173 $css = $comments->detokenize($css);
174 $css = $noFlipClass->detokenize($css);
175 $css = $noFlipSingle->detokenize($css);
176
177 return $css;
178 }
179
180 /**
181 * Replace direction: ltr; with direction: rtl; and vice versa.
182 *
183 * The original implementation only does this inside body selectors
184 * and misses "body\n{\ndirection:ltr;\n}". This function does not have
185 * these problems.
186 *
187 * See https://code.google.com/p/cssjanus/issues/detail?id=15
188 *
189 * @param $css string
190 * @return string
191 */
192 private static function fixDirection($css) {
193 $css = preg_replace(
194 self::$patterns['direction_ltr'],
195 '$1' . self::$patterns['tmpToken'],
196 $css
197 );
198 $css = preg_replace(self::$patterns['direction_rtl'], '$1ltr', $css);
199 $css = str_replace(self::$patterns['tmpToken'], 'rtl', $css);
200
201 return $css;
202 }
203
204 /**
205 * Replace 'ltr' with 'rtl' and vice versa in background URLs
206 * @param $css string
207 * @return string
208 */
209 private static function fixLtrRtlInURL($css) {
210 $css = preg_replace(self::$patterns['ltr_in_url'], self::$patterns['tmpToken'], $css);
211 $css = preg_replace(self::$patterns['rtl_in_url'], 'ltr', $css);
212 $css = str_replace(self::$patterns['tmpToken'], 'rtl', $css);
213
214 return $css;
215 }
216
217 /**
218 * Replace 'left' with 'right' and vice versa in background URLs
219 * @param $css string
220 * @return string
221 */
222 private static function fixLeftRightInURL($css) {
223 $css = preg_replace(self::$patterns['left_in_url'], self::$patterns['tmpToken'], $css);
224 $css = preg_replace(self::$patterns['right_in_url'], 'left', $css);
225 $css = str_replace(self::$patterns['tmpToken'], 'right', $css);
226
227 return $css;
228 }
229
230 /**
231 * Flip rules like left: , padding-right: , etc.
232 * @param $css string
233 * @return string
234 */
235 private static function fixLeftAndRight($css) {
236 $css = preg_replace(self::$patterns['left'], self::$patterns['tmpToken'], $css);
237 $css = preg_replace(self::$patterns['right'], 'left', $css);
238 $css = str_replace(self::$patterns['tmpToken'], 'right', $css);
239
240 return $css;
241 }
242
243 /**
244 * Flip East and West in rules like cursor: nw-resize;
245 * @param $css string
246 * @return string
247 */
248 private static function fixCursorProperties($css) {
249 $css = preg_replace(
250 self::$patterns['cursor_east'],
251 '$1' . self::$patterns['tmpToken'],
252 $css
253 );
254 $css = preg_replace(self::$patterns['cursor_west'], '$1e-resize', $css);
255 $css = str_replace(self::$patterns['tmpToken'], 'w-resize', $css);
256
257 return $css;
258 }
259
260 /**
261 * Swap the second and fourth parts in four-part notation rules like
262 * padding: 1px 2px 3px 4px;
263 *
264 * Unlike the original implementation, this function doesn't suffer from
265 * the bug where whitespace is not preserved when flipping four-part rules
266 * and four-part color rules with multiple whitespace characters between
267 * colors are not recognized.
268 * See https://code.google.com/p/cssjanus/issues/detail?id=16
269 * @param $css string
270 * @return string
271 */
272 private static function fixFourPartNotation($css) {
273 $css = preg_replace(self::$patterns['four_notation_quantity'], '$1$2$3$8$5$6$7$4$9', $css);
274 $css = preg_replace(self::$patterns['four_notation_color'], '$1$2$3$8$5$6$7$4$9', $css);
275 return $css;
276 }
277
278 /**
279 * Swaps appropriate corners in border-radius values.
280 *
281 * @param $css string
282 * @return string
283 */
284 private static function fixBorderRadius($css) {
285 $css = preg_replace_callback(self::$patterns['border_radius'], function ($matches) {
286 $pre = $matches[1];
287 $values = $matches[2];
288 $numValues = count(preg_split('/\s+/', trim($values)));
289 switch ($numValues) {
290 case 4:
291 $values = preg_replace('/^(\S+)(\s*)(\S+)(\s*)(\S+)(\s*)(\S+)/', '$3$2$1$4$7$6$5', $values);
292 break;
293 case 3:
294 case 2:
295 $values = preg_replace('/^(\S+)(\s*)(\S+)/', '$3$2$1', $values);
296 break;
297 }
298 return $pre . $values;
299 }, $css);
300
301 return $css;
302 }
303
304 /**
305 * Negates horizontal offset in box-shadow and text-shadow rules.
306 *
307 * @param $css string
308 * @return string
309 */
310 private static function fixShadows($css) {
311 // Flips the sign of a CSS value, possibly with a unit.
312 // (We can't just negate the value with unary minus due to the units.)
313 $flipSign = function ($cssValue) {
314 // Don't mangle zeroes
315 if (floatval($cssValue) === 0.0) {
316 return $cssValue;
317 } elseif ($cssValue[0] === '-') {
318 return substr($cssValue, 1);
319 } else {
320 return "-" . $cssValue;
321 }
322 };
323
324 $css = preg_replace_callback(self::$patterns['box_shadow'], function ($matches) use ($flipSign) {
325 return $matches[1] . $flipSign($matches[2]);
326 }, $css);
327
328 $css = preg_replace_callback(self::$patterns['text_shadow1'], function ($matches) use ($flipSign) {
329 return $matches[1] . $matches[2] . $matches[3] . $flipSign($matches[4]);
330 }, $css);
331
332 $css = preg_replace_callback(self::$patterns['text_shadow2'], function ($matches) use ($flipSign) {
333 return $matches[1] . $flipSign($matches[2]);
334 }, $css);
335
336 return $css;
337 }
338
339 /**
340 * Flip horizontal background percentages.
341 * @param $css string
342 * @return string
343 */
344 private static function fixBackgroundPosition($css) {
345 $replaced = preg_replace_callback(
346 self::$patterns['bg_horizontal_percentage'],
347 array('self', 'calculateNewBackgroundPosition'),
348 $css
349 );
350 if ($replaced !== null) {
351 // preg_replace_callback() sometimes returns null
352 $css = $replaced;
353 }
354 $replaced = preg_replace_callback(
355 self::$patterns['bg_horizontal_percentage_x'],
356 array('self', 'calculateNewBackgroundPosition'),
357 $css
358 );
359 if ($replaced !== null) {
360 $css = $replaced;
361 }
362
363 return $css;
364 }
365
366 /**
367 * Callback for fixBackgroundPosition()
368 * @param $matches array
369 * @return string
370 */
371 private static function calculateNewBackgroundPosition($matches) {
372 $value = $matches[2];
373 if (substr($value, -1) === '%') {
374 $idx = strpos($value, '.');
375 if ($idx !== false) {
376 $len = strlen($value) - $idx - 2;
377 $value = number_format(100 - $value, $len) . '%';
378 } else {
379 $value = (100 - $value) . '%';
380 }
381 }
382 return $matches[1] . $value;
383 }
384 }
385
386 /**
387 * Utility class used by CSSJanus that tokenizes and untokenizes things we want
388 * to protect from being janused.
389 * @author Roan Kattouw
390 */
391 class CSSJanusTokenizer {
392 private $regex;
393 private $token;
394 private $originals;
395
396 /**
397 * Constructor
398 * @param string $regex Regular expression whose matches to replace by a token.
399 * @param string $token Token
400 */
401 public function __construct($regex, $token) {
402 $this->regex = $regex;
403 $this->token = $token;
404 $this->originals = array();
405 }
406
407 /**
408 * Replace all occurrences of $regex in $str with a token and remember
409 * the original strings.
410 * @param string $str to tokenize
411 * @return string Tokenized string
412 */
413 public function tokenize($str) {
414 return preg_replace_callback($this->regex, array($this, 'tokenizeCallback'), $str);
415 }
416
417 /**
418 * @param $matches array
419 * @return string
420 */
421 private function tokenizeCallback($matches) {
422 $this->originals[] = $matches[0];
423 return $this->token;
424 }
425
426 /**
427 * Replace tokens with their originals. If multiple strings were tokenized, it's important they be
428 * detokenized in exactly the SAME ORDER.
429 * @param string $str previously run through tokenize()
430 * @return string Original string
431 */
432 public function detokenize($str) {
433 // PHP has no function to replace only the first occurrence or to
434 // replace occurrences of the same string with different values,
435 // so we use preg_replace_callback() even though we don't really need a regex
436 return preg_replace_callback(
437 '/' . preg_quote($this->token, '/') . '/',
438 array($this, 'detokenizeCallback'),
439 $str
440 );
441 }
442
443 /**
444 * @param $matches
445 * @return mixed
446 */
447 private function detokenizeCallback($matches) {
448 $retval = current($this->originals);
449 next($this->originals);
450
451 return $retval;
452 }
453 }