Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace Sabberworm\CSS\RuleSet;
4
 
5
use Sabberworm\CSS\CSSList\CSSList;
6
use Sabberworm\CSS\CSSList\KeyFrame;
7
use Sabberworm\CSS\OutputFormat;
8
use Sabberworm\CSS\Parsing\OutputException;
9
use Sabberworm\CSS\Parsing\ParserState;
10
use Sabberworm\CSS\Parsing\UnexpectedEOFException;
11
use Sabberworm\CSS\Parsing\UnexpectedTokenException;
12
use Sabberworm\CSS\Property\KeyframeSelector;
13
use Sabberworm\CSS\Property\Selector;
14
use Sabberworm\CSS\Rule\Rule;
15
use Sabberworm\CSS\Value\Color;
16
use Sabberworm\CSS\Value\RuleValueList;
17
use Sabberworm\CSS\Value\Size;
18
use Sabberworm\CSS\Value\URL;
19
use Sabberworm\CSS\Value\Value;
20
 
21
/**
22
 * This class represents a `RuleSet` constrained by a `Selector`.
23
 *
24
 * It contains an array of selector objects (comma-separated in the CSS) as well as the rules to be applied to the
25
 * matching elements.
26
 *
27
 * Declaration blocks usually appear directly inside a `Document` or another `CSSList` (mostly a `MediaQuery`).
28
 */
29
class DeclarationBlock extends RuleSet
30
{
31
    /**
32
     * @var array<int, Selector|string>
33
     */
34
    private $aSelectors;
35
 
36
    /**
37
     * @param int $iLineNo
38
     */
39
    public function __construct($iLineNo = 0)
40
    {
41
        parent::__construct($iLineNo);
42
        $this->aSelectors = [];
43
    }
44
 
45
    /**
46
     * @param CSSList|null $oList
47
     *
48
     * @return DeclarationBlock|false
49
     *
50
     * @throws UnexpectedTokenException
51
     * @throws UnexpectedEOFException
52
     */
53
    public static function parse(ParserState $oParserState, $oList = null)
54
    {
55
        $aComments = [];
56
        $oResult = new DeclarationBlock($oParserState->currentLine());
57
        try {
58
            $aSelectorParts = [];
59
            $sStringWrapperChar = false;
60
            do {
61
                $aSelectorParts[] = $oParserState->consume(1)
62
                    . $oParserState->consumeUntil(['{', '}', '\'', '"'], false, false, $aComments);
63
                if (in_array($oParserState->peek(), ['\'', '"']) && substr(end($aSelectorParts), -1) != "\\") {
64
                    if ($sStringWrapperChar === false) {
65
                        $sStringWrapperChar = $oParserState->peek();
66
                    } elseif ($sStringWrapperChar == $oParserState->peek()) {
67
                        $sStringWrapperChar = false;
68
                    }
69
                }
70
            } while (!in_array($oParserState->peek(), ['{', '}']) || $sStringWrapperChar !== false);
71
            $oResult->setSelectors(implode('', $aSelectorParts), $oList);
72
            if ($oParserState->comes('{')) {
73
                $oParserState->consume(1);
74
            }
75
        } catch (UnexpectedTokenException $e) {
76
            if ($oParserState->getSettings()->bLenientParsing) {
77
                if (!$oParserState->comes('}')) {
78
                    $oParserState->consumeUntil('}', false, true);
79
                }
80
                return false;
81
            } else {
82
                throw $e;
83
            }
84
        }
85
        $oResult->setComments($aComments);
86
        RuleSet::parseRuleSet($oParserState, $oResult);
87
        return $oResult;
88
    }
89
 
90
    /**
91
     * @param array<int, Selector|string>|string $mSelector
92
     * @param CSSList|null $oList
93
     *
94
     * @throws UnexpectedTokenException
95
     */
96
    public function setSelectors($mSelector, $oList = null)
97
    {
98
        if (is_array($mSelector)) {
99
            $this->aSelectors = $mSelector;
100
        } else {
101
            $this->aSelectors = explode(',', $mSelector);
102
        }
103
        foreach ($this->aSelectors as $iKey => $mSelector) {
104
            if (!($mSelector instanceof Selector)) {
105
                if ($oList === null || !($oList instanceof KeyFrame)) {
106
                    if (!Selector::isValid($mSelector)) {
107
                        throw new UnexpectedTokenException(
108
                            "Selector did not match '" . Selector::SELECTOR_VALIDATION_RX . "'.",
109
                            $mSelector,
110
                            "custom"
111
                        );
112
                    }
113
                    $this->aSelectors[$iKey] = new Selector($mSelector);
114
                } else {
115
                    if (!KeyframeSelector::isValid($mSelector)) {
116
                        throw new UnexpectedTokenException(
117
                            "Selector did not match '" . KeyframeSelector::SELECTOR_VALIDATION_RX . "'.",
118
                            $mSelector,
119
                            "custom"
120
                        );
121
                    }
122
                    $this->aSelectors[$iKey] = new KeyframeSelector($mSelector);
123
                }
124
            }
125
        }
126
    }
127
 
128
    /**
129
     * Remove one of the selectors of the block.
130
     *
131
     * @param Selector|string $mSelector
132
     *
133
     * @return bool
134
     */
135
    public function removeSelector($mSelector)
136
    {
137
        if ($mSelector instanceof Selector) {
138
            $mSelector = $mSelector->getSelector();
139
        }
140
        foreach ($this->aSelectors as $iKey => $oSelector) {
141
            if ($oSelector->getSelector() === $mSelector) {
142
                unset($this->aSelectors[$iKey]);
143
                return true;
144
            }
145
        }
146
        return false;
147
    }
148
 
149
    /**
150
     * @return array<int, Selector|string>
151
     *
152
     * @deprecated will be removed in version 9.0; use `getSelectors()` instead
153
     */
154
    public function getSelector()
155
    {
156
        return $this->getSelectors();
157
    }
158
 
159
    /**
160
     * @param Selector|string $mSelector
161
     * @param CSSList|null $oList
162
     *
163
     * @return void
164
     *
165
     * @deprecated will be removed in version 9.0; use `setSelectors()` instead
166
     */
167
    public function setSelector($mSelector, $oList = null)
168
    {
169
        $this->setSelectors($mSelector, $oList);
170
    }
171
 
172
    /**
173
     * @return array<int, Selector|string>
174
     */
175
    public function getSelectors()
176
    {
177
        return $this->aSelectors;
178
    }
179
 
180
    /**
181
     * Splits shorthand declarations (e.g. `margin` or `font`) into their constituent parts.
182
     *
183
     * @return void
184
     *
185
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
186
     */
187
    public function expandShorthands()
188
    {
189
        // border must be expanded before dimensions
190
        $this->expandBorderShorthand();
191
        $this->expandDimensionsShorthand();
192
        $this->expandFontShorthand();
193
        $this->expandBackgroundShorthand();
194
        $this->expandListStyleShorthand();
195
    }
196
 
197
    /**
198
     * Creates shorthand declarations (e.g. `margin` or `font`) whenever possible.
199
     *
200
     * @return void
201
     *
202
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
203
     */
204
    public function createShorthands()
205
    {
206
        $this->createBackgroundShorthand();
207
        $this->createDimensionsShorthand();
208
        // border must be shortened after dimensions
209
        $this->createBorderShorthand();
210
        $this->createFontShorthand();
211
        $this->createListStyleShorthand();
212
    }
213
 
214
    /**
215
     * Splits shorthand border declarations (e.g. `border: 1px red;`).
216
     *
217
     * Additional splitting happens in expandDimensionsShorthand.
218
     *
219
     * Multiple borders are not yet supported as of 3.
220
     *
221
     * @return void
222
     *
223
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
224
     */
225
    public function expandBorderShorthand()
226
    {
227
        $aBorderRules = [
228
            'border',
229
            'border-left',
230
            'border-right',
231
            'border-top',
232
            'border-bottom',
233
        ];
234
        $aBorderSizes = [
235
            'thin',
236
            'medium',
237
            'thick',
238
        ];
239
        $aRules = $this->getRulesAssoc();
240
        foreach ($aBorderRules as $sBorderRule) {
241
            if (!isset($aRules[$sBorderRule])) {
242
                continue;
243
            }
244
            $oRule = $aRules[$sBorderRule];
245
            $mRuleValue = $oRule->getValue();
246
            $aValues = [];
247
            if (!$mRuleValue instanceof RuleValueList) {
248
                $aValues[] = $mRuleValue;
249
            } else {
250
                $aValues = $mRuleValue->getListComponents();
251
            }
252
            foreach ($aValues as $mValue) {
253
                if ($mValue instanceof Value) {
254
                    $mNewValue = clone $mValue;
255
                } else {
256
                    $mNewValue = $mValue;
257
                }
258
                if ($mValue instanceof Size) {
259
                    $sNewRuleName = $sBorderRule . "-width";
260
                } elseif ($mValue instanceof Color) {
261
                    $sNewRuleName = $sBorderRule . "-color";
262
                } else {
263
                    if (in_array($mValue, $aBorderSizes)) {
264
                        $sNewRuleName = $sBorderRule . "-width";
265
                    } else {
266
                        $sNewRuleName = $sBorderRule . "-style";
267
                    }
268
                }
269
                $oNewRule = new Rule($sNewRuleName, $oRule->getLineNo(), $oRule->getColNo());
270
                $oNewRule->setIsImportant($oRule->getIsImportant());
271
                $oNewRule->addValue([$mNewValue]);
272
                $this->addRule($oNewRule);
273
            }
274
            $this->removeRule($sBorderRule);
275
        }
276
    }
277
 
278
    /**
279
     * Splits shorthand dimensional declarations (e.g. `margin: 0px auto;`)
280
     * into their constituent parts.
281
     *
282
     * Handles `margin`, `padding`, `border-color`, `border-style` and `border-width`.
283
     *
284
     * @return void
285
     *
286
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
287
     */
288
    public function expandDimensionsShorthand()
289
    {
290
        $aExpansions = [
291
            'margin' => 'margin-%s',
292
            'padding' => 'padding-%s',
293
            'border-color' => 'border-%s-color',
294
            'border-style' => 'border-%s-style',
295
            'border-width' => 'border-%s-width',
296
        ];
297
        $aRules = $this->getRulesAssoc();
298
        foreach ($aExpansions as $sProperty => $sExpanded) {
299
            if (!isset($aRules[$sProperty])) {
300
                continue;
301
            }
302
            $oRule = $aRules[$sProperty];
303
            $mRuleValue = $oRule->getValue();
304
            $aValues = [];
305
            if (!$mRuleValue instanceof RuleValueList) {
306
                $aValues[] = $mRuleValue;
307
            } else {
308
                $aValues = $mRuleValue->getListComponents();
309
            }
310
            $top = $right = $bottom = $left = null;
311
            switch (count($aValues)) {
312
                case 1:
313
                    $top = $right = $bottom = $left = $aValues[0];
314
                    break;
315
                case 2:
316
                    $top = $bottom = $aValues[0];
317
                    $left = $right = $aValues[1];
318
                    break;
319
                case 3:
320
                    $top = $aValues[0];
321
                    $left = $right = $aValues[1];
322
                    $bottom = $aValues[2];
323
                    break;
324
                case 4:
325
                    $top = $aValues[0];
326
                    $right = $aValues[1];
327
                    $bottom = $aValues[2];
328
                    $left = $aValues[3];
329
                    break;
330
            }
331
            foreach (['top', 'right', 'bottom', 'left'] as $sPosition) {
332
                $oNewRule = new Rule(sprintf($sExpanded, $sPosition), $oRule->getLineNo(), $oRule->getColNo());
333
                $oNewRule->setIsImportant($oRule->getIsImportant());
334
                $oNewRule->addValue(${$sPosition});
335
                $this->addRule($oNewRule);
336
            }
337
            $this->removeRule($sProperty);
338
        }
339
    }
340
 
341
    /**
342
     * Converts shorthand font declarations
343
     * (e.g. `font: 300 italic 11px/14px verdana, helvetica, sans-serif;`)
344
     * into their constituent parts.
345
     *
346
     * @return void
347
     *
348
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
349
     */
350
    public function expandFontShorthand()
351
    {
352
        $aRules = $this->getRulesAssoc();
353
        if (!isset($aRules['font'])) {
354
            return;
355
        }
356
        $oRule = $aRules['font'];
357
        // reset properties to 'normal' per http://www.w3.org/TR/21/fonts.html#font-shorthand
358
        $aFontProperties = [
359
            'font-style' => 'normal',
360
            'font-variant' => 'normal',
361
            'font-weight' => 'normal',
362
            'font-size' => 'normal',
363
            'line-height' => 'normal',
364
        ];
365
        $mRuleValue = $oRule->getValue();
366
        $aValues = [];
367
        if (!$mRuleValue instanceof RuleValueList) {
368
            $aValues[] = $mRuleValue;
369
        } else {
370
            $aValues = $mRuleValue->getListComponents();
371
        }
372
        foreach ($aValues as $mValue) {
373
            if (!$mValue instanceof Value) {
374
                $mValue = mb_strtolower($mValue);
375
            }
376
            if (in_array($mValue, ['normal', 'inherit'])) {
377
                foreach (['font-style', 'font-weight', 'font-variant'] as $sProperty) {
378
                    if (!isset($aFontProperties[$sProperty])) {
379
                        $aFontProperties[$sProperty] = $mValue;
380
                    }
381
                }
382
            } elseif (in_array($mValue, ['italic', 'oblique'])) {
383
                $aFontProperties['font-style'] = $mValue;
384
            } elseif ($mValue == 'small-caps') {
385
                $aFontProperties['font-variant'] = $mValue;
386
            } elseif (
387
                in_array($mValue, ['bold', 'bolder', 'lighter'])
388
                || ($mValue instanceof Size
389
                    && in_array($mValue->getSize(), range(100, 900, 100)))
390
            ) {
391
                $aFontProperties['font-weight'] = $mValue;
392
            } elseif ($mValue instanceof RuleValueList && $mValue->getListSeparator() == '/') {
393
                list($oSize, $oHeight) = $mValue->getListComponents();
394
                $aFontProperties['font-size'] = $oSize;
395
                $aFontProperties['line-height'] = $oHeight;
396
            } elseif ($mValue instanceof Size && $mValue->getUnit() !== null) {
397
                $aFontProperties['font-size'] = $mValue;
398
            } else {
399
                $aFontProperties['font-family'] = $mValue;
400
            }
401
        }
402
        foreach ($aFontProperties as $sProperty => $mValue) {
403
            $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
404
            $oNewRule->addValue($mValue);
405
            $oNewRule->setIsImportant($oRule->getIsImportant());
406
            $this->addRule($oNewRule);
407
        }
408
        $this->removeRule('font');
409
    }
410
 
411
    /**
412
     * Converts shorthand background declarations
413
     * (e.g. `background: url("chess.png") gray 50% repeat fixed;`)
414
     * into their constituent parts.
415
     *
416
     * @see http://www.w3.org/TR/21/colors.html#propdef-background
417
     *
418
     * @return void
419
     *
420
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
421
     */
422
    public function expandBackgroundShorthand()
423
    {
424
        $aRules = $this->getRulesAssoc();
425
        if (!isset($aRules['background'])) {
426
            return;
427
        }
428
        $oRule = $aRules['background'];
429
        $aBgProperties = [
430
            'background-color' => ['transparent'],
431
            'background-image' => ['none'],
432
            'background-repeat' => ['repeat'],
433
            'background-attachment' => ['scroll'],
434
            'background-position' => [
435
                new Size(0, '%', null, false, $this->iLineNo),
436
                new Size(0, '%', null, false, $this->iLineNo),
437
            ],
438
        ];
439
        $mRuleValue = $oRule->getValue();
440
        $aValues = [];
441
        if (!$mRuleValue instanceof RuleValueList) {
442
            $aValues[] = $mRuleValue;
443
        } else {
444
            $aValues = $mRuleValue->getListComponents();
445
        }
446
        if (count($aValues) == 1 && $aValues[0] == 'inherit') {
447
            foreach ($aBgProperties as $sProperty => $mValue) {
448
                $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
449
                $oNewRule->addValue('inherit');
450
                $oNewRule->setIsImportant($oRule->getIsImportant());
451
                $this->addRule($oNewRule);
452
            }
453
            $this->removeRule('background');
454
            return;
455
        }
456
        $iNumBgPos = 0;
457
        foreach ($aValues as $mValue) {
458
            if (!$mValue instanceof Value) {
459
                $mValue = mb_strtolower($mValue);
460
            }
461
            if ($mValue instanceof URL) {
462
                $aBgProperties['background-image'] = $mValue;
463
            } elseif ($mValue instanceof Color) {
464
                $aBgProperties['background-color'] = $mValue;
465
            } elseif (in_array($mValue, ['scroll', 'fixed'])) {
466
                $aBgProperties['background-attachment'] = $mValue;
467
            } elseif (in_array($mValue, ['repeat', 'no-repeat', 'repeat-x', 'repeat-y'])) {
468
                $aBgProperties['background-repeat'] = $mValue;
469
            } elseif (
470
                in_array($mValue, ['left', 'center', 'right', 'top', 'bottom'])
471
                || $mValue instanceof Size
472
            ) {
473
                if ($iNumBgPos == 0) {
474
                    $aBgProperties['background-position'][0] = $mValue;
475
                    $aBgProperties['background-position'][1] = 'center';
476
                } else {
477
                    $aBgProperties['background-position'][$iNumBgPos] = $mValue;
478
                }
479
                $iNumBgPos++;
480
            }
481
        }
482
        foreach ($aBgProperties as $sProperty => $mValue) {
483
            $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
484
            $oNewRule->setIsImportant($oRule->getIsImportant());
485
            $oNewRule->addValue($mValue);
486
            $this->addRule($oNewRule);
487
        }
488
        $this->removeRule('background');
489
    }
490
 
491
    /**
492
     * @return void
493
     *
494
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
495
     */
496
    public function expandListStyleShorthand()
497
    {
498
        $aListProperties = [
499
            'list-style-type' => 'disc',
500
            'list-style-position' => 'outside',
501
            'list-style-image' => 'none',
502
        ];
503
        $aListStyleTypes = [
504
            'none',
505
            'disc',
506
            'circle',
507
            'square',
508
            'decimal-leading-zero',
509
            'decimal',
510
            'lower-roman',
511
            'upper-roman',
512
            'lower-greek',
513
            'lower-alpha',
514
            'lower-latin',
515
            'upper-alpha',
516
            'upper-latin',
517
            'hebrew',
518
            'armenian',
519
            'georgian',
520
            'cjk-ideographic',
521
            'hiragana',
522
            'hira-gana-iroha',
523
            'katakana-iroha',
524
            'katakana',
525
        ];
526
        $aListStylePositions = [
527
            'inside',
528
            'outside',
529
        ];
530
        $aRules = $this->getRulesAssoc();
531
        if (!isset($aRules['list-style'])) {
532
            return;
533
        }
534
        $oRule = $aRules['list-style'];
535
        $mRuleValue = $oRule->getValue();
536
        $aValues = [];
537
        if (!$mRuleValue instanceof RuleValueList) {
538
            $aValues[] = $mRuleValue;
539
        } else {
540
            $aValues = $mRuleValue->getListComponents();
541
        }
542
        if (count($aValues) == 1 && $aValues[0] == 'inherit') {
543
            foreach ($aListProperties as $sProperty => $mValue) {
544
                $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
545
                $oNewRule->addValue('inherit');
546
                $oNewRule->setIsImportant($oRule->getIsImportant());
547
                $this->addRule($oNewRule);
548
            }
549
            $this->removeRule('list-style');
550
            return;
551
        }
552
        foreach ($aValues as $mValue) {
553
            if (!$mValue instanceof Value) {
554
                $mValue = mb_strtolower($mValue);
555
            }
556
            if ($mValue instanceof Url) {
557
                $aListProperties['list-style-image'] = $mValue;
558
            } elseif (in_array($mValue, $aListStyleTypes)) {
559
                $aListProperties['list-style-types'] = $mValue;
560
            } elseif (in_array($mValue, $aListStylePositions)) {
561
                $aListProperties['list-style-position'] = $mValue;
562
            }
563
        }
564
        foreach ($aListProperties as $sProperty => $mValue) {
565
            $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
566
            $oNewRule->setIsImportant($oRule->getIsImportant());
567
            $oNewRule->addValue($mValue);
568
            $this->addRule($oNewRule);
569
        }
570
        $this->removeRule('list-style');
571
    }
572
 
573
    /**
574
     * @param array<array-key, string> $aProperties
575
     * @param string $sShorthand
576
     *
577
     * @return void
578
     *
579
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
580
     */
581
    public function createShorthandProperties(array $aProperties, $sShorthand)
582
    {
583
        $aRules = $this->getRulesAssoc();
584
        $oRule = null;
585
        $aNewValues = [];
586
        foreach ($aProperties as $sProperty) {
587
            if (!isset($aRules[$sProperty])) {
588
                continue;
589
            }
590
            $oRule = $aRules[$sProperty];
591
            if (!$oRule->getIsImportant()) {
592
                $mRuleValue = $oRule->getValue();
593
                $aValues = [];
594
                if (!$mRuleValue instanceof RuleValueList) {
595
                    $aValues[] = $mRuleValue;
596
                } else {
597
                    $aValues = $mRuleValue->getListComponents();
598
                }
599
                foreach ($aValues as $mValue) {
600
                    $aNewValues[] = $mValue;
601
                }
602
                $this->removeRule($sProperty);
603
            }
604
        }
605
        if ($aNewValues !== [] && $oRule instanceof Rule) {
606
            $oNewRule = new Rule($sShorthand, $oRule->getLineNo(), $oRule->getColNo());
607
            foreach ($aNewValues as $mValue) {
608
                $oNewRule->addValue($mValue);
609
            }
610
            $this->addRule($oNewRule);
611
        }
612
    }
613
 
614
    /**
615
     * @return void
616
     *
617
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
618
     */
619
    public function createBackgroundShorthand()
620
    {
621
        $aProperties = [
622
            'background-color',
623
            'background-image',
624
            'background-repeat',
625
            'background-position',
626
            'background-attachment',
627
        ];
628
        $this->createShorthandProperties($aProperties, 'background');
629
    }
630
 
631
    /**
632
     * @return void
633
     *
634
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
635
     */
636
    public function createListStyleShorthand()
637
    {
638
        $aProperties = [
639
            'list-style-type',
640
            'list-style-position',
641
            'list-style-image',
642
        ];
643
        $this->createShorthandProperties($aProperties, 'list-style');
644
    }
645
 
646
    /**
647
     * Combines `border-color`, `border-style` and `border-width` into `border`.
648
     *
649
     * Should be run after `create_dimensions_shorthand`!
650
     *
651
     * @return void
652
     *
653
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
654
     */
655
    public function createBorderShorthand()
656
    {
657
        $aProperties = [
658
            'border-width',
659
            'border-style',
660
            'border-color',
661
        ];
662
        $this->createShorthandProperties($aProperties, 'border');
663
    }
664
 
665
    /**
666
     * Looks for long format CSS dimensional properties
667
     * (margin, padding, border-color, border-style and border-width)
668
     * and converts them into shorthand CSS properties.
669
     *
670
     * @return void
671
     *
672
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
673
     */
674
    public function createDimensionsShorthand()
675
    {
676
        $aPositions = ['top', 'right', 'bottom', 'left'];
677
        $aExpansions = [
678
            'margin' => 'margin-%s',
679
            'padding' => 'padding-%s',
680
            'border-color' => 'border-%s-color',
681
            'border-style' => 'border-%s-style',
682
            'border-width' => 'border-%s-width',
683
        ];
684
        $aRules = $this->getRulesAssoc();
685
        foreach ($aExpansions as $sProperty => $sExpanded) {
686
            $aFoldable = [];
687
            foreach ($aRules as $sRuleName => $oRule) {
688
                foreach ($aPositions as $sPosition) {
689
                    if ($sRuleName == sprintf($sExpanded, $sPosition)) {
690
                        $aFoldable[$sRuleName] = $oRule;
691
                    }
692
                }
693
            }
694
            // All four dimensions must be present
695
            if (count($aFoldable) == 4) {
696
                $aValues = [];
697
                foreach ($aPositions as $sPosition) {
698
                    $oRule = $aRules[sprintf($sExpanded, $sPosition)];
699
                    $mRuleValue = $oRule->getValue();
700
                    $aRuleValues = [];
701
                    if (!$mRuleValue instanceof RuleValueList) {
702
                        $aRuleValues[] = $mRuleValue;
703
                    } else {
704
                        $aRuleValues = $mRuleValue->getListComponents();
705
                    }
706
                    $aValues[$sPosition] = $aRuleValues;
707
                }
708
                $oNewRule = new Rule($sProperty, $oRule->getLineNo(), $oRule->getColNo());
709
                if ((string)$aValues['left'][0] == (string)$aValues['right'][0]) {
710
                    if ((string)$aValues['top'][0] == (string)$aValues['bottom'][0]) {
711
                        if ((string)$aValues['top'][0] == (string)$aValues['left'][0]) {
712
                            // All 4 sides are equal
713
                            $oNewRule->addValue($aValues['top']);
714
                        } else {
715
                            // Top and bottom are equal, left and right are equal
716
                            $oNewRule->addValue($aValues['top']);
717
                            $oNewRule->addValue($aValues['left']);
718
                        }
719
                    } else {
720
                        // Only left and right are equal
721
                        $oNewRule->addValue($aValues['top']);
722
                        $oNewRule->addValue($aValues['left']);
723
                        $oNewRule->addValue($aValues['bottom']);
724
                    }
725
                } else {
726
                    // No sides are equal
727
                    $oNewRule->addValue($aValues['top']);
728
                    $oNewRule->addValue($aValues['left']);
729
                    $oNewRule->addValue($aValues['bottom']);
730
                    $oNewRule->addValue($aValues['right']);
731
                }
732
                $this->addRule($oNewRule);
733
                foreach ($aPositions as $sPosition) {
734
                    $this->removeRule(sprintf($sExpanded, $sPosition));
735
                }
736
            }
737
        }
738
    }
739
 
740
    /**
741
     * Looks for long format CSS font properties (e.g. `font-weight`) and
742
     * tries to convert them into a shorthand CSS `font` property.
743
     *
744
     * At least `font-size` AND `font-family` must be present in order to create a shorthand declaration.
745
     *
746
     * @return void
747
     *
748
     * @deprecated since 8.7.0, will be removed without substitution in version 9.0 in #511
749
     */
750
    public function createFontShorthand()
751
    {
752
        $aFontProperties = [
753
            'font-style',
754
            'font-variant',
755
            'font-weight',
756
            'font-size',
757
            'line-height',
758
            'font-family',
759
        ];
760
        $aRules = $this->getRulesAssoc();
761
        if (!isset($aRules['font-size']) || !isset($aRules['font-family'])) {
762
            return;
763
        }
764
        $oOldRule = isset($aRules['font-size']) ? $aRules['font-size'] : $aRules['font-family'];
765
        $oNewRule = new Rule('font', $oOldRule->getLineNo(), $oOldRule->getColNo());
766
        unset($oOldRule);
767
        foreach (['font-style', 'font-variant', 'font-weight'] as $sProperty) {
768
            if (isset($aRules[$sProperty])) {
769
                $oRule = $aRules[$sProperty];
770
                $mRuleValue = $oRule->getValue();
771
                $aValues = [];
772
                if (!$mRuleValue instanceof RuleValueList) {
773
                    $aValues[] = $mRuleValue;
774
                } else {
775
                    $aValues = $mRuleValue->getListComponents();
776
                }
777
                if ($aValues[0] !== 'normal') {
778
                    $oNewRule->addValue($aValues[0]);
779
                }
780
            }
781
        }
782
        // Get the font-size value
783
        $oRule = $aRules['font-size'];
784
        $mRuleValue = $oRule->getValue();
785
        $aFSValues = [];
786
        if (!$mRuleValue instanceof RuleValueList) {
787
            $aFSValues[] = $mRuleValue;
788
        } else {
789
            $aFSValues = $mRuleValue->getListComponents();
790
        }
791
        // But wait to know if we have line-height to add it
792
        if (isset($aRules['line-height'])) {
793
            $oRule = $aRules['line-height'];
794
            $mRuleValue = $oRule->getValue();
795
            $aLHValues = [];
796
            if (!$mRuleValue instanceof RuleValueList) {
797
                $aLHValues[] = $mRuleValue;
798
            } else {
799
                $aLHValues = $mRuleValue->getListComponents();
800
            }
801
            if ($aLHValues[0] !== 'normal') {
802
                $val = new RuleValueList('/', $this->iLineNo);
803
                $val->addListComponent($aFSValues[0]);
804
                $val->addListComponent($aLHValues[0]);
805
                $oNewRule->addValue($val);
806
            }
807
        } else {
808
            $oNewRule->addValue($aFSValues[0]);
809
        }
810
        $oRule = $aRules['font-family'];
811
        $mRuleValue = $oRule->getValue();
812
        $aFFValues = [];
813
        if (!$mRuleValue instanceof RuleValueList) {
814
            $aFFValues[] = $mRuleValue;
815
        } else {
816
            $aFFValues = $mRuleValue->getListComponents();
817
        }
818
        $oFFValue = new RuleValueList(',', $this->iLineNo);
819
        $oFFValue->setListComponents($aFFValues);
820
        $oNewRule->addValue($oFFValue);
821
 
822
        $this->addRule($oNewRule);
823
        foreach ($aFontProperties as $sProperty) {
824
            $this->removeRule($sProperty);
825
        }
826
    }
827
 
828
    /**
829
     * @return string
830
     *
831
     * @throws OutputException
832
     */
833
    public function __toString()
834
    {
835
        return $this->render(new OutputFormat());
836
    }
837
 
838
    /**
839
     * @param OutputFormat|null $oOutputFormat
840
     *
841
     * @return string
842
     *
843
     * @throws OutputException
844
     */
845
    public function render($oOutputFormat)
846
    {
847
        $sResult = $oOutputFormat->comments($this);
848
        if (count($this->aSelectors) === 0) {
849
            // If all the selectors have been removed, this declaration block becomes invalid
850
            throw new OutputException("Attempt to print declaration block with missing selector", $this->iLineNo);
851
        }
852
        $sResult .= $oOutputFormat->sBeforeDeclarationBlock;
853
        $sResult .= $oOutputFormat->implode(
854
            $oOutputFormat->spaceBeforeSelectorSeparator() . ',' . $oOutputFormat->spaceAfterSelectorSeparator(),
855
            $this->aSelectors
856
        );
857
        $sResult .= $oOutputFormat->sAfterDeclarationBlockSelectors;
858
        $sResult .= $oOutputFormat->spaceBeforeOpeningBrace() . '{';
859
        $sResult .= $this->renderRules($oOutputFormat);
860
        $sResult .= '}';
861
        $sResult .= $oOutputFormat->sAfterDeclarationBlock;
862
        return $sResult;
863
    }
864
}