PluginProbe
WooCommerce / 11.1.0
WooCommerce v11.1.0
11.1.0 11.1.0-rc.2 11.1.0-rc.1 11.1.0-beta.2 11.1.0-beta.1 11.0.1 11.0.0 11.0.0-rc.3 11.0.0-rc.2 11.0.0-rc.1 11.0.0-beta.2 11.0.0-beta.1 10.9.4 10.9.3 10.9.2 10.9.1 10.9.0 10.9.0-rc.1 10.9.0-beta.2 10.9.0-beta.1 10.8.1 10.8.0 10.8.0-rc.1 10.8.0-beta.2 10.8.0-beta.1 All 648 releases
woocommerce / lib / packages / Sabberworm / CSS / Value / Size.php
Size.php
248 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Automattic\WooCommerce\Vendor\Sabberworm\CSS\Value;
4
5 use Automattic\WooCommerce\Vendor\Sabberworm\CSS\OutputFormat;
6 use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\ParserState;
7 use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\UnexpectedEOFException;
8 use Automattic\WooCommerce\Vendor\Sabberworm\CSS\Parsing\UnexpectedTokenException;
9
10 /**
11 * A `Size` consists of a numeric `size` value and a unit.
12 */
13 class Size extends PrimitiveValue
14 {
15 /**
16 * vh/vw/vm(ax)/vmin/rem are absolute insofar as they don’t scale to the immediate parent (only the viewport)
17 *
18 * @var array<int, string>
19 *
20 * @internal
21 */
22 const ABSOLUTE_SIZE_UNITS = [
23 'px',
24 'pt',
25 'pc',
26 'cm',
27 'mm',
28 'mozmm',
29 'in',
30 'vh',
31 'dvh',
32 'svh',
33 'lvh',
34 'vw',
35 'vmin',
36 'vmax',
37 'rem',
38 ];
39
40 /**
41 * @var array<int, string>
42 *
43 * @internal
44 */
45 const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr'];
46
47 /**
48 * @var array<int, string>
49 *
50 * @internal
51 */
52 const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turn', 'Hz', 'kHz'];
53
54 /**
55 * @var array<int, array<string, string>>|null
56 */
57 private static $SIZE_UNITS = null;
58
59 /**
60 * @var float
61 */
62 private $fSize;
63
64 /**
65 * @var string|null
66 */
67 private $sUnit;
68
69 /**
70 * @var bool
71 */
72 private $bIsColorComponent;
73
74 /**
75 * @param float|int|string $fSize
76 * @param string|null $sUnit
77 * @param bool $bIsColorComponent
78 * @param int $iLineNo
79 */
80 public function __construct($fSize, $sUnit = null, $bIsColorComponent = false, $iLineNo = 0)
81 {
82 parent::__construct($iLineNo);
83 $this->fSize = (float)$fSize;
84 $this->sUnit = $sUnit;
85 $this->bIsColorComponent = $bIsColorComponent;
86 }
87
88 /**
89 * @param bool $bIsColorComponent
90 *
91 * @return Size
92 *
93 * @throws UnexpectedEOFException
94 * @throws UnexpectedTokenException
95 *
96 * @internal since V8.8.0
97 */
98 public static function parse(ParserState $oParserState, $bIsColorComponent = false)
99 {
100 $sSize = '';
101 if ($oParserState->comes('-')) {
102 $sSize .= $oParserState->consume('-');
103 }
104 while (is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e', true)) {
105 if ($oParserState->comes('.')) {
106 $sSize .= $oParserState->consume('.');
107 } elseif ($oParserState->comes('e', true)) {
108 $sLookahead = $oParserState->peek(1, 1);
109 if (is_numeric($sLookahead) || $sLookahead === '+' || $sLookahead === '-') {
110 $sSize .= $oParserState->consume(2);
111 } else {
112 break; // Reached the unit part of the number like "em" or "ex"
113 }
114 } else {
115 $sSize .= $oParserState->consume(1);
116 }
117 }
118
119 $sUnit = null;
120 $aSizeUnits = self::getSizeUnits();
121 foreach ($aSizeUnits as $iLength => &$aValues) {
122 $sKey = strtolower($oParserState->peek($iLength));
123 if (array_key_exists($sKey, $aValues)) {
124 if (($sUnit = $aValues[$sKey]) !== null) {
125 $oParserState->consume($iLength);
126 break;
127 }
128 }
129 }
130 return new Size((float)$sSize, $sUnit, $bIsColorComponent, $oParserState->currentLine());
131 }
132
133 /**
134 * @return array<int, array<string, string>>
135 */
136 private static function getSizeUnits()
137 {
138 if (!is_array(self::$SIZE_UNITS)) {
139 self::$SIZE_UNITS = [];
140 foreach (array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS) as $val) {
141 $iSize = strlen($val);
142 if (!isset(self::$SIZE_UNITS[$iSize])) {
143 self::$SIZE_UNITS[$iSize] = [];
144 }
145 self::$SIZE_UNITS[$iSize][strtolower($val)] = $val;
146 }
147
148 krsort(self::$SIZE_UNITS, SORT_NUMERIC);
149 }
150
151 return self::$SIZE_UNITS;
152 }
153
154 /**
155 * @param string $sUnit
156 *
157 * @return void
158 */
159 public function setUnit($sUnit)
160 {
161 $this->sUnit = $sUnit;
162 }
163
164 /**
165 * @return string|null
166 */
167 public function getUnit()
168 {
169 return $this->sUnit;
170 }
171
172 /**
173 * @param float|int|string $fSize
174 */
175 public function setSize($fSize)
176 {
177 $this->fSize = (float)$fSize;
178 }
179
180 /**
181 * @return float
182 */
183 public function getSize()
184 {
185 return $this->fSize;
186 }
187
188 /**
189 * @return bool
190 */
191 public function isColorComponent()
192 {
193 return $this->bIsColorComponent;
194 }
195
196 /**
197 * Returns whether the number stored in this Size really represents a size (as in a length of something on screen).
198 *
199 * @return false if the unit an angle, a duration, a frequency or the number is a component in a Color object.
200 */
201 public function isSize()
202 {
203 if (in_array($this->sUnit, self::NON_SIZE_UNITS, true)) {
204 return false;
205 }
206 return !$this->isColorComponent();
207 }
208
209 /**
210 * @return bool
211 */
212 public function isRelative()
213 {
214 if (in_array($this->sUnit, self::RELATIVE_SIZE_UNITS, true)) {
215 return true;
216 }
217 if ($this->sUnit === null && $this->fSize != 0) {
218 return true;
219 }
220 return false;
221 }
222
223 /**
224 * @return string
225 *
226 * @deprecated in V8.8.0, will be removed in V9.0.0. Use `render` instead.
227 */
228 public function __toString()
229 {
230 return $this->render(new OutputFormat());
231 }
232
233 /**
234 * @param OutputFormat|null $oOutputFormat
235 *
236 * @return string
237 */
238 public function render($oOutputFormat)
239 {
240 $l = localeconv();
241 $sPoint = preg_quote($l['decimal_point'], '/');
242 $sSize = preg_match("/[\d\.]+e[+-]?\d+/i", (string)$this->fSize)
243 ? preg_replace("/$sPoint?0+$/", "", sprintf("%f", $this->fSize)) : (string)$this->fSize;
244 return preg_replace(["/$sPoint/", "/^(-?)0\./"], ['.', '$1.'], $sSize)
245 . ($this->sUnit === null ? '' : $this->sUnit);
246 }
247 }
248