From cd6bedb75b1155426cd7f21de6a5314965b1cffc Mon Sep 17 00:00:00 2001 From: peterou Date: Thu, 17 Jan 2019 20:26:24 +0800 Subject: [PATCH] Add interpolation support selector. Update phpunit 7. Fix compile code like: @at-root a#{&} { } --- composer.json | 7 +++++-- src/Block.php | 8 ++++++++ src/Compiler.php | 21 +++++++++++++++++++-- src/Parser.php | 31 ++++++++++++++++++++++++++++--- tests/ApiTest.php | 2 +- tests/Base64VLQTest.php | 2 +- tests/ExceptionTest.php | 2 +- tests/FailingTest.php | 2 +- tests/InputTest.php | 2 +- tests/ScssTest.php | 2 +- tests/ServerTest.php | 2 +- 11 files changed, 67 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 95118a32..e1d0082e 100644 --- a/composer.json +++ b/composer.json @@ -18,14 +18,17 @@ "psr-4": { "Leafo\\ScssPhp\\": "src/" } }, "autoload-dev": { - "psr-4": { "Leafo\\ScssPhp\\Test\\": "tests/" } + "psr-4": { "Leafo\\ScssPhp\\Test\\": "tests/" }, + "classmap": [ + "src/" + ] }, "require": { "php": ">=5.4.0" }, "require-dev": { "squizlabs/php_codesniffer": "~2.5", - "phpunit/phpunit": "~4.6" + "phpunit/phpunit": "^7" }, "bin": ["bin/pscss"], "archive": { diff --git a/src/Block.php b/src/Block.php index a6ef8e03..49ac410f 100644 --- a/src/Block.php +++ b/src/Block.php @@ -62,4 +62,12 @@ class Block * @var array */ public $children; + /** + * @var \Leafo\ScssPhp\Block + */ + public $atrootParent; + /** + * @var array + */ + public $atrootParentSelectors; } diff --git a/src/Compiler.php b/src/Compiler.php index ed80d8d3..cbebb776 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -796,6 +796,8 @@ protected function compileAtRoot(Block $block) $this->env = $this->filterWithout($envs, $without); $newBlock = $this->spliceTree($envs, $block, $without); + if (isset($block->atrootParent)) + $newBlock[1]->atrootParent = $block->atrootParent; $saveScope = $this->scope; $this->scope = $this->rootBlock; @@ -1293,6 +1295,9 @@ protected function hasSelectorPlaceholder($selector) foreach ($selector as $parts) { foreach ($parts as $part) { + if (is_array($part)) { + return false; + } if (strlen($part) && '%' === $part[0]) { return true; } @@ -1878,7 +1883,11 @@ protected function compileChild($child, OutputBlock $out) if (isset($mixin->args)) { $this->applyArguments($mixin->args, $argValues); } - + foreach ($mixin->children as $stm) { + if ($stm[0] == Type::T_AT_ROOT) { + $stm[1]->atrootParentSelectors = $callingScope->selectors; + } + } $this->env->marker = 'mixin'; $this->compileChildrenNoReturn($mixin->children, $out); @@ -2912,8 +2921,16 @@ protected function multiplySelectors(Environment $env) if (empty($env->selectors)) { continue; } - + if ($env->block->type == Type::T_AT_ROOT) { + break; + } $selectors = []; + if (!empty($env->block->parent->atrootParent) && !empty($env->block->parent->atrootParent->selectors)) { + $parentSelectors = $env->block->parent->atrootParent->selectors; + } + if (!empty($env->parent->parent->block->atrootParentSelectors)) { + $parentSelectors = $env->parent->parent->block->atrootParentSelectors; + } foreach ($env->selectors as $selector) { foreach ($parentSelectors as $parent) { diff --git a/src/Parser.php b/src/Parser.php index 748d38ae..88a7afc2 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -715,6 +715,7 @@ protected function pushBlock($selectors, $pos = 0) $b->selectors = $selectors; $b->comments = []; $b->parent = $this->env; + $b->atrootParent = $this->env; if (! $this->env) { $b->children = []; @@ -762,7 +763,9 @@ protected function popBlock() if (empty($block->parent)) { $this->throwParseError('unexpected }'); } - +// if ($block->type == Type::T_AT_ROOT || $block->type == Type::T_MIXIN || $block->type == Type::T_INCLUDE) { +// $block->atrootParent = $block->parent; +// } $this->env = $block->parent; unset($block->parent); @@ -1897,18 +1900,35 @@ protected function interpolation(&$out, $lookWhite = true) { $oldWhite = $this->eatWhiteDefault; $this->eatWhiteDefault = true; + $selector = false; $s = $this->seek(); if ($this->literal('#{') && $this->valueList($value) && $this->literal('}', false)) { + if ($lookWhite) { $left = preg_match('/\s/', $this->buffer[$s - 1]) ? ' ' : ''; - $right = preg_match('/\s/', $this->buffer[$this->count]) ? ' ': ''; + $right = preg_match('/\s/', $this->buffer[$this->count]) ? ' ' : ''; } else { $left = $right = false; } - $out = [Type::T_INTERPOLATE, $value, $left, $right]; + + $this->eatWhiteDefault = $oldWhite; + + if ($this->eatWhiteDefault) { + $this->whitespace(); + } + + return true; + } + + $this->seek($s); + + if ($this->literal('#{') && $selector = $this->selectorSingle($sel) && $this->literal('}', false)) { + + $out = $sel[0]; + $this->eatWhiteDefault = $oldWhite; if ($this->eatWhiteDefault) { @@ -2095,6 +2115,11 @@ protected function selectorSingle(&$out) $parts[] = Compiler::$selfSelector; continue; } + // self +// if ($this->literal('#{&}', true)) { +// $parts[] = Compiler::$selfSelector; +// continue; +// } if ($this->literal('.', false)) { $parts[] = '.'; diff --git a/tests/ApiTest.php b/tests/ApiTest.php index f39e7f62..5ad9b74c 100644 --- a/tests/ApiTest.php +++ b/tests/ApiTest.php @@ -18,7 +18,7 @@ * * @author Leaf Corcoran */ -class ApiTest extends \PHPUnit_Framework_TestCase +class ApiTest extends \PHPUnit\Framework\TestCase { public function setUp() { diff --git a/tests/Base64VLQTest.php b/tests/Base64VLQTest.php index a3b638de..8dba54c2 100644 --- a/tests/Base64VLQTest.php +++ b/tests/Base64VLQTest.php @@ -18,7 +18,7 @@ * * @author Anthon Pang */ -class Base64VLQTest extends \PHPUnit_Framework_TestCase +class Base64VLQTest extends \PHPUnit\Framework\TestCase { /** * Test encode diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index f5fa17a5..e0233c37 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -18,7 +18,7 @@ * * @author Leaf Corcoran */ -class ExceptionTest extends \PHPUnit_Framework_TestCase +class ExceptionTest extends \PHPUnit\Framework\TestCase { public function setUp() { diff --git a/tests/FailingTest.php b/tests/FailingTest.php index 86865049..8fa0f0b9 100644 --- a/tests/FailingTest.php +++ b/tests/FailingTest.php @@ -22,7 +22,7 @@ * * @author Anthon Pang */ -class FailingTest extends \PHPUnit_Framework_TestCase +class FailingTest extends \PHPUnit\Framework\TestCase { public function setUp() { diff --git a/tests/InputTest.php b/tests/InputTest.php index 709a9596..76dceec7 100644 --- a/tests/InputTest.php +++ b/tests/InputTest.php @@ -29,7 +29,7 @@ function _quote($str) * * @author Leaf Corcoran */ -class InputTest extends \PHPUnit_Framework_TestCase +class InputTest extends \PHPUnit\Framework\TestCase { protected static $inputDir = 'inputs'; protected static $outputDir = 'outputs'; diff --git a/tests/ScssTest.php b/tests/ScssTest.php index 96f0d76e..1f2536f8 100644 --- a/tests/ScssTest.php +++ b/tests/ScssTest.php @@ -18,7 +18,7 @@ * * @author Leaf Corcoran */ -class ScssTest extends \PHPUnit_Framework_TestCase +class ScssTest extends \PHPUnit\Framework\TestCase { /** * @param string $name diff --git a/tests/ServerTest.php b/tests/ServerTest.php index 290d8fbf..48a6392c 100644 --- a/tests/ServerTest.php +++ b/tests/ServerTest.php @@ -20,7 +20,7 @@ * * @author Zimzat */ -class ServerTest extends \PHPUnit_Framework_TestCase +class ServerTest extends \PHPUnit\Framework\TestCase { public function testCheckedCachedCompile() {