From 7e547154fe5864ed04e9e07655317384df082281 Mon Sep 17 00:00:00 2001 From: Cerdic Date: Mon, 3 Jun 2019 15:57:04 +0200 Subject: [PATCH 1/6] Unit test for https://github.com/leafo/scssphp/issues/565 : interpolation in comments --- tests/inputs/comments.scss | 8 ++++++++ tests/outputs/comments.css | 4 ++++ tests/outputs_numbered/comments.css | 4 ++++ 3 files changed, 16 insertions(+) diff --git a/tests/inputs/comments.scss b/tests/inputs/comments.scss index 3e641cc1..65cf8791 100644 --- a/tests/inputs/comments.scss +++ b/tests/inputs/comments.scss @@ -45,4 +45,12 @@ a { // коммент +$color:red; +$radius:2px; + +/* This is a comment with vars references: +color:#{$color} +radius:#{$radius} + */ + // end diff --git a/tests/outputs/comments.css b/tests/outputs/comments.css index f6f8d184..51088288 100644 --- a/tests/outputs/comments.css +++ b/tests/outputs/comments.css @@ -30,3 +30,7 @@ a { /* comment 6 */ } /* comment 7 */ /* коммент */ +/* This is a comment with vars references: +color:red +radius:2px + */ diff --git a/tests/outputs_numbered/comments.css b/tests/outputs_numbered/comments.css index 3669d1d8..874ae9f9 100644 --- a/tests/outputs_numbered/comments.css +++ b/tests/outputs_numbered/comments.css @@ -32,3 +32,7 @@ a { /* comment 6 */ } /* comment 7 */ /* коммент */ +/* This is a comment with vars references: +color:red +radius:2px + */ From b91f01d87d0fe17dfd873855b3c4ba439408761a Mon Sep 17 00:00:00 2001 From: Cerdic Date: Mon, 3 Jun 2019 15:58:41 +0200 Subject: [PATCH 2/6] Fix https://github.com/leafo/scssphp/issues/565 : parse and evaluate interpolation in comments --- src/Compiler.php | 6 +++++- src/Parser.php | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/src/Compiler.php b/src/Compiler.php index e3f51d7c..bf269676 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -1349,7 +1349,11 @@ protected function compileBlock(Block $block) protected function compileComment($block) { $out = $this->makeOutputBlock(Type::T_COMMENT); - $out->lines[] = $block[1]; + if (is_string($block[1])) { + $out->lines[] = $block[1]; + } else { + $out->lines[] = $this->compileValue($block[1]); + } $this->scope->children[] = $out; } diff --git a/src/Parser.php b/src/Parser.php index 076f1620..9c286d49 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -980,12 +980,47 @@ protected function whitespace() while (preg_match(static::$whitePattern, $this->buffer, $m, null, $this->count)) { if (isset($m[1]) && empty($this->commentsSeen[$this->count])) { - $this->appendComment([Type::T_COMMENT, $m[1]]); + // comment that are kept in the output CSS + $comment = []; + $endCommentCount = $this->count + strlen($m[1]); + // find interpolations in comment + $p = strpos($this->buffer, '#{', $this->count); + while ($p !== false && $p < $endCommentCount) { + $c = substr($this->buffer, $this->count, $p - $this->count); + $comment[] = $c; + $this->count = $p; + + $out = null; + if ($this->interpolation($out)) { + // keep right spaces in the following string part + if ($out[3]) { + while($this->buffer[$this->count-1] !== '}') { + $this->count--; + } + $out[3] = ''; + } + $comment[] = $out; + } + $p = strpos($this->buffer, '#{', $this->count); + } + // remaining part + $c = substr($this->buffer, $this->count, $endCommentCount - $this->count); + + if (!$comment) { + // single part static comment + $this->appendComment([Type::T_COMMENT, $c]); + } else { + $comment[] = $c; + var_dump($comment); + $this->appendComment([Type::T_COMMENT, [Type::T_STRING, '', $comment]]); + } $this->commentsSeen[$this->count] = true; + $this->count = $endCommentCount; + } else { + // comment that are ignored and not kept in the output css + $this->count += strlen($m[0]); } - - $this->count += strlen($m[0]); $gotWhite = true; } @@ -999,7 +1034,9 @@ protected function whitespace() */ protected function appendComment($comment) { - $comment[1] = substr(preg_replace(['/^\s+/m', '/^(.)/m'], ['', ' \1'], $comment[1]), 1); + if ($comment[0] === Type::T_COMMENT && is_string($comment[1])) { + $comment[1] = substr(preg_replace(['/^\s+/m', '/^(.)/m'], ['', ' \1'], $comment[1]), 1); + } $this->env->comments[] = $comment; } From 0ca337762d7a123c1f5cab245a532f9db78de428 Mon Sep 17 00:00:00 2001 From: Cerdic Date: Tue, 4 Jun 2019 09:19:37 +0200 Subject: [PATCH 3/6] Fix for infinite loop with a non interpolation #{ sequence, add this to the test case and a forgotten var_dump --- src/Parser.php | 5 ++++- tests/inputs/comments.scss | 1 + tests/outputs/comments.css | 1 + tests/outputs_numbered/comments.css | 1 + 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/Parser.php b/src/Parser.php index 9c286d49..db96cfa4 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -1001,6 +1001,10 @@ protected function whitespace() } $comment[] = $out; } + else { + $comment[] = substr($this->buffer, $this->count, 2); + $this->count += 2; + } $p = strpos($this->buffer, '#{', $this->count); } @@ -1012,7 +1016,6 @@ protected function whitespace() $this->appendComment([Type::T_COMMENT, $c]); } else { $comment[] = $c; - var_dump($comment); $this->appendComment([Type::T_COMMENT, [Type::T_STRING, '', $comment]]); } $this->commentsSeen[$this->count] = true; diff --git a/tests/inputs/comments.scss b/tests/inputs/comments.scss index 65cf8791..10ff670e 100644 --- a/tests/inputs/comments.scss +++ b/tests/inputs/comments.scss @@ -50,6 +50,7 @@ $radius:2px; /* This is a comment with vars references: color:#{$color} +and a fake #{ doing nothing radius:#{$radius} */ diff --git a/tests/outputs/comments.css b/tests/outputs/comments.css index 51088288..6cafd02e 100644 --- a/tests/outputs/comments.css +++ b/tests/outputs/comments.css @@ -32,5 +32,6 @@ a { /* коммент */ /* This is a comment with vars references: color:red +and a fake #{ doing nothing radius:2px */ diff --git a/tests/outputs_numbered/comments.css b/tests/outputs_numbered/comments.css index 874ae9f9..7d751889 100644 --- a/tests/outputs_numbered/comments.css +++ b/tests/outputs_numbered/comments.css @@ -34,5 +34,6 @@ a { /* коммент */ /* This is a comment with vars references: color:red +and a fake #{ doing nothing radius:2px */ From b69e8802b7a9e5c883db01eb7cb66cc40af07c08 Mon Sep 17 00:00:00 2001 From: Cerdic Date: Tue, 4 Jun 2019 11:38:05 +0200 Subject: [PATCH 4/6] PSR2 --- src/Parser.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index db96cfa4..15a3d719 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -994,14 +994,13 @@ protected function whitespace() if ($this->interpolation($out)) { // keep right spaces in the following string part if ($out[3]) { - while($this->buffer[$this->count-1] !== '}') { + while ($this->buffer[$this->count-1] !== '}') { $this->count--; } $out[3] = ''; } $comment[] = $out; - } - else { + } else { $comment[] = substr($this->buffer, $this->count, 2); $this->count += 2; } From abb02a443a04d3ce62f17d02a5fa662a8182a874 Mon Sep 17 00:00:00 2001 From: Cerdic Date: Tue, 4 Jun 2019 11:40:52 +0200 Subject: [PATCH 5/6] Test case for https://github.com/leafo/scssphp/issues/479 --- tests/inputs/comments.scss | 10 ++++++++++ tests/outputs/comments.css | 5 +++++ tests/outputs_numbered/comments.css | 7 +++++++ 3 files changed, 22 insertions(+) diff --git a/tests/inputs/comments.scss b/tests/inputs/comments.scss index 10ff670e..31712ac7 100644 --- a/tests/inputs/comments.scss +++ b/tests/inputs/comments.scss @@ -55,3 +55,13 @@ radius:#{$radius} */ // end + +// comment position in blocks +.textimage-background-fullheight { + .background { + /* @noflip */ + display: block; + /* hop */ + width: 100%; + } +} diff --git a/tests/outputs/comments.css b/tests/outputs/comments.css index 6cafd02e..c66b30bf 100644 --- a/tests/outputs/comments.css +++ b/tests/outputs/comments.css @@ -35,3 +35,8 @@ color:red and a fake #{ doing nothing radius:2px */ +.textimage-background-fullheight .background { + /* @noflip */ + display: block; + /* hop */ + width: 100%; } diff --git a/tests/outputs_numbered/comments.css b/tests/outputs_numbered/comments.css index 7d751889..f7256abb 100644 --- a/tests/outputs_numbered/comments.css +++ b/tests/outputs_numbered/comments.css @@ -37,3 +37,10 @@ color:red and a fake #{ doing nothing radius:2px */ +/* line 60, inputs/comments.scss */ +/* line 61, inputs/comments.scss */ + .textimage-background-fullheight .background { + /* @noflip */ + display: block; + /* hop */ + width: 100%; } From 8020c8128c83bd2d8ae65b0a4709d9fa9cb89420 Mon Sep 17 00:00:00 2001 From: Cerdic Date: Tue, 4 Jun 2019 11:37:38 +0200 Subject: [PATCH 6/6] Fix https://github.com/leafo/scssphp/issues/479: ensure comments starting a block will be in this block even if previous is empty --- src/Parser.php | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/Parser.php b/src/Parser.php index 15a3d719..f99e231e 100644 --- a/src/Parser.php +++ b/src/Parser.php @@ -662,9 +662,12 @@ protected function parseChunk() } // opening css block - if ($this->selectors($selectors) && $this->matchChar('{')) { + 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; } @@ -1051,15 +1054,17 @@ protected function appendComment($comment) */ protected function append($statement, $pos = null) { - if ($pos !== null) { - list($line, $column) = $this->getSourcePosition($pos); + if (! is_null($statement)) { + if ($pos !== null) { + list($line, $column) = $this->getSourcePosition($pos); - $statement[static::SOURCE_LINE] = $line; - $statement[static::SOURCE_COLUMN] = $column; - $statement[static::SOURCE_INDEX] = $this->sourceIndex; - } + $statement[static::SOURCE_LINE] = $line; + $statement[static::SOURCE_COLUMN] = $column; + $statement[static::SOURCE_INDEX] = $this->sourceIndex; + } - $this->env->children[] = $statement; + $this->env->children[] = $statement; + } $comments = $this->env->comments;