| @@ -1,9 +1,9 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | 3 | * SCSSPHP |
| 4 | 4 | * |
| 5 | - * @copyright 2012-2018 Leaf Corcoran | |
| 5 | + * @copyright 2012-2015 Leaf Corcoran | |
| 6 | 6 | * |
| 7 | 7 | * @license http://opensource.org/licenses/MIT MIT |
| 8 | 8 | * |
| 9 | 9 | * @link http://leafo.github.io/scssphp |
| @@ -10,14 +10,13 @@ | ||
| 10 | 10 | */ |
| 11 | 11 | |
| 12 | 12 | namespace Leafo\ScssPhp\Node; |
| 13 | 13 | |
| 14 | -use Leafo\ScssPhp\Compiler; | |
| 15 | 14 | use Leafo\ScssPhp\Node; |
| 16 | 15 | use Leafo\ScssPhp\Type; |
| 17 | 16 | |
| 18 | 17 | /** |
| 19 | - * Dimension + optional units | |
| 18 | + * SCSS dimension + optional units | |
| 20 | 19 | * |
| 21 | 20 | * {@internal |
| 22 | 21 | * This is a work-in-progress. |
| 23 | 22 | * |
| @@ -30,9 +29,9 @@ | ||
| 30 | 29 | { |
| 31 | 30 | /** |
| 32 | 31 | * @var integer |
| 33 | 32 | */ |
| 34 | - static public $precision = 10; | |
| 33 | + static public $precision = 5; | |
| 35 | 34 | |
| 36 | 35 | /** |
| 37 | 36 | * @see http://www.w3.org/TR/2012/WD-css3-values-20120308/ |
| 38 | 37 | * |
| @@ -37,10 +36,10 @@ | ||
| 37 | 36 | * @see http://www.w3.org/TR/2012/WD-css3-values-20120308/ |
| 38 | 37 | * |
| 39 | 38 | * @var array |
| 40 | 39 | */ |
| 41 | - static protected $unitTable = [ | |
| 42 | - 'in' => [ | |
| 40 | + static protected $unitTable = array( | |
| 41 | + 'in' => array( | |
| 43 | 42 | 'in' => 1, |
| 44 | 43 | 'pc' => 6, |
| 45 | 44 | 'pt' => 72, |
| 46 | 45 | 'px' => 96, |
| @@ -46,29 +45,29 @@ | ||
| 46 | 45 | 'px' => 96, |
| 47 | 46 | 'cm' => 2.54, |
| 48 | 47 | 'mm' => 25.4, |
| 49 | 48 | 'q' => 101.6, |
| 50 | - ], | |
| 51 | - 'turn' => [ | |
| 52 | - 'deg' => 360, | |
| 53 | - 'grad' => 400, | |
| 54 | - 'rad' => 6.28318530717958647692528676, // 2 * M_PI | |
| 55 | - 'turn' => 1, | |
| 56 | - ], | |
| 57 | - 's' => [ | |
| 58 | - 's' => 1, | |
| 49 | + ), | |
| 50 | + 'turn' => array( | |
| 51 | + 'deg' => 180, | |
| 52 | + 'grad' => 200, | |
| 53 | + 'rad' => M_PI, | |
| 54 | + 'turn' => 0.5, | |
| 55 | + ), | |
| 56 | + 's' => array( | |
| 57 | + 's' => 1, | |
| 59 | 58 | 'ms' => 1000, |
| 60 | - ], | |
| 61 | - 'Hz' => [ | |
| 62 | - 'Hz' => 1, | |
| 59 | + ), | |
| 60 | + 'Hz' => array( | |
| 61 | + 'Hz' => 1, | |
| 63 | 62 | 'kHz' => 0.001, |
| 64 | - ], | |
| 65 | - 'dpi' => [ | |
| 66 | - 'dpi' => 1, | |
| 63 | + ), | |
| 64 | + 'dpi' => array( | |
| 65 | + 'dpi' => 1, | |
| 67 | 66 | 'dpcm' => 2.54, |
| 68 | 67 | 'dppx' => 96, |
| 69 | - ], | |
| 70 | - ]; | |
| 68 | + ), | |
| 69 | + ); | |
| 71 | 70 | |
| 72 | 71 | /** |
| 73 | 72 | * @var integer|float |
| 74 | 73 | */ |
| @@ -74,9 +73,9 @@ | ||
| 74 | 73 | */ |
| 75 | 74 | public $dimension; |
| 76 | 75 | |
| 77 | 76 | /** |
| 78 | - * @var array | |
| 77 | + * @var string | |
| 79 | 78 | */ |
| 80 | 79 | public $units; |
| 81 | 80 | |
| 82 | 81 | /** |
| @@ -81,19 +80,16 @@ | ||
| 81 | 80 | |
| 82 | 81 | /** |
| 83 | 82 | * Initialize number |
| 84 | 83 | * |
| 85 | - * @param mixed $dimension | |
| 86 | - * @param mixed $initialUnit | |
| 84 | + * @param mixed $dimension | |
| 85 | + * @param string $initialUnit | |
| 87 | 86 | */ |
| 88 | 87 | public function __construct($dimension, $initialUnit) |
| 89 | 88 | { |
| 90 | 89 | $this->type = Type::T_NUMBER; |
| 91 | 90 | $this->dimension = $dimension; |
| 92 | - $this->units = is_array($initialUnit) | |
| 93 | - ? $initialUnit | |
| 94 | - : ($initialUnit ? [$initialUnit => 1] | |
| 95 | - : []); | |
| 91 | + $this->units = $initialUnit; | |
| 96 | 92 | } |
| 97 | 93 | |
| 98 | 94 | /** |
| 99 | 95 | * Coerce number to target units |
| @@ -103,22 +99,15 @@ | ||
| 103 | 99 | * @return \Leafo\ScssPhp\Node\Number |
| 104 | 100 | */ |
| 105 | 101 | public function coerce($units) |
| 106 | 102 | { |
| 107 | - if ($this->unitless()) { | |
| 108 | - return new Number($this->dimension, $units); | |
| 109 | - } | |
| 103 | + $value = $this->dimension; | |
| 110 | 104 | |
| 111 | - $dimension = $this->dimension; | |
| 112 | - | |
| 113 | - foreach (static::$unitTable['in'] as $unit => $conv) { | |
| 114 | - $from = isset($this->units[$unit]) ? $this->units[$unit] : 0; | |
| 115 | - $to = isset($units[$unit]) ? $units[$unit] : 0; | |
| 116 | - $factor = pow($conv, $from - $to); | |
| 117 | - $dimension /= $factor; | |
| 105 | + if (isset(self::$unitTable[$this->units][$units])) { | |
| 106 | + $value *= self::$unitTable[$this->units][$units]; | |
| 118 | 107 | } |
| 119 | 108 | |
| 120 | - return new Number($dimension, $units); | |
| 109 | + return new Number($value, $units); | |
| 121 | 110 | } |
| 122 | 111 | |
| 123 | 112 | /** |
| 124 | 113 | * Normalize number |
| @@ -126,14 +115,15 @@ | ||
| 126 | 115 | * @return \Leafo\ScssPhp\Node\Number |
| 127 | 116 | */ |
| 128 | 117 | public function normalize() |
| 129 | 118 | { |
| 130 | - $dimension = $this->dimension; | |
| 131 | - $units = []; | |
| 119 | + if (isset(self::$unitTable['in'][$this->units])) { | |
| 120 | + $conv = self::$unitTable['in'][$this->units]; | |
| 132 | 121 | |
| 133 | - $this->normalizeUnits($dimension, $units, 'in'); | |
| 122 | + return new Number($this->dimension / $conv, 'in'); | |
| 123 | + } | |
| 134 | 124 | |
| 135 | - return new Number($dimension, $units); | |
| 125 | + return new Number($this->dimension, $this->units); | |
| 136 | 126 | } |
| 137 | 127 | |
| 138 | 128 | /** |
| 139 | 129 | * {@inheritdoc} |
| @@ -139,14 +129,10 @@ | ||
| 139 | 129 | * {@inheritdoc} |
| 140 | 130 | */ |
| 141 | 131 | public function offsetExists($offset) |
| 142 | 132 | { |
| 143 | - if ($offset === -3) { | |
| 144 | - return $this->sourceColumn !== null; | |
| 145 | - } | |
| 146 | - | |
| 147 | 133 | if ($offset === -2) { |
| 148 | - return $this->sourceLine !== null; | |
| 134 | + return $sourceIndex !== null; | |
| 149 | 135 | } |
| 150 | 136 | |
| 151 | 137 | if ($offset === -1 |
| 152 | 138 | || $offset === 0 |
| @@ -164,16 +150,13 @@ | ||
| 164 | 150 | */ |
| 165 | 151 | public function offsetGet($offset) |
| 166 | 152 | { |
| 167 | 153 | switch ($offset) { |
| 168 | - case -3: | |
| 169 | - return $this->sourceColumn; | |
| 170 | - | |
| 171 | 154 | case -2: |
| 172 | - return $this->sourceLine; | |
| 155 | + return $this->sourceIndex; | |
| 173 | 156 | |
| 174 | 157 | case -1: |
| 175 | - return $this->sourceIndex; | |
| 158 | + return $this->sourcePosition; | |
| 176 | 159 | |
| 177 | 160 | case 0: |
| 178 | 161 | return $this->type; |
| 179 | 162 | |
| @@ -194,13 +177,11 @@ | ||
| 194 | 177 | $this->dimension = $value; |
| 195 | 178 | } elseif ($offset === 2) { |
| 196 | 179 | $this->units = $value; |
| 197 | 180 | } elseif ($offset == -1) { |
| 181 | + $this->sourcePosition = $value; | |
| 182 | + } elseif ($offset == -2) { | |
| 198 | 183 | $this->sourceIndex = $value; |
| 199 | - } elseif ($offset == -2) { | |
| 200 | - $this->sourceLine = $value; | |
| 201 | - } elseif ($offset == -3) { | |
| 202 | - $this->sourceColumn = $value; | |
| 203 | 184 | } |
| 204 | 185 | } |
| 205 | 186 | |
| 206 | 187 | /** |
| @@ -212,13 +193,11 @@ | ||
| 212 | 193 | $this->dimension = null; |
| 213 | 194 | } elseif ($offset === 2) { |
| 214 | 195 | $this->units = null; |
| 215 | 196 | } elseif ($offset === -1) { |
| 197 | + $this->sourcePosition = null; | |
| 198 | + } elseif ($offset === -2) { | |
| 216 | 199 | $this->sourceIndex = null; |
| 217 | - } elseif ($offset === -2) { | |
| 218 | - $this->sourceLine = null; | |
| 219 | - } elseif ($offset === -3) { | |
| 220 | - $this->sourceColumn = null; | |
| 221 | 200 | } |
| 222 | 201 | } |
| 223 | 202 | |
| 224 | 203 | /** |
| @@ -227,9 +206,9 @@ | ||
| 227 | 206 | * @return boolean |
| 228 | 207 | */ |
| 229 | 208 | public function unitless() |
| 230 | 209 | { |
| 231 | - return ! array_sum($this->units); | |
| 210 | + return empty($this->units); | |
| 232 | 211 | } |
| 233 | 212 | |
| 234 | 213 | /** |
| 235 | 214 | * Returns unit(s) as the product of numerator units divided by the product of denominator units |
| @@ -237,94 +216,21 @@ | ||
| 237 | 216 | * @return string |
| 238 | 217 | */ |
| 239 | 218 | public function unitStr() |
| 240 | 219 | { |
| 241 | - $numerators = []; | |
| 242 | - $denominators = []; | |
| 243 | - | |
| 244 | - foreach ($this->units as $unit => $unitSize) { | |
| 245 | - if ($unitSize > 0) { | |
| 246 | - $numerators = array_pad($numerators, count($numerators) + $unitSize, $unit); | |
| 247 | - continue; | |
| 248 | - } | |
| 249 | - | |
| 250 | - if ($unitSize < 0) { | |
| 251 | - $denominators = array_pad($denominators, count($denominators) + $unitSize, $unit); | |
| 252 | - continue; | |
| 253 | - } | |
| 254 | - } | |
| 255 | - | |
| 256 | - return implode('*', $numerators) . (count($denominators) ? '/' . implode('*', $denominators) : ''); | |
| 220 | + return $this->units; | |
| 257 | 221 | } |
| 258 | 222 | |
| 259 | 223 | /** |
| 260 | - * Output number | |
| 261 | - * | |
| 262 | - * @param \Leafo\ScssPhp\Compiler $compiler | |
| 263 | - * | |
| 264 | - * @return string | |
| 265 | - */ | |
| 266 | - public function output(Compiler $compiler = null) | |
| 267 | - { | |
| 268 | - $dimension = round($this->dimension, static::$precision); | |
| 269 | - | |
| 270 | - $units = array_filter($this->units, function ($unitSize) { | |
| 271 | - return $unitSize; | |
| 272 | - }); | |
| 273 | - | |
| 274 | - if (count($units) > 1 && array_sum($units) === 0) { | |
| 275 | - $dimension = $this->dimension; | |
| 276 | - $units = []; | |
| 277 | - | |
| 278 | - $this->normalizeUnits($dimension, $units, 'in'); | |
| 279 | - | |
| 280 | - $dimension = round($dimension, static::$precision); | |
| 281 | - $units = array_filter($units, function ($unitSize) { | |
| 282 | - return $unitSize; | |
| 283 | - }); | |
| 284 | - } | |
| 285 | - | |
| 286 | - $unitSize = array_sum($units); | |
| 287 | - | |
| 288 | - if ($compiler && ($unitSize > 1 || $unitSize < 0 || count($units) > 1)) { | |
| 289 | - $compiler->throwError((string) $dimension . $this->unitStr() . " isn't a valid CSS value."); | |
| 290 | - } | |
| 291 | - | |
| 292 | - reset($units); | |
| 293 | - $unit = key($units); | |
| 294 | - $dimension = number_format($dimension, static::$precision, '.', ''); | |
| 295 | - | |
| 296 | - return (static::$precision ? rtrim(rtrim($dimension, '0'), '.') : $dimension) . $unit; | |
| 297 | - } | |
| 298 | - | |
| 299 | - /** | |
| 300 | 224 | * {@inheritdoc} |
| 301 | 225 | */ |
| 302 | 226 | public function __toString() |
| 303 | 227 | { |
| 304 | - return $this->output(); | |
| 305 | - } | |
| 228 | + $value = round($this->dimension, self::$precision); | |
| 306 | 229 | |
| 307 | - /** | |
| 308 | - * Normalize units | |
| 309 | - * | |
| 310 | - * @param integer|float $dimension | |
| 311 | - * @param array $units | |
| 312 | - * @param string $baseUnit | |
| 313 | - */ | |
| 314 | - private function normalizeUnits(&$dimension, &$units, $baseUnit = 'in') | |
| 315 | - { | |
| 316 | - $dimension = $this->dimension; | |
| 317 | - $units = []; | |
| 230 | + if (empty($this->units)) { | |
| 231 | + return (string) $value; | |
| 232 | + } | |
| 318 | 233 | |
| 319 | - foreach ($this->units as $unit => $exp) { | |
| 320 | - if (isset(static::$unitTable[$baseUnit][$unit])) { | |
| 321 | - $factor = pow(static::$unitTable[$baseUnit][$unit], $exp); | |
| 322 | - | |
| 323 | - $unit = $baseUnit; | |
| 324 | - $dimension /= $factor; | |
| 325 | - } | |
| 326 | - | |
| 327 | - $units[$unit] = $exp + (isset($units[$unit]) ? $units[$unit] : 0); | |
| 328 | - } | |
| 234 | + return (string) $value . $this->units; | |
| 329 | 235 | } |
| 330 | 236 | } |