Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
70 changes: 57 additions & 13 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand All @@ -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;

Expand Down
19 changes: 19 additions & 0 deletions tests/inputs/comments.scss
Original file line number Diff line number Diff line change
Expand Up @@ -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%;
}
}
10 changes: 10 additions & 0 deletions tests/outputs/comments.css
Original file line number Diff line number Diff line change
Expand Up @@ -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%; }
12 changes: 12 additions & 0 deletions tests/outputs_numbered/comments.css
Original file line number Diff line number Diff line change
Expand Up @@ -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%; }