refactored Ruleset/find() method, and added a match() method to Selector, to do selector matching.
This commit is contained in:
parent
3e8abb2abf
commit
2644014bdf
@ -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
|
||||||
|
@ -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') {
|
||||||
|
Loading…
Reference in New Issue
Block a user