Skip to content

Commit 4c6a873

Browse files
committed
add comment support
1 parent db8f5ca commit 4c6a873

File tree

4 files changed

+109
-0
lines changed

4 files changed

+109
-0
lines changed

index.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ module.exports = function(css){
1414
function stylesheet() {
1515
var rules = [];
1616
var node;
17+
comments();
1718
while (node = rule()) {
19+
comments();
1820
rules.push(node);
1921
}
2022
return { stylesheet: { rules: rules }};
@@ -31,6 +33,37 @@ module.exports = function(css){
3133
return m;
3234
}
3335

36+
/**
37+
* Parse whitespace.
38+
*/
39+
40+
function whitespace() {
41+
match(/^\s*/);
42+
}
43+
44+
/**
45+
* Parse comments;
46+
*/
47+
48+
function comments() {
49+
while (comment()) ;
50+
}
51+
52+
/**
53+
* Parse comment.
54+
*/
55+
56+
function comment() {
57+
if ('/' == css[0] && '*' == css[1]) {
58+
var i = 2;
59+
while ('*' != css[i] && '/' != css[i + 1]) ++i;
60+
i += 2;
61+
css = css.slice(i);
62+
whitespace();
63+
return true;
64+
}
65+
}
66+
3467
/**
3568
* Parse selector.
3669
*/
@@ -71,12 +104,23 @@ module.exports = function(css){
71104

72105
function rule() {
73106
var node = { selector: selector(), declarations: [] };
107+
108+
// selector
74109
if (!node.selector) return;
110+
comments();
111+
112+
// {
75113
if (!match(/^{\s+/)) return;
114+
comments();
115+
116+
// declarations
76117
var decl;
77118
while (decl = declaration()) {
78119
node.declarations.push(decl);
120+
comments();
79121
}
122+
123+
// }
80124
if (!match(/^}\s*/)) return;
81125
return node;
82126
}

npm-debug.log

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
0 info it worked if it ends with ok
2+
1 verbose cli [ '/usr/local/n/versions/0.8.2/bin/node',
3+
1 verbose cli '/usr/local/bin/npm',
4+
1 verbose cli 'info',
5+
1 verbose cli 'css',
6+
1 verbose cli 'version' ]
7+
2 info using npm@1.1.33
8+
3 info using node@v0.8.2
9+
4 verbose config file /Users/tj/.npmrc
10+
5 verbose config file /usr/local/n/versions/0.8.2/etc/npmrc
11+
6 verbose config file /usr/local/lib/node_modules/npm/npmrc
12+
7 verbose url raw css
13+
8 verbose url resolving [ 'https://registry.npmjs.org/', './css' ]
14+
9 verbose url resolved https://registry.npmjs.org/css
15+
10 info retry registry request attempt 1 at 16:21:45
16+
11 http GET https://registry.npmjs.org/css
17+
12 http 404 https://registry.npmjs.org/css
18+
13 silly registry.get cb [ 404,
19+
13 silly registry.get { server: 'CouchDB/1.2.0 (Erlang OTP/R15B)',
20+
13 silly registry.get date: 'Wed, 18 Jul 2012 23:21:46 GMT',
21+
13 silly registry.get 'content-type': 'application/json',
22+
13 silly registry.get 'content-length': '52',
23+
13 silly registry.get 'cache-control': 'must-revalidate' } ]
24+
14 error 404 'css' is not in the npm registry.
25+
14 error 404 You should bug the author to publish it
26+
14 error 404
27+
14 error 404 Note that you can also install from a
28+
14 error 404 tarball, folder, or http url, or git url.
29+
15 error System Darwin 11.3.0
30+
16 error command "/usr/local/n/versions/0.8.2/bin/node" "/usr/local/bin/npm" "info" "css" "version"
31+
17 error cwd /Users/tj/projects/css-parse
32+
18 error node -v v0.8.2
33+
19 error npm -v 1.1.33
34+
20 error code E404
35+
21 error message 404 Not Found: css
36+
22 verbose exit [ 1, true ]

test/cases/comment.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/* foo */
2+
/* bar */
3+
/* baz
4+
5+
asdfasdfasdf
6+
asdfasdfasdf
7+
asdfasdfasdf
8+
asdfasdfasdf
9+
10+
*/
11+
12+
foo { /* hey */
13+
/**/ bar: baz; /* hey */ /* hey */
14+
}

test/cases/comment.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"stylesheet": {
3+
"rules": [
4+
{
5+
"selector": "foo",
6+
"declarations": [
7+
{
8+
"property": "bar",
9+
"value": "baz"
10+
}
11+
]
12+
}
13+
]
14+
}
15+
}

0 commit comments

Comments
 (0)