From 4767b264e73b722ad1a14951b03cd843f6af5665 Mon Sep 17 00:00:00 2001 From: cloudhead Date: Wed, 3 Mar 2010 21:21:32 -0500 Subject: [PATCH] basic mixins with namespaces working. swoosh. --- lib/less/node/mixin.js | 17 ++++++++++++++--- lib/less/node/ruleset.js | 24 ++++++++++++++++++++++++ lib/less/parser.js | 2 +- 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/less/node/mixin.js b/lib/less/node/mixin.js index ea8f350..0a6be4e 100644 --- a/lib/less/node/mixin.js +++ b/lib/less/node/mixin.js @@ -1,11 +1,22 @@ if (typeof(window) === 'undefined') { var tree = require(require('path').join(__dirname, '..', '..', 'less', 'tree')); } tree.mixin = {}; -tree.mixin.Call = function MixinCall(mixins) { - this.mixins = mixins; +tree.mixin.Call = function MixinCall(elements, args) { + this.selector = new(tree.Selector)(elements); + this.arguments = args; }; 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"); } }; diff --git a/lib/less/node/ruleset.js b/lib/less/node/ruleset.js index 7e01711..ce7b37e 100644 --- a/lib/less/node/ruleset.js +++ b/lib/less/node/ruleset.js @@ -10,6 +10,24 @@ 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) { 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 // @@ -33,6 +51,12 @@ tree.Ruleset.prototype = { } 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++) { if (this.rules[i] instanceof tree.Ruleset) { rulesets.push(this.rules[i].toCSS(paths, env)); diff --git a/lib/less/parser.js b/lib/less/parser.js index 4a4655f..08e9a22 100644 --- a/lib/less/parser.js +++ b/lib/less/parser.js @@ -392,7 +392,7 @@ less.parser = { if (match = peek(/([a-z.#: _-]+)[\s\n]*\{/g)) { i += match[0].length - 1; - selectors = [new(tree.Selector)([match[1]])]; + selectors = [new(tree.Selector)([new(tree.Element)(null, match[1])])]; } else { while (s = $(this.selector)) { selectors.push(s);