diff --git a/lib/mess/index.js b/lib/mess/index.js index 8da86f2..7be7d35 100644 --- a/lib/mess/index.js +++ b/lib/mess/index.js @@ -10,7 +10,7 @@ var mess = { Renderer: require('mess/renderer').Renderer, importer: require('mess/parser').importer, tree: require('mess/tree'), - render: function (input, options, callback) { + render: function(input, options, callback) { options = options || {}; if (typeof(options) === 'function') { @@ -21,23 +21,23 @@ var mess = { ee; if (callback) { - parser.parse(input, function (e, root) { + parser.parse(input, function(e, root) { callback(e, root.toCSS()); }); } else { ee = new(require('events').EventEmitter); - process.nextTick(function () { - parser.parse(input, function (e, root) { + process.nextTick(function() { + parser.parse(input, function(e, root) { if (e) { ee.emit('error', e) } - else { ee.emit('success', root.toCSS()) } + else { ee.emit('success', root.toCSS()) } }); }); return ee; } }, - writeError: function (ctx, options) { - var message = ""; + writeError: function(ctx, options) { + var message = ''; var extract = ctx.extract; var error = []; @@ -70,24 +70,24 @@ var mess = { sys.error(message, error); if (ctx.callLine) { - sys.error(stylize('from ', 'red') + (ctx.filename || '')); + sys.error(stylize('from ', 'red') + (ctx.filename || '')); sys.error(stylize(ctx.callLine, 'grey') + ' ' + ctx.callExtract); } if (ctx.stack) { sys.error(stylize(ctx.stack, 'red')) } } }; -['color', 'directive', 'operation', 'dimension', - 'keyword', 'variable', 'ruleset', 'element', - 'selector', 'quoted', 'expression', 'rule', - 'call', 'url', 'alpha', 'import', - 'mixin', 'comment', 'anonymous', 'value', 'javascript', - 'comparison', 'reference', 'filter', 'zoomfilter' -].forEach(function (n) { +['color', 'directive', 'operation', 'dimension', + 'keyword', 'variable', 'ruleset', 'element', + 'selector', 'quoted', 'expression', 'rule', + 'call', 'url', 'alpha', 'import', + 'mixin', 'comment', 'anonymous', 'value', 'javascript', + 'comparison', 'reference', 'filter', 'definition', 'zoomfilter' +].forEach(function(n) { require(path.join('mess', 'tree', n)); }); -mess.Parser.importer = function (file, paths, callback) { +mess.Parser.importer = function(file, paths, callback) { var pathname; paths.unshift('.'); @@ -109,7 +109,7 @@ mess.Parser.importer = function (file, paths, callback) { new(mess.Parser)({ paths: [path.dirname(pathname)], filename: pathname - }).parse(data, function (e, root) { + }).parse(data, function(e, root) { if (e) mess.writeError(e); callback(root); }); @@ -118,7 +118,7 @@ mess.Parser.importer = function (file, paths, callback) { sys.error("file '" + file + "' wasn't found.\n"); process.exit(1); } -} +}; require('mess/functions'); @@ -127,13 +127,13 @@ for (var k in mess) { exports[k] = mess[k] } // Stylize a string function stylize(str, style) { var styles = { - 'bold' : [1, 22], - 'inverse' : [7, 27], - 'underline' : [4, 24], - 'yellow' : [33, 39], - 'green' : [32, 39], - 'red' : [31, 39], - 'grey' : [90, 39] + 'bold' : [1, 22], + 'inverse' : [7, 27], + 'underline' : [4, 24], + 'yellow' : [33, 39], + 'green' : [32, 39], + 'red' : [31, 39], + 'grey' : [90, 39] }; return '\033[' + styles[style][0] + 'm' + str + '\033[' + styles[style][1] + 'm'; diff --git a/lib/mess/parser.js b/lib/mess/parser.js index a5ed03d..7994c41 100644 --- a/lib/mess/parser.js +++ b/lib/mess/parser.js @@ -314,10 +314,9 @@ mess.Parser = function Parser(env) { options.compress : (env.compress || false); try { - var rulesets = this.flatten(); - rulesets = splitSelectors(rulesets); - rulesets.sort(specificitySort); - return rulesets; + var definitions = this.flatten([]); + definitions.sort(specificitySort); + return definitions; } catch (e) { lines = input.split('\n'); @@ -351,15 +350,6 @@ mess.Parser = function Parser(env) { }; })(); - var splitSelectors = function(rulesets) { - return rulesets.reduce(function(result, ruleset) { - ruleset.selectors.forEach(function(selector) { - result.push({ selector: selector, rules: ruleset.rules }); - }); - return result; - }, []); - }; - /** * Sort rules by specificity: this function expects selectors to be * split already. diff --git a/lib/mess/tree/definition.js b/lib/mess/tree/definition.js new file mode 100644 index 0000000..0e91eab --- /dev/null +++ b/lib/mess/tree/definition.js @@ -0,0 +1,8 @@ +(function(tree) { + +tree.Definition = function(selector, rules) { + this.selector = selector; + this.rules = rules; +}; + +})(require('mess/tree')); diff --git a/lib/mess/tree/ruleset.js b/lib/mess/tree/ruleset.js index 45aaa42..6125be0 100644 --- a/lib/mess/tree/ruleset.js +++ b/lib/mess/tree/ruleset.js @@ -131,7 +131,7 @@ tree.Ruleset.prototype = { }, - flatten: function(parentSelectors) { + flatten: function(result, parentSelectors) { var selectors = []; for (var i = 0; i < this.selectors.length; i++) { var selector = this.selectors[i]; @@ -156,12 +156,11 @@ tree.Ruleset.prototype = { } var rules = []; - var rulesets = []; for (var i = 0; i < this.rules.length; i++) { var rule = this.rules[i]; if (rule instanceof tree.Ruleset) { - Array.prototype.push.apply(rulesets, rule.flatten(selectors)); + rule.flatten(result, selectors); } else { rule.value = rule.value.value[0]; @@ -169,11 +168,11 @@ tree.Ruleset.prototype = { } } - var ruleset = new tree.Ruleset(selectors, rules); - ruleset.flattened = true; - rulesets.push(ruleset); + for (var i = 0; i < selectors.length; i++) { + result.push(new tree.Definition(selectors[i], rules)); + } - return rulesets; + return result; }, toMSS: function(env) {