Skip to content

Commit

Permalink
add .type to all nodes. Closes #18
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed May 21, 2013
1 parent 1fba7fb commit 8f6c1e4
Show file tree
Hide file tree
Showing 26 changed files with 177 additions and 60 deletions.
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
test:
@./node_modules/.bin/mocha \
--require should \
--reporter spec
--reporter spec \
--bail

.PHONY: test
.PHONY: test
62 changes: 37 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ module.exports = function(css){
*/

function stylesheet() {
return { stylesheet: { rules: rules() }};
return {
type: 'stylesheet',
stylesheet: {
rules: rules()
}
};
}

/**
Expand Down Expand Up @@ -86,7 +91,10 @@ module.exports = function(css){
css = css.slice(i);
whitespace();

return { comment: str };
return {
type: 'comment',
comment: str
};
}

/**
Expand Down Expand Up @@ -120,7 +128,11 @@ module.exports = function(css){
// ;
match(/^[;\s]*/);

return { property: prop, value: val };
return {
type: 'declaration',
property: prop,
value: val
};
}

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

return {
type: 'keyframe',
values: vals,
declarations: declarations()
};
Expand Down Expand Up @@ -171,6 +184,7 @@ module.exports = function(css){
if (!close()) return;

return {
type: 'keyframes',
name: name,
vendor: vendor,
keyframes: frames
Expand All @@ -193,7 +207,11 @@ module.exports = function(css){

if (!close()) return;

return { supports: supports, rules: style };
return {
type: 'supports',
supports: supports,
rules: style
};
}

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

if (!close()) return;

return { media: media, rules: style };
return {
type: 'media',
media: media,
rules: style
};
}

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

// declarations
var decl;
while (decl = declaration() || atmargin()) {
while (decl = declaration()) {
decls.push(decl);
comments();
}

if (!close()) return;

return {
type: "page",
type: 'page',
selectors: sel,
declarations: decls
};
Expand All @@ -263,33 +285,19 @@ module.exports = function(css){
if (!close()) return;

return {
type: 'document',
document: doc,
vendor: vendor,
rules: style
};
}

/**
* Parse margin at-rules

This comment has been minimized.

Copy link
@necolas

necolas May 21, 2014

Contributor

do you remember why support for margin at-rules was removed in this commit?

This comment has been minimized.

Copy link
@abernier

abernier May 26, 2014

could solve https://github.com/reworkcss/css-parse/issues/80 if re-introducing it

*/

function atmargin() {
var m = match(/^@([a-z\-]+) */);
if (!m) return;
var type = m[1]

return {
type: type,
declarations: declarations()
}
}

/**
* Parse import
*/

function atimport() {
return _atrule('import')
return _atrule('import');
}

/**
Expand All @@ -315,7 +323,7 @@ module.exports = function(css){
function _atrule(name) {
var m = match(new RegExp('^@' + name + ' *([^;\\n]+);\\s*'));
if (!m) return;
var ret = {}
var ret = { type: name };
ret[name] = m[1].trim();
return ret;
}
Expand Down Expand Up @@ -364,7 +372,11 @@ module.exports = function(css){
var sel = selector();
if (!sel) return;
comments();
return { selectors: sel, declarations: declarations() };
return {
type: 'rule',
selectors: sel,
declarations: declarations()
};
}

return stylesheet();
Expand Down
5 changes: 5 additions & 0 deletions test/cases/charset.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "charset",
"charset": "\"UTF-8\""
},
{
"type": "comment",
"comment": " Set the encoding of the style sheet to Unicode UTF-8"
},
{
"type": "charset",
"charset": "'iso-8859-15'"
},
{
"type": "comment",
"comment": " Set the encoding of the style sheet to Latin-9 (Western European languages, with euro sign) "
}
]
Expand Down
6 changes: 6 additions & 0 deletions test/cases/comment.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "comment",
"comment": " foo "
},
{
"type": "comment",
"comment": " bar "
},
{
"type": "comment",
"comment": " baz\n\nasdfasdfasdf\nasdfasdfasdf\nasdfasdfasdf\nasdfasdfasdf\n\n"
},
{
"type": "rule",
"selectors": [
"foo"
],
"declarations": [
{
"type": "declaration",
"property": "bar",
"value": "baz"
}
Expand Down
7 changes: 6 additions & 1 deletion test/cases/comment.url.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "comment",
"comment": " http://foo.com/bar/baz.html "
},
{
"type": "comment",
"comment": ""
},
{
"type": "rule",
"selectors": [
"foo"
],
"declarations": [
{
"type": "declaration",
"property": "bar",
"value": "baz"
}
]
}
]
}
}
}
6 changes: 5 additions & 1 deletion test/cases/document.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "document",
"document": "url-prefix()",
"vendor": "-moz-",
"rules": [
{
"type": "rule",
"selectors": [
".ui-select .ui-btn select"
],
"declarations": [
{
"type": "declaration",
"property": "opacity",
"value": ".0001"
}
Expand All @@ -20,4 +24,4 @@
}
]
}
}
}
1 change: 1 addition & 0 deletions test/cases/empty.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": []
}
Expand Down
6 changes: 6 additions & 0 deletions test/cases/import.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "import",
"import": "url(\"fineprint.css\") print"
},
{
"type": "import",
"import": "url(\"bluish.css\") projection, tv"
},
{
"type": "import",
"import": "'custom.css'"
},
{
"type": "import",
"import": "\"common.css\" screen, projection"
},
{
"type": "import",
"import": "url('landscape.css') screen and (orientation:landscape)"
}
]
Expand Down
6 changes: 6 additions & 0 deletions test/cases/import.messed.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": [
{
"type": "import",
"import": "url(\"fineprint.css\") print"
},
{
"type": "import",
"import": "url(\"bluish.css\") projection, tv"
},
{
"type": "import",
"import": "'custom.css'"
},
{
"type": "import",
"import": "\"common.css\" screen, projection"
},
{
"type": "import",
"import": "url('landscape.css') screen and (orientation:landscape)"
}
]
Expand Down
1 change: 1 addition & 0 deletions test/cases/invalid.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"type": "stylesheet",
"stylesheet": {
"rules": []
}
Expand Down

0 comments on commit 8f6c1e4

Please sign in to comment.