forked from moodle/moodle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTLCSS.php
428 lines (352 loc) · 13.9 KB
/
RTLCSS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
<?php
/**
* RTLCSS.
*
* @package MoodleHQ\RTLCSS
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license https://opensource.org/licenses/MIT MIT
*/
namespace MoodleHQ\RTLCSS;
use Sabberworm\CSS\CSSList\CSSList;
use Sabberworm\CSS\CSSList\Document;
use Sabberworm\CSS\OutputFormat;
use Sabberworm\CSS\Parser;
use Sabberworm\CSS\Rule\Rule;
use Sabberworm\CSS\RuleSet\RuleSet;
use Sabberworm\CSS\Settings;
use Sabberworm\CSS\Value\CSSFunction;
use Sabberworm\CSS\Value\CSSString;
use Sabberworm\CSS\Value\PrimitiveValue;
use Sabberworm\CSS\Value\RuleValueList;
use Sabberworm\CSS\Value\Size;
use Sabberworm\CSS\Value\ValueList;
/**
* RTLCSS Class.
*
* @package MoodleHQ\RTLCSS
* @copyright 2016 Frédéric Massart - FMCorz.net
* @license https://opensource.org/licenses/MIT MIT
*/
class RTLCSS {
protected $tree;
protected $shouldAddCss = [];
protected $shouldIgnore = false;
protected $shouldRemove = false;
public function __construct(Document $tree) {
$this->tree = $tree;
}
protected function compare($what, $to, $ignoreCase) {
if ($ignoreCase) {
return strtolower($what) === strtolower($to);
}
return $what === $to;
}
protected function complement($value) {
if ($value instanceof Size) {
$value->setSize(100 - $value->getSize());
} else if ($value instanceof CSSFunction) {
$arguments = implode($value->getListSeparator(), $value->getArguments());
$arguments = "100% - ($arguments)";
$value->setListComponents([$arguments]);
}
}
public function flip() {
$this->processBlock($this->tree);
return $this->tree;
}
protected function negate($value) {
if ($value instanceof ValueList) {
foreach ($value->getListComponents() as $part) {
$this->negate($part);
}
} else if ($value instanceof Size) {
if ($value->getSize() != 0) {
$value->setSize(-$value->getSize());
}
}
}
protected function parseComments(array $comments) {
$startRule = '^(\s|\*)*!?rtl:';
foreach ($comments as $comment) {
$content = $comment->getComment();
if (preg_match('/' . $startRule . 'ignore/', $content)) {
$this->shouldIgnore = 1;
} else if (preg_match('/' . $startRule . 'begin:ignore/', $content)) {
$this->shouldIgnore = true;
} else if (preg_match('/' . $startRule . 'end:ignore/', $content)) {
$this->shouldIgnore = false;
} else if (preg_match('/' . $startRule . 'remove/', $content)) {
$this->shouldRemove = 1;
} else if (preg_match('/' . $startRule . 'begin:remove/', $content)) {
$this->shouldRemove = true;
} else if (preg_match('/' . $startRule . 'end:remove/', $content)) {
$this->shouldRemove = false;
} else if (preg_match('/' . $startRule . 'raw:/', $content)) {
$this->shouldAddCss[] = preg_replace('/' . $startRule . 'raw:/', '', $content);
}
}
}
protected function processBackground(Rule $rule) {
$value = $rule->getValue();
// TODO Fix upstream library as it does not parse this well, commas don't take precedence.
// There can be multiple sets of properties per rule.
$hasItems = false;
$items = [$value];
if ($value instanceof RuleValueList && $value->getListSeparator() == ',') {
$hasItems = true;
$items = $value->getListComponents();
}
// Foreach set.
foreach ($items as $itemKey => $item) {
// There can be multiple values in the same set.
$hasValues = false;
$parts = [$item];
if ($item instanceof RuleValueList) {
$hasValues = true;
$parts = $value->getListComponents();
}
$requiresPositionalArgument = false;
$hasPositionalArgument = false;
foreach ($parts as $key => $part) {
$part = $parts[$key];
if (!is_object($part)) {
$flipped = $this->swapLeftRight($part);
// Positional arguments can have a size following.
$hasPositionalArgument = $parts[$key] != $flipped;
$requiresPositionalArgument = true;
$parts[$key] = $flipped;
continue;
} else if ($part instanceof CSSFunction && strpos($part->getName(), 'gradient') !== false) {
// TODO Fix this.
} else if ($part instanceof Size && ($part->getUnit() === '%' || !$part->getUnit())) {
// Is this a value we're interested in?
if (!$requiresPositionalArgument || $hasPositionalArgument) {
$this->complement($part);
$part->setUnit('%');
// We only need to change one value.
break;
}
}
$hasPositionalArgument = false;
}
if ($hasValues) {
$item->setListComponents($parts);
} else {
$items[$itemKey] = $parts[$key];
}
}
if ($hasItems) {
$value->setListComponents($items);
} else {
$rule->setValue($items[0]);
}
}
protected function processBlock($block) {
$contents = [];
foreach ($block->getContents() as $node) {
$this->parseComments($node->getComments());
if ($toAdd = $this->shouldAddCss()) {
foreach ($toAdd as $add) {
$parser = new Parser($add);
$contents[] = $parser->parse();
}
}
if ($this->shouldRemoveNext()) {
continue;
} else if (!$this->shouldIgnoreNext()) {
if ($node instanceof CSSList) {
$this->processBlock($node);
}
if ($node instanceof RuleSet) {
$this->processDeclaration($node);
}
}
$contents[] = $node;
}
$block->setContents($contents);
}
protected function processDeclaration($node) {
$rules = [];
foreach ($node->getRules() as $key => $rule) {
$this->parseComments($rule->getComments());
if ($toAdd = $this->shouldAddCss()) {
foreach ($toAdd as $add) {
$parser = new Parser('.wrapper{' . $add . '}');
$tree = $parser->parse();
$contents = $tree->getContents();
foreach ($contents[0]->getRules() as $newRule) {
$rules[] = $newRule;
}
}
}
if ($this->shouldRemoveNext()) {
continue;
} else if (!$this->shouldIgnoreNext()) {
$this->processRule($rule);
}
$rules[] = $rule;
}
$node->setRules($rules);
}
protected function processRule($rule) {
$property = $rule->getRule();
$value = $rule->getValue();
if (preg_match('/direction$/im', $property)) {
$rule->setValue($this->swapLtrRtl($value));
} else if (preg_match('/left/im', $property)) {
$rule->setRule(str_replace('left', 'right', $property));
} else if (preg_match('/right/im', $property)) {
$rule->setRule(str_replace('right', 'left', $property));
} else if (preg_match('/transition(-property)?$/i', $property)) {
$rule->setValue($this->swapLeftRight($value));
} else if (preg_match('/float|clear|text-align/i', $property)) {
$rule->setValue($this->swapLeftRight($value));
} else if (preg_match('/^(margin|padding|border-(color|style|width))$/i', $property)) {
if ($value instanceof RuleValueList) {
$values = $value->getListComponents();
$count = count($values);
if ($count == 4) {
$right = $values[3];
$values[3] = $values[1];
$values[1] = $right;
}
$value->setListComponents($values);
}
} else if (preg_match('/border-radius/i', $property)) {
if ($value instanceof RuleValueList) {
// Border radius can contain two lists separated by a slash.
$groups = $value->getListComponents();
if ($value->getListSeparator() !== '/') {
$groups = [$value];
}
foreach ($groups as $group) {
$values = $group->getListComponents();
switch (count($values)) {
case 2:
$group->setListComponents(array_reverse($values));
break;
case 3:
$group->setListComponents([$values[1], $values[0], $values[1], $values[2]]);
break;
case 4:
$group->setListComponents([$values[1], $values[0], $values[3], $values[2]]);
break;
}
}
}
} else if (preg_match('/shadow/i', $property)) {
// TODO Fix upstream, each shadow should be in a RuleValueList.
if ($value instanceof RuleValueList) {
// negate($value->getListComponents()[0]);
}
} else if (preg_match('/transform-origin/i', $property)) {
$this->processTransformOrigin($rule);
} else if (preg_match('/^(?!text\-).*?transform$/i', $property)) {
// TODO Parse function parameters first.
} else if (preg_match('/background(-position(-x)?|-image)?$/i', $property)) {
$this->processBackground($rule);
} else if (preg_match('/cursor/i', $property)) {
$hasList = false;
$parts = [$value];
if ($value instanceof RuleValueList) {
$hastList = true;
$parts = $value->getListComponents();
}
foreach ($parts as $key => $part) {
if (!is_object($part)) {
$parts[$key] = preg_replace_callback('/\b(ne|nw|se|sw|nesw|nwse)-resize/', function($matches) {
return str_replace($matches[1], str_replace(['e', 'w', '*'], ['*', 'e', 'w'], $matches[1]), $matches[0]);
}, $part);
}
}
if ($hasList) {
$value->setListComponents($parts);
} else {
$rule->setValue($parts[0]);
}
}
}
protected function processTransformOrigin(Rule $rule) {
$value = $rule->getValue();
$foundLeftOrRight = false;
// Search for left or right.
$parts = [$value];
if ($value instanceof RuleValueList) {
$parts = $value->getListComponents();
$isInList = true;
}
foreach ($parts as $key => $part) {
if (!is_object($part) && preg_match('/left|right/i', $part)) {
$foundLeftOrRight = true;
$parts[$key] = $this->swapLeftRight($part);
}
}
if ($foundLeftOrRight) {
// We need to reconstruct the value because left/right are not represented by an object.
$list = new RuleValueList(' ');
$list->setListComponents($parts);
$rule->setValue($list);
} else {
$value = $parts[0];
// The first value may be referencing top or bottom (y instead of x).
if (!is_object($value) && preg_match('/top|bottom/i', $value)) {
$value = $parts[1];
}
// Flip the value.
if ($value instanceof Size) {
if ($value->getSize() == 0) {
$value->setSize(100);
$value->setUnit('%');
} else if ($value->getUnit() === '%') {
$this->complement($value);
}
} else if ($value instanceof CSSFunction && strpos($value->getName(), 'calc') !== false) {
// TODO Fix upstream calc parsing.
$this->complement($value);
}
}
}
protected function shouldAddCss() {
if (!empty($this->shouldAddCss)) {
$css = $this->shouldAddCss;
$this->shouldAddCss = [];
return $css;
}
return [];
}
protected function shouldIgnoreNext() {
if ($this->shouldIgnore) {
if (is_int($this->shouldIgnore)) {
$this->shouldIgnore--;
}
return true;
}
return false;
}
protected function shouldRemoveNext() {
if ($this->shouldRemove) {
if (is_int($this->shouldRemove)) {
$this->shouldRemove--;
}
return true;
}
return false;
}
protected function swap($value, $a, $b, $options = ['scope' => '*', 'ignoreCase' => true]) {
$expr = preg_quote($a) . '|' . preg_quote($b);
if (!empty($options['greedy'])) {
$expr = '\\b(' . $expr . ')\\b';
}
$flags = !empty($options['ignoreCase']) ? 'im' : 'm';
$expr = "/$expr/$flags";
return preg_replace_callback($expr, function($matches) use ($a, $b, $options) {
return $this->compare($matches[0], $a, !empty($options['ignoreCase'])) ? $b : $a;
}, $value);
}
protected function swapLeftRight($value) {
return $this->swap($value, 'left', 'right');
}
protected function swapLtrRtl($value) {
return $this->swap($value, 'ltr', 'rtl');
}
}