Skip to content

Commit cd6bedb

Browse files
committed
Add interpolation support selector.
Update phpunit 7. Fix compile code like: @at-root a#{&} { }
1 parent 5caf261 commit cd6bedb

File tree

11 files changed

+67
-14
lines changed

11 files changed

+67
-14
lines changed

composer.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
1818
"psr-4": { "Leafo\\ScssPhp\\": "src/" }
1919
},
2020
"autoload-dev": {
21-
"psr-4": { "Leafo\\ScssPhp\\Test\\": "tests/" }
21+
"psr-4": { "Leafo\\ScssPhp\\Test\\": "tests/" },
22+
"classmap": [
23+
"src/"
24+
]
2225
},
2326
"require": {
2427
"php": ">=5.4.0"
2528
},
2629
"require-dev": {
2730
"squizlabs/php_codesniffer": "~2.5",
28-
"phpunit/phpunit": "~4.6"
31+
"phpunit/phpunit": "^7"
2932
},
3033
"bin": ["bin/pscss"],
3134
"archive": {

src/Block.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,12 @@ class Block
6262
* @var array
6363
*/
6464
public $children;
65+
/**
66+
* @var \Leafo\ScssPhp\Block
67+
*/
68+
public $atrootParent;
69+
/**
70+
* @var array
71+
*/
72+
public $atrootParentSelectors;
6573
}

src/Compiler.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,8 @@ protected function compileAtRoot(Block $block)
796796

797797
$this->env = $this->filterWithout($envs, $without);
798798
$newBlock = $this->spliceTree($envs, $block, $without);
799+
if (isset($block->atrootParent))
800+
$newBlock[1]->atrootParent = $block->atrootParent;
799801

800802
$saveScope = $this->scope;
801803
$this->scope = $this->rootBlock;
@@ -1293,6 +1295,9 @@ protected function hasSelectorPlaceholder($selector)
12931295

12941296
foreach ($selector as $parts) {
12951297
foreach ($parts as $part) {
1298+
if (is_array($part)) {
1299+
return false;
1300+
}
12961301
if (strlen($part) && '%' === $part[0]) {
12971302
return true;
12981303
}
@@ -1878,7 +1883,11 @@ protected function compileChild($child, OutputBlock $out)
18781883
if (isset($mixin->args)) {
18791884
$this->applyArguments($mixin->args, $argValues);
18801885
}
1881-
1886+
foreach ($mixin->children as $stm) {
1887+
if ($stm[0] == Type::T_AT_ROOT) {
1888+
$stm[1]->atrootParentSelectors = $callingScope->selectors;
1889+
}
1890+
}
18821891
$this->env->marker = 'mixin';
18831892

18841893
$this->compileChildrenNoReturn($mixin->children, $out);
@@ -2912,8 +2921,16 @@ protected function multiplySelectors(Environment $env)
29122921
if (empty($env->selectors)) {
29132922
continue;
29142923
}
2915-
2924+
if ($env->block->type == Type::T_AT_ROOT) {
2925+
break;
2926+
}
29162927
$selectors = [];
2928+
if (!empty($env->block->parent->atrootParent) && !empty($env->block->parent->atrootParent->selectors)) {
2929+
$parentSelectors = $env->block->parent->atrootParent->selectors;
2930+
}
2931+
if (!empty($env->parent->parent->block->atrootParentSelectors)) {
2932+
$parentSelectors = $env->parent->parent->block->atrootParentSelectors;
2933+
}
29172934

29182935
foreach ($env->selectors as $selector) {
29192936
foreach ($parentSelectors as $parent) {

src/Parser.php

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ protected function pushBlock($selectors, $pos = 0)
715715
$b->selectors = $selectors;
716716
$b->comments = [];
717717
$b->parent = $this->env;
718+
$b->atrootParent = $this->env;
718719

719720
if (! $this->env) {
720721
$b->children = [];
@@ -762,7 +763,9 @@ protected function popBlock()
762763
if (empty($block->parent)) {
763764
$this->throwParseError('unexpected }');
764765
}
765-
766+
// if ($block->type == Type::T_AT_ROOT || $block->type == Type::T_MIXIN || $block->type == Type::T_INCLUDE) {
767+
// $block->atrootParent = $block->parent;
768+
// }
766769
$this->env = $block->parent;
767770
unset($block->parent);
768771

@@ -1897,18 +1900,35 @@ protected function interpolation(&$out, $lookWhite = true)
18971900
{
18981901
$oldWhite = $this->eatWhiteDefault;
18991902
$this->eatWhiteDefault = true;
1903+
$selector = false;
19001904

19011905
$s = $this->seek();
19021906

19031907
if ($this->literal('#{') && $this->valueList($value) && $this->literal('}', false)) {
1908+
19041909
if ($lookWhite) {
19051910
$left = preg_match('/\s/', $this->buffer[$s - 1]) ? ' ' : '';
1906-
$right = preg_match('/\s/', $this->buffer[$this->count]) ? ' ': '';
1911+
$right = preg_match('/\s/', $this->buffer[$this->count]) ? ' ' : '';
19071912
} else {
19081913
$left = $right = false;
19091914
}
1910-
19111915
$out = [Type::T_INTERPOLATE, $value, $left, $right];
1916+
1917+
$this->eatWhiteDefault = $oldWhite;
1918+
1919+
if ($this->eatWhiteDefault) {
1920+
$this->whitespace();
1921+
}
1922+
1923+
return true;
1924+
}
1925+
1926+
$this->seek($s);
1927+
1928+
if ($this->literal('#{') && $selector = $this->selectorSingle($sel) && $this->literal('}', false)) {
1929+
1930+
$out = $sel[0];
1931+
19121932
$this->eatWhiteDefault = $oldWhite;
19131933

19141934
if ($this->eatWhiteDefault) {
@@ -2095,6 +2115,11 @@ protected function selectorSingle(&$out)
20952115
$parts[] = Compiler::$selfSelector;
20962116
continue;
20972117
}
2118+
// self
2119+
// if ($this->literal('#{&}', true)) {
2120+
// $parts[] = Compiler::$selfSelector;
2121+
// continue;
2122+
// }
20982123

20992124
if ($this->literal('.', false)) {
21002125
$parts[] = '.';

tests/ApiTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Leaf Corcoran <leafot@gmail.com>
2020
*/
21-
class ApiTest extends \PHPUnit_Framework_TestCase
21+
class ApiTest extends \PHPUnit\Framework\TestCase
2222
{
2323
public function setUp()
2424
{

tests/Base64VLQTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Anthon Pang <anthon.pang@gmail.com>
2020
*/
21-
class Base64VLQTest extends \PHPUnit_Framework_TestCase
21+
class Base64VLQTest extends \PHPUnit\Framework\TestCase
2222
{
2323
/**
2424
* Test encode

tests/ExceptionTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Leaf Corcoran <leafot@gmail.com>
2020
*/
21-
class ExceptionTest extends \PHPUnit_Framework_TestCase
21+
class ExceptionTest extends \PHPUnit\Framework\TestCase
2222
{
2323
public function setUp()
2424
{

tests/FailingTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*
2323
* @author Anthon Pang <anthon.pang@gmail.com>
2424
*/
25-
class FailingTest extends \PHPUnit_Framework_TestCase
25+
class FailingTest extends \PHPUnit\Framework\TestCase
2626
{
2727
public function setUp()
2828
{

tests/InputTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function _quote($str)
2929
*
3030
* @author Leaf Corcoran <leafot@gmail.com>
3131
*/
32-
class InputTest extends \PHPUnit_Framework_TestCase
32+
class InputTest extends \PHPUnit\Framework\TestCase
3333
{
3434
protected static $inputDir = 'inputs';
3535
protected static $outputDir = 'outputs';

tests/ScssTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @author Leaf Corcoran <leafot@gmail.com>
2020
*/
21-
class ScssTest extends \PHPUnit_Framework_TestCase
21+
class ScssTest extends \PHPUnit\Framework\TestCase
2222
{
2323
/**
2424
* @param string $name

0 commit comments

Comments
 (0)