refactored Ruleset/find() method, and added a match() method to Selector, to do selector matching.

This commit is contained in:
cloudhead 2010-03-04 13:50:52 -05:00
parent 3e8abb2abf
commit 2644014bdf
2 changed files with 28 additions and 18 deletions

View File

@ -10,27 +10,30 @@ tree.Ruleset.prototype = {
if (r instanceof tree.Rule && r.variable === true) { return r }
});
},
mixable: function (fun) {
return this.rules.filter(function (r) {
if (r instanceof tree.mixin.Definition || r instanceof tree.Ruleset) {
if (typeof(fun) === 'function') {
return fun.call(this, r);
} else { return r }
}
});
},
find: function (selector) {
return this.mixable(function (rule) {
for (var j = 0; j < rule.selectors.length; j++) {
if (selector.elements[0].value === rule.selectors[j].elements[0].value) {
if (selector.elements.length > 1) {
return rule.find(new(tree.Selector)(selector.elements.slice(1)));
} else {
return rule;
find: function (selector, self) {
self = self || this;
var rules = [], rule, match;
for (var i = 0; i < this.rules.length; i++) {
rule = this.rules[i];
if ((rule instanceof tree.mixin.Definition ||
rule instanceof tree.Ruleset) && rule !== self) {
for (var j = 0; j < rule.selectors.length; j++) {
if (match = selector.match(rule.selectors[j])) {
if (selector.elements.length > 1) {
Array.prototype.push.apply(rules, rule.find(
new(tree.Selector)(selector.elements.slice(1)), self));
} else {
rules.push(rule);
}
break;
}
}
}
});
}
return rules
},
//
// Entry point for code generation

View File

@ -1,6 +1,13 @@
if (typeof(window) === 'undefined') { var tree = require(require('path').join(__dirname, '..', '..', 'less', 'tree')); }
tree.Selector = function Selector(elements) { this.elements = elements };
tree.Selector.prototype.match = function (other) {
if (this.elements[0].value === other.elements[0].value) {
return true;
} else {
return false;
}
};
tree.Selector.prototype.toCSS = function () {
return this.elements.map(function (e) {
if (typeof(e) === 'string') {