From 901bb8636dc1940b0578978b72903a46d4af5adb Mon Sep 17 00:00:00 2001 From: austin chau Date: Sat, 8 Mar 2014 22:07:49 -0800 Subject: [PATCH 1/5] handle IE-7 style, declaration name starts with * and . --- index.js | 86 ++++++++++++++++++++++---------------------------------- 1 file changed, 34 insertions(+), 52 deletions(-) diff --git a/index.js b/index.js index a57f156..036d327 100644 --- a/index.js +++ b/index.js @@ -1,10 +1,6 @@ -// http://www.w3.org/TR/CSS21/grammar.html -// https://github.com/visionmedia/css-parse/pull/49#issuecomment-30088027 -var commentre = /\/\*[^*]*\*+([^/*][^*]*\*+)*\//g module.exports = function(css, options){ options = options || {}; - options.position = options.position === false ? false : true; /** * Positional. @@ -33,28 +29,17 @@ module.exports = function(css, options){ if (!options.position) return positionNoop; return function(node){ - node.position = new Position(start); + node.position = { + start: start, + end: { line: lineno, column: column }, + source: options.source + }; + whitespace(); return node; - }; - } - - /** - * Store position information for a node - */ - - function Position(start) { - this.start = start; - this.end = { line: lineno, column: column }; - this.source = options.source; + } } - /** - * Non-enumerable source string - */ - - Position.prototype.content = css; - /** * Return `node`. */ @@ -75,6 +60,7 @@ module.exports = function(css, options){ err.column = column; err.source = css; throw err; + // console.log(err); } /** @@ -115,7 +101,7 @@ module.exports = function(css, options){ var rules = []; whitespace(); comments(rules); - while (css.length && css.charAt(0) != '}' && (node = atrule() || rule())) { + while (css.charAt(0) != '}' && (node = atrule() || rule())) { rules.push(node); comments(rules); } @@ -163,13 +149,9 @@ module.exports = function(css, options){ if ('/' != css.charAt(0) || '*' != css.charAt(1)) return; var i = 2; - while ("" != css.charAt(i) && ('*' != css.charAt(i) || '/' != css.charAt(i + 1))) ++i; + while (null != css.charAt(i) && ('*' != css.charAt(i) || '/' != css.charAt(i + 1))) ++i; i += 2; - if ("" === css.charAt(i-1)) { - return error('End of comment missing'); - } - var str = css.slice(2, i - 2); column += 2; updatePosition(str); @@ -189,9 +171,7 @@ module.exports = function(css, options){ function selector() { var m = match(/^([^{]+)/); if (!m) return; - /* @fix Remove all comments from selectors - * http://ostermiller.org/findcomment.html */ - return trim(m[0]).replace(/\/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*\/+/g, '').split(/\s*,\s*/); + return trim(m[0]).split(/\s*,\s*/); } /** @@ -201,12 +181,12 @@ module.exports = function(css, options){ function declaration() { var pos = position(); - // prop - var prop = match(/^(\*?[-#\/\*\w]+(\[[0-9a-z_-]+\])?)\s*/); + // prop /* AUSTIN HACK */ + // var prop = match(/^(\*?\.*[-#\/\*\w]+(\[[0-9a-z_-]+\])?)\s*/); + var prop = match(/^(\*?[-\.#\/\* \w]+(\[[0-9a-z_-]+\])?)\s*/); if (!prop) return; prop = trim(prop[0]); - - // : + // : /* AUSTIN HACK! */ if (!match(/^:\s*/)) return error("property missing ':'"); // val @@ -215,8 +195,8 @@ module.exports = function(css, options){ var ret = pos({ type: 'declaration', - property: prop.replace(commentre, ''), - value: trim(val[0]).replace(commentre, '') + property: prop, + value: trim(val[0]) }); // ; @@ -434,35 +414,37 @@ module.exports = function(css, options){ * Parse import */ - var atimport = _compileAtrule('import'); + function atimport() { + return _atrule('import'); + } /** * Parse charset */ - var atcharset = _compileAtrule('charset'); + function atcharset() { + return _atrule('charset'); + } /** * Parse namespace */ - var atnamespace = _compileAtrule('namespace'); + function atnamespace() { + return _atrule('namespace') + } /** * Parse non-block at-rules */ - - function _compileAtrule(name) { - var re = new RegExp('^@' + name + ' *([^;\\n]+);'); - return function() { - var pos = position(); - var m = match(re); - if (!m) return; - var ret = { type: name }; - ret[name] = m[1].trim(); - return pos(ret); - } + function _atrule(name) { + var pos = position(); + var m = match(new RegExp('^@' + name + ' *([^;\\n]+);')); + if (!m) return; + var ret = { type: name }; + ret[name] = trim(m[1]); + return pos(ret); } /** @@ -491,7 +473,7 @@ module.exports = function(css, options){ var pos = position(); var sel = selector(); - if (!sel) return error('selector missing'); + if (!sel) return; comments(); return pos({ From 4c6e2f54ee71c2a91fbaea8ee3f7b662ed1fe0e6 Mon Sep 17 00:00:00 2001 From: austin chau Date: Tue, 11 Mar 2014 14:39:55 -0700 Subject: [PATCH 2/5] allow rule to have empty empty (more forgiving) --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 036d327..88ea03d 100644 --- a/index.js +++ b/index.js @@ -191,7 +191,8 @@ module.exports = function(css, options){ // val var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/); - if (!val) return error('property missing value'); + // if (!val) return error('property missing value'); + if (!val) val = ''; var ret = pos({ type: 'declaration', From 0e52e630540887b3cd8a30ed7a27013e15f56263 Mon Sep 17 00:00:00 2001 From: austin chau Date: Wed, 12 Mar 2014 01:43:52 -0700 Subject: [PATCH 3/5] . --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 88ea03d..b83f61c 100644 --- a/index.js +++ b/index.js @@ -187,7 +187,7 @@ module.exports = function(css, options){ if (!prop) return; prop = trim(prop[0]); // : /* AUSTIN HACK! */ - if (!match(/^:\s*/)) return error("property missing ':'"); + // if (!match(/^:\s*/)) return error("property missing ':'"); // val var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/); From e3603a6f5dbc753f960d6af39fbd9336f31a85e9 Mon Sep 17 00:00:00 2001 From: austin chau Date: Wed, 12 Mar 2014 01:50:39 -0700 Subject: [PATCH 4/5] handle declaration with key but no value --- index.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/index.js b/index.js index b83f61c..40111fc 100644 --- a/index.js +++ b/index.js @@ -188,6 +188,9 @@ module.exports = function(css, options){ prop = trim(prop[0]); // : /* AUSTIN HACK! */ // if (!match(/^:\s*/)) return error("property missing ':'"); + if (!match(/^:\s*/)) { + match(/^\s*/) + } // val var val = match(/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\([^\)]*?\)|[^};])+)/); From 82c2951c5db8f94c36fbc9d8d05f4b6f2854a38d Mon Sep 17 00:00:00 2001 From: austin chau Date: Tue, 18 Mar 2014 00:13:12 -0700 Subject: [PATCH 5/5] . --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 40111fc..58a83e4 100644 --- a/index.js +++ b/index.js @@ -226,7 +226,10 @@ module.exports = function(css, options){ comments(decls); } - if (!close()) return error("missing '}'"); + if (!close()) { + css += '}'; + if (!close()) return error("missing '}'"); + } return decls; }