Merge branch '3.0'
[GitHub/WoltLab/WCF.git] / wcfsetup / install / files / lib / util / StyleUtil.class.php
CommitLineData
11ade432
AE
1<?php
2namespace wcf\util;
06a95535 3use wcf\system\style\StyleCompiler;
11ade432
AE
4
5/**
6 * Contains Style-related functions.
a17de04e 7 *
0c166126 8 * @author Marcel Werk
c839bd49 9 * @copyright 2001-2018 WoltLab GmbH
11ade432 10 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
e71525e4 11 * @package WoltLabSuite\Core\Util
11ade432 12 */
18284789 13final class StyleUtil {
11ade432
AE
14 /**
15 * Converts css code from LTR to RTL.
16 *
17 * @param string $contents
18 * @return string
19 */
20 public static function convertCSSToRTL($contents) {
21 // parse style attributes
22 // background
23 // background-position
24 $contents = preg_replace('/background-position:\s*left/', 'wcf-background-position:left', $contents);
25 $contents = preg_replace('/background-position:\s*right/', 'background-position:left', $contents);
26 $contents = str_replace('wcf-background-position:left', 'background-position:right', $contents);
01ac3eb2
TD
27 $contents = preg_replace_callback('/background-position:\s*([\d\.]+)%/', function ($matches) {
28 return 'background-position:'.(100.0-$matches[1]).'%';
29 }, $contents);
11ade432
AE
30
31 // background-image
32 $contents = str_replace('-ltr', '-rtl', $contents);
33
34 // (border, margin, padding) left / right
35 $contents = str_replace('left:', 'wcf-left:', $contents);
36 $contents = str_replace('right:', 'left:', $contents);
37 $contents = str_replace('wcf-left:', 'right:', $contents);
38
39 // border-width
40 $contents = preg_replace('/border-width:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/', 'border-width:\\1 \\4 \\3 \\2', $contents);
41
42 // (border-left-width, border-right-width)
43 $contents = str_replace('border-left-width:', 'wcf-border-left-width:', $contents);
44 $contents = str_replace('border-right-width:', 'border-left-width:', $contents);
45 $contents = str_replace('wcf-border-left-width:', 'border-right-width:', $contents);
46
448a5a29
MW
47 // border-style
48 $contents = preg_replace('/border-style:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/', 'border-style:\\1 \\4 \\3 \\2', $contents);
49
50 // (border-left-style, border-right-style)
51 $contents = str_replace('border-left-style:', 'wcf-border-left-style:', $contents);
52 $contents = str_replace('border-right-style:', 'border-left-style:', $contents);
53 $contents = str_replace('wcf-border-left-style:', 'border-right-style:', $contents);
54
55 // border-color
c035ecce
MW
56 //$contents = preg_replace('/border-color:\s*(rgba?\(.*?\))\s+(rgba?\(.*?\))\s+(rgba?\(.*?\))\s+(rgba?\(.*?\))/', 'border-color:\\1 \\4 \\3 \\2', $contents);
57 //$contents = preg_replace('/border-color:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/', 'border-color:\\1 \\4 \\3 \\2', $contents);
448a5a29
MW
58
59 // (border-left-color, border-right-color)
60 $contents = str_replace('border-left-color:', 'wcf-border-left-color:', $contents);
61 $contents = str_replace('border-right-color:', 'border-left-color:', $contents);
62 $contents = str_replace('wcf-border-left-color:', 'border-right-color:', $contents);
63
8c8bbcbe
AE
64 // box-shadow
65 $contents = preg_replace_callback('~box-shadow:\s*(?P<inset>inset)?\s*(?P<negate>-)?(?P<number>\d+)~', function($matches) {
66 return 'box-shadow: ' . $matches['inset'] . ' ' . ($matches['negate'] ? '' : '-') . $matches['number'];
67 }, $contents);
68
11ade432
AE
69 // clear
70 $contents = preg_replace('/clear:\s*left/', 'wcf-clear:left', $contents);
71 $contents = preg_replace('/clear:\s*right/', 'clear:left', $contents);
72 $contents = str_replace('wcf-clear:left', 'clear:right', $contents);
73
11ade432
AE
74 // float
75 $contents = preg_replace('/float:\s*left/', 'wcf-float:left', $contents);
76 $contents = preg_replace('/float:\s*right/', 'float:left', $contents);
77 $contents = str_replace('wcf-float:left', 'float:right', $contents);
78
79 // margin
80 $contents = preg_replace('/margin:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/', 'margin:\\1 \\4 \\3 \\2', $contents);
81
82 // padding
83 $contents = preg_replace('/padding:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/', 'padding:\\1 \\4 \\3 \\2', $contents);
84
85 // text-align
86 $contents = preg_replace('/text-align:\s*left/', 'wcf-text-align:left', $contents);
87 $contents = preg_replace('/text-align:\s*right/', 'text-align:left', $contents);
88 $contents = str_replace('wcf-text-align:left', 'text-align:right', $contents);
89
90 // text-shadow
91 $contents = preg_replace('/text-shadow:\s*(\d)/', 'text-shadow:-\\1', $contents);
92
448a5a29
MW
93 // border-radius
94 $contents = preg_replace('/border-radius:\s*([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)\s+([^\s;\}]+)/', 'border-radius:\\2 \\1 \\4 \\3', $contents);
95 $contents = str_replace('border-top-left-radius:', 'wcf-border-top-left-radius:', $contents);
96 $contents = str_replace('border-top-right-radius:', 'border-top-left-radius:', $contents);
97 $contents = str_replace('wcf-border-top-left-radius:', 'border-top-right-radius:', $contents);
98 $contents = str_replace('border-bottom-left-radius:', 'wcf-border-bottom-left-radius:', $contents);
99 $contents = str_replace('border-bottom-right-radius:', 'border-bottom-left-radius:', $contents);
100 $contents = str_replace('wcf-border-bottom-left-radius:', 'border-bottom-right-radius:', $contents);
101
cce3cca7
MS
102 // transform: translateX
103 $contents = preg_replace_callback('/transform:\s*translateX\((?P<negate>-)?(?P<number>\d+)(?P<unit>[^\s\)]+)\)/', function($matches) {
104 return 'transform: translateX(' . ($matches['negate'] ? '' : '-') . $matches['number'] . $matches['unit'] . ')';
105 }, $contents);
106
11ade432
AE
107 return $contents;
108 }
109
110 /**
111 * Compresses css code.
112 *
113 * @param string $string
114 * @return string
115 */
116 public static function compressCSS($string) {
117 $string = StringUtil::unifyNewlines($string);
118 // remove comments
119 $string = preg_replace('!/\*.*?\*/\r?\n?!s', '', $string);
120 // remove tabs
121 $string = preg_replace('!\t+!', '', $string);
122 // remove line breaks
123 $string = preg_replace('!(?<=\{|;) *\n!', '', $string);
124 $string = preg_replace('! *\n(?=})!', '', $string);
125 // remove empty lines
126 $string = preg_replace('~\n{2,}~s', "\n", $string);
127
128 return StringUtil::trim($string);
129 }
130
131 /**
132 * Updates the acp style file.
133 */
134 public static function updateStyleFile() {
06a95535 135 StyleCompiler::getInstance()->compileACP();
11ade432 136 }
18284789 137
1d5f9363
MS
138 /**
139 * Forbid creation of StyleUtil objects.
140 */
141 private function __construct() {
142 // does nothing
143 }
11ade432 144}