CSSFunction.php
2 years ago
CSSString.php
4 years ago
CalcFunction.php
2 years ago
CalcRuleValueList.php
4 years ago
Color.php
2 years ago
LineName.php
4 years ago
PrimitiveValue.php
4 years ago
RuleValueList.php
4 years ago
Size.php
2 years ago
URL.php
2 years ago
Value.php
2 years ago
ValueList.php
4 years ago
index.php
3 years ago
Size.php
122 lines
| 1 | <?php |
| 2 | namespace MailPoetVendor\Sabberworm\CSS\Value; |
| 3 | if (!defined('ABSPATH')) exit; |
| 4 | use MailPoetVendor\Sabberworm\CSS\OutputFormat; |
| 5 | use MailPoetVendor\Sabberworm\CSS\Parsing\ParserState; |
| 6 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedEOFException; |
| 7 | use MailPoetVendor\Sabberworm\CSS\Parsing\UnexpectedTokenException; |
| 8 | class Size extends PrimitiveValue |
| 9 | { |
| 10 | const ABSOLUTE_SIZE_UNITS = ['px', 'cm', 'mm', 'mozmm', 'in', 'pt', 'pc', 'vh', 'vw', 'vmin', 'vmax', 'rem']; |
| 11 | const RELATIVE_SIZE_UNITS = ['%', 'em', 'ex', 'ch', 'fr']; |
| 12 | const NON_SIZE_UNITS = ['deg', 'grad', 'rad', 's', 'ms', 'turn', 'Hz', 'kHz']; |
| 13 | private static $SIZE_UNITS = null; |
| 14 | private $fSize; |
| 15 | private $sUnit; |
| 16 | private $bIsColorComponent; |
| 17 | public function __construct($fSize, $sUnit = null, $bIsColorComponent = \false, $iLineNo = 0) |
| 18 | { |
| 19 | parent::__construct($iLineNo); |
| 20 | $this->fSize = (float) $fSize; |
| 21 | $this->sUnit = $sUnit; |
| 22 | $this->bIsColorComponent = $bIsColorComponent; |
| 23 | } |
| 24 | public static function parse(ParserState $oParserState, $bIsColorComponent = \false) |
| 25 | { |
| 26 | $sSize = ''; |
| 27 | if ($oParserState->comes('-')) { |
| 28 | $sSize .= $oParserState->consume('-'); |
| 29 | } |
| 30 | while (\is_numeric($oParserState->peek()) || $oParserState->comes('.') || $oParserState->comes('e', \true)) { |
| 31 | if ($oParserState->comes('.')) { |
| 32 | $sSize .= $oParserState->consume('.'); |
| 33 | } elseif ($oParserState->comes('e', \true)) { |
| 34 | $sLookahead = $oParserState->peek(1, 1); |
| 35 | if (\is_numeric($sLookahead) || $sLookahead === '+' || $sLookahead === '-') { |
| 36 | $sSize .= $oParserState->consume(2); |
| 37 | } else { |
| 38 | break; |
| 39 | // Reached the unit part of the number like "em" or "ex" |
| 40 | } |
| 41 | } else { |
| 42 | $sSize .= $oParserState->consume(1); |
| 43 | } |
| 44 | } |
| 45 | $sUnit = null; |
| 46 | $aSizeUnits = self::getSizeUnits(); |
| 47 | foreach ($aSizeUnits as $iLength => &$aValues) { |
| 48 | $sKey = \strtolower($oParserState->peek($iLength)); |
| 49 | if (\array_key_exists($sKey, $aValues)) { |
| 50 | if (($sUnit = $aValues[$sKey]) !== null) { |
| 51 | $oParserState->consume($iLength); |
| 52 | break; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | return new Size((float) $sSize, $sUnit, $bIsColorComponent, $oParserState->currentLine()); |
| 57 | } |
| 58 | private static function getSizeUnits() |
| 59 | { |
| 60 | if (!\is_array(self::$SIZE_UNITS)) { |
| 61 | self::$SIZE_UNITS = []; |
| 62 | foreach (\array_merge(self::ABSOLUTE_SIZE_UNITS, self::RELATIVE_SIZE_UNITS, self::NON_SIZE_UNITS) as $val) { |
| 63 | $iSize = \strlen($val); |
| 64 | if (!isset(self::$SIZE_UNITS[$iSize])) { |
| 65 | self::$SIZE_UNITS[$iSize] = []; |
| 66 | } |
| 67 | self::$SIZE_UNITS[$iSize][\strtolower($val)] = $val; |
| 68 | } |
| 69 | \krsort(self::$SIZE_UNITS, \SORT_NUMERIC); |
| 70 | } |
| 71 | return self::$SIZE_UNITS; |
| 72 | } |
| 73 | public function setUnit($sUnit) |
| 74 | { |
| 75 | $this->sUnit = $sUnit; |
| 76 | } |
| 77 | public function getUnit() |
| 78 | { |
| 79 | return $this->sUnit; |
| 80 | } |
| 81 | public function setSize($fSize) |
| 82 | { |
| 83 | $this->fSize = (float) $fSize; |
| 84 | } |
| 85 | public function getSize() |
| 86 | { |
| 87 | return $this->fSize; |
| 88 | } |
| 89 | public function isColorComponent() |
| 90 | { |
| 91 | return $this->bIsColorComponent; |
| 92 | } |
| 93 | public function isSize() |
| 94 | { |
| 95 | if (\in_array($this->sUnit, self::NON_SIZE_UNITS, \true)) { |
| 96 | return \false; |
| 97 | } |
| 98 | return !$this->isColorComponent(); |
| 99 | } |
| 100 | public function isRelative() |
| 101 | { |
| 102 | if (\in_array($this->sUnit, self::RELATIVE_SIZE_UNITS, \true)) { |
| 103 | return \true; |
| 104 | } |
| 105 | if ($this->sUnit === null && $this->fSize != 0) { |
| 106 | return \true; |
| 107 | } |
| 108 | return \false; |
| 109 | } |
| 110 | public function __toString() |
| 111 | { |
| 112 | return $this->render(new OutputFormat()); |
| 113 | } |
| 114 | public function render(OutputFormat $oOutputFormat) |
| 115 | { |
| 116 | $l = \localeconv(); |
| 117 | $sPoint = \preg_quote($l['decimal_point'], '/'); |
| 118 | $sSize = \preg_match("/[\\d\\.]+e[+-]?\\d+/i", (string) $this->fSize) ? \preg_replace("/{$sPoint}?0+\$/", "", \sprintf("%f", $this->fSize)) : $this->fSize; |
| 119 | return \preg_replace(["/{$sPoint}/", "/^(-?)0\\./"], ['.', '$1.'], $sSize) . ($this->sUnit === null ? '' : $this->sUnit); |
| 120 | } |
| 121 | } |
| 122 |