From b915f270373a17dbbf70d68cd8a02bf4a68e3e3a Mon Sep 17 00:00:00 2001 From: "dependabot-preview[bot]" <27856297+dependabot-preview[bot]@users.noreply.github.com> Date: Wed, 15 Jan 2020 10:03:07 +0000 Subject: [PATCH] Bump leafo/scssphp from 0.7.7 to 0.8.4 Bumps [leafo/scssphp](https://github.com/leafo/scssphp) from 0.7.7 to 0.8.4. - [Release notes](https://github.com/leafo/scssphp/releases) - [Commits](https://github.com/leafo/scssphp/compare/v0.7.7...v0.8.4) Signed-off-by: dependabot-preview[bot] Signed-off-by: Christoph Wurst Co-authored-by: Christoph Wurst --- composer.json | 2 +- composer.lock | 20 +- composer/autoload_classmap.php | 3 + composer/autoload_static.php | 3 + composer/installed.json | 18 +- leafo/scssphp/README.md | 19 +- leafo/scssphp/composer.json | 9 +- leafo/scssphp/scss.inc.php | 4 +- leafo/scssphp/src/Block.php | 5 + leafo/scssphp/src/Cache.php | 239 +++ leafo/scssphp/src/Compiler.php | 1874 ++++++++++++++--- leafo/scssphp/src/Compiler/Environment.php | 5 + leafo/scssphp/src/Formatter.php | 3 +- leafo/scssphp/src/Formatter/Compressed.php | 19 + leafo/scssphp/src/Formatter/Crunched.php | 19 + leafo/scssphp/src/Parser.php | 925 +++++--- leafo/scssphp/src/SourceMap/Base64.php | 184 ++ leafo/scssphp/src/SourceMap/Base64VLQ.php | 137 ++ .../src/SourceMap/Base64VLQEncoder.php | 8 +- .../src/SourceMap/SourceMapGenerator.php | 70 +- leafo/scssphp/src/Util.php | 2 +- leafo/scssphp/src/Version.php | 2 +- 22 files changed, 2902 insertions(+), 668 deletions(-) create mode 100644 leafo/scssphp/src/Cache.php create mode 100644 leafo/scssphp/src/SourceMap/Base64.php create mode 100644 leafo/scssphp/src/SourceMap/Base64VLQ.php diff --git a/composer.json b/composer.json index 2d37fa9cb..b45e85402 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "icewind/streams": "v0.7.1", "interfasys/lognormalizer": "^v1.0", "jeremeamia/superclosure": "^2.4", - "leafo/scssphp": "0.7.7", + "leafo/scssphp": "0.8.4", "league/flysystem": "^1.0", "microsoft/azure-storage-blob": "1.2.0", "nikic/php-parser": "^4.2", diff --git a/composer.lock b/composer.lock index 709703f37..e860dd33c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "dd15110dbd9e1955add07a690419e2d0", + "content-hash": "29f99efaaffa9e997730b7edbcca09c6", "packages": [ { "name": "aws/aws-sdk-php", @@ -1473,7 +1473,7 @@ "time": "2015-08-01T16:27:37+00:00" }, { - "name": "jeremeamia/SuperClosure", + "name": "jeremeamia/superclosure", "version": "2.4.0", "source": { "type": "git", @@ -1598,24 +1598,26 @@ }, { "name": "leafo/scssphp", - "version": "v0.7.7", + "version": "v0.8.4", "source": { "type": "git", "url": "https://github.com/leafo/scssphp.git", - "reference": "1d656f8c02a3a69404bba6b28ec4e06edddf0f49" + "reference": "b9cdea3e42c3bcb1a9faafd04ccce4e8ec860ad9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/leafo/scssphp/zipball/1d656f8c02a3a69404bba6b28ec4e06edddf0f49", - "reference": "1d656f8c02a3a69404bba6b28ec4e06edddf0f49", + "url": "https://api.github.com/repos/leafo/scssphp/zipball/b9cdea3e42c3bcb1a9faafd04ccce4e8ec860ad9", + "reference": "b9cdea3e42c3bcb1a9faafd04ccce4e8ec860ad9", "shasum": "" }, "require": { - "php": ">=5.4.0" + "php": "^5.4.0 || ^7" }, "require-dev": { "phpunit/phpunit": "~4.6", - "squizlabs/php_codesniffer": "~2.5" + "squizlabs/php_codesniffer": "~2.5", + "twbs/bootstrap": "~4.3", + "zurb/foundation": "~6.5" }, "bin": [ "bin/pscss" @@ -1646,7 +1648,7 @@ "scss", "stylesheet" ], - "time": "2018-07-22T01:22:08+00:00" + "time": "2019-06-18T21:15:44+00:00" }, { "name": "league/flysystem", diff --git a/composer/autoload_classmap.php b/composer/autoload_classmap.php index 5d91a6bff..9bc3dff1f 100644 --- a/composer/autoload_classmap.php +++ b/composer/autoload_classmap.php @@ -1182,6 +1182,7 @@ 'JsonSchema\\Validator' => $vendorDir . '/justinrainbow/json-schema/src/JsonSchema/Validator.php', 'Leafo\\ScssPhp\\Base\\Range' => $vendorDir . '/leafo/scssphp/src/Base/Range.php', 'Leafo\\ScssPhp\\Block' => $vendorDir . '/leafo/scssphp/src/Block.php', + 'Leafo\\ScssPhp\\Cache' => $vendorDir . '/leafo/scssphp/src/Cache.php', 'Leafo\\ScssPhp\\Colors' => $vendorDir . '/leafo/scssphp/src/Colors.php', 'Leafo\\ScssPhp\\Compiler' => $vendorDir . '/leafo/scssphp/src/Compiler.php', 'Leafo\\ScssPhp\\Compiler\\Environment' => $vendorDir . '/leafo/scssphp/src/Compiler/Environment.php', @@ -1200,6 +1201,8 @@ 'Leafo\\ScssPhp\\Node' => $vendorDir . '/leafo/scssphp/src/Node.php', 'Leafo\\ScssPhp\\Node\\Number' => $vendorDir . '/leafo/scssphp/src/Node/Number.php', 'Leafo\\ScssPhp\\Parser' => $vendorDir . '/leafo/scssphp/src/Parser.php', + 'Leafo\\ScssPhp\\SourceMap\\Base64' => $vendorDir . '/leafo/scssphp/src/SourceMap/Base64.php', + 'Leafo\\ScssPhp\\SourceMap\\Base64VLQ' => $vendorDir . '/leafo/scssphp/src/SourceMap/Base64VLQ.php', 'Leafo\\ScssPhp\\SourceMap\\Base64VLQEncoder' => $vendorDir . '/leafo/scssphp/src/SourceMap/Base64VLQEncoder.php', 'Leafo\\ScssPhp\\SourceMap\\SourceMapGenerator' => $vendorDir . '/leafo/scssphp/src/SourceMap/SourceMapGenerator.php', 'Leafo\\ScssPhp\\Type' => $vendorDir . '/leafo/scssphp/src/Type.php', diff --git a/composer/autoload_static.php b/composer/autoload_static.php index 905064740..d7911ca33 100644 --- a/composer/autoload_static.php +++ b/composer/autoload_static.php @@ -1643,6 +1643,7 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652 'JsonSchema\\Validator' => __DIR__ . '/..' . '/justinrainbow/json-schema/src/JsonSchema/Validator.php', 'Leafo\\ScssPhp\\Base\\Range' => __DIR__ . '/..' . '/leafo/scssphp/src/Base/Range.php', 'Leafo\\ScssPhp\\Block' => __DIR__ . '/..' . '/leafo/scssphp/src/Block.php', + 'Leafo\\ScssPhp\\Cache' => __DIR__ . '/..' . '/leafo/scssphp/src/Cache.php', 'Leafo\\ScssPhp\\Colors' => __DIR__ . '/..' . '/leafo/scssphp/src/Colors.php', 'Leafo\\ScssPhp\\Compiler' => __DIR__ . '/..' . '/leafo/scssphp/src/Compiler.php', 'Leafo\\ScssPhp\\Compiler\\Environment' => __DIR__ . '/..' . '/leafo/scssphp/src/Compiler/Environment.php', @@ -1661,6 +1662,8 @@ class ComposerStaticInit2f23f73bc0cc116b4b1eee1521aa8652 'Leafo\\ScssPhp\\Node' => __DIR__ . '/..' . '/leafo/scssphp/src/Node.php', 'Leafo\\ScssPhp\\Node\\Number' => __DIR__ . '/..' . '/leafo/scssphp/src/Node/Number.php', 'Leafo\\ScssPhp\\Parser' => __DIR__ . '/..' . '/leafo/scssphp/src/Parser.php', + 'Leafo\\ScssPhp\\SourceMap\\Base64' => __DIR__ . '/..' . '/leafo/scssphp/src/SourceMap/Base64.php', + 'Leafo\\ScssPhp\\SourceMap\\Base64VLQ' => __DIR__ . '/..' . '/leafo/scssphp/src/SourceMap/Base64VLQ.php', 'Leafo\\ScssPhp\\SourceMap\\Base64VLQEncoder' => __DIR__ . '/..' . '/leafo/scssphp/src/SourceMap/Base64VLQEncoder.php', 'Leafo\\ScssPhp\\SourceMap\\SourceMapGenerator' => __DIR__ . '/..' . '/leafo/scssphp/src/SourceMap/SourceMapGenerator.php', 'Leafo\\ScssPhp\\Type' => __DIR__ . '/..' . '/leafo/scssphp/src/Type.php', diff --git a/composer/installed.json b/composer/installed.json index 2710555e5..6db82b029 100644 --- a/composer/installed.json +++ b/composer/installed.json @@ -1643,27 +1643,29 @@ }, { "name": "leafo/scssphp", - "version": "v0.7.7", - "version_normalized": "0.7.7.0", + "version": "v0.8.4", + "version_normalized": "0.8.4.0", "source": { "type": "git", "url": "https://github.com/leafo/scssphp.git", - "reference": "1d656f8c02a3a69404bba6b28ec4e06edddf0f49" + "reference": "b9cdea3e42c3bcb1a9faafd04ccce4e8ec860ad9" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/leafo/scssphp/zipball/1d656f8c02a3a69404bba6b28ec4e06edddf0f49", - "reference": "1d656f8c02a3a69404bba6b28ec4e06edddf0f49", + "url": "https://api.github.com/repos/leafo/scssphp/zipball/b9cdea3e42c3bcb1a9faafd04ccce4e8ec860ad9", + "reference": "b9cdea3e42c3bcb1a9faafd04ccce4e8ec860ad9", "shasum": "" }, "require": { - "php": ">=5.4.0" + "php": "^5.4.0 || ^7" }, "require-dev": { "phpunit/phpunit": "~4.6", - "squizlabs/php_codesniffer": "~2.5" + "squizlabs/php_codesniffer": "~2.5", + "twbs/bootstrap": "~4.3", + "zurb/foundation": "~6.5" }, - "time": "2018-07-22T01:22:08+00:00", + "time": "2019-06-18T21:15:44+00:00", "bin": [ "bin/pscss" ], diff --git a/leafo/scssphp/README.md b/leafo/scssphp/README.md index 53a4c7a41..d3fbf154a 100644 --- a/leafo/scssphp/README.md +++ b/leafo/scssphp/README.md @@ -1,14 +1,17 @@ -# scssphp -### +# This repo has been archived -[![Build](https://travis-ci.org/leafo/scssphp.svg?branch=master)](http://travis-ci.org/leafo/scssphp) -[![License](https://poser.pugx.org/leafo/scssphp/license.svg)](https://packagist.org/packages/leafo/scssphp) -`scssphp` is a compiler for SCSS written in PHP. +#### Please go to https://github.com/scssphp/scssphp + +---- + +## scssphp -Checkout the homepage, , for directions on how to use. +![License](https://poser.pugx.org/leafo/scssphp/license.svg) + +`scssphp` is a compiler for SCSS written in PHP. -## Running Tests +### Running Tests `scssphp` uses [PHPUnit](https://github.com/sebastianbergmann/phpunit) for testing. @@ -38,7 +41,7 @@ To enable the `scss` compatibility tests: TEST_SCSS_COMPAT=1 vendor/bin/phpunit tests -## Coding Standard +### Coding Standard `scssphp` source conforms to [PSR2](http://www.php-fig.org/psr/psr-2/). diff --git a/leafo/scssphp/composer.json b/leafo/scssphp/composer.json index 95118a32b..eaa8e87be 100644 --- a/leafo/scssphp/composer.json +++ b/leafo/scssphp/composer.json @@ -21,11 +21,13 @@ "psr-4": { "Leafo\\ScssPhp\\Test\\": "tests/" } }, "require": { - "php": ">=5.4.0" + "php": "^5.4.0 || ^7" }, "require-dev": { "squizlabs/php_codesniffer": "~2.5", - "phpunit/phpunit": "~4.6" + "phpunit/phpunit": "~4.6", + "twbs/bootstrap": "~4.3", + "zurb/foundation": "~6.5" }, "bin": ["bin/pscss"], "archive": { @@ -37,5 +39,6 @@ "/phpunit.xml.dist", "/tests" ] - } + }, + "abandoned": "scssphp/scssphp" } diff --git a/leafo/scssphp/scss.inc.php b/leafo/scssphp/scss.inc.php index 13c84bf5d..2a5f07740 100644 --- a/leafo/scssphp/scss.inc.php +++ b/leafo/scssphp/scss.inc.php @@ -6,6 +6,7 @@ if (! class_exists('Leafo\ScssPhp\Version', false)) { include_once __DIR__ . '/src/Base/Range.php'; include_once __DIR__ . '/src/Block.php'; + include_once __DIR__ . '/src/Cache.php'; include_once __DIR__ . '/src/Colors.php'; include_once __DIR__ . '/src/Compiler.php'; include_once __DIR__ . '/src/Compiler/Environment.php'; @@ -24,7 +25,8 @@ include_once __DIR__ . '/src/Node.php'; include_once __DIR__ . '/src/Node/Number.php'; include_once __DIR__ . '/src/Parser.php'; - include_once __DIR__ . '/src/SourceMap/Base64VLQEncoder.php'; + include_once __DIR__ . '/src/SourceMap/Base64.php'; + include_once __DIR__ . '/src/SourceMap/Base64VLQ.php'; include_once __DIR__ . '/src/SourceMap/SourceMapGenerator.php'; include_once __DIR__ . '/src/Type.php'; include_once __DIR__ . '/src/Util.php'; diff --git a/leafo/scssphp/src/Block.php b/leafo/scssphp/src/Block.php index a6ef8e034..41abf01a8 100644 --- a/leafo/scssphp/src/Block.php +++ b/leafo/scssphp/src/Block.php @@ -62,4 +62,9 @@ class Block * @var array */ public $children; + + /** + * @var \Leafo\ScssPhp\Block + */ + public $selfParent; } diff --git a/leafo/scssphp/src/Cache.php b/leafo/scssphp/src/Cache.php new file mode 100644 index 000000000..49cf631b3 --- /dev/null +++ b/leafo/scssphp/src/Cache.php @@ -0,0 +1,239 @@ + $lastModified) + && $cacheTime + self::$gcLifetime > time() + ) { + $c = file_get_contents($fileCache); + $c = unserialize($c); + + if (is_array($c) && isset($c['value'])) { + return $c['value']; + } + } + } + + return null; + } + + /** + * Put in cache the result of $operation on $what, + * which is known as dependant from the content of $options + * + * @param string $operation + * @param mixed $what + * @param mixed $value + * @param array $options + */ + public function setCache($operation, $what, $value, $options = []) + { + $fileCache = self::$cacheDir . self::cacheName($operation, $what, $options); + + $c = ['value' => $value]; + $c = serialize($c); + file_put_contents($fileCache, $c); + + if (self::$forceRefresh === 'once') { + self::$refreshed[$fileCache] = true; + } + } + + /** + * Get the cache name for the caching of $operation on $what, + * which is known as dependant from the content of $options + * + * @param string $operation + * @param mixed $what + * @param array $options + * + * @return string + */ + private static function cacheName($operation, $what, $options = []) + { + $t = [ + 'version' => self::CACHE_VERSION, + 'operation' => $operation, + 'what' => $what, + 'options' => $options + ]; + + $t = self::$prefix + . sha1(json_encode($t)) + . ".$operation" + . ".scsscache"; + + return $t; + } + + /** + * Check that the cache dir exists and is writeable + * + * @throws \Exception + */ + public static function checkCacheDir() + { + self::$cacheDir = str_replace('\\', '/', self::$cacheDir); + self::$cacheDir = rtrim(self::$cacheDir, '/') . '/'; + + if (! file_exists(self::$cacheDir)) { + if (! mkdir(self::$cacheDir)) { + throw new Exception('Cache directory couldn\'t be created: ' . self::$cacheDir); + } + } elseif (! is_dir(self::$cacheDir)) { + throw new Exception('Cache directory doesn\'t exist: ' . self::$cacheDir); + } elseif (! is_writable(self::$cacheDir)) { + throw new Exception('Cache directory isn\'t writable: ' . self::$cacheDir); + } + } + + /** + * Delete unused cached files + */ + public static function cleanCache() + { + static $clean = false; + + if ($clean || empty(self::$cacheDir)) { + return; + } + + $clean = true; + + // only remove files with extensions created by SCSSPHP Cache + // css files removed based on the list files + $removeTypes = ['scsscache' => 1]; + + $files = scandir(self::$cacheDir); + + if (! $files) { + return; + } + + $checkTime = time() - self::$gcLifetime; + + foreach ($files as $file) { + // don't delete if the file wasn't created with SCSSPHP Cache + if (strpos($file, self::$prefix) !== 0) { + continue; + } + + $parts = explode('.', $file); + $type = array_pop($parts); + + if (! isset($removeTypes[$type])) { + continue; + } + + $fullPath = self::$cacheDir . $file; + $mtime = filemtime($fullPath); + + // don't delete if it's a relatively new file + if ($mtime > $checkTime) { + continue; + } + + unlink($fullPath); + } + } +} diff --git a/leafo/scssphp/src/Compiler.php b/leafo/scssphp/src/Compiler.php index 637f1c1c9..d22d0a35a 100644 --- a/leafo/scssphp/src/Compiler.php +++ b/leafo/scssphp/src/Compiler.php @@ -13,6 +13,7 @@ use Leafo\ScssPhp\Base\Range; use Leafo\ScssPhp\Block; +use Leafo\ScssPhp\Cache; use Leafo\ScssPhp\Colors; use Leafo\ScssPhp\Compiler\Environment; use Leafo\ScssPhp\Exception\CompilerException; @@ -98,17 +99,17 @@ class Compiler 'function' => '^', ]; - static public $true = [Type::T_KEYWORD, 'true']; - static public $false = [Type::T_KEYWORD, 'false']; - static public $null = [Type::T_NULL]; - static public $nullString = [Type::T_STRING, '', []]; + static public $true = [Type::T_KEYWORD, 'true']; + static public $false = [Type::T_KEYWORD, 'false']; + static public $null = [Type::T_NULL]; + static public $nullString = [Type::T_STRING, '', []]; static public $defaultValue = [Type::T_KEYWORD, '']; static public $selfSelector = [Type::T_SELF]; - static public $emptyList = [Type::T_LIST, '', []]; - static public $emptyMap = [Type::T_MAP, [], []]; - static public $emptyString = [Type::T_STRING, '"', []]; - static public $with = [Type::T_KEYWORD, 'with']; - static public $without = [Type::T_KEYWORD, 'without']; + static public $emptyList = [Type::T_LIST, '', []]; + static public $emptyMap = [Type::T_MAP, [], []]; + static public $emptyString = [Type::T_STRING, '"', []]; + static public $with = [Type::T_KEYWORD, 'with']; + static public $without = [Type::T_KEYWORD, 'without']; protected $importPaths = ['']; protected $importCache = []; @@ -145,26 +146,48 @@ class Compiler protected $charsetSeen; protected $sourceNames; - private $indentLevel; - private $commentsSeen; - private $extends; - private $extendsMap; - private $parsedFiles; - private $parser; - private $sourceIndex; - private $sourceLine; - private $sourceColumn; - private $stderr; - private $shouldEvaluate; - private $ignoreErrors; + protected $cache; + + protected $indentLevel; + protected $extends; + protected $extendsMap; + protected $parsedFiles; + protected $parser; + protected $sourceIndex; + protected $sourceLine; + protected $sourceColumn; + protected $stderr; + protected $shouldEvaluate; + protected $ignoreErrors; + + protected $callStack = []; /** * Constructor */ - public function __construct() + public function __construct($cacheOptions = null) { $this->parsedFiles = []; $this->sourceNames = []; + + if ($cacheOptions) { + $this->cache = new Cache($cacheOptions); + } + } + + public function getCompileOptions() + { + $options = [ + 'importPaths' => $this->importPaths, + 'registeredVars' => $this->registeredVars, + 'registeredFeatures' => $this->registeredFeatures, + 'encoding' => $this->encoding, + 'sourceMap' => serialize($this->sourceMap), + 'sourceMapOptions' => $this->sourceMapOptions, + 'formatter' => $this->formatter, + ]; + + return $options; } /** @@ -179,8 +202,33 @@ public function __construct() */ public function compile($code, $path = null) { + if ($this->cache) { + $cacheKey = ($path ? $path : "(stdin)") . ":" . md5($code); + $compileOptions = $this->getCompileOptions(); + $cache = $this->cache->getCache("compile", $cacheKey, $compileOptions); + + if (is_array($cache) + && isset($cache['dependencies']) + && isset($cache['out']) + ) { + // check if any dependency file changed before accepting the cache + foreach ($cache['dependencies'] as $file => $mtime) { + if (! file_exists($file) + || filemtime($file) !== $mtime + ) { + unset($cache); + break; + } + } + + if (isset($cache)) { + return $cache['out']; + } + } + } + + $this->indentLevel = -1; - $this->commentsSeen = []; $this->extends = []; $this->extendsMap = []; $this->sourceIndex = null; @@ -235,6 +283,15 @@ public function compile($code, $path = null) $out .= sprintf('/*# sourceMappingURL=%s */', $sourceMapUrl); } + if ($this->cache && isset($cacheKey) && isset($compileOptions)) { + $v = [ + 'dependencies' => $this->getParsedFiles(), + 'out' => &$out, + ]; + + $this->cache->setCache("compile", $cacheKey, $v, $compileOptions); + } + return $out; } @@ -247,7 +304,7 @@ public function compile($code, $path = null) */ protected function parserFactory($path) { - $parser = new Parser($path, count($this->sourceNames), $this->encoding); + $parser = new Parser($path, count($this->sourceNames), $this->encoding, $this->cache); $this->sourceNames[] = $path; $this->addParsedFile($path); @@ -428,6 +485,37 @@ protected function flattenSelectors(OutputBlock $block, $parentKey = null) } } + /** + * Glue parts of :not( or :nth-child( ... that are in general splitted in selectors parts + * + * @param array $parts + * + * @return array + */ + protected function glueFunctionSelectors($parts) + { + $new = []; + + foreach ($parts as $part) { + if (is_array($part)) { + $part = $this->glueFunctionSelectors($part); + $new[] = $part; + } else { + // a selector part finishing with a ) is the last part of a :not( or :nth-child( + // and need to be joined to this + if (count($new) && is_string($new[count($new) - 1]) + && strlen($part) && substr($part, -1) === ')' && strpos($part, '(') === false + ) { + $new[count($new) - 1] .= $part; + } else { + $new[] = $part; + } + } + } + + return $new; + } + /** * Match extends * @@ -438,14 +526,29 @@ protected function flattenSelectors(OutputBlock $block, $parentKey = null) */ protected function matchExtends($selector, &$out, $from = 0, $initial = true) { + static $partsPile = []; + + $selector = $this->glueFunctionSelectors($selector); + foreach ($selector as $i => $part) { if ($i < $from) { continue; } + // check that we are not building an infinite loop of extensions + // if the new part is just including a previous part don't try to extend anymore + if (count($part) > 1) { + foreach ($partsPile as $previousPart) { + if (! count(array_diff($previousPart, $part))) { + continue 2; + } + } + } + if ($this->matchExtendsSingle($part, $origin)) { - $after = array_slice($selector, $i + 1); - $before = array_slice($selector, 0, $i); + $partsPile[] = $part; + $after = array_slice($selector, $i + 1); + $before = array_slice($selector, 0, $i); list($before, $nonBreakableBefore) = $this->extractRelationshipFromFragment($before); @@ -453,7 +556,7 @@ protected function matchExtends($selector, &$out, $from = 0, $initial = true) $k = 0; // remove shared parts - if ($initial) { + if (count($new) > 1) { while ($k < $i && isset($new[$k]) && $selector[$k] === $new[$k]) { $k++; } @@ -463,7 +566,14 @@ protected function matchExtends($selector, &$out, $from = 0, $initial = true) $tempReplacement = $k > 0 ? array_slice($new, $k) : $new; for ($l = count($tempReplacement) - 1; $l >= 0; $l--) { - $slice = $tempReplacement[$l]; + $slice = []; + + foreach ($tempReplacement[$l] as $chunk) { + if (! in_array($chunk, $slice)) { + $slice[] = $chunk; + } + } + array_unshift($replacement, $slice); if (! $this->isImmediateRelationshipCombinator(end($slice))) { @@ -490,18 +600,19 @@ protected function matchExtends($selector, &$out, $from = 0, $initial = true) $out[] = $result; // recursively check for more matches - $this->matchExtends($result, $out, count($before) + count($mergedBefore), false); + $startRecurseFrom = count($before) + min(count($nonBreakableBefore), count($mergedBefore)); + $this->matchExtends($result, $out, $startRecurseFrom, false); // selector sequence merging if (! empty($before) && count($new) > 1) { - $sharedParts = $k > 0 ? array_slice($before, 0, $k) : []; + $preSharedParts = $k > 0 ? array_slice($before, 0, $k) : []; $postSharedParts = $k > 0 ? array_slice($before, $k) : $before; - list($injectBetweenSharedParts, $nonBreakable2) = $this->extractRelationshipFromFragment($afterBefore); + list($betweenSharedParts, $nonBreakable2) = $this->extractRelationshipFromFragment($afterBefore); $result2 = array_merge( - $sharedParts, - $injectBetweenSharedParts, + $preSharedParts, + $betweenSharedParts, $postSharedParts, $nonBreakable2, $nonBreakableBefore, @@ -512,6 +623,8 @@ protected function matchExtends($selector, &$out, $from = 0, $initial = true) $out[] = $result2; } } + + array_pop($partsPile); } } } @@ -529,6 +642,11 @@ protected function matchExtendsSingle($rawSingle, &$outOrigin) $counts = []; $single = []; + // simple usual cases, no need to do the whole trick + if (in_array($rawSingle, [['>'],['+'],['~']])) { + return false; + } + foreach ($rawSingle as $part) { // matches Number if (! is_string($part)) { @@ -563,6 +681,8 @@ protected function matchExtendsSingle($rawSingle, &$outOrigin) foreach ($counts as $idx => $count) { list($target, $origin, /* $block */) = $this->extends[$idx]; + $origin = $this->glueFunctionSelectors($origin); + // check count if ($count !== count($target)) { continue; @@ -603,7 +723,6 @@ protected function matchExtendsSingle($rawSingle, &$outOrigin) return $found; } - /** * Extract a relationship from the fragment. * @@ -613,6 +732,7 @@ protected function matchExtendsSingle($rawSingle, &$outOrigin) * the rest. * * @param array $fragment The selector fragment maybe ending with a direction relationship combinator. + * * @return array The selector without the relationship fragment if any, the relationship fragment. */ protected function extractRelationshipFromFragment(array $fragment) @@ -682,13 +802,18 @@ protected function compileMedia(Block $media) { $this->pushEnv($media); - $mediaQuery = $this->compileMediaQuery($this->multiplyMedia($this->env)); - - if (! empty($mediaQuery)) { - $this->scope = $this->makeOutputBlock(Type::T_MEDIA, [$mediaQuery]); + $mediaQueries = $this->compileMediaQuery($this->multiplyMedia($this->env)); + if (! empty($mediaQueries) && $mediaQueries) { + $previousScope = $this->scope; $parentScope = $this->mediaParent($this->scope); - $parentScope->children[] = $this->scope; + + foreach ($mediaQueries as $mediaQuery) { + $this->scope = $this->makeOutputBlock(Type::T_MEDIA, [$mediaQuery]); + + $parentScope->children[] = $this->scope; + $parentScope = $this->scope; + } // top level properties in a media cause it to be wrapped $needsWrap = false; @@ -708,21 +833,44 @@ protected function compileMedia(Block $media) if ($needsWrap) { $wrapped = new Block; - $wrapped->sourceName = $media->sourceName; - $wrapped->sourceIndex = $media->sourceIndex; - $wrapped->sourceLine = $media->sourceLine; + $wrapped->sourceName = $media->sourceName; + $wrapped->sourceIndex = $media->sourceIndex; + $wrapped->sourceLine = $media->sourceLine; $wrapped->sourceColumn = $media->sourceColumn; - $wrapped->selectors = []; - $wrapped->comments = []; - $wrapped->parent = $media; - $wrapped->children = $media->children; + $wrapped->selectors = []; + $wrapped->comments = []; + $wrapped->parent = $media; + $wrapped->children = $media->children; $media->children = [[Type::T_BLOCK, $wrapped]]; + if (isset($this->lineNumberStyle)) { + $annotation = $this->makeOutputBlock(Type::T_COMMENT); + $annotation->depth = 0; + + $file = $this->sourceNames[$media->sourceIndex]; + $line = $media->sourceLine; + + switch ($this->lineNumberStyle) { + case static::LINE_COMMENTS: + $annotation->lines[] = '/* line ' . $line + . ($file ? ', ' . $file : '') + . ' */'; + break; + + case static::DEBUG_INFO: + $annotation->lines[] = '@media -sass-debug-info{' + . ($file ? 'filename{font-family:"' . $file . '"}' : '') + . 'line{font-family:' . $line . '}}'; + break; + } + + $this->scope->children[] = $annotation; + } } $this->compileChildrenNoReturn($media->children, $this->scope); - $this->scope = $this->scope->parent; + $this->scope = $previousScope; } $this->popEnv(); @@ -790,18 +938,29 @@ protected function compileAtRoot(Block $block) $wrapped->comments = []; $wrapped->parent = $block; $wrapped->children = $block->children; + $wrapped->selfParent = $block->selfParent; $block->children = [[Type::T_BLOCK, $wrapped]]; + $block->selector = null; + } + + $selfParent = $block->selfParent; + + if (! $block->selfParent->selectors && isset($block->parent) && $block->parent && + isset($block->parent->selectors) && $block->parent->selectors + ) { + $selfParent = $block->parent; } $this->env = $this->filterWithout($envs, $without); - $newBlock = $this->spliceTree($envs, $block, $without); $saveScope = $this->scope; - $this->scope = $this->rootBlock; + $this->scope = $this->filterScopeWithout($saveScope, $without); - $this->compileChild($newBlock, $this->scope); + // propagate selfParent to the children where they still can be useful + $this->compileChildrenNoReturn($block->children, $this->scope, $selfParent); + $this->scope = $this->completeScope($this->scope, $saveScope); $this->scope = $saveScope; $this->env = $this->extractEnv($envs); @@ -809,82 +968,119 @@ protected function compileAtRoot(Block $block) } /** - * Splice parse tree + * Filter at-root scope depending of with/without option * - * @param array $envs - * @param \Leafo\ScssPhp\Block $block - * @param integer $without + * @param \Leafo\ScssPhp\Formatter\OutputBlock $scope + * @param mixed $without * - * @return array + * @return mixed */ - private function spliceTree($envs, Block $block, $without) + protected function filterScopeWithout($scope, $without) { - $newBlock = null; + $filteredScopes = []; - foreach ($envs as $e) { - if (! isset($e->block)) { - continue; - } + if ($scope->type === TYPE::T_ROOT) { + return $scope; + } - if ($e->block === $block) { - continue; + // start from the root + while ($scope->parent && $scope->parent->type !== TYPE::T_ROOT) { + $scope = $scope->parent; + } + + for (;;) { + if (! $scope) { + break; } - if (isset($e->block->type) && $e->block->type === Type::T_AT_ROOT) { - continue; + if (! $this->isWithout($without, $scope)) { + $s = clone $scope; + $s->children = []; + $s->lines = []; + $s->parent = null; + + if ($s->type !== Type::T_MEDIA && $s->type !== Type::T_DIRECTIVE) { + $s->selectors = []; + } + + $filteredScopes[] = $s; } - if ($e->block && $this->isWithout($without, $e->block)) { - continue; + if ($scope->children) { + $scope = end($scope->children); + } else { + $scope = null; } + } - $b = new Block; - $b->sourceName = $e->block->sourceName; - $b->sourceIndex = $e->block->sourceIndex; - $b->sourceLine = $e->block->sourceLine; - $b->sourceColumn = $e->block->sourceColumn; - $b->selectors = []; - $b->comments = $e->block->comments; - $b->parent = null; + if (! count($filteredScopes)) { + return $this->rootBlock; + } - if ($newBlock) { - $type = isset($newBlock->type) ? $newBlock->type : Type::T_BLOCK; + $newScope = array_shift($filteredScopes); + $newScope->parent = $this->rootBlock; - $b->children = [[$type, $newBlock]]; + $this->rootBlock->children[] = $newScope; - $newBlock->parent = $b; - } elseif (count($block->children)) { - foreach ($block->children as $child) { - if ($child[0] === Type::T_BLOCK) { - $child[1]->parent = $b; - } - } + $p = &$newScope; - $b->children = $block->children; - } + while (count($filteredScopes)) { + $s = array_shift($filteredScopes); + $s->parent = $p; + $p->children[] = &$s; + $p = $s; + } - if (isset($e->block->type)) { - $b->type = $e->block->type; - } + return $newScope; + } - if (isset($e->block->name)) { - $b->name = $e->block->name; - } + /** + * found missing selector from a at-root compilation in the previous scope + * (if at-root is just enclosing a property, the selector is in the parent tree) + * + * @param \Leafo\ScssPhp\Formatter\OutputBlock $scope + * @param \Leafo\ScssPhp\Formatter\OutputBlock $previousScope + * + * @return mixed + */ + protected function completeScope($scope, $previousScope) + { + if (! $scope->type && (! $scope->selectors || ! count($scope->selectors)) && count($scope->lines)) { + $scope->selectors = $this->findScopeSelectors($previousScope, $scope->depth); + } - if (isset($e->block->queryList)) { - $b->queryList = $e->block->queryList; + if ($scope->children) { + foreach ($scope->children as $k => $c) { + $scope->children[$k] = $this->completeScope($c, $previousScope); } + } - if (isset($e->block->value)) { - $b->value = $e->block->value; - } + return $scope; + } - $newBlock = $b; + /** + * Find a selector by the depth node in the scope + * + * @param \Leafo\ScssPhp\Formatter\OutputBlock $scope + * @param integer $depth + * + * @return array + */ + protected function findScopeSelectors($scope, $depth) + { + if ($scope->depth === $depth && $scope->selectors) { + return $scope->selectors; } - $type = isset($newBlock->type) ? $newBlock->type : Type::T_BLOCK; + if ($scope->children) { + foreach (array_reverse($scope->children) as $c) { + if ($s = $this->findScopeSelectors($c, $depth)) { + return $s; + } + } + } - return [$type, $newBlock]; + return []; } /** @@ -894,7 +1090,7 @@ private function spliceTree($envs, Block $block, $without) * * @return integer */ - private function compileWith($with) + protected function compileWith($with) { static $mapping = [ 'rule' => self::WITH_RULE, @@ -945,16 +1141,19 @@ private function compileWith($with) * * @return \Leafo\ScssPhp\Compiler\Environment */ - private function filterWithout($envs, $without) + protected function filterWithout($envs, $without) { $filtered = []; foreach ($envs as $e) { if ($e->block && $this->isWithout($without, $e->block)) { - continue; + $ec = clone $e; + $ec->block = null; + $ec->selectors = []; + $filtered[] = $ec; + } else { + $filtered[] = $e; } - - $filtered[] = $e; } return $this->extractEnv($filtered); @@ -963,20 +1162,30 @@ private function filterWithout($envs, $without) /** * Filter WITH rules * - * @param integer $without - * @param \Leafo\ScssPhp\Block $block + * @param integer $without + * @param \Leafo\ScssPhp\Block|\Leafo\ScssPhp\Formatter\OutputBlock $block * * @return boolean */ - private function isWithout($without, Block $block) + protected function isWithout($without, $block) { - if ((($without & static::WITH_RULE) && isset($block->selectors)) || - (($without & static::WITH_MEDIA) && - isset($block->type) && $block->type === Type::T_MEDIA) || - (($without & static::WITH_SUPPORTS) && - isset($block->type) && $block->type === Type::T_DIRECTIVE && - isset($block->name) && $block->name === 'supports') - ) { + if (isset($block->type)) { + if ($block->type === Type::T_MEDIA) { + return ($without & static::WITH_MEDIA) ? true : false; + } + + if ($block->type === Type::T_DIRECTIVE) { + if (isset($block->name) && $block->name === 'supports') { + return ($without & static::WITH_SUPPORTS) ? true : false; + } + + if (isset($block->selectors) && strpos(serialize($block->selectors), '@supports') !== false) { + return ($without & static::WITH_SUPPORTS) ? true : false; + } + } + } + + if ((($without & static::WITH_RULE) && isset($block->selectors))) { return true; } @@ -1024,6 +1233,35 @@ protected function compileNestedBlock(Block $block, $selectors) $this->scope = $this->makeOutputBlock($block->type, $selectors); $this->scope->parent->children[] = $this->scope; + // wrap assign children in a block + // except for @font-face + if ($block->type !== Type::T_DIRECTIVE || $block->name !== "font-face") { + // need wrapping? + $needWrapping = false; + + foreach ($block->children as $child) { + if ($child[0] === Type::T_ASSIGN) { + $needWrapping = true; + break; + } + } + + if ($needWrapping) { + $wrapped = new Block; + $wrapped->sourceName = $block->sourceName; + $wrapped->sourceIndex = $block->sourceIndex; + $wrapped->sourceLine = $block->sourceLine; + $wrapped->sourceColumn = $block->sourceColumn; + $wrapped->selectors = []; + $wrapped->comments = []; + $wrapped->parent = $block; + $wrapped->children = $block->children; + $wrapped->selfParent = $block->selfParent; + + $block->children = [[Type::T_BLOCK, $wrapped]]; + } + } + $this->compileChildrenNoReturn($block->children, $this->scope); $this->scope = $this->scope->parent; @@ -1083,9 +1321,22 @@ protected function compileBlock(Block $block) $this->scope->children[] = $out; if (count($block->children)) { - $out->selectors = $this->multiplySelectors($env); + $out->selectors = $this->multiplySelectors($env, $block->selfParent); + + // propagate selfParent to the children where they still can be useful + $selfParentSelectors = null; + + if (isset($block->selfParent->selectors)) { + $selfParentSelectors = $block->selfParent->selectors; + $block->selfParent->selectors = $out->selectors; + } + + $this->compileChildrenNoReturn($block->children, $out, $block->selfParent); - $this->compileChildrenNoReturn($block->children, $out); + // and revert for the following childs of the same block + if ($selfParentSelectors) { + $block->selfParent->selectors = $selfParentSelectors; + } } $this->formatter->stripSemicolon($out->lines); @@ -1101,7 +1352,8 @@ protected function compileBlock(Block $block) protected function compileComment($block) { $out = $this->makeOutputBlock(Type::T_COMMENT); - $out->lines[] = $block[1]; + $out->lines[] = is_string($block[1]) ? $block[1] : $this->compileValue($block[1]); + $this->scope->children[] = $out; } @@ -1120,6 +1372,7 @@ protected function evalSelectors($selectors) // after evaluating interpolates, we might need a second pass if ($this->shouldEvaluate) { + $selectors = $this->revertSelfSelector($selectors); $buffer = $this->collapseSelectors($selectors); $parser = $this->parserFactory(__METHOD__); @@ -1174,28 +1427,87 @@ protected function evalSelectorPart($part) /** * Collapse selectors * - * @param array $selectors + * @param array $selectors + * @param boolean $selectorFormat + * if false return a collapsed string + * if true return an array description of a structured selector * * @return string */ - protected function collapseSelectors($selectors) + protected function collapseSelectors($selectors, $selectorFormat = false) { $parts = []; foreach ($selectors as $selector) { - $output = ''; + $output = []; + $glueNext = false; - array_walk_recursive( - $selector, - function ($value, $key) use (&$output) { - $output .= $value; + foreach ($selector as $node) { + $compound = ''; + + array_walk_recursive( + $node, + function ($value, $key) use (&$compound) { + $compound .= $value; + } + ); + + if ($selectorFormat && $this->isImmediateRelationshipCombinator($compound)) { + if (count($output)) { + $output[count($output) - 1] .= ' ' . $compound; + } else { + $output[] = $compound; + } + $glueNext = true; + } elseif ($glueNext) { + $output[count($output) - 1] .= ' ' . $compound; + $glueNext = false; + } else { + $output[] = $compound; } - ); + } + + if ($selectorFormat) { + foreach ($output as &$o) { + $o = [Type::T_STRING, '', [$o]]; + } + $output = [Type::T_LIST, ' ', $output]; + } else { + $output = implode(' ', $output); + } $parts[] = $output; } - return implode(', ', $parts); + if ($selectorFormat) { + $parts = [Type::T_LIST, ',', $parts]; + } else { + $parts = implode(', ', $parts); + } + + return $parts; + } + + /** + * Parse down the selector and revert [self] to "&" before a reparsing + * + * @param array $selectors + * + * @return array + */ + protected function revertSelfSelector($selectors) + { + foreach ($selectors as &$part) { + if (is_array($part)) { + if ($part === [Type::T_SELF]) { + $part = '&'; + } else { + $part = $this->revertSelfSelector($part); + } + } + } + + return $selectors; } /** @@ -1302,16 +1614,42 @@ protected function hasSelectorPlaceholder($selector) return false; } + protected function pushCallStack($name = '') + { + $this->callStack[] = [ + 'n' => $name, + Parser::SOURCE_INDEX => $this->sourceIndex, + Parser::SOURCE_LINE => $this->sourceLine, + Parser::SOURCE_COLUMN => $this->sourceColumn + ]; + + // infinite calling loop + if (count($this->callStack) > 25000) { + // not displayed but you can var_dump it to deep debug + $msg = $this->callStackMessage(true, 100); + $msg = "Infinite calling loop"; + $this->throwError($msg); + } + } + + protected function popCallStack() + { + array_pop($this->callStack); + } + /** * Compile children and return result * * @param array $stms * @param \Leafo\ScssPhp\Formatter\OutputBlock $out + * @param string $traceName * - * @return array + * @return array|null */ - protected function compileChildren($stms, OutputBlock $out) + protected function compileChildren($stms, OutputBlock $out, $traceName = '') { + $this->pushCallStack($traceName); + foreach ($stms as $stm) { $ret = $this->compileChild($stm, $out); @@ -1319,6 +1657,10 @@ protected function compileChildren($stms, OutputBlock $out) return $ret; } } + + $this->popCallStack(); + + return null; } /** @@ -1326,13 +1668,27 @@ protected function compileChildren($stms, OutputBlock $out) * * @param array $stms * @param \Leafo\ScssPhp\Formatter\OutputBlock $out + * @param \Leafo\ScssPhp\Block $selfParent + * @param string $traceName * * @throws \Exception */ - protected function compileChildrenNoReturn($stms, OutputBlock $out) + protected function compileChildrenNoReturn($stms, OutputBlock $out, $selfParent = null, $traceName = '') { + $this->pushCallStack($traceName); + foreach ($stms as $stm) { - $ret = $this->compileChild($stm, $out); + if ($selfParent && isset($stm[1]) && is_object($stm[1]) && $stm[1] instanceof Block) { + $stm[1]->selfParent = $selfParent; + $ret = $this->compileChild($stm, $out); + $stm[1]->selfParent = null; + } elseif ($selfParent && $stm[0] === TYPE::T_INCLUDE) { + $stm['selfParent'] = $selfParent; + $ret = $this->compileChild($stm, $out); + unset($stm['selfParent']); + } else { + $ret = $this->compileChild($stm, $out); + } if (isset($ret)) { $this->throwError('@return may only be used within a function'); @@ -1340,90 +1696,208 @@ protected function compileChildrenNoReturn($stms, OutputBlock $out) return; } } + + $this->popCallStack(); } + /** - * Compile media query + * evaluate media query : compile internal value keeping the structure inchanged * * @param array $queryList * - * @return string + * @return array */ - protected function compileMediaQuery($queryList) + protected function evaluateMediaQuery($queryList) { - $out = '@media'; - $first = true; + foreach ($queryList as $kql => $query) { + foreach ($query as $kq => $q) { + for ($i = 1; $i < count($q); $i++) { + $value = $this->compileValue($q[$i]); - foreach ($queryList as $query) { - $type = null; - $parts = []; + // the parser had no mean to know if media type or expression if it was an interpolation + if ($q[0] == Type::T_MEDIA_TYPE && + (strpos($value, '(') !== false || + strpos($value, ')') !== false || + strpos($value, ':') !== false) + ) { + $queryList[$kql][$kq][0] = Type::T_MEDIA_EXPRESSION; - foreach ($query as $q) { - switch ($q[0]) { - case Type::T_MEDIA_TYPE: - if ($type) { - $type = $this->mergeMediaTypes( - $type, - array_map([$this, 'compileValue'], array_slice($q, 1)) - ); - - if (empty($type)) { // merge failed - return null; + if (strpos($value, 'and') !== false) { + $values = explode('and', $value); + $value = trim(array_pop($values)); + + while ($v = trim(array_pop($values))) { + $type = Type::T_MEDIA_EXPRESSION; + + if (strpos($v, '(') === false && + strpos($v, ')') === false && + strpos($v, ':') === false + ) { + $type = Type::T_MEDIA_TYPE; + } + + if (substr($v, 0, 1) === '(' && substr($v, -1) === ')') { + $v = substr($v, 1, -1); + } + + $queryList[$kql][] = [$type,[Type::T_KEYWORD, $v]]; } - } else { - $type = array_map([$this, 'compileValue'], array_slice($q, 1)); } - break; - case Type::T_MEDIA_EXPRESSION: - if (isset($q[2])) { - $parts[] = '(' - . $this->compileValue($q[1]) - . $this->formatter->assignSeparator - . $this->compileValue($q[2]) - . ')'; - } else { - $parts[] = '(' - . $this->compileValue($q[1]) - . ')'; + if (substr($value, 0, 1) === '(' && substr($value, -1) === ')') { + $value = substr($value, 1, -1); } - break; - - case Type::T_MEDIA_VALUE: - $parts[] = $this->compileValue($q[1]); - break; - } - } - - if ($type) { - array_unshift($parts, implode(' ', array_filter($type))); - } + } - if (! empty($parts)) { - if ($first) { - $first = false; - $out .= ' '; - } else { - $out .= $this->formatter->tagSeparator; + $queryList[$kql][$kq][$i] = [Type::T_KEYWORD, $value]; } - - $out .= implode(' and ', $parts); } } - return $out; + return $queryList; } - protected function mergeDirectRelationships($selectors1, $selectors2) - { - if (empty($selectors1) || empty($selectors2)) { - return array_merge($selectors1, $selectors2); + /** + * Compile media query + * + * @param array $queryList + * + * @return array + */ + protected function compileMediaQuery($queryList) + { + $start = '@media '; + $default = trim($start); + $out = []; + $current = ""; + + foreach ($queryList as $query) { + $type = null; + $parts = []; + + $mediaTypeOnly = true; + + foreach ($query as $q) { + if ($q[0] !== Type::T_MEDIA_TYPE) { + $mediaTypeOnly = false; + break; + } + } + + foreach ($query as $q) { + switch ($q[0]) { + case Type::T_MEDIA_TYPE: + $newType = array_map([$this, 'compileValue'], array_slice($q, 1)); + // combining not and anything else than media type is too risky and should be avoided + if (! $mediaTypeOnly) { + if (in_array(Type::T_NOT, $newType) || ($type && in_array(Type::T_NOT, $type) )) { + if ($type) { + array_unshift($parts, implode(' ', array_filter($type))); + } + + if (! empty($parts)) { + if (strlen($current)) { + $current .= $this->formatter->tagSeparator; + } + + $current .= implode(' and ', $parts); + } + + if ($current) { + $out[] = $start . $current; + } + + $current = ""; + $type = null; + $parts = []; + } + } + + if ($newType === ['all'] && $default) { + $default = $start . 'all'; + } + + // all can be safely ignored and mixed with whatever else + if ($newType !== ['all']) { + if ($type) { + $type = $this->mergeMediaTypes($type, $newType); + + if (empty($type)) { + // merge failed : ignore this query that is not valid, skip to the next one + $parts = []; + $default = ''; // if everything fail, no @media at all + continue 3; + } + } else { + $type = $newType; + } + } + break; + + case Type::T_MEDIA_EXPRESSION: + if (isset($q[2])) { + $parts[] = '(' + . $this->compileValue($q[1]) + . $this->formatter->assignSeparator + . $this->compileValue($q[2]) + . ')'; + } else { + $parts[] = '(' + . $this->compileValue($q[1]) + . ')'; + } + break; + + case Type::T_MEDIA_VALUE: + $parts[] = $this->compileValue($q[1]); + break; + } + } + + if ($type) { + array_unshift($parts, implode(' ', array_filter($type))); + } + + if (! empty($parts)) { + if (strlen($current)) { + $current .= $this->formatter->tagSeparator; + } + + $current .= implode(' and ', $parts); + } + } + + if ($current) { + $out[] = $start . $current; + } + + // no @media type except all, and no conflict? + if (! $out && $default) { + $out[] = $default; + } + + return $out; + } + + /** + * Merge direct relationships between selectors + * + * @param array $selectors1 + * @param array $selectors2 + * + * @return array + */ + protected function mergeDirectRelationships($selectors1, $selectors2) + { + if (empty($selectors1) || empty($selectors2)) { + return array_merge($selectors1, $selectors2); } $part1 = end($selectors1); $part2 = end($selectors2); - if (! $this->isImmediateRelationshipCombinator($part1[0]) || $part1 !== $part2) { + if (! $this->isImmediateRelationshipCombinator($part1[0]) && $part1 !== $part2) { return array_merge($selectors1, $selectors2); } @@ -1433,13 +1907,18 @@ protected function mergeDirectRelationships($selectors1, $selectors2) $part1 = array_pop($selectors1); $part2 = array_pop($selectors2); - if ($this->isImmediateRelationshipCombinator($part1[0]) && $part1 !== $part2) { - $merged = array_merge($selectors1, [$part1], $selectors2, [$part2], $merged); + if (! $this->isImmediateRelationshipCombinator($part1[0]) && $part1 !== $part2) { + if ($this->isImmediateRelationshipCombinator(reset($merged)[0])) { + array_unshift($merged, [$part1[0] . $part2[0]]); + $merged = array_merge($selectors1, $selectors2, $merged); + } else { + $merged = array_merge($selectors1, [$part1], $selectors2, [$part2], $merged); + } + break; } array_unshift($merged, $part1); - array_unshift($merged, [array_pop($selectors1)[0] . array_pop($selectors2)[0]]); } while (! empty($selectors1) && ! empty($selectors2)); return $merged; @@ -1514,13 +1993,13 @@ protected function mergeMediaTypes($type1, $type2) /** * Compile import; returns true if the value was something that could be imported * - * @param array $rawPath - * @param array $out - * @param boolean $once + * @param array $rawPath + * @param \Leafo\ScssPhp\Formatter\OutputBlock $out + * @param boolean $once * * @return boolean */ - protected function compileImport($rawPath, $out, $once = false) + protected function compileImport($rawPath, OutputBlock $out, $once = false) { if ($rawPath[0] === Type::T_STRING) { $path = $this->compileStringContent($rawPath); @@ -1569,15 +2048,26 @@ protected function compileImport($rawPath, $out, $once = false) */ protected function compileChild($child, OutputBlock $out) { - $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->sourceColumn = isset($child[Parser::SOURCE_COLUMN]) ? $child[Parser::SOURCE_COLUMN] : -1; + 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->sourceColumn = isset($child[Parser::SOURCE_COLUMN]) ? $child[Parser::SOURCE_COLUMN] : -1; + } elseif (is_array($child) && isset($child[1]->sourceLine)) { + $this->sourceIndex = $child[1]->sourceIndex; + $this->sourceLine = $child[1]->sourceLine; + $this->sourceColumn = $child[1]->sourceColumn; + } elseif (! empty($out->sourceLine) && ! empty($out->sourceName)) { + $this->sourceLine = $out->sourceLine; + $this->sourceIndex = array_search($out->sourceName, $this->sourceNames); + + if ($this->sourceIndex === false) { + $this->sourceIndex = null; + } + } switch ($child[0]) { case Type::T_SCSSPHP_IMPORT_ONCE: - list(, $rawPath) = $child; - - $rawPath = $this->reduce($rawPath); + $rawPath = $this->reduce($child[1]); if (! $this->compileImport($rawPath, $out, true)) { $out->lines[] = '@import ' . $this->compileValue($rawPath) . ';'; @@ -1585,9 +2075,7 @@ protected function compileChild($child, OutputBlock $out) break; case Type::T_IMPORT: - list(, $rawPath) = $child; - - $rawPath = $this->reduce($rawPath); + $rawPath = $this->reduce($child[1]); if (! $this->compileImport($rawPath, $out)) { $out->lines[] = '@import ' . $this->compileValue($rawPath) . ';'; @@ -1627,7 +2115,7 @@ protected function compileChild($child, OutputBlock $out) $isGlobal = in_array('!global', $flags); if ($isGlobal) { - $this->set($name[1], $this->reduce($value), false, $this->rootEnv); + $this->set($name[1], $this->reduce($value), false, $this->rootEnv, $value); break; } @@ -1636,7 +2124,7 @@ protected function compileChild($child, OutputBlock $out) || $result === static::$null); if (! $isDefault || $shouldSet) { - $this->set($name[1], $this->reduce($value)); + $this->set($name[1], $this->reduce($value), true, null, $value); } break; } @@ -1644,11 +2132,25 @@ protected function compileChild($child, OutputBlock $out) $compiledName = $this->compileValue($name); // handle shorthand syntax: size / line-height - if ($compiledName === 'font') { - if ($value[0] === Type::T_EXPRESSION && $value[1] === '/') { - $value = $this->expToString($value); - } elseif ($value[0] === Type::T_LIST) { - foreach ($value[2] as &$item) { + if ($compiledName === 'font' || $compiledName === 'grid-row' || $compiledName === 'grid-column') { + if ($value[0] === Type::T_VARIABLE) { + // if the font value comes from variable, the content is already reduced + // (i.e., formulas were already calculated), so we need the original unreduced value + $value = $this->get($value[1], true, null, true); + } + + $fontValue=&$value; + + if ($value[0] === Type::T_LIST && $value[1]==',') { + // this is the case if more than one font is given: example: "font: 400 1em/1.3 arial,helvetica" + // we need to handle the first list element + $fontValue=&$value[2][0]; + } + + if ($fontValue[0] === Type::T_EXPRESSION && $fontValue[1] === '/') { + $fontValue = $this->expToString($fontValue); + } elseif ($fontValue[0] === Type::T_LIST) { + foreach ($fontValue[2] as &$item) { if ($item[0] === Type::T_EXPRESSION && $item[1] === '/') { $item = $this->expToString($item); } @@ -1691,9 +2193,7 @@ protected function compileChild($child, OutputBlock $out) break; case Type::T_EXTEND: - list(, $selectors) = $child; - - foreach ($selectors as $sel) { + foreach ($child[1] as $sel) { $results = $this->evalSelectors([$sel]); foreach ($results as $result) { @@ -1869,10 +2369,34 @@ protected function compileChild($child, OutputBlock $out) $storeEnv = $this->storeEnv; $this->storeEnv = $this->env; + // Find the parent selectors in the env to be able to know what '&' refers to in the mixin + // and assign this fake parent to childs + $selfParent = null; + + if (isset($child['selfParent']) && isset($child['selfParent']->selectors)) { + $selfParent = $child['selfParent']; + } else { + $parentSelectors = $this->multiplySelectors($this->env); + + if ($parentSelectors) { + $parent = new Block(); + $parent->selectors = $parentSelectors; + + foreach ($mixin->children as $k => $child) { + if (isset($child[1]) && is_object($child[1]) && $child[1] instanceof Block) { + $mixin->children[$k][1]->parent = $parent; + } + } + } + } + + // clone the stored content to not have its scope spoiled by a further call to the same mixin + // i.e., recursive @include of the same mixin if (isset($content)) { - $content->scope = $callingScope; + $copyContent = clone $content; + $copyContent->scope = $callingScope; - $this->setRaw(static::$namespaces['special'] . 'content', $content, $this->env); + $this->setRaw(static::$namespaces['special'] . 'content', $copyContent, $this->env); } if (isset($mixin->args)) { @@ -1881,7 +2405,7 @@ protected function compileChild($child, OutputBlock $out) $this->env->marker = 'mixin'; - $this->compileChildrenNoReturn($mixin->children, $out); + $this->compileChildrenNoReturn($mixin->children, $out, $selfParent, $this->env->marker . " " . $name); $this->storeEnv = $storeEnv; @@ -1889,8 +2413,8 @@ protected function compileChild($child, OutputBlock $out) break; case Type::T_MIXIN_CONTENT: - $content = $this->get(static::$namespaces['special'] . 'content', false, $this->getStoreEnv()) - ?: $this->get(static::$namespaces['special'] . 'content', false, $this->env); + $env = isset($this->storeEnv) ? $this->storeEnv : $this->env; + $content = $this->get(static::$namespaces['special'] . 'content', false, $env); if (! $content) { $content = new \stdClass(); @@ -1901,7 +2425,6 @@ protected function compileChild($child, OutputBlock $out) $storeEnv = $this->storeEnv; $this->storeEnv = $content->scope; - $this->compileChildrenNoReturn($content->children, $out); $this->storeEnv = $storeEnv; @@ -1910,25 +2433,28 @@ protected function compileChild($child, OutputBlock $out) case Type::T_DEBUG: list(, $value) = $child; + $fname = $this->sourceNames[$this->sourceIndex]; $line = $this->sourceLine; $value = $this->compileValue($this->reduce($value, true)); - fwrite($this->stderr, "Line $line DEBUG: $value\n"); + fwrite($this->stderr, "File $fname on line $line DEBUG: $value\n"); break; case Type::T_WARN: list(, $value) = $child; + $fname = $this->sourceNames[$this->sourceIndex]; $line = $this->sourceLine; $value = $this->compileValue($this->reduce($value, true)); - fwrite($this->stderr, "Line $line WARN: $value\n"); + fwrite($this->stderr, "File $fname on line $line WARN: $value\n"); break; case Type::T_ERROR: list(, $value) = $child; + $fname = $this->sourceNames[$this->sourceIndex]; $line = $this->sourceLine; $value = $this->compileValue($this->reduce($value, true)); - $this->throwError("Line $line ERROR: $value\n"); + $this->throwError("File $fname on line $line ERROR: $value\n"); break; case Type::T_CONTROL: @@ -1973,7 +2499,7 @@ protected function expToString($exp) * * @param array $value * - * @return array + * @return boolean */ protected function isTruthy($value) { @@ -2004,7 +2530,7 @@ protected function shouldEval($value) switch ($value[0]) { case Type::T_EXPRESSION: if ($value[1] === '/') { - return $this->shouldEval($value[2], $value[3]); + return $this->shouldEval($value[2]) || $this->shouldEval($value[3]); } // fall-thru @@ -2026,9 +2552,8 @@ protected function shouldEval($value) */ protected function reduce($value, $inExp = false) { - list($type) = $value; - switch ($type) { + switch ($value[0]) { case Type::T_EXPRESSION: list(, $op, $left, $right, $inParens) = $value; @@ -2161,9 +2686,7 @@ protected function reduce($value, $inExp = false) return [Type::T_STRING, '', [$op, $exp]]; case Type::T_VARIABLE: - list(, $name) = $value; - - return $this->reduce($this->get($name)); + return $this->reduce($this->get($value[1])); case Type::T_LIST: foreach ($value[2] as &$item) { @@ -2194,13 +2717,19 @@ protected function reduce($value, $inExp = false) case Type::T_INTERPOLATE: $value[1] = $this->reduce($value[1]); + if ($inExp) { + return $value[1]; + } return $value; case Type::T_FUNCTION_CALL: - list(, $name, $argValues) = $value; + return $this->fncall($value[1], $value[2]); - return $this->fncall($name, $argValues); + case Type::T_SELF: + $selfSelector = $this->multiplySelectors($this->env); + $selfSelector = $this->collapseSelectors($selfSelector, true); + return $selfSelector; default: return $value; @@ -2215,7 +2744,7 @@ protected function reduce($value, $inExp = false) * * @return array|null */ - private function fncall($name, $argValues) + protected function fncall($name, $argValues) { // SCSS @function if ($this->callScssFunction($name, $argValues, $returnValue)) { @@ -2261,9 +2790,8 @@ protected function normalizeName($name) public function normalizeValue($value) { $value = $this->coerceForExpression($this->reduce($value)); - list($type) = $value; - switch ($type) { + switch ($value[0]) { case Type::T_LIST: $value = $this->extractInterpolation($value); @@ -2278,7 +2806,7 @@ public function normalizeValue($value) return $value; case Type::T_STRING: - return [$type, '"', [$this->compileStringContent($value)]]; + return [$value[0], '"', [$this->compileStringContent($value)]]; case Type::T_NUMBER: return $value->normalize(); @@ -2366,7 +2894,7 @@ protected function opModNumberNumber($left, $right) * @param array $left * @param array $right * - * @return array + * @return array|null */ protected function opAdd($left, $right) { @@ -2389,6 +2917,8 @@ protected function opAdd($left, $right) return $strRight; } + + return null; } /** @@ -2398,15 +2928,21 @@ protected function opAdd($left, $right) * @param array $right * @param boolean $shouldEval * - * @return array + * @return array|null */ protected function opAnd($left, $right, $shouldEval) { + $truthy = ($left === static::$null || $right === static::$null) || + ($left === static::$false || $left === static::$true) && + ($right === static::$false || $right === static::$true); + if (! $shouldEval) { - return; + if (! $truthy) { + return null; + } } - if ($left !== static::$false and $left !== static::$null) { + if ($left !== static::$false && $left !== static::$null) { return $this->reduce($right, true); } @@ -2420,15 +2956,21 @@ protected function opAnd($left, $right, $shouldEval) * @param array $right * @param boolean $shouldEval * - * @return array + * @return array|null */ protected function opOr($left, $right, $shouldEval) { + $truthy = ($left === static::$null || $right === static::$null) || + ($left === static::$false || $left === static::$true) && + ($right === static::$false || $right === static::$true); + if (! $shouldEval) { - return; + if (! $truthy) { + return null; + } } - if ($left !== static::$false and $left !== static::$null) { + if ($left !== static::$false && $left !== static::$null) { return $left; } @@ -2683,9 +3225,7 @@ public function compileValue($value) { $value = $this->reduce($value); - list($type) = $value; - - switch ($type) { + switch ($value[0]) { case Type::T_KEYWORD: return $value[1]; @@ -2780,11 +3320,8 @@ public function compileValue($value) return $left . $this->compileValue($interpolate) . $right; case Type::T_INTERPOLATE: - // raw parse node - list(, $exp) = $value; - // strip quotes if it's a string - $reduced = $this->reduce($exp); + $reduced = $this->reduce($value[1]); switch ($reduced[0]) { case Type::T_LIST: @@ -2834,7 +3371,7 @@ public function compileValue($value) return 'null'; default: - $this->throwError("unknown value type: $type"); + $this->throwError("unknown value type: $value[0]"); } } @@ -2899,43 +3436,69 @@ protected function extractInterpolation($list) * Find the final set of selectors * * @param \Leafo\ScssPhp\Compiler\Environment $env + * @param \Leafo\ScssPhp\Block $selfParent * * @return array */ - protected function multiplySelectors(Environment $env) + protected function multiplySelectors(Environment $env, $selfParent = null) { $envs = $this->compactEnv($env); $selectors = []; $parentSelectors = [[]]; + $selfParentSelectors = null; + + if (! is_null($selfParent) && $selfParent->selectors) { + $selfParentSelectors = $this->evalSelectors($selfParent->selectors); + } + while ($env = array_pop($envs)) { if (empty($env->selectors)) { continue; } - $selectors = []; + $selectors = $env->selectors; + + do { + $stillHasSelf = false; + $prevSelectors = $selectors; + $selectors = []; - foreach ($env->selectors as $selector) { - foreach ($parentSelectors as $parent) { - $selectors[] = $this->joinSelectors($parent, $selector); + foreach ($prevSelectors as $selector) { + foreach ($parentSelectors as $parent) { + if ($selfParentSelectors) { + foreach ($selfParentSelectors as $selfParent) { + // if no '&' in the selector, each call will give same result, only add once + $s = $this->joinSelectors($parent, $selector, $stillHasSelf, $selfParent); + $selectors[serialize($s)] = $s; + } + } else { + $s = $this->joinSelectors($parent, $selector, $stillHasSelf); + $selectors[serialize($s)] = $s; + } + } } - } + } while ($stillHasSelf); $parentSelectors = $selectors; } + $selectors = array_values($selectors); + return $selectors; } /** * Join selectors; looks for & to replace, or append parent before child * - * @param array $parent - * @param array $child - * + * @param array $parent + * @param array $child + * @param boolean &$stillHasSelf + * @param array $selfParentSelectors + * @return array */ - protected function joinSelectors($parent, $child) + protected function joinSelectors($parent, $child, &$stillHasSelf, $selfParentSelectors = null) { $setSelf = false; $out = []; @@ -2944,16 +3507,33 @@ protected function joinSelectors($parent, $child) $newPart = []; foreach ($part as $p) { - if ($p === static::$selfSelector) { + // only replace & once and should be recalled to be able to make combinations + if ($p === static::$selfSelector && $setSelf) { + $stillHasSelf = true; + } + + if ($p === static::$selfSelector && ! $setSelf) { $setSelf = true; - foreach ($parent as $i => $parentPart) { + if (is_null($selfParentSelectors)) { + $selfParentSelectors = $parent; + } + + foreach ($selfParentSelectors as $i => $parentPart) { if ($i > 0) { $out[] = $newPart; $newPart = []; } foreach ($parentPart as $pp) { + if (is_array($pp)) { + $flatten = []; + array_walk_recursive($pp, function ($a) use (&$flatten) { + $flatten[] = $a; + }); + $pp = implode($flatten); + } + $newPart[] = $pp; } } @@ -2993,6 +3573,12 @@ protected function multiplyMedia(Environment $env = null, $childQueries = null) ? $env->block->queryList : [[[Type::T_MEDIA_VALUE, $env->block->value]]]; + $store = [$this->env, $this->storeEnv]; + $this->env = $env; + $this->storeEnv = null; + $parentQueries = $this->evaluateMediaQuery($parentQueries); + list($this->env, $this->storeEnv) = $store; + if ($childQueries === null) { $childQueries = $parentQueries; } else { @@ -3001,7 +3587,11 @@ protected function multiplyMedia(Environment $env = null, $childQueries = null) foreach ($parentQueries as $parentQuery) { foreach ($originalQueries as $childQuery) { - $childQueries []= array_merge($parentQuery, $childQuery); + $childQueries[] = array_merge( + $parentQuery, + [[Type::T_MEDIA_TYPE, [Type::T_KEYWORD, 'all']]], + $childQuery + ); } } } @@ -3016,7 +3606,7 @@ protected function multiplyMedia(Environment $env = null, $childQueries = null) * * @return array */ - private function compactEnv(Environment $env) + protected function compactEnv(Environment $env) { for ($envs = []; $env; $env = $env->parent) { $envs[] = $env; @@ -3032,7 +3622,7 @@ private function compactEnv(Environment $env) * * @return \Leafo\ScssPhp\Compiler\Environment */ - private function extractEnv($envs) + protected function extractEnv($envs) { for ($env = null; $e = array_pop($envs);) { $e->parent = $env; @@ -3087,8 +3677,9 @@ protected function getStoreEnv() * @param mixed $value * @param boolean $shadow * @param \Leafo\ScssPhp\Compiler\Environment $env + * @param mixed $valueUnreduced */ - protected function set($name, $value, $shadow = false, Environment $env = null) + protected function set($name, $value, $shadow = false, Environment $env = null, $valueUnreduced = null) { $name = $this->normalizeName($name); @@ -3097,9 +3688,9 @@ protected function set($name, $value, $shadow = false, Environment $env = null) } if ($shadow) { - $this->setRaw($name, $value, $env); + $this->setRaw($name, $value, $env, $valueUnreduced); } else { - $this->setExisting($name, $value, $env); + $this->setExisting($name, $value, $env, $valueUnreduced); } } @@ -3109,8 +3700,9 @@ protected function set($name, $value, $shadow = false, Environment $env = null) * @param string $name * @param mixed $value * @param \Leafo\ScssPhp\Compiler\Environment $env + * @param mixed $valueUnreduced */ - protected function setExisting($name, $value, Environment $env) + protected function setExisting($name, $value, Environment $env, $valueUnreduced = null) { $storeEnv = $env; @@ -3135,6 +3727,10 @@ protected function setExisting($name, $value, Environment $env) } $env->store[$name] = $value; + + if ($valueUnreduced) { + $env->storeUnreduced[$name] = $valueUnreduced; + } } /** @@ -3143,10 +3739,15 @@ protected function setExisting($name, $value, Environment $env) * @param string $name * @param mixed $value * @param \Leafo\ScssPhp\Compiler\Environment $env + * @param mixed $valueUnreduced */ - protected function setRaw($name, $value, Environment $env) + protected function setRaw($name, $value, Environment $env, $valueUnreduced = null) { $env->store[$name] = $value; + + if ($valueUnreduced) { + $env->storeUnreduced[$name] = $valueUnreduced; + } } /** @@ -3157,10 +3758,11 @@ protected function setRaw($name, $value, Environment $env) * @param string $name * @param boolean $shouldThrow * @param \Leafo\ScssPhp\Compiler\Environment $env + * @param boolean $unreduced * - * @return mixed + * @return mixed|null */ - public function get($name, $shouldThrow = true, Environment $env = null) + public function get($name, $shouldThrow = true, Environment $env = null, $unreduced = false) { $normalizedName = $this->normalizeName($name); $specialContentKey = static::$namespaces['special'] . 'content'; @@ -3172,15 +3774,24 @@ public function get($name, $shouldThrow = true, Environment $env = null) $nextIsRoot = false; $hasNamespace = $normalizedName[0] === '^' || $normalizedName[0] === '@' || $normalizedName[0] === '%'; + $maxDepth = 10000; + for (;;) { + if ($maxDepth-- <= 0) { + break; + } + if (array_key_exists($normalizedName, $env->store)) { + if ($unreduced && isset($env->storeUnreduced[$normalizedName])) { + return $env->storeUnreduced[$normalizedName]; + } + return $env->store[$normalizedName]; } if (! $hasNamespace && isset($env->marker)) { if (! $nextIsRoot && ! empty($env->store[$specialContentKey])) { $env = $env->store[$specialContentKey]->scope; - $nextIsRoot = true; continue; } @@ -3196,10 +3807,11 @@ public function get($name, $shouldThrow = true, Environment $env = null) } if ($shouldThrow) { - $this->throwError("Undefined variable \$$name"); + $this->throwError("Undefined variable \$$name" . ($maxDepth<=0 ? " (infinite recursion)" : "")); } // found nothing + return null; } /** @@ -3308,7 +3920,7 @@ public function getParsedFiles() * * @api * - * @param string $path + * @param string|callable $path */ public function addImportPath($path) { @@ -3430,10 +4042,10 @@ public function addFeature($name) /** * Import file * - * @param string $path - * @param array $out + * @param string $path + * @param \Leafo\ScssPhp\Formatter\OutputBlock $out */ - protected function importFile($path, $out) + protected function importFile($path, OutputBlock $out) { // see if tree is cached $realPath = realpath($path); @@ -3481,9 +4093,12 @@ public function findImport($url) if (is_string($dir)) { // check urls for normal import paths foreach ($urls as $full) { - $full = $dir - . (! empty($dir) && substr($dir, -1) !== '/' ? '/' : '') - . $full; + $separator = ( + ! empty($dir) && + substr($dir, -1) !== '/' && + substr($full, 0, 1) !== '/' + ) ? '/' : ''; + $full = $dir . $separator . $full; if ($this->fileExists($file = $full . '.scss') || ($hasExtension && $this->fileExists($file = $full)) @@ -3528,6 +4143,8 @@ public function setEncoding($encoding) public function setIgnoreErrors($ignoreErrors) { $this->ignoreErrors = $ignoreErrors; + + return $this; } /** @@ -3545,16 +4162,61 @@ public function throwError($msg) return; } + $line = $this->sourceLine; + $column = $this->sourceColumn; + + $loc = isset($this->sourceNames[$this->sourceIndex]) + ? $this->sourceNames[$this->sourceIndex] . " on line $line, at column $column" + : "line: $line, column: $column"; + if (func_num_args() > 1) { $msg = call_user_func_array('sprintf', func_get_args()); } - $line = $this->sourceLine; - $msg = "$msg: line: $line"; + $msg = "$msg: $loc"; + + $callStackMsg = $this->callStackMessage(); + + if ($callStackMsg) { + $msg .= "\nCall Stack:\n" . $callStackMsg; + } throw new CompilerException($msg); } + /** + * Beautify call stack for output + * + * @param boolean $all + * @param null $limit + * + * @return string + */ + protected function callStackMessage($all = false, $limit = null) + { + $callStackMsg = []; + $ncall = 0; + + if ($this->callStack) { + foreach (array_reverse($this->callStack) as $call) { + if ($all || (isset($call['n']) && $call['n'])) { + $msg = "#" . $ncall++ . " " . $call['n'] . " "; + $msg .= (isset($this->sourceNames[$call[Parser::SOURCE_INDEX]]) + ? $this->sourceNames[$call[Parser::SOURCE_INDEX]] + : '(unknown file)'); + $msg .= " on line " . $call[Parser::SOURCE_LINE]; + $callStackMsg[] = $msg; + + if (! is_null($limit) && $ncall>$limit) { + break; + } + } + } + } + + return implode("\n", $callStackMsg); + } + /** * Handle import loop * @@ -3620,7 +4282,7 @@ protected function callScssFunction($name, $argValues, &$returnValue) $this->env->marker = 'function'; - $ret = $this->compileChildren($func->children, $tmp); + $ret = $this->compileChildren($func->children, $tmp, $this->env->marker . " " . $name); $this->storeEnv = $storeEnv; @@ -3864,7 +4526,7 @@ protected function applyArguments($argDef, $argValues) * * @return array|\Leafo\ScssPhp\Node\Number */ - private function coerceValue($value) + protected function coerceValue($value) { if (is_array($value) || $value instanceof \ArrayAccess) { return $value; @@ -4066,7 +4728,7 @@ public function assertMap($value) $value = $this->coerceMap($value); if ($value[0] !== Type::T_MAP) { - $this->throwError('expecting map'); + $this->throwError('expecting map, %s received', $value[0]); } return $value; @@ -4086,7 +4748,7 @@ public function assertMap($value) public function assertList($value) { if ($value[0] !== Type::T_LIST) { - $this->throwError('expecting list'); + $this->throwError('expecting list, %s received', $value[0]); } return $value; @@ -4109,7 +4771,7 @@ public function assertColor($value) return $color; } - $this->throwError('expecting color'); + $this->throwError('expecting color, %s received', $value[0]); } /** @@ -4126,7 +4788,7 @@ public function assertColor($value) public function assertNumber($value) { if ($value[0] !== Type::T_NUMBER) { - $this->throwError('expecting number'); + $this->throwError('expecting number, %s received', $value[0]); } return $value[1]; @@ -4203,7 +4865,7 @@ public function toHSL($red, $green, $blue) * * @return float */ - private function hueToRGB($m1, $m2, $h) + protected function hueToRGB($m1, $m2, $h) { if ($h < 0) { $h += 1; @@ -4925,7 +5587,7 @@ protected function libSetNth($args) if (! isset($list[2][$n])) { $this->throwError('Invalid argument for "n"'); - return; + return null; } $list[2][$n] = $args[2]; @@ -5163,7 +5825,7 @@ protected function libComparable($args) ) { $this->throwError('Invalid argument(s) for "comparable"'); - return; + return null; } $number1 = $number1->normalize(); @@ -5343,7 +6005,7 @@ protected function libRandom($args) if ($n < 1) { $this->throwError("limit must be greater than or equal to 1"); - return; + return null; } return new Node\Number(mt_rand(1, $n), ''); @@ -5374,4 +6036,624 @@ protected function libInspect($args) return $args[0]; } + + /** + * Preprocess selector args + * + * @param array $arg + * + * @return array|boolean + */ + protected function getSelectorArg($arg) + { + static $parser = null; + + if (is_null($parser)) { + $parser = $this->parserFactory(__METHOD__); + } + + $arg = $this->libUnquote([$arg]); + $arg = $this->compileValue($arg); + + $parsedSelector = []; + + if ($parser->parseSelector($arg, $parsedSelector)) { + $selector = $this->evalSelectors($parsedSelector); + $gluedSelector = $this->glueFunctionSelectors($selector); + + return $gluedSelector; + } + + return false; + } + + /** + * Postprocess selector to output in right format + * + * @param array $selectors + * + * @return string + */ + protected function formatOutputSelector($selectors) + { + $selectors = $this->collapseSelectors($selectors, true); + + return $selectors; + } + + protected static $libIsSuperselector = ['super', 'sub']; + protected function libIsSuperselector($args) + { + list($super, $sub) = $args; + + $super = $this->getSelectorArg($super); + $sub = $this->getSelectorArg($sub); + + return $this->isSuperSelector($super, $sub); + } + + /** + * Test a $super selector again $sub + * + * @param array $super + * @param array $sub + * + * @return boolean + */ + protected function isSuperSelector($super, $sub) + { + // one and only one selector for each arg + if (! $super || count($super) !== 1) { + $this->throwError("Invalid super selector for isSuperSelector()"); + } + + if (! $sub || count($sub) !== 1) { + $this->throwError("Invalid sub selector for isSuperSelector()"); + } + + $super = reset($super); + $sub = reset($sub); + + $i = 0; + $nextMustMatch = false; + + foreach ($super as $node) { + $compound = ''; + + array_walk_recursive( + $node, + function ($value, $key) use (&$compound) { + $compound .= $value; + } + ); + + if ($this->isImmediateRelationshipCombinator($compound)) { + if ($node !== $sub[$i]) { + return false; + } + + $nextMustMatch = true; + $i++; + } else { + while ($i < count($sub) && ! $this->isSuperPart($node, $sub[$i])) { + if ($nextMustMatch) { + return false; + } + + $i++; + } + + if ($i >= count($sub)) { + return false; + } + + $nextMustMatch = false; + $i++; + } + } + + return true; + } + + /** + * Test a part of super selector again a part of sub selector + * + * @param array $superParts + * @param array $subParts + * + * @return boolean + */ + protected function isSuperPart($superParts, $subParts) + { + $i = 0; + + foreach ($superParts as $superPart) { + while ($i < count($subParts) && $subParts[$i] !== $superPart) { + $i++; + } + + if ($i >= count($subParts)) { + return false; + } + + $i++; + } + + return true; + } + + //protected static $libSelectorAppend = ['selector...']; + protected function libSelectorAppend($args) + { + if (count($args) < 1) { + $this->throwError("selector-append() needs at least 1 argument"); + } + + $selectors = array_map([$this, 'getSelectorArg'], $args); + + return $this->formatOutputSelector($this->selectorAppend($selectors)); + } + + /** + * Append parts of the last selector in the list to the previous, recursively + * + * @param array $selectors + * + * @return array + * + * @throws \Leafo\ScssPhp\Exception\CompilerException + */ + protected function selectorAppend($selectors) + { + $lastSelectors = array_pop($selectors); + + if (! $lastSelectors) { + $this->throwError("Invalid selector list in selector-append()"); + } + + while (count($selectors)) { + $previousSelectors = array_pop($selectors); + + if (! $previousSelectors) { + $this->throwError("Invalid selector list in selector-append()"); + } + + // do the trick, happening $lastSelector to $previousSelector + $appended = []; + + foreach ($lastSelectors as $lastSelector) { + $previous = $previousSelectors; + + foreach ($lastSelector as $lastSelectorParts) { + foreach ($lastSelectorParts as $lastSelectorPart) { + foreach ($previous as $i => $previousSelector) { + foreach ($previousSelector as $j => $previousSelectorParts) { + $previous[$i][$j][] = $lastSelectorPart; + } + } + } + } + + foreach ($previous as $ps) { + $appended[] = $ps; + } + } + + $lastSelectors = $appended; + } + + return $lastSelectors; + } + + protected static $libSelectorExtend = ['selectors', 'extendee', 'extender']; + protected function libSelectorExtend($args) + { + list($selectors, $extendee, $extender) = $args; + + $selectors = $this->getSelectorArg($selectors); + $extendee = $this->getSelectorArg($extendee); + $extender = $this->getSelectorArg($extender); + + if (! $selectors || ! $extendee || ! $extender) { + $this->throwError("selector-extend() invalid arguments"); + } + + $extended = $this->extendOrReplaceSelectors($selectors, $extendee, $extender); + + return $this->formatOutputSelector($extended); + } + + protected static $libSelectorReplace = ['selectors', 'original', 'replacement']; + protected function libSelectorReplace($args) + { + list($selectors, $original, $replacement) = $args; + + $selectors = $this->getSelectorArg($selectors); + $original = $this->getSelectorArg($original); + $replacement = $this->getSelectorArg($replacement); + + if (! $selectors || ! $original || ! $replacement) { + $this->throwError("selector-replace() invalid arguments"); + } + + $replaced = $this->extendOrReplaceSelectors($selectors, $original, $replacement, true); + + return $this->formatOutputSelector($replaced); + } + + /** + * Extend/replace in selectors + * used by selector-extend and selector-replace that use the same logic + * + * @param array $selectors + * @param array $extendee + * @param array $extender + * @param boolean $replace + * + * @return array + */ + protected function extendOrReplaceSelectors($selectors, $extendee, $extender, $replace = false) + { + $saveExtends = $this->extends; + $saveExtendsMap = $this->extendsMap; + + $this->extends = []; + $this->extendsMap = []; + + foreach ($extendee as $es) { + // only use the first one + $this->pushExtends(reset($es), $extender, null); + } + + $extended = []; + + foreach ($selectors as $selector) { + if (! $replace) { + $extended[] = $selector; + } + + $n = count($extended); + + $this->matchExtends($selector, $extended); + + // if didnt match, keep the original selector if we are in a replace operation + if ($replace and count($extended) === $n) { + $extended[] = $selector; + } + } + + $this->extends = $saveExtends; + $this->extendsMap = $saveExtendsMap; + + return $extended; + } + + //protected static $libSelectorNest = ['selector...']; + protected function libSelectorNest($args) + { + if (count($args) < 1) { + $this->throwError("selector-nest() needs at least 1 argument"); + } + + $selectorsMap = array_map([$this, 'getSelectorArg'], $args); + + $envs = []; + foreach ($selectorsMap as $selectors) { + $env = new Environment(); + $env->selectors = $selectors; + + $envs[] = $env; + } + + $envs = array_reverse($envs); + $env = $this->extractEnv($envs); + $outputSelectors = $this->multiplySelectors($env); + + return $this->formatOutputSelector($outputSelectors); + } + + protected static $libSelectorParse = ['selectors']; + protected function libSelectorParse($args) + { + $selectors = reset($args); + $selectors = $this->getSelectorArg($selectors); + + return $this->formatOutputSelector($selectors); + } + + protected static $libSelectorUnify = ['selectors1', 'selectors2']; + protected function libSelectorUnify($args) + { + list($selectors1, $selectors2) = $args; + + $selectors1 = $this->getSelectorArg($selectors1); + $selectors2 = $this->getSelectorArg($selectors2); + + if (! $selectors1 || ! $selectors2) { + $this->throwError("selector-unify() invalid arguments"); + } + + // only consider the first compound of each + $compound1 = reset($selectors1); + $compound2 = reset($selectors2); + + // unify them and that's it + $unified = $this->unifyCompoundSelectors($compound1, $compound2); + + return $this->formatOutputSelector($unified); + } + + /** + * The selector-unify magic as its best + * (at least works as expected on test cases) + * + * @param array $compound1 + * @param array $compound2 + * @return array|mixed + */ + protected function unifyCompoundSelectors($compound1, $compound2) + { + if (! count($compound1)) { + return $compound2; + } + + if (! count($compound2)) { + return $compound1; + } + + // check that last part are compatible + $lastPart1 = array_pop($compound1); + $lastPart2 = array_pop($compound2); + $last = $this->mergeParts($lastPart1, $lastPart2); + + if (! $last) { + return [[]]; + } + + $unifiedCompound = [$last]; + $unifiedSelectors = [$unifiedCompound]; + + // do the rest + while (count($compound1) || count($compound2)) { + $part1 = end($compound1); + $part2 = end($compound2); + + if ($part1 && ($match2 = $this->matchPartInCompound($part1, $compound2))) { + list($compound2, $part2, $after2) = $match2; + + if ($after2) { + $unifiedSelectors = $this->prependSelectors($unifiedSelectors, $after2); + } + + $c = $this->mergeParts($part1, $part2); + $unifiedSelectors = $this->prependSelectors($unifiedSelectors, [$c]); + $part1 = $part2 = null; + + array_pop($compound1); + } + + if ($part2 && ($match1 = $this->matchPartInCompound($part2, $compound1))) { + list($compound1, $part1, $after1) = $match1; + + if ($after1) { + $unifiedSelectors = $this->prependSelectors($unifiedSelectors, $after1); + } + + $c = $this->mergeParts($part2, $part1); + $unifiedSelectors = $this->prependSelectors($unifiedSelectors, [$c]); + $part1 = $part2 = null; + + array_pop($compound2); + } + + $new = []; + + if ($part1 && $part2) { + array_pop($compound1); + array_pop($compound2); + + $s = $this->prependSelectors($unifiedSelectors, [$part2]); + $new = array_merge($new, $this->prependSelectors($s, [$part1])); + $s = $this->prependSelectors($unifiedSelectors, [$part1]); + $new = array_merge($new, $this->prependSelectors($s, [$part2])); + } elseif ($part1) { + array_pop($compound1); + + $new = array_merge($new, $this->prependSelectors($unifiedSelectors, [$part1])); + } elseif ($part2) { + array_pop($compound2); + + $new = array_merge($new, $this->prependSelectors($unifiedSelectors, [$part2])); + } + + if ($new) { + $unifiedSelectors = $new; + } + } + + return $unifiedSelectors; + } + + /** + * Prepend each selector from $selectors with $parts + * + * @param array $selectors + * @param array $parts + * + * @return array + */ + protected function prependSelectors($selectors, $parts) + { + $new = []; + + foreach ($selectors as $compoundSelector) { + array_unshift($compoundSelector, $parts); + + $new[] = $compoundSelector; + } + + return $new; + } + + /** + * Try to find a matching part in a compound: + * - with same html tag name + * - with some class or id or something in common + * + * @param array $part + * @param array $compound + * + * @return array|boolean + */ + protected function matchPartInCompound($part, $compound) + { + $partTag = $this->findTagName($part); + $before = $compound; + $after = []; + + // try to find a match by tag name first + while (count($before)) { + $p = array_pop($before); + + if ($partTag && $partTag !== '*' && $partTag == $this->findTagName($p)) { + return [$before, $p, $after]; + } + + $after[] = $p; + } + + // try again matching a non empty intersection and a compatible tagname + $before = $compound; + $after = []; + + while (count($before)) { + $p = array_pop($before); + + if ($this->checkCompatibleTags($partTag, $this->findTagName($p))) { + if (count(array_intersect($part, $p))) { + return [$before, $p, $after]; + } + } + + $after[] = $p; + } + + return false; + } + + /** + * Merge two part list taking care that + * - the html tag is coming first - if any + * - the :something are coming last + * + * @param array $parts1 + * @param array $parts2 + * + * @return array + */ + protected function mergeParts($parts1, $parts2) + { + $tag1 = $this->findTagName($parts1); + $tag2 = $this->findTagName($parts2); + $tag = $this->checkCompatibleTags($tag1, $tag2); + + // not compatible tags + if ($tag === false) { + return []; + } + + if ($tag) { + if ($tag1) { + $parts1 = array_diff($parts1, [$tag1]); + } + + if ($tag2) { + $parts2 = array_diff($parts2, [$tag2]); + } + } + + $mergedParts = array_merge($parts1, $parts2); + $mergedOrderedParts = []; + + foreach ($mergedParts as $part) { + if (strpos($part, ':') === 0) { + $mergedOrderedParts[] = $part; + } + } + + $mergedParts = array_diff($mergedParts, $mergedOrderedParts); + $mergedParts = array_merge($mergedParts, $mergedOrderedParts); + + if ($tag) { + array_unshift($mergedParts, $tag); + } + + return $mergedParts; + } + + /** + * Check the compatibility between two tag names: + * if both are defined they should be identical or one has to be '*' + * + * @param string $tag1 + * @param string $tag2 + * + * @return array|boolean + */ + protected function checkCompatibleTags($tag1, $tag2) + { + $tags = [$tag1, $tag2]; + $tags = array_unique($tags); + $tags = array_filter($tags); + + if (count($tags)>1) { + $tags = array_diff($tags, ['*']); + } + + // not compatible nodes + if (count($tags)>1) { + return false; + } + + return $tags; + } + + /** + * Find the html tag name in a selector parts list + * + * @param array $parts + * + * @return mixed|string + */ + protected function findTagName($parts) + { + foreach ($parts as $part) { + if (! preg_match('/^[\[.:#%_-]/', $part)) { + return $part; + } + } + + return ''; + } + + protected static $libSimpleSelectors = ['selector']; + protected function libSimpleSelectors($args) + { + $selector = reset($args); + $selector = $this->getSelectorArg($selector); + + // remove selectors list layer, keeping the first one + $selector = reset($selector); + + // remove parts list layer, keeping the first part + $part = reset($selector); + + $listParts = []; + + foreach ($part as $p) { + $listParts[] = [Type::T_STRING, '', [$p]]; + } + + return [Type::T_LIST, ',', $listParts]; + } } diff --git a/leafo/scssphp/src/Compiler/Environment.php b/leafo/scssphp/src/Compiler/Environment.php index fe309dd30..99231f368 100644 --- a/leafo/scssphp/src/Compiler/Environment.php +++ b/leafo/scssphp/src/Compiler/Environment.php @@ -33,6 +33,11 @@ class Environment */ public $store; + /** + * @var array + */ + public $storeUnreduced; + /** * @var integer */ diff --git a/leafo/scssphp/src/Formatter.php b/leafo/scssphp/src/Formatter.php index b4f90aa9d..1403859db 100644 --- a/leafo/scssphp/src/Formatter.php +++ b/leafo/scssphp/src/Formatter.php @@ -256,7 +256,8 @@ protected function write($str) $this->currentLine, $this->currentColumn, $this->currentBlock->sourceLine, - $this->currentBlock->sourceColumn - 1, //columns from parser are off by one + //columns from parser are off by one + $this->currentBlock->sourceColumn > 0 ? $this->currentBlock->sourceColumn - 1 : 0, $this->currentBlock->sourceName ); diff --git a/leafo/scssphp/src/Formatter/Compressed.php b/leafo/scssphp/src/Formatter/Compressed.php index 1faa7e11e..ab38529dd 100644 --- a/leafo/scssphp/src/Formatter/Compressed.php +++ b/leafo/scssphp/src/Formatter/Compressed.php @@ -59,4 +59,23 @@ public function blockLines(OutputBlock $block) $this->write($this->break); } } + + /** + * Output block selectors + * + * @param \Leafo\ScssPhp\Formatter\OutputBlock $block + */ + protected function blockSelectors(OutputBlock $block) + { + $inner = $this->indentStr(); + + $this->write( + $inner + . implode( + $this->tagSeparator, + str_replace([' > ', ' + ', ' ~ '], ['>', '+', '~'], $block->selectors) + ) + . $this->open . $this->break + ); + } } diff --git a/leafo/scssphp/src/Formatter/Crunched.php b/leafo/scssphp/src/Formatter/Crunched.php index 42d77b5f7..da740ccd1 100644 --- a/leafo/scssphp/src/Formatter/Crunched.php +++ b/leafo/scssphp/src/Formatter/Crunched.php @@ -57,4 +57,23 @@ public function blockLines(OutputBlock $block) $this->write($this->break); } } + + /** + * Output block selectors + * + * @param \Leafo\ScssPhp\Formatter\OutputBlock $block + */ + protected function blockSelectors(OutputBlock $block) + { + $inner = $this->indentStr(); + + $this->write( + $inner + . implode( + $this->tagSeparator, + str_replace([' > ', ' + ', ' ~ '], ['>', '+', '~'], $block->selectors) + ) + . $this->open . $this->break + ); + } } diff --git a/leafo/scssphp/src/Parser.php b/leafo/scssphp/src/Parser.php index de86f094f..c05e4ee8a 100644 --- a/leafo/scssphp/src/Parser.php +++ b/leafo/scssphp/src/Parser.php @@ -12,6 +12,7 @@ namespace Leafo\ScssPhp; use Leafo\ScssPhp\Block; +use Leafo\ScssPhp\Cache; use Leafo\ScssPhp\Compiler; use Leafo\ScssPhp\Exception\ParserException; use Leafo\ScssPhp\Node; @@ -53,6 +54,8 @@ class Parser protected static $operatorPattern; protected static $whitePattern; + protected $cache; + private $sourceName; private $sourceIndex; private $sourcePositions; @@ -65,23 +68,26 @@ class Parser private $utf8; private $encoding; private $patternModifiers; + private $commentsSeen; /** * Constructor * * @api * - * @param string $sourceName - * @param integer $sourceIndex - * @param string $encoding + * @param string $sourceName + * @param integer $sourceIndex + * @param string $encoding + * @param \Leafo\ScssPhp\Cache $cache */ - public function __construct($sourceName, $sourceIndex = 0, $encoding = 'utf-8') + public function __construct($sourceName, $sourceIndex = 0, $encoding = 'utf-8', $cache = null) { $this->sourceName = $sourceName ?: '(stdin)'; $this->sourceIndex = $sourceIndex; $this->charset = null; $this->utf8 = ! $encoding || strtolower($encoding) === 'utf-8'; $this->patternModifiers = $this->utf8 ? 'Aisu' : 'Ais'; + $this->commentsSeen = []; if (empty(static::$operatorPattern)) { static::$operatorPattern = '([*\/%+-]|[!=]\=|\>\=?|\<\=\>|\<\=?|and|or)'; @@ -95,6 +101,10 @@ public function __construct($sourceName, $sourceIndex = 0, $encoding = 'utf-8') ? '/' . $commentSingle . '[^\n]*\s*|(' . static::$commentPattern . ')\s*|\s+/AisuS' : '/' . $commentSingle . '[^\n]*\s*|(' . static::$commentPattern . ')\s*|\s+/AisS'; } + + if ($cache) { + $this->cache = $cache; + } } /** @@ -120,9 +130,11 @@ public function getSourceName() */ public function throwParseError($msg = 'parse error') { - list($line, /* $column */) = $this->getSourcePosition($this->count); + list($line, $column) = $this->getSourcePosition($this->count); - $loc = empty($this->sourceName) ? "line: $line" : "$this->sourceName on line $line"; + $loc = empty($this->sourceName) + ? "line: $line, column: $column" + : "$this->sourceName on line $line, at column $column"; if ($this->peek("(.*?)(\n|$)", $m, $this->count)) { throw new ParserException("$msg: failed at `$m[1]` $loc"); @@ -142,6 +154,19 @@ public function throwParseError($msg = 'parse error') */ public function parse($buffer) { + if ($this->cache) { + $cacheKey = $this->sourceName . ":" . md5($buffer); + $parseOptions = [ + 'charset' => $this->charset, + 'utf8' => $this->utf8, + ]; + $v = $this->cache->getCache("parse", $cacheKey, $parseOptions); + + if (! is_null($v)) { + return $v; + } + } + // strip BOM (byte order marker) if (substr($buffer, 0, 3) === "\xef\xbb\xbf") { $buffer = substr($buffer, 3); @@ -177,10 +202,12 @@ public function parse($buffer) array_unshift($this->env->children, $this->charset); } - $this->env->isRoot = true; - $this->restoreEncoding(); + if ($this->cache) { + $this->cache->setCache("parse", $cacheKey, $this->env, $parseOptions); + } + return $this->env; } @@ -271,7 +298,7 @@ public function parseSelector($buffer, &$out) * the buffer position will be left at an invalid state. In order to * avoid this, Compiler::seek() is used to remember and set buffer positions. * - * Before parsing a chain, use $s = $this->seek() to remember the current + * Before parsing a chain, use $s = $this->count to remember the current * position into $s. Then if a chain fails, use $this->seek($s) to * go back where we started. * @@ -279,14 +306,14 @@ public function parseSelector($buffer, &$out) */ protected function parseChunk() { - $s = $this->seek(); + $s = $this->count; // the directives if (isset($this->buffer[$this->count]) && $this->buffer[$this->count] === '@') { - if ($this->literal('@at-root') && + if ($this->literal('@at-root', 8) && ($this->selectors($selector) || true) && ($this->map($with) || true) && - $this->literal('{') + $this->matchChar('{') ) { $atRoot = $this->pushSpecialBlock(Type::T_AT_ROOT, $s); $atRoot->selector = $selector; @@ -297,7 +324,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@media') && $this->mediaQueryList($mediaQueryList) && $this->literal('{')) { + if ($this->literal('@media', 6) && $this->mediaQueryList($mediaQueryList) && $this->matchChar('{')) { $media = $this->pushSpecialBlock(Type::T_MEDIA, $s); $media->queryList = $mediaQueryList[2]; @@ -306,10 +333,10 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@mixin') && + if ($this->literal('@mixin', 6) && $this->keyword($mixinName) && ($this->argumentDef($args) || true) && - $this->literal('{') + $this->matchChar('{') ) { $mixin = $this->pushSpecialBlock(Type::T_MIXIN, $s); $mixin->name = $mixinName; @@ -320,13 +347,13 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@include') && + if ($this->literal('@include', 8) && $this->keyword($mixinName) && - ($this->literal('(') && + ($this->matchChar('(') && ($this->argValues($argValues) || true) && - $this->literal(')') || true) && + $this->matchChar(')') || true) && ($this->end() || - $this->literal('{') && $hasBlock = true) + $this->matchChar('{') && $hasBlock = true) ) { $child = [Type::T_INCLUDE, $mixinName, isset($argValues) ? $argValues : null, null]; @@ -342,7 +369,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@scssphp-import-once') && + if ($this->literal('@scssphp-import-once', 20) && $this->valueList($importPath) && $this->end() ) { @@ -353,7 +380,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@import') && + if ($this->literal('@import', 7) && $this->valueList($importPath) && $this->end() ) { @@ -364,7 +391,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@import') && + if ($this->literal('@import', 7) && $this->url($importPath) && $this->end() ) { @@ -375,7 +402,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@extend') && + if ($this->literal('@extend', 7) && $this->selectors($selectors) && $this->end() ) { @@ -388,10 +415,10 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@function') && + if ($this->literal('@function', 9) && $this->keyword($fnName) && $this->argumentDef($args) && - $this->literal('{') + $this->matchChar('{') ) { $func = $this->pushSpecialBlock(Type::T_FUNCTION, $s); $func->name = $fnName; @@ -402,7 +429,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@break') && $this->end()) { + if ($this->literal('@break', 6) && $this->end()) { $this->append([Type::T_BREAK], $s); return true; @@ -410,7 +437,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@continue') && $this->end()) { + if ($this->literal('@continue', 9) && $this->end()) { $this->append([Type::T_CONTINUE], $s); return true; @@ -418,8 +445,7 @@ protected function parseChunk() $this->seek($s); - - if ($this->literal('@return') && ($this->valueList($retVal) || true) && $this->end()) { + if ($this->literal('@return', 7) && ($this->valueList($retVal) || true) && $this->end()) { $this->append([Type::T_RETURN, isset($retVal) ? $retVal : [Type::T_NULL]], $s); return true; @@ -427,11 +453,11 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@each') && + if ($this->literal('@each', 5) && $this->genericList($varNames, 'variable', ',', false) && - $this->literal('in') && + $this->literal('in', 2) && $this->valueList($list) && - $this->literal('{') + $this->matchChar('{') ) { $each = $this->pushSpecialBlock(Type::T_EACH, $s); @@ -446,9 +472,9 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@while') && + if ($this->literal('@while', 6) && $this->expression($cond) && - $this->literal('{') + $this->matchChar('{') ) { $while = $this->pushSpecialBlock(Type::T_WHILE, $s); $while->cond = $cond; @@ -458,14 +484,14 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@for') && + if ($this->literal('@for', 4) && $this->variable($varName) && - $this->literal('from') && + $this->literal('from', 4) && $this->expression($start) && - ($this->literal('through') || - ($forUntil = true && $this->literal('to'))) && + ($this->literal('through', 7) || + ($forUntil = true && $this->literal('to', 2))) && $this->expression($end) && - $this->literal('{') + $this->matchChar('{') ) { $for = $this->pushSpecialBlock(Type::T_FOR, $s); $for->var = $varName[1]; @@ -478,7 +504,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@if') && $this->valueList($cond) && $this->literal('{')) { + if ($this->literal('@if', 3) && $this->valueList($cond) && $this->matchChar('{')) { $if = $this->pushSpecialBlock(Type::T_IF, $s); $if->cond = $cond; $if->cases = []; @@ -488,7 +514,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@debug') && + if ($this->literal('@debug', 6) && $this->valueList($value) && $this->end() ) { @@ -499,7 +525,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@warn') && + if ($this->literal('@warn', 5) && $this->valueList($value) && $this->end() ) { @@ -510,7 +536,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@error') && + if ($this->literal('@error', 6) && $this->valueList($value) && $this->end() ) { @@ -521,7 +547,7 @@ protected function parseChunk() $this->seek($s); - if ($this->literal('@content') && $this->end()) { + if ($this->literal('@content', 8) && $this->end()) { $this->append([Type::T_MIXIN_CONTENT], $s); return true; @@ -534,10 +560,10 @@ protected function parseChunk() if (isset($last) && $last[0] === Type::T_IF) { list(, $if) = $last; - if ($this->literal('@else')) { - if ($this->literal('{')) { + if ($this->literal('@else', 5)) { + if ($this->matchChar('{')) { $else = $this->pushSpecialBlock(Type::T_ELSE, $s); - } elseif ($this->literal('if') && $this->valueList($cond) && $this->literal('{')) { + } elseif ($this->literal('if', 2) && $this->valueList($cond) && $this->matchChar('{')) { $else = $this->pushSpecialBlock(Type::T_ELSEIF, $s); $else->cond = $cond; } @@ -554,7 +580,7 @@ protected function parseChunk() } // only retain the first @charset directive encountered - if ($this->literal('@charset') && + if ($this->literal('@charset', 8) && $this->valueList($charset) && $this->end() ) { @@ -576,10 +602,10 @@ protected function parseChunk() $this->seek($s); // doesn't match built in directive, do generic one - if ($this->literal('@', false) && + if ($this->matchChar('@', false) && $this->keyword($dirName) && ($this->variable($dirValue) || $this->openString('{', $dirValue) || true) && - $this->literal('{') + $this->matchChar('{') ) { if ($dirName === 'media') { $directive = $this->pushSpecialBlock(Type::T_MEDIA, $s); @@ -603,7 +629,7 @@ protected function parseChunk() // property shortcut // captures most properties before having to parse a selector if ($this->keyword($name, false) && - $this->literal(': ') && + $this->literal(': ', 2) && $this->valueList($value) && $this->end() ) { @@ -617,7 +643,7 @@ protected function parseChunk() // variable assigns if ($this->variable($name) && - $this->literal(':') && + $this->matchChar(':') && $this->valueList($value) && $this->end() ) { @@ -631,29 +657,38 @@ protected function parseChunk() $this->seek($s); // misc - if ($this->literal('-->')) { + if ($this->literal('-->', 3)) { return true; } // opening css block - if ($this->selectors($selectors) && $this->literal('{')) { + if ($this->selectors($selectors) && $this->matchChar('{', false)) { $this->pushBlock($selectors, $s); + if ($this->eatWhiteDefault) { + $this->whitespace(); + $this->append(null); // collect comments at the begining if needed + } + return true; } $this->seek($s); // property assign, or nested assign - if ($this->propertyName($name) && $this->literal(':')) { + if ($this->propertyName($name) && $this->matchChar(':')) { $foundSomething = false; if ($this->valueList($value)) { + if (empty($this->env->parent)) { + $this->throwParseError('expected "{"'); + } + $this->append([Type::T_ASSIGN, $name, $value], $s); $foundSomething = true; } - if ($this->literal('{')) { + if ($this->matchChar('{')) { $propBlock = $this->pushSpecialBlock(Type::T_NESTED_PROPERTY, $s); $propBlock->prefix = $name; $foundSomething = true; @@ -669,7 +704,7 @@ protected function parseChunk() $this->seek($s); // closing a block - if ($this->literal('}')) { + if ($this->matchChar('}')) { $block = $this->popBlock(); if (isset($block->type) && $block->type === Type::T_INCLUDE) { @@ -686,8 +721,8 @@ protected function parseChunk() } // extra stuff - if ($this->literal(';') || - $this->literal('