d3634318444b2f985174e57030a587bacbeb1ddc
6 * Contains Style-related functions.
9 * @copyright 2001-2019 WoltLab GmbH
10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * Converts css code from LTR to RTL.
17 public static function convertCSSToRTL(string $contents): string
19 // parse style attributes
21 // background-position
22 $contents = \
preg_replace('/background-position:\s*left/', 'wcf-background-position:left', $contents);
23 $contents = \
preg_replace('/background-position:\s*right/', 'background-position:left', $contents);
24 $contents = \
str_replace('wcf-background-position:left', 'background-position:right', $contents);
25 $contents = \
preg_replace_callback('/background-position:\s*([\d\.]+)%/', static function ($matches) {
26 return 'background-position:' . (100.0 - $matches[1]) . '%';
30 $contents = \
str_replace('-ltr', '-rtl', $contents);
32 // (border, margin, padding) left / right
33 $contents = \
str_replace('left:', 'wcf-left:', $contents);
34 $contents = \
str_replace('right:', 'left:', $contents);
35 $contents = \
str_replace('wcf-left:', 'right:', $contents);
38 $contents = \
preg_replace(
39 '/border-width:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/',
40 'border-width:\\1 \\4 \\3 \\2',
44 // (border-left-width, border-right-width)
45 $contents = \
str_replace('border-left-width:', 'wcf-border-left-width:', $contents);
46 $contents = \
str_replace('border-right-width:', 'border-left-width:', $contents);
47 $contents = \
str_replace('wcf-border-left-width:', 'border-right-width:', $contents);
50 $contents = \
preg_replace(
51 '/border-style:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/',
52 'border-style:\\1 \\4 \\3 \\2',
56 // (border-left-style, border-right-style)
57 $contents = \
str_replace('border-left-style:', 'wcf-border-left-style:', $contents);
58 $contents = \
str_replace('border-right-style:', 'border-left-style:', $contents);
59 $contents = \
str_replace('wcf-border-left-style:', 'border-right-style:', $contents);
61 // (border-left-color, border-right-color)
62 $contents = \
str_replace('border-left-color:', 'wcf-border-left-color:', $contents);
63 $contents = \
str_replace('border-right-color:', 'border-left-color:', $contents);
64 $contents = \
str_replace('wcf-border-left-color:', 'border-right-color:', $contents);
67 $contents = \
preg_replace_callback(
68 '~box-shadow:\s*(?P<inset>inset)?\s*(?P<negate>-)?(?P<number>\d+)~',
69 static function ($matches) {
70 return 'box-shadow: ' . $matches['inset'] . ' ' . ($matches['negate'] ?
'' : '-') . $matches['number'];
76 $contents = \
preg_replace('/clear:\s*left/', 'wcf-clear:left', $contents);
77 $contents = \
preg_replace('/clear:\s*right/', 'clear:left', $contents);
78 $contents = \
str_replace('wcf-clear:left', 'clear:right', $contents);
81 $contents = \
preg_replace('/float:\s*left/', 'wcf-float:left', $contents);
82 $contents = \
preg_replace('/float:\s*right/', 'float:left', $contents);
83 $contents = \
str_replace('wcf-float:left', 'float:right', $contents);
86 $contents = \
preg_replace(
87 '/margin:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/',
88 'margin:\\1 \\4 \\3 \\2',
93 $contents = \
preg_replace(
94 '/padding:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/',
95 'padding:\\1 \\4 \\3 \\2',
100 $contents = \
preg_replace('/text-align:\s*left/', 'wcf-text-align:left', $contents);
101 $contents = \
preg_replace('/text-align:\s*right/', 'text-align:left', $contents);
102 $contents = \
str_replace('wcf-text-align:left', 'text-align:right', $contents);
105 $contents = \
preg_replace('/text-shadow:\s*(\d)/', 'text-shadow:-\\1', $contents);
108 $contents = \
preg_replace(
109 '/border-radius:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/',
110 'border-radius:\\2 \\1 \\4 \\3',
113 $contents = \
str_replace('border-top-left-radius:', 'wcf-border-top-left-radius:', $contents);
114 $contents = \
str_replace('border-top-right-radius:', 'border-top-left-radius:', $contents);
115 $contents = \
str_replace('wcf-border-top-left-radius:', 'border-top-right-radius:', $contents);
116 $contents = \
str_replace('border-bottom-left-radius:', 'wcf-border-bottom-left-radius:', $contents);
117 $contents = \
str_replace('border-bottom-right-radius:', 'border-bottom-left-radius:', $contents);
118 $contents = \
str_replace('wcf-border-bottom-left-radius:', 'border-bottom-right-radius:', $contents);
120 // transform: translateX
121 return \
preg_replace_callback(
122 '/transform:\s*translateX\((?P<negate>-)?(?P<number>\d+)(?P<unit>[^\s\)]+)\)/',
123 static function ($matches) {
124 return 'transform: translateX(' . ($matches['negate'] ?
'' : '-') . $matches['number'] . $matches['unit'] . ')';
131 * Compresses css code.
133 public static function compressCSS(string $string): string
135 $string = StringUtil
::unifyNewlines($string);
138 $string = \
preg_replace('!/\*.*?\*/\r?\n?!s', '', $string);
140 $string = \
preg_replace('!\t+!', '', $string);
141 // remove line breaks
142 $string = \
preg_replace('!(?<=\{|;) *\n!', '', $string);
143 $string = \
preg_replace('! *\n(?=})!', '', $string);
144 // remove empty lines
145 $string = \
preg_replace('~\n{2,}~s', "\n", $string);
147 return StringUtil
::trim($string);
151 * Forbid creation of StyleUtil objects.
153 private function __construct()