From 36daf50fada3f236f9c822e204b15923a82e424c Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Sun, 2 Jun 2013 11:53:43 -0700 Subject: [PATCH] add support for regular css multi-line comments. Closes #12 --- examples/nesting.css | 12 +++++++++++- lib/compiler.js | 10 ++++++++++ lib/lexer.js | 17 +++++++++++++++++ lib/parser.js | 1 + test/cases/comments.css | 15 +++++++++++---- test/cases/comments.out.css | 8 +++++++- 6 files changed, 57 insertions(+), 6 deletions(-) diff --git a/examples/nesting.css b/examples/nesting.css index 1eadcb2..500805a 100644 --- a/examples/nesting.css +++ b/examples/nesting.css @@ -1,8 +1,18 @@ +/* + +regular multi-line comment + +*/ + +/* regular comment */ + body background: #888 color: #eee +// stripped comment + ul margin: 0 li @@ -16,4 +26,4 @@ ul ul width: 50px li - list-style: disc \ No newline at end of file + list-style: disc diff --git a/lib/compiler.js b/lib/compiler.js index 47f7098..78cca57 100644 --- a/lib/compiler.js +++ b/lib/compiler.js @@ -47,6 +47,8 @@ module.exports = function(node){ return ret; case 'prop': return prop(node); + case 'comment': + return comment(node); default: throw new Error('invalid node "' + node[0] + '"'); } @@ -67,6 +69,14 @@ module.exports = function(node){ return buf.join(''); } + /** + * Visit comment. + */ + + function comment(node) { + return indent() + '/*' + node[1] + '*/\n'; + } + /** * Visit prop. */ diff --git a/lib/lexer.js b/lib/lexer.js index e692255..2ac3795 100644 --- a/lib/lexer.js +++ b/lib/lexer.js @@ -93,6 +93,7 @@ module.exports = function(str) { function next() { return stashed() || comment() + || csscomment() || indentation() || prop() || rule(); @@ -117,6 +118,22 @@ module.exports = function(str) { return next(); } + /** + * Multiline comment. + */ + + function csscomment() { + if ('/' != str[0] || '*' != str[1]) return; + str = str.slice(2); + + var i = 0; + while ('*' != str[i] && '/' != str[i + 1]) ++i; + + var buf = str.slice(0, i); + str = str.slice(buf.length + 2); + + return ['comment', buf]; + } /** * INDENT diff --git a/lib/parser.js b/lib/parser.js index fb2ea36..dbe325f 100644 --- a/lib/parser.js +++ b/lib/parser.js @@ -78,6 +78,7 @@ module.exports = function(str) { function stmt() { if (is('rule')) return rule(); if (is('prop')) return prop(); + return next(); } /** diff --git a/test/cases/comments.css b/test/cases/comments.css index be57418..a814f3b 100644 --- a/test/cases/comments.css +++ b/test/cases/comments.css @@ -1,11 +1,18 @@ +/* + +this is a button + +*/ + // foo button // bar color: #eee // bar - - - + + + // baz - background: blue \ No newline at end of file + /* css style */ + background: blue diff --git a/test/cases/comments.out.css b/test/cases/comments.out.css index 9254db0..4104216 100644 --- a/test/cases/comments.out.css +++ b/test/cases/comments.out.css @@ -1,4 +1,10 @@ +/* +this is a button +*/ + + button { color: #eee; + /* css style */ background: blue; -} \ No newline at end of file +}