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..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; } @@ -980,12 +983,49 @@ 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; + } else { + $comment[] = substr($this->buffer, $this->count, 2); + $this->count += 2; + } + + $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; + $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 +1039,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; } @@ -1012,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; diff --git a/tests/inputs/comments.scss b/tests/inputs/comments.scss index 3e641cc1..31712ac7 100644 --- a/tests/inputs/comments.scss +++ b/tests/inputs/comments.scss @@ -45,4 +45,23 @@ a { // коммент +$color:red; +$radius:2px; + +/* This is a comment with vars references: +color:#{$color} +and a fake #{ doing nothing +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 f6f8d184..c66b30bf 100644 --- a/tests/outputs/comments.css +++ b/tests/outputs/comments.css @@ -30,3 +30,13 @@ a { /* comment 6 */ } /* comment 7 */ /* коммент */ +/* This is a comment with vars references: +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 3669d1d8..f7256abb 100644 --- a/tests/outputs_numbered/comments.css +++ b/tests/outputs_numbered/comments.css @@ -32,3 +32,15 @@ a { /* comment 6 */ } /* comment 7 */ /* коммент */ +/* This is a comment with vars references: +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%; }