Adding reduceRules - see commented code to check out

This commit is contained in:
Tom MacWright 2011-01-19 16:28:52 -05:00
parent 549a266b9e
commit ea5f76c8cf
2 changed files with 27 additions and 3 deletions

View File

@ -317,8 +317,9 @@ mess.Parser = function Parser(env) {
var rulesets = this.flatten();
var mss = [];
rulesets.sort(specificitySort);
// console.log(matchingRulesets(rulesets, '#red', ['.blue']));
for (var i = 0; i < rulesets.length; i++) {
var rb = matchingRulesets(rulesets, '#red', ['.blue']);
// console.log(reduceRules(rb));
for (var i = 0, l = rulesets.length; i < l; i++) {
mss.push(rulesets[i].toCSS([], env));
}
return mss.join('\n');
@ -392,7 +393,7 @@ mess.Parser = function Parser(env) {
*/
var matchingRulesets = function(rulesets, id, classes) {
var matching = [];
for (var i = 0, l = rulesets.length; i < l; i++) {
for (var i = 0; i < rulesets.length; i++) {
if (rulesets[i].selectors.length &&
rulesets[i].selectors[0].match(id, classes)) {
matching.push(rulesets[i]);
@ -401,6 +402,14 @@ mess.Parser = function Parser(env) {
return matching;
};
var reduceRules = function(rulesets) {
var n = rulesets.length - 1;
for (var i = n; i > 0; i--) {
rulesets[0].extend(rulesets[i]);
}
return rulesets[0];
};
// If `i` is smaller than the `input.length - 1`,
// it means the parser wasn't able to parse the whole
// string, so we've got a parsing error.

View File

@ -72,6 +72,21 @@ tree.Ruleset.prototype = {
variable: function(name) {
return this.variables()[name];
},
findRule: function(name) {
for (var i = 0; i < this.rules.length; i++) {
if (this.rules[i].name == name) {
return this.rules[i];
}
}
},
extend: function(ruleset) {
for (var i = 0; i < ruleset.rules.length; i++) {
if (!this.findRule(ruleset.rules[i].name)) {
console.log('pushing rule');
this.rules.push(ruleset.rules[i]);
}
}
},
rulesets: function() {
if (this._rulesets) { return this._rulesets }
else {