Skip to content

Commit

Permalink
add support for regular css multi-line comments. Closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Jun 2, 2013
1 parent 3d9d9b8 commit 36daf50
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 6 deletions.
12 changes: 11 additions & 1 deletion examples/nesting.css
@@ -1,8 +1,18 @@

/*
regular multi-line comment
*/

/* regular comment */

body
background: #888
color: #eee

// stripped comment

ul
margin: 0
li
Expand All @@ -16,4 +26,4 @@ ul
ul
width: 50px
li
list-style: disc
list-style: disc
10 changes: 10 additions & 0 deletions lib/compiler.js
Expand Up @@ -47,6 +47,8 @@ module.exports = function(node){
return ret;
case 'prop':
return prop(node);
case 'comment':
return comment(node);
default:
throw new Error('invalid node "' + node[0] + '"');
}
Expand All @@ -67,6 +69,14 @@ module.exports = function(node){
return buf.join('');
}

/**
* Visit comment.
*/

function comment(node) {
return indent() + '/*' + node[1] + '*/\n';
}

/**
* Visit prop.
*/
Expand Down
17 changes: 17 additions & 0 deletions lib/lexer.js
Expand Up @@ -93,6 +93,7 @@ module.exports = function(str) {
function next() {
return stashed()
|| comment()
|| csscomment()
|| indentation()
|| prop()
|| rule();
Expand All @@ -117,6 +118,22 @@ module.exports = function(str) {
return next();
}

/**
* Multiline comment.
*/

function csscomment() {
if ('/' != str[0] || '*' != str[1]) return;
str = str.slice(2);

var i = 0;
while ('*' != str[i] && '/' != str[i + 1]) ++i;

var buf = str.slice(0, i);
str = str.slice(buf.length + 2);

return ['comment', buf];
}

/**
* INDENT
Expand Down
1 change: 1 addition & 0 deletions lib/parser.js
Expand Up @@ -78,6 +78,7 @@ module.exports = function(str) {
function stmt() {
if (is('rule')) return rule();
if (is('prop')) return prop();
return next();
}

/**
Expand Down
15 changes: 11 additions & 4 deletions test/cases/comments.css
@@ -1,11 +1,18 @@

/*
this is a button
*/

// foo
button
// bar
color: #eee
// bar



// baz
background: blue
/* css style */
background: blue
8 changes: 7 additions & 1 deletion test/cases/comments.out.css
@@ -1,4 +1,10 @@
/*
this is a button
*/


button {
color: #eee;
/* css style */
background: blue;
}
}

0 comments on commit 36daf50

Please sign in to comment.