Skip to content

Commit

Permalink
Merge branch 'MDL-79666' of https://github.com/paulholden/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
sarjona committed Jan 24, 2024
2 parents f8dddad + b037cd5 commit e36bc60
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 39 deletions.
3 changes: 2 additions & 1 deletion lib/scssphp/Block/CallableBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use ScssPhp\ScssPhp\Block;
use ScssPhp\ScssPhp\Compiler\Environment;
use ScssPhp\ScssPhp\Node\Number;

/**
* @internal
Expand All @@ -26,7 +27,7 @@ class CallableBlock extends Block
public $name;

/**
* @var array|null
* @var list<array{string, array|Number|null, bool}>|null
*/
public $args;

Expand Down
102 changes: 72 additions & 30 deletions lib/scssphp/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -458,10 +458,33 @@ public function compile($code, $path = null)
}

/**
* Compile scss
* Compiles the provided scss file into CSS.
*
* @param string $path
*
* @return CompilationResult
*
* @throws SassException when the source fails to compile
*/
public function compileFile($path)
{
$source = file_get_contents($path);

if ($source === false) {
throw new \RuntimeException('Could not read the file content');
}

return $this->compileString($source, $path);
}

/**
* Compiles the provided scss source code into CSS.
*
* If provided, the path is considered to be the path from which the source code comes
* from, which will be used to resolve relative imports.
*
* @param string $source
* @param string|null $path
* @param string|null $path The path for the source, used to resolve relative imports
*
* @return CompilationResult
*
Expand Down Expand Up @@ -548,7 +571,7 @@ public function compileString($source, $path = null)

$sourceMap = null;

if (! empty($out) && $this->sourceMap && $this->sourceMap !== self::SOURCE_MAP_NONE) {
if (! empty($out) && $this->sourceMap !== self::SOURCE_MAP_NONE && $this->sourceMap) {
assert($sourceMapGenerator !== null);
$sourceMap = $sourceMapGenerator->generateJson($prefix);
$sourceMapUrl = null;
Expand Down Expand Up @@ -1508,6 +1531,7 @@ protected function filterScopeWithWithout($scope, $with, $without)
// start from the root
while ($scope->parent && $scope->parent->type !== Type::T_ROOT) {
array_unshift($childStash, $scope);
\assert($scope->parent !== null);
$scope = $scope->parent;
}

Expand Down Expand Up @@ -2090,6 +2114,11 @@ protected function collapseSelectors($selectors)
foreach ($selector as $node) {
$compound = '';

if (!is_array($node)) {
$output[] = $node;
continue;
}

array_walk_recursive(
$node,
function ($value, $key) use (&$compound) {
Expand Down Expand Up @@ -2124,12 +2153,16 @@ private function collapseSelectorsAsList($selectors)
foreach ($selector as $node) {
$compound = '';

array_walk_recursive(
$node,
function ($value, $key) use (&$compound) {
$compound .= $value;
}
);
if (!is_array($node)) {
$compound .= $node;
} else {
array_walk_recursive(
$node,
function ($value, $key) use (&$compound) {
$compound .= $value;
}
);
}

if ($this->isImmediateRelationshipCombinator($compound)) {
if (\count($output)) {
Expand Down Expand Up @@ -2885,7 +2918,7 @@ protected function compileChild($child, OutputBlock $out)
{
if (isset($child[Parser::SOURCE_LINE])) {
$this->sourceIndex = isset($child[Parser::SOURCE_INDEX]) ? $child[Parser::SOURCE_INDEX] : null;
$this->sourceLine = isset($child[Parser::SOURCE_LINE]) ? $child[Parser::SOURCE_LINE] : -1;
$this->sourceLine = $child[Parser::SOURCE_LINE];
$this->sourceColumn = isset($child[Parser::SOURCE_COLUMN]) ? $child[Parser::SOURCE_COLUMN] : -1;
} elseif (\is_array($child) && isset($child[1]->sourceLine) && $child[1] instanceof Block) {
$this->sourceIndex = $child[1]->sourceIndex;
Expand Down Expand Up @@ -4529,8 +4562,10 @@ public function compileValue($value, $quote = true)
return $colorName;
}

if (is_numeric($alpha)) {
if (\is_int($alpha) || \is_float($alpha)) {
$a = new Number($alpha, '');
} elseif (is_numeric($alpha)) {
$a = new Number((float) $alpha, '');
} else {
$a = $alpha;
}
Expand Down Expand Up @@ -5806,13 +5841,13 @@ public function findImport($url, $currentDir = null)

if (! \is_null($file)) {
if (\is_array($dir)) {
$callableDescription = (\is_object($dir[0]) ? \get_class($dir[0]) : $dir[0]).'::'.$dir[1];
$callableDescription = (\is_object($dir[0]) ? \get_class($dir[0]) : $dir[0]) . '::' . $dir[1];
} elseif ($dir instanceof \Closure) {
$r = new \ReflectionFunction($dir);
if (false !== strpos($r->name, '{closure}')) {
$callableDescription = sprintf('closure{%s:%s}', $r->getFileName(), $r->getStartLine());
} elseif ($class = $r->getClosureScopeClass()) {
$callableDescription = $class->name.'::'.$r->name;
$callableDescription = $class->name . '::' . $r->name;
} else {
$callableDescription = $r->name;
}
Expand Down Expand Up @@ -5925,15 +5960,15 @@ private function checkImportPathConflicts(array $paths)
private function tryImportPathWithExtensions($path)
{
$result = array_merge(
$this->tryImportPath($path.'.sass'),
$this->tryImportPath($path.'.scss')
$this->tryImportPath($path . '.sass'),
$this->tryImportPath($path . '.scss')
);

if ($result) {
return $result;
}

return $this->tryImportPath($path.'.css');
return $this->tryImportPath($path . '.css');
}

/**
Expand All @@ -5943,7 +5978,7 @@ private function tryImportPathWithExtensions($path)
*/
private function tryImportPath($path)
{
$partial = dirname($path).'/_'.basename($path);
$partial = dirname($path) . '/_' . basename($path);

$candidates = [];

Expand All @@ -5969,7 +6004,7 @@ private function tryImportPathAsDirectory($path)
return null;
}

return $this->checkImportPathConflicts($this->tryImportPathWithExtensions($path.'/index'));
return $this->checkImportPathConflicts($this->tryImportPathWithExtensions($path . '/index'));
}

/**
Expand All @@ -5984,7 +6019,7 @@ private function getPrettyPath($path)
}

$normalizedPath = $path;
$normalizedRootDirectory = $this->rootDirectory.'/';
$normalizedRootDirectory = $this->rootDirectory . '/';

if (\DIRECTORY_SEPARATOR === '\\') {
$normalizedRootDirectory = str_replace('\\', '/', $normalizedRootDirectory);
Expand Down Expand Up @@ -6371,8 +6406,6 @@ public static function isNativeFunction($name)
*/
protected function sortNativeFunctionArgs($functionName, $prototypes, $args)
{
static $parser = null;

if (! isset($prototypes)) {
$keyArgs = [];
$posArgs = [];
Expand Down Expand Up @@ -6526,7 +6559,7 @@ private function parseFunctionPrototype(array $prototype)
*
* @return array
*
* @phpstan-param non-empty-list<array{arguments: list<array{0: string, 1: string, 2: array|Number|null}>, rest_argument: string|null}> $prototypes
* @phpstan-param non-empty-array<array{arguments: list<array{0: string, 1: string, 2: array|Number|null}>, rest_argument: string|null}> $prototypes
* @phpstan-return array{arguments: list<array{0: string, 1: string, 2: array|Number|null}>, rest_argument: string|null}
*/
private function selectFunctionPrototype(array $prototypes, $positional, array $names)
Expand Down Expand Up @@ -6984,10 +7017,14 @@ protected function coerceValue($value)
return static::$null;
}

if (is_numeric($value)) {
if (\is_int($value) || \is_float($value)) {
return new Number($value, '');
}

if (is_numeric($value)) {
return new Number((float) $value, '');
}

if ($value === '') {
return static::$emptyString;
}
Expand Down Expand Up @@ -7675,9 +7712,9 @@ private function HWBtoRGB($hue, $whiteness, $blackness)
$b = min(1.0 - $w, $b);

$rgb = $this->toRGB($hue, 100, 50);
for($i = 1; $i < 4; $i++) {
$rgb[$i] *= (1.0 - $w - $b);
$rgb[$i] = round($rgb[$i] + 255 * $w + 0.0001);
for ($i = 1; $i < 4; $i++) {
$rgb[$i] *= (1.0 - $w - $b);
$rgb[$i] = round($rgb[$i] + 255 * $w + 0.0001);
}

return $rgb;
Expand All @@ -7704,7 +7741,6 @@ private function RGBtoHWB($red, $green, $blue)
if ((int) $d === 0) {
$h = 0;
} else {

if ($red == $max) {
$h = 60 * ($green - $blue) / $d;
} elseif ($green == $max) {
Expand All @@ -7714,7 +7750,7 @@ private function RGBtoHWB($red, $green, $blue)
}
}

return [Type::T_HWB, fmod($h, 360), $min / 255 * 100, 100 - $max / 255 *100];
return [Type::T_HWB, fmod($h, 360), $min / 255 * 100, 100 - $max / 255 * 100];
}


Expand Down Expand Up @@ -7923,7 +7959,13 @@ protected function alterColor(array $args, $operation, $fn)
$scale = $operation === 'scale';
$change = $operation === 'change';

/** @phpstan-var callable(string, float|int, bool=, bool=): (float|int|null) $getParam */
/**
* @param string $name
* @param float|int $max
* @param bool $checkPercent
* @param bool $assertPercent
* @return float|int|null
*/
$getParam = function ($name, $max, $checkPercent = false, $assertPercent = false) use (&$kwargs, $scale, $change) {
if (!isset($kwargs[$name])) {
return null;
Expand Down Expand Up @@ -8065,7 +8107,7 @@ protected function libAdjustColor($args)
protected static $libChangeColor = ['color', 'kwargs...'];
protected function libChangeColor($args)
{
return $this->alterColor($args,'change', function ($base, $alter, $max) {
return $this->alterColor($args, 'change', function ($base, $alter, $max) {
if ($alter === null) {
return $base;
}
Expand Down
18 changes: 14 additions & 4 deletions lib/scssphp/Node/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* @template-implements \ArrayAccess<int, mixed>
*/
class Number extends Node implements \ArrayAccess
class Number extends Node implements \ArrayAccess, \JsonSerializable
{
const PRECISION = 10;

Expand Down Expand Up @@ -131,21 +131,31 @@ public function getDimension()
}

/**
* @return string[]
* @return list<string>
*/
public function getNumeratorUnits()
{
return $this->numeratorUnits;
}

/**
* @return string[]
* @return list<string>
*/
public function getDenominatorUnits()
{
return $this->denominatorUnits;
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
// Passing a compiler instance makes the method output a Sass representation instead of a CSS one, supporting full units.
return $this->output(new Compiler());
}

/**
* @return bool
*/
Expand Down Expand Up @@ -554,7 +564,7 @@ public function equals(Number $other)

try {
return $this->coerceUnits($other, function ($num1, $num2) {
return round($num1,self::PRECISION) == round($num2, self::PRECISION);
return round($num1, self::PRECISION) == round($num2, self::PRECISION);
});
} catch (SassScriptException $e) {
return false;
Expand Down
53 changes: 53 additions & 0 deletions lib/scssphp/OutputStyle.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,62 @@
<?php

/**
* SCSSPHP
*
* @copyright 2012-2020 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://scssphp.github.io/scssphp
*/

namespace ScssPhp\ScssPhp;

final class OutputStyle
{
const EXPANDED = 'expanded';
const COMPRESSED = 'compressed';

/**
* Converts a string to an output style.
*
* Using this method allows to write code which will support both
* versions 1.12+ and 2.0 of Scssphp. In 2.0, OutputStyle will be
* an enum instead of using string constants.
*
* @param string $string
*
* @return self::*
*/
public static function fromString($string)
{
switch ($string) {
case 'expanded':
return self::EXPANDED;

case 'compressed':
return self::COMPRESSED;

default:
throw new \InvalidArgumentException('Invalid output style');
}
}

/**
* Converts an output style to a string supported by {@see OutputStyle::fromString()}.
*
* Using this method allows to write code which will support both
* versions 1.12+ and 2.0 of Scssphp. In 2.0, OutputStyle will be
* an enum instead of using string constants.
* The returned string representation is guaranteed to be compatible
* between 1.12 and 2.0.
*
* @param self::* $outputStyle
*
* @return string
*/
public static function toString($outputStyle)
{
return $outputStyle;
}
}
Loading

0 comments on commit e36bc60

Please sign in to comment.