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

View File

@ -1,6 +1,13 @@
if (typeof(window) === 'undefined') { var tree = require(require('path').join(__dirname, '..', '..', 'less', 'tree')); } if (typeof(window) === 'undefined') { var tree = require(require('path').join(__dirname, '..', '..', 'less', 'tree')); }
tree.Selector = function Selector(elements) { this.elements = elements }; 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 () { tree.Selector.prototype.toCSS = function () {
return this.elements.map(function (e) { return this.elements.map(function (e) {
if (typeof(e) === 'string') { if (typeof(e) === 'string') {