diff --git a/Readme.md b/Readme.md index 1c05b32..b36ceaf 100644 --- a/Readme.md +++ b/Readme.md @@ -3,6 +3,18 @@ JavaScript CSS parser for nodejs and the browser. +## Installation + +``` +$ npm install css-parse +$ component install visionmedia/css-parse +``` + +## Options + + - `position` add line number and column position data to nodes + - `comments` add comments array to the output + ## Example css: diff --git a/index.js b/index.js index d922f9b..1520243 100644 --- a/index.js +++ b/index.js @@ -2,6 +2,16 @@ module.exports = function(css, options){ options = options || {}; + /** + * Root node. + */ + + var root = { + type: 'stylesheet', + stylesheet: {}, + comments: [] + }; + /** * Positional. */ @@ -49,19 +59,6 @@ module.exports = function(css, options){ return node; } - /** - * Parse stylesheet. - */ - - function stylesheet() { - return { - type: 'stylesheet', - stylesheet: { - rules: rules() - } - }; - } - /** * Opening brace. */ @@ -119,11 +116,13 @@ module.exports = function(css, options){ * Parse comments; */ - function comments(rules) { + function comments() { var c; - rules = rules || []; - while (c = comment()) rules.push(c); - return rules; + while (c = comment()) { + if (options.comments) { + root.comments.push(c); + } + } } /** @@ -198,13 +197,13 @@ module.exports = function(css, options){ var decls = []; if (!open()) return; - comments(decls); + comments(); // declarations var decl; while (decl = declaration()) { decls.push(decl); - comments(decls); + comments(); } if (!close()) return; @@ -448,6 +447,8 @@ module.exports = function(css, options){ }); } - return stylesheet(); + root.stylesheet.rules = rules(); + + return root; }; diff --git a/test/cases/charset.json b/test/cases/charset.json index f7d72ab..c704f94 100644 --- a/test/cases/charset.json +++ b/test/cases/charset.json @@ -16,20 +16,6 @@ } } }, - { - "type": "comment", - "comment": " Set the encoding of the style sheet to Unicode UTF-8", - "position": { - "start": { - "line": 1, - "column": 25 - }, - "end": { - "line": 1, - "column": 82 - } - } - }, { "type": "charset", "charset": "'iso-8859-15'", @@ -43,21 +29,37 @@ "column": 24 } } - }, - { - "type": "comment", - "comment": " Set the encoding of the style sheet to Latin-9 (Western European languages, with euro sign) ", - "position": { - "start": { - "line": 2, - "column": 25 - }, - "end": { - "line": 2, - "column": 122 - } - } } ] - } + }, + "comments": [ + { + "type": "comment", + "comment": " Set the encoding of the style sheet to Unicode UTF-8", + "position": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 82 + } + } + }, + { + "type": "comment", + "comment": " Set the encoding of the style sheet to Latin-9 (Western European languages, with euro sign) ", + "position": { + "start": { + "line": 2, + "column": 25 + }, + "end": { + "line": 2, + "column": 122 + } + } + } + ] } \ No newline at end of file diff --git a/test/cases/comment.json b/test/cases/comment.json index 5459552..c9670bf 100644 --- a/test/cases/comment.json +++ b/test/cases/comment.json @@ -2,68 +2,12 @@ "type": "stylesheet", "stylesheet": { "rules": [ - { - "type": "comment", - "comment": " 1 ", - "position": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 8 - } - } - }, { "type": "rule", "selectors": [ "body" ], "declarations": [ - { - "type": "comment", - "comment": " 2 ", - "position": { - "start": { - "line": 3, - "column": 8 - }, - "end": { - "line": 3, - "column": 15 - } - } - }, - { - "type": "comment", - "comment": " 3 ", - "position": { - "start": { - "line": 4, - "column": 3 - }, - "end": { - "line": 4, - "column": 10 - } - } - }, - { - "type": "comment", - "comment": "", - "position": { - "start": { - "line": 5, - "column": 3 - }, - "end": { - "line": 5, - "column": 7 - } - } - }, { "type": "declaration", "property": "foo", @@ -78,20 +22,6 @@ "column": 17 } } - }, - { - "type": "comment", - "comment": " 4 ", - "position": { - "start": { - "line": 6, - "column": 3 - }, - "end": { - "line": 6, - "column": 10 - } - } } ], "position": { @@ -104,35 +34,107 @@ "column": 2 } } - }, - { - "type": "comment", - "comment": " 5 ", - "position": { - "start": { - "line": 7, - "column": 3 - }, - "end": { - "line": 7, - "column": 10 - } + } + ] + }, + "comments": [ + { + "type": "comment", + "comment": " 1 ", + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 8 } - }, - { - "type": "comment", - "comment": " 6 ", - "position": { - "start": { - "line": 9, - "column": 1 - }, - "end": { - "line": 9, - "column": 8 - } + } + }, + { + "type": "comment", + "comment": " 2 ", + "position": { + "start": { + "line": 3, + "column": 8 + }, + "end": { + "line": 3, + "column": 15 } } - ] - } + }, + { + "type": "comment", + "comment": " 3 ", + "position": { + "start": { + "line": 4, + "column": 3 + }, + "end": { + "line": 4, + "column": 10 + } + } + }, + { + "type": "comment", + "comment": "", + "position": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 7 + } + } + }, + { + "type": "comment", + "comment": " 4 ", + "position": { + "start": { + "line": 6, + "column": 3 + }, + "end": { + "line": 6, + "column": 10 + } + } + }, + { + "type": "comment", + "comment": " 5 ", + "position": { + "start": { + "line": 7, + "column": 3 + }, + "end": { + "line": 7, + "column": 10 + } + } + }, + { + "type": "comment", + "comment": " 6 ", + "position": { + "start": { + "line": 9, + "column": 1 + }, + "end": { + "line": 9, + "column": 8 + } + } + } + ] } \ No newline at end of file diff --git a/test/cases/comment.url.json b/test/cases/comment.url.json index 12c6f46..f63c943 100644 --- a/test/cases/comment.url.json +++ b/test/cases/comment.url.json @@ -2,68 +2,12 @@ "type": "stylesheet", "stylesheet": { "rules": [ - { - "type": "comment", - "comment": " http://foo.com/bar/baz.html ", - "position": { - "start": { - "line": 1, - "column": 1 - }, - "end": { - "line": 1, - "column": 34 - } - } - }, - { - "type": "comment", - "comment": "", - "position": { - "start": { - "line": 2, - "column": 1 - }, - "end": { - "line": 2, - "column": 5 - } - } - }, { "type": "rule", "selectors": [ "foo" ], "declarations": [ - { - "type": "comment", - "comment": "/", - "position": { - "start": { - "line": 4, - "column": 7 - }, - "end": { - "line": 4, - "column": 12 - } - } - }, - { - "type": "comment", - "comment": " something ", - "position": { - "start": { - "line": 5, - "column": 3 - }, - "end": { - "line": 5, - "column": 18 - } - } - }, { "type": "declaration", "property": "bar", @@ -78,20 +22,6 @@ "column": 11 } } - }, - { - "type": "comment", - "comment": " http://foo.com/bar/baz.html ", - "position": { - "start": { - "line": 6, - "column": 13 - }, - "end": { - "line": 6, - "column": 46 - } - } } ], "position": { @@ -106,5 +36,77 @@ } } ] - } + }, + "comments": [ + { + "type": "comment", + "comment": " http://foo.com/bar/baz.html ", + "position": { + "start": { + "line": 1, + "column": 1 + }, + "end": { + "line": 1, + "column": 34 + } + } + }, + { + "type": "comment", + "comment": "", + "position": { + "start": { + "line": 2, + "column": 1 + }, + "end": { + "line": 2, + "column": 5 + } + } + }, + { + "type": "comment", + "comment": "/", + "position": { + "start": { + "line": 4, + "column": 7 + }, + "end": { + "line": 4, + "column": 12 + } + } + }, + { + "type": "comment", + "comment": " something ", + "position": { + "start": { + "line": 5, + "column": 3 + }, + "end": { + "line": 5, + "column": 18 + } + } + }, + { + "type": "comment", + "comment": " http://foo.com/bar/baz.html ", + "position": { + "start": { + "line": 6, + "column": 13 + }, + "end": { + "line": 6, + "column": 46 + } + } + } + ] } \ No newline at end of file diff --git a/test/cases/document.json b/test/cases/document.json index f2b4db7..0ef7962 100644 --- a/test/cases/document.json +++ b/test/cases/document.json @@ -53,5 +53,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/empty.json b/test/cases/empty.json index 9a68693..3b9e5f8 100644 --- a/test/cases/empty.json +++ b/test/cases/empty.json @@ -2,5 +2,6 @@ "type": "stylesheet", "stylesheet": { "rules": [] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/import.json b/test/cases/import.json index eb520b6..a0f3822 100644 --- a/test/cases/import.json +++ b/test/cases/import.json @@ -73,5 +73,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/import.messed.json b/test/cases/import.messed.json index 5c8a07c..c8a0093 100644 --- a/test/cases/import.messed.json +++ b/test/cases/import.messed.json @@ -73,5 +73,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/invalid.json b/test/cases/invalid.json index 9a68693..3b9e5f8 100644 --- a/test/cases/invalid.json +++ b/test/cases/invalid.json @@ -2,5 +2,6 @@ "type": "stylesheet", "stylesheet": { "rules": [] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/keyframes.complex.json b/test/cases/keyframes.complex.json index b6035ca..5d489e1 100644 --- a/test/cases/keyframes.complex.json +++ b/test/cases/keyframes.complex.json @@ -183,5 +183,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/keyframes.json b/test/cases/keyframes.json index d507055..cd1fc33 100644 --- a/test/cases/keyframes.json +++ b/test/cases/keyframes.json @@ -85,5 +85,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/keyframes.messed.json b/test/cases/keyframes.messed.json index b322137..0061682 100644 --- a/test/cases/keyframes.messed.json +++ b/test/cases/keyframes.messed.json @@ -85,5 +85,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/keyframes.vendor.json b/test/cases/keyframes.vendor.json index 66a0783..df52ff4 100644 --- a/test/cases/keyframes.vendor.json +++ b/test/cases/keyframes.vendor.json @@ -86,5 +86,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/media.json b/test/cases/media.json index c31bf36..a7292b0 100644 --- a/test/cases/media.json +++ b/test/cases/media.json @@ -227,5 +227,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/media.messed.json b/test/cases/media.messed.json index 3deaeb2..097274e 100644 --- a/test/cases/media.messed.json +++ b/test/cases/media.messed.json @@ -227,5 +227,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/messed-up.json b/test/cases/messed-up.json index 53f7079..e32fe42 100644 --- a/test/cases/messed-up.json +++ b/test/cases/messed-up.json @@ -132,5 +132,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/namespace.json b/test/cases/namespace.json index 49565b1..469962c 100644 --- a/test/cases/namespace.json +++ b/test/cases/namespace.json @@ -31,5 +31,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/no-semi.json b/test/cases/no-semi.json index dd7c80f..66e45cf 100644 --- a/test/cases/no-semi.json +++ b/test/cases/no-semi.json @@ -51,5 +51,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/paged-media.json b/test/cases/paged-media.json index baa70c6..34f0c33 100644 --- a/test/cases/paged-media.json +++ b/test/cases/paged-media.json @@ -68,5 +68,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/props.json b/test/cases/props.json index 0c355b5..3ecf89a 100644 --- a/test/cases/props.json +++ b/test/cases/props.json @@ -66,5 +66,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/quoted.json b/test/cases/quoted.json index 770b858..9e5958e 100644 --- a/test/cases/quoted.json +++ b/test/cases/quoted.json @@ -36,5 +36,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/rule.json b/test/cases/rule.json index 9892baf..5ab85ea 100644 --- a/test/cases/rule.json +++ b/test/cases/rule.json @@ -36,5 +36,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/rules.json b/test/cases/rules.json index a5d651f..aaae2aa 100644 --- a/test/cases/rules.json +++ b/test/cases/rules.json @@ -69,5 +69,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/cases/supports.json b/test/cases/supports.json index 8326299..5e7a874 100644 --- a/test/cases/supports.json +++ b/test/cases/supports.json @@ -67,5 +67,6 @@ } } ] - } + }, + "comments": [] } \ No newline at end of file diff --git a/test/css-parse.js b/test/css-parse.js index c6e6127..715be10 100644 --- a/test/css-parse.js +++ b/test/css-parse.js @@ -16,7 +16,7 @@ describe('parse(str)', function(){ it('should parse ' + file, function(){ var css = read(path.join('test', 'cases', file + '.css'), 'utf8'); var json = read(path.join('test', 'cases', file + '.json'), 'utf8'); - var ret = parse(css, { position: true }); + var ret = parse(css, { position: true, comments: true }); ret = JSON.stringify(ret, null, 2); ret.should.equal(json); })