Skip to content

Commit 36daf50

Browse files
committed
add support for regular css multi-line comments. Closes reworkcss#12
1 parent 3d9d9b8 commit 36daf50

File tree

6 files changed

+57
-6
lines changed

6 files changed

+57
-6
lines changed

examples/nesting.css

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11

2+
/*
3+
4+
regular multi-line comment
5+
6+
*/
7+
8+
/* regular comment */
9+
210
body
311
background: #888
412
color: #eee
513

14+
// stripped comment
15+
616
ul
717
margin: 0
818
li
@@ -16,4 +26,4 @@ ul
1626
ul
1727
width: 50px
1828
li
19-
list-style: disc
29+
list-style: disc

lib/compiler.js

+10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ module.exports = function(node){
4747
return ret;
4848
case 'prop':
4949
return prop(node);
50+
case 'comment':
51+
return comment(node);
5052
default:
5153
throw new Error('invalid node "' + node[0] + '"');
5254
}
@@ -67,6 +69,14 @@ module.exports = function(node){
6769
return buf.join('');
6870
}
6971

72+
/**
73+
* Visit comment.
74+
*/
75+
76+
function comment(node) {
77+
return indent() + '/*' + node[1] + '*/\n';
78+
}
79+
7080
/**
7181
* Visit prop.
7282
*/

lib/lexer.js

+17
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ module.exports = function(str) {
9393
function next() {
9494
return stashed()
9595
|| comment()
96+
|| csscomment()
9697
|| indentation()
9798
|| prop()
9899
|| rule();
@@ -117,6 +118,22 @@ module.exports = function(str) {
117118
return next();
118119
}
119120

121+
/**
122+
* Multiline comment.
123+
*/
124+
125+
function csscomment() {
126+
if ('/' != str[0] || '*' != str[1]) return;
127+
str = str.slice(2);
128+
129+
var i = 0;
130+
while ('*' != str[i] && '/' != str[i + 1]) ++i;
131+
132+
var buf = str.slice(0, i);
133+
str = str.slice(buf.length + 2);
134+
135+
return ['comment', buf];
136+
}
120137

121138
/**
122139
* INDENT

lib/parser.js

+1
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ module.exports = function(str) {
7878
function stmt() {
7979
if (is('rule')) return rule();
8080
if (is('prop')) return prop();
81+
return next();
8182
}
8283

8384
/**

test/cases/comments.css

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11

2+
/*
3+
4+
this is a button
5+
6+
*/
7+
28
// foo
39
button
410
// bar
511
color: #eee
612
// bar
7-
8-
9-
13+
14+
15+
1016
// baz
11-
background: blue
17+
/* css style */
18+
background: blue

test/cases/comments.out.css

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1+
/*
2+
this is a button
3+
*/
4+
5+
16
button {
27
color: #eee;
8+
/* css style */
39
background: blue;
4-
}
10+
}

0 commit comments

Comments
 (0)