Skip to content

Commit 8f6c1e4

Browse files
committed
add .type to all nodes. Closes #18
1 parent 1fba7fb commit 8f6c1e4

26 files changed

+177
-60
lines changed

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
test:
33
@./node_modules/.bin/mocha \
44
--require should \
5-
--reporter spec
5+
--reporter spec \
6+
--bail
67

7-
.PHONY: test
8+
.PHONY: test

index.js

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ module.exports = function(css){
66
*/
77

88
function stylesheet() {
9-
return { stylesheet: { rules: rules() }};
9+
return {
10+
type: 'stylesheet',
11+
stylesheet: {
12+
rules: rules()
13+
}
14+
};
1015
}
1116

1217
/**
@@ -86,7 +91,10 @@ module.exports = function(css){
8691
css = css.slice(i);
8792
whitespace();
8893

89-
return { comment: str };
94+
return {
95+
type: 'comment',
96+
comment: str
97+
};
9098
}
9199

92100
/**
@@ -120,7 +128,11 @@ module.exports = function(css){
120128
// ;
121129
match(/^[;\s]*/);
122130

123-
return { property: prop, value: val };
131+
return {
132+
type: 'declaration',
133+
property: prop,
134+
value: val
135+
};
124136
}
125137

126138
/**
@@ -139,6 +151,7 @@ module.exports = function(css){
139151
if (!vals.length) return;
140152

141153
return {
154+
type: 'keyframe',
142155
values: vals,
143156
declarations: declarations()
144157
};
@@ -171,6 +184,7 @@ module.exports = function(css){
171184
if (!close()) return;
172185

173186
return {
187+
type: 'keyframes',
174188
name: name,
175189
vendor: vendor,
176190
keyframes: frames
@@ -193,7 +207,11 @@ module.exports = function(css){
193207

194208
if (!close()) return;
195209

196-
return { supports: supports, rules: style };
210+
return {
211+
type: 'supports',
212+
supports: supports,
213+
rules: style
214+
};
197215
}
198216

199217
/**
@@ -212,7 +230,11 @@ module.exports = function(css){
212230

213231
if (!close()) return;
214232

215-
return { media: media, rules: style };
233+
return {
234+
type: 'media',
235+
media: media,
236+
rules: style
237+
};
216238
}
217239

218240
/**
@@ -231,15 +253,15 @@ module.exports = function(css){
231253

232254
// declarations
233255
var decl;
234-
while (decl = declaration() || atmargin()) {
256+
while (decl = declaration()) {
235257
decls.push(decl);
236258
comments();
237259
}
238260

239261
if (!close()) return;
240262

241263
return {
242-
type: "page",
264+
type: 'page',
243265
selectors: sel,
244266
declarations: decls
245267
};
@@ -263,33 +285,19 @@ module.exports = function(css){
263285
if (!close()) return;
264286

265287
return {
288+
type: 'document',
266289
document: doc,
267290
vendor: vendor,
268291
rules: style
269292
};
270293
}
271294

272-
/**
273-
* Parse margin at-rules
274-
*/
275-
276-
function atmargin() {
277-
var m = match(/^@([a-z\-]+) */);
278-
if (!m) return;
279-
var type = m[1]
280-
281-
return {
282-
type: type,
283-
declarations: declarations()
284-
}
285-
}
286-
287295
/**
288296
* Parse import
289297
*/
290298

291299
function atimport() {
292-
return _atrule('import')
300+
return _atrule('import');
293301
}
294302

295303
/**
@@ -315,7 +323,7 @@ module.exports = function(css){
315323
function _atrule(name) {
316324
var m = match(new RegExp('^@' + name + ' *([^;\\n]+);\\s*'));
317325
if (!m) return;
318-
var ret = {}
326+
var ret = { type: name };
319327
ret[name] = m[1].trim();
320328
return ret;
321329
}
@@ -364,7 +372,11 @@ module.exports = function(css){
364372
var sel = selector();
365373
if (!sel) return;
366374
comments();
367-
return { selectors: sel, declarations: declarations() };
375+
return {
376+
type: 'rule',
377+
selectors: sel,
378+
declarations: declarations()
379+
};
368380
}
369381

370382
return stylesheet();

test/cases/charset.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
{
2+
"type": "stylesheet",
23
"stylesheet": {
34
"rules": [
45
{
6+
"type": "charset",
57
"charset": "\"UTF-8\""
68
},
79
{
10+
"type": "comment",
811
"comment": " Set the encoding of the style sheet to Unicode UTF-8"
912
},
1013
{
14+
"type": "charset",
1115
"charset": "'iso-8859-15'"
1216
},
1317
{
18+
"type": "comment",
1419
"comment": " Set the encoding of the style sheet to Latin-9 (Western European languages, with euro sign) "
1520
}
1621
]

test/cases/comment.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
{
2+
"type": "stylesheet",
23
"stylesheet": {
34
"rules": [
45
{
6+
"type": "comment",
57
"comment": " foo "
68
},
79
{
10+
"type": "comment",
811
"comment": " bar "
912
},
1013
{
14+
"type": "comment",
1115
"comment": " baz\n\nasdfasdfasdf\nasdfasdfasdf\nasdfasdfasdf\nasdfasdfasdf\n\n"
1216
},
1317
{
18+
"type": "rule",
1419
"selectors": [
1520
"foo"
1621
],
1722
"declarations": [
1823
{
24+
"type": "declaration",
1925
"property": "bar",
2026
"value": "baz"
2127
}

test/cases/comment.url.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,28 @@
11
{
2+
"type": "stylesheet",
23
"stylesheet": {
34
"rules": [
45
{
6+
"type": "comment",
57
"comment": " http://foo.com/bar/baz.html "
68
},
79
{
10+
"type": "comment",
811
"comment": ""
912
},
1013
{
14+
"type": "rule",
1115
"selectors": [
1216
"foo"
1317
],
1418
"declarations": [
1519
{
20+
"type": "declaration",
1621
"property": "bar",
1722
"value": "baz"
1823
}
1924
]
2025
}
2126
]
2227
}
23-
}
28+
}

test/cases/document.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
{
2+
"type": "stylesheet",
23
"stylesheet": {
34
"rules": [
45
{
6+
"type": "document",
57
"document": "url-prefix()",
68
"vendor": "-moz-",
79
"rules": [
810
{
11+
"type": "rule",
912
"selectors": [
1013
".ui-select .ui-btn select"
1114
],
1215
"declarations": [
1316
{
17+
"type": "declaration",
1418
"property": "opacity",
1519
"value": ".0001"
1620
}
@@ -20,4 +24,4 @@
2024
}
2125
]
2226
}
23-
}
27+
}

test/cases/empty.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"type": "stylesheet",
23
"stylesheet": {
34
"rules": []
45
}

test/cases/import.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
{
2+
"type": "stylesheet",
23
"stylesheet": {
34
"rules": [
45
{
6+
"type": "import",
57
"import": "url(\"fineprint.css\") print"
68
},
79
{
10+
"type": "import",
811
"import": "url(\"bluish.css\") projection, tv"
912
},
1013
{
14+
"type": "import",
1115
"import": "'custom.css'"
1216
},
1317
{
18+
"type": "import",
1419
"import": "\"common.css\" screen, projection"
1520
},
1621
{
22+
"type": "import",
1723
"import": "url('landscape.css') screen and (orientation:landscape)"
1824
}
1925
]

test/cases/import.messed.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
{
2+
"type": "stylesheet",
23
"stylesheet": {
34
"rules": [
45
{
6+
"type": "import",
57
"import": "url(\"fineprint.css\") print"
68
},
79
{
10+
"type": "import",
811
"import": "url(\"bluish.css\") projection, tv"
912
},
1013
{
14+
"type": "import",
1115
"import": "'custom.css'"
1216
},
1317
{
18+
"type": "import",
1419
"import": "\"common.css\" screen, projection"
1520
},
1621
{
22+
"type": "import",
1723
"import": "url('landscape.css') screen and (orientation:landscape)"
1824
}
1925
]

test/cases/invalid.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"type": "stylesheet",
23
"stylesheet": {
34
"rules": []
45
}

0 commit comments

Comments
 (0)