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
79 changes: 59 additions & 20 deletions scss.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,13 @@ protected function compileBlock($block) {
$this->popEnv();
}

// root level comment
protected function compileComment($block) {
$out = $this->makeOutputBlock('comment');
$out->lines[] = $block[1];
$this->scope->children[] = $out;
}

// joins together .classes and #ids
protected function flattenSelectorSingle($single) {
$joined = array();
Expand Down Expand Up @@ -563,6 +570,11 @@ protected function compileChild($child, $out) {
$compiledValue);
break;
case "comment":
if ($out->type == 'root') {
$this->compileComment($child);
break;
}

$out->lines[] = $child[1];
break;
case "mixin":
Expand Down Expand Up @@ -2496,7 +2508,6 @@ function parse($buffer) {
$this->inParens = false;
$this->pushBlock(null); // root block
$this->eatWhiteDefault = true;
$this->insertComments = true;

$this->buffer = $buffer;

Expand Down Expand Up @@ -2758,16 +2769,12 @@ protected function parseChunk() {
}

// opening css block
$oldComments = $this->insertComments;
$this->insertComments = false;
if ($this->selectors($selectors) && $this->literal("{")) {
$this->pushBlock($selectors);
$this->insertComments = $oldComments;
$b = $this->pushBlock($selectors);
return true;
} else {
$this->seek($s);
}
$this->insertComments = $oldComments;

// property assign, or nested assign
if ($this->propertyName($name) && $this->literal(":")) {
Expand Down Expand Up @@ -2845,7 +2852,18 @@ protected function pushBlock($selectors) {
$b->parent = $this->env; // not sure if we need this yet

$b->selectors = $selectors;
$b->children = array();
$b->comments = array();

if (!$this->env) {
$b->children = array();
} elseif (empty($this->env->children)) {
$this->env->children = $this->env->comments;
$b->children = array();
$this->env->comments = array();
} else {
$b->children = $this->env->comments;
$this->env->comments = array();
}

$this->env = $b;
return $b;
Expand All @@ -2858,22 +2876,41 @@ protected function pushSpecialBlock($type) {
}

protected function popBlock() {
if (empty($this->env->parent)) {
$block = $this->env;

if (empty($block->parent)) {
$this->throwParseError("unexpected }");
}

$old = $this->env;
$this->env = $this->env->parent;
unset($old->parent);
return $old;
$this->env = $block->parent;
unset($block->parent);

$comments = $block->comments;
if (count($comments)) {
$this->env->comments = $comments;
unset($block->comments);
}

return $block;
}

protected function appendComment($comment) {
$this->env->comments[] = $comment;
}

protected function append($statement, $pos=null) {
if ($pos !== null) {
$statement[-1] = $pos;
if (!$this->rootParser) $statement[-2] = $this;
}

$this->env->children[] = $statement;

$comments = $this->env->comments;
if (count($comments)) {
$this->env->children = array_merge($this->env->children, $comments);
$this->env->comments = array();
}
}

// last child that was appended
Expand Down Expand Up @@ -3455,7 +3492,9 @@ protected function interpolation(&$out, $lookWhite=true) {

$out = array("interpolate", $value, $left, $right);
$this->eatWhiteDefault = $oldWhite;
if ($this->eatWhiteDefault) $this->whitespace();
if ($this->eatWhiteDefault) {
$this->whitespace();
}
return true;
}

Expand Down Expand Up @@ -3534,7 +3573,7 @@ protected function selector(&$out) {
$selector[] = array($m[0]);
} elseif ($this->selectorSingle($part)) {
$selector[] = $part;
$this->whitespace();
$this->match('\s+', $m);
} elseif ($this->match('\/[^\/]+\/', $m)) {
$selector[] = array($m[0]);
} else {
Expand Down Expand Up @@ -3782,7 +3821,9 @@ protected function match($regex, &$out, $eatWhitespace = null) {
$r = '/'.$regex.'/Ais';
if (preg_match($r, $this->buffer, $out, null, $this->count)) {
$this->count += strlen($out[0]);
if ($eatWhitespace) $this->whitespace();
if ($eatWhitespace) {
$this->whitespace();
}
return true;
}
return false;
Expand All @@ -3792,11 +3833,9 @@ protected function match($regex, &$out, $eatWhitespace = null) {
protected function whitespace() {
$gotWhite = false;
while (preg_match(self::$whitePattern, $this->buffer, $m, null, $this->count)) {
if ($this->insertComments) {
if (isset($m[1]) && empty($this->commentsSeen[$this->count])) {
$this->append(array("comment", $m[1]));
$this->commentsSeen[$this->count] = true;
}
if (isset($m[1]) && empty($this->commentsSeen[$this->count])) {
$this->appendComment(array("comment", $m[1]));
$this->commentsSeen[$this->count] = true;
}
$this->count += strlen($m[0]);
$gotWhite = true;
Expand Down
16 changes: 16 additions & 0 deletions tests/inputs/comments.scss
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,19 @@ Here is a block comment
background: url(/*this is not a comment?*/); // uhh what happens here
}

// begin

.dummy {
color: blue;
}

/* comment 1 */
a {
/* comment 2 */
/* comment 3 */ color: red; /* comment 4 */
background-color: red; /* comment 5 */
/* comment 6 */
}
/* comment 7 */

// end
22 changes: 18 additions & 4 deletions tests/outputs/comments.css
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,29 @@ Here is a block comment

**/
/*hello*/
/*yeah*/
div {
/* another property */
border: 1px solid red;
/* another property */
color: url('http://mage-page.com');
string: "hello /* this is not a comment */";
world: "// neither is this";
/*what if this is a comment */
string: 'hello /* this is not a comment */';
/*what if this is a comment */
world: '// neither is this';
what-ever: 100px;
/*this is not a comment?*/
background: url(); }
background: url();
/*this is not a comment?*/ }

.dummy {
color: blue; }
/* comment 1 */
a {
/* comment 2 */
/* comment 3 */
color: red;
/* comment 4 */
background-color: red;
/* comment 5 */
/* comment 6 */ }
/* comment 7 */
Loading