basic mixins with namespaces working. swoosh.

This commit is contained in:
cloudhead 2010-03-03 21:21:32 -05:00
parent fe0ed621b9
commit 4767b264e7
3 changed files with 39 additions and 4 deletions

View File

@ -1,11 +1,22 @@
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.mixin = {}; tree.mixin = {};
tree.mixin.Call = function MixinCall(mixins) { tree.mixin.Call = function MixinCall(elements, args) {
this.mixins = mixins; this.selector = new(tree.Selector)(elements);
this.arguments = args;
}; };
tree.mixin.Call.prototype = { tree.mixin.Call.prototype = {
toCSS: function (context, env) { eval: function (env) {
var mixin, rules = [];
for (var i = 0; i < env.frames.length; i++) {
if (mixin = env.frames[i].find(this.selector)) {
for (var r = 0; r < mixin.rules.length; r++) {
rules.push(mixin.rules[r]);
}
return rules;
}
}
throw new(Error)("mixin " + this.selector.toCSS() + " is undefined");
} }
}; };

View File

@ -10,6 +10,24 @@ 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) {
return this.rules.filter(function (r) {
if (r instanceof tree.mixin.Definition || r instanceof tree.Ruleset) { return r }
});
},
find: function (selector) {
return this.mixable().find(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;
}
}
}
});
},
// //
// Entry point for code generation // Entry point for code generation
// //
@ -33,6 +51,12 @@ tree.Ruleset.prototype = {
} }
env.frames.unshift(this); env.frames.unshift(this);
for (var i = 0; i < this.rules.length; i++) {
if (this.rules[i] instanceof tree.mixin.Call) {
Array.prototype.splice
.apply(this.rules, [i, 1].concat(this.rules[i].eval(env)));
}
}
for (var i = 0; i < this.rules.length; i++) { for (var i = 0; i < this.rules.length; i++) {
if (this.rules[i] instanceof tree.Ruleset) { if (this.rules[i] instanceof tree.Ruleset) {
rulesets.push(this.rules[i].toCSS(paths, env)); rulesets.push(this.rules[i].toCSS(paths, env));

View File

@ -392,7 +392,7 @@ less.parser = {
if (match = peek(/([a-z.#: _-]+)[\s\n]*\{/g)) { if (match = peek(/([a-z.#: _-]+)[\s\n]*\{/g)) {
i += match[0].length - 1; i += match[0].length - 1;
selectors = [new(tree.Selector)([match[1]])]; selectors = [new(tree.Selector)([new(tree.Element)(null, match[1])])];
} else { } else {
while (s = $(this.selector)) { while (s = $(this.selector)) {
selectors.push(s); selectors.push(s);