Skip to content

Commit

Permalink
use a hash to store variables, fixes variable redifinition bug
Browse files Browse the repository at this point in the history
  • Loading branch information
cloudhead committed Apr 23, 2010
1 parent fee1e30 commit 3047f76
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
13 changes: 8 additions & 5 deletions lib/less/tree/ruleset.js
Expand Up @@ -22,12 +22,15 @@ tree.Ruleset.prototype = {
this.rules = rules;
return this;
},
variables: function () {
if (this._variables) { return this._variables }
variables: function (name) {
if (this._variables) { return this._variables[name] }
else {
return this._variables = this.rules.filter(function (r) {
if (r instanceof tree.Rule && r.variable === true) { return r }
});
return (this._variables = this.rules.reduce(function (hash, r) {
if (r instanceof tree.Rule && r.variable === true) {
hash[r.name] = r;
}
return hash;
}, {}))[name];
}
},
rulesets: function () {
Expand Down
11 changes: 4 additions & 7 deletions lib/less/tree/variable.js
Expand Up @@ -4,15 +4,12 @@ tree.Variable = function Variable(name) { this.name = name };
tree.Variable.prototype = {
toCSS: function (env) { return this.eval(env).toCSS(env) },
eval: function (env) {
var variable, name = this.name;
var variable, v, name = this.name;

if (variable = tree.find(env.frames, function (frame) {
return tree.find(frame.variables(), function (variable) {
if (variable.name === name) {
return variable.value.eval ? variable.value.eval(env)
: variable.value;
}
});
if (v = frame.variables(name)) {
return v.value.eval(env);
}
})) { return variable }
else {
throw new(Error)("variable " + this.name + " is undefined");
Expand Down
3 changes: 3 additions & 0 deletions test/css/variables.css
Expand Up @@ -7,3 +7,6 @@
font-family: "Trebuchet MS", Verdana, sans-serif;
quotes: "~" "~";
}
.redefinition {
three: 3;
}
7 changes: 7 additions & 0 deletions test/less/variables.less
Expand Up @@ -22,3 +22,10 @@
font-family: @f;
quotes: @q;
}

.redefinition {
@var: 4;
@var: 2;
@var: 3;
three: @var;
}

0 comments on commit 3047f76

Please sign in to comment.