Skip to content

Commit f8f45b3

Browse files
committed
Fix shadow flipping
1 parent 49984e3 commit f8f45b3

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/MoodleHQ/RTLCSS/Transformation/FlipShadow.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
<?php
22
namespace MoodleHQ\RTLCSS\Transformation;
33

4+
use MoodleHQ\RTLCSS\Transformation\Operation\SizeFlipper;
45
use Sabberworm\CSS\Rule\Rule;
56
use Sabberworm\CSS\Value\RuleValueList;
7+
use Sabberworm\CSS\Value\Size;
68

79
class FlipShadow implements TransformationInterface
810
{
11+
/**
12+
* @var SizeFlipper
13+
*/
14+
private $sizeFlipper;
15+
16+
public function __construct()
17+
{
18+
$this->sizeFlipper = new SizeFlipper();
19+
}
20+
921
/**
1022
* @inheritDoc
1123
*/
@@ -21,9 +33,40 @@ public function transform(Rule $rule)
2133
{
2234
$value = $rule->getValue();
2335

24-
// TODO Fix upstream, each shadow should be in a RuleValueList.
36+
// Flip X offset
37+
if ($value instanceof RuleValueList) {
38+
$parameters = $value->getListComponents();
39+
$index = $this->getOffsetXIndex($rule);
40+
/** @var Size $oldX */
41+
$oldX = $parameters[$index];
42+
$parameters[$index] = $this->sizeFlipper->invertSize($oldX);
43+
44+
$value->setListComponents($parameters);
45+
}
46+
}
47+
48+
/**
49+
* Retrieve the index of the X offset within the rule values
50+
*
51+
* @param Rule $rule
52+
*
53+
* @return int
54+
* @throws TransformationException
55+
*/
56+
private function getOffsetXIndex(Rule $rule)
57+
{
58+
$property = $rule->getRule();
59+
$value = $rule->getValue();
2560
if ($value instanceof RuleValueList) {
26-
// negate($value->getListComponents()[0]);
61+
$parameters = $value->getListComponents();
62+
// the offset X parameter can be either the 1st or 2nd parameter
63+
foreach ([0, 1] as $i) {
64+
if (isset($parameters[$i]) && $parameters[$i] instanceof Size) {
65+
return $i;
66+
}
67+
}
2768
}
69+
70+
throw new TransformationException("Invalid value for \"$property\"");
2871
}
2972
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
namespace MoodleHQ\RTLCSS\Transformation\Operation;
3+
4+
use Sabberworm\CSS\Value\Size;
5+
6+
/**
7+
* Flips sizes
8+
*/
9+
class SizeFlipper
10+
{
11+
/**
12+
* Inverts a size by multiplying it by -1
13+
*
14+
* @param Size $size
15+
*
16+
* @return Size Flipped size
17+
*/
18+
public function invertSize(Size $size)
19+
{
20+
$scalarSize = $size->getSize();
21+
if ($scalarSize === 0.0) {
22+
return $size;
23+
}
24+
25+
return new Size(-1 * $scalarSize, $size->getUnit(), $size->isColorComponent());
26+
}
27+
}

0 commit comments

Comments
 (0)