From a8a91949fafae8e1e647f5bf980d0ce8972a75d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 20 Jan 2011 11:15:17 -0500 Subject: [PATCH 1/3] doc --- lib/mess/parser.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/mess/parser.js b/lib/mess/parser.js index b08eb20..466d895 100644 --- a/lib/mess/parser.js +++ b/lib/mess/parser.js @@ -351,6 +351,10 @@ mess.Parser = function Parser(env) { }; })(); + /** + * Splits up rulesets with multiple selectors into individual rulesets + * that only have one selector, but the same rules. + */ var splitSelectors = function(rulesets) { return rulesets.reduce(function(result, ruleset) { ruleset.selectors.forEach(function(selector) { From 004578c2b64b45ee566315b77c89d11edcc01b2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 20 Jan 2011 11:45:29 -0500 Subject: [PATCH 2/3] add definition class for flattened rulesets --- lib/mess/index.js | 2 +- lib/mess/parser.js | 16 +--------------- lib/mess/tree/definition.js | 8 ++++++++ lib/mess/tree/ruleset.js | 13 ++++++------- 4 files changed, 16 insertions(+), 23 deletions(-) create mode 100644 lib/mess/tree/definition.js diff --git a/lib/mess/index.js b/lib/mess/index.js index f7d84c1..1ed201d 100644 --- a/lib/mess/index.js +++ b/lib/mess/index.js @@ -82,7 +82,7 @@ var mess = { 'selector', 'quoted', 'expression', 'rule', 'call', 'url', 'alpha', 'import', 'mixin', 'comment', 'anonymous', 'value', 'javascript', - 'comparison', 'reference', 'filter' + 'comparison', 'reference', 'filter', 'definition' ].forEach(function (n) { require(path.join('mess', 'tree', n)); }); diff --git a/lib/mess/parser.js b/lib/mess/parser.js index 466d895..bc240c3 100644 --- a/lib/mess/parser.js +++ b/lib/mess/parser.js @@ -314,8 +314,7 @@ mess.Parser = function Parser(env) { options.compress : (env.compress || false); try { - var rulesets = this.flatten(); - rulesets = splitSelectors(rulesets); + var rulesets = this.flatten([]); rulesets.sort(specificitySort); return rulesets; } @@ -351,19 +350,6 @@ mess.Parser = function Parser(env) { }; })(); - /** - * Splits up rulesets with multiple selectors into individual rulesets - * that only have one selector, but the same rules. - */ - 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) { From ef597af7c0d7b69bfc222551c565f94991817f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Thu, 20 Jan 2011 11:48:00 -0500 Subject: [PATCH 3/3] rename rulesets to definitions --- lib/mess/parser.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/mess/parser.js b/lib/mess/parser.js index bc240c3..07fc073 100644 --- a/lib/mess/parser.js +++ b/lib/mess/parser.js @@ -314,9 +314,9 @@ mess.Parser = function Parser(env) { options.compress : (env.compress || false); try { - var rulesets = this.flatten([]); - rulesets.sort(specificitySort); - return rulesets; + var definitions = this.flatten([]); + definitions.sort(specificitySort); + return definitions; } catch (e) { lines = input.split('\n');