Skip to content

Support || operator for variables #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,16 @@ less.Parser = function Parser(env) {
return operation || m;
}
},
bor: function() {

var m, a, op, operation;
if (m = $(this.addition)) {
while ((op = $(/^\|\|\s+/)) && (a = $(this.addition))) {
operation = new(tree.Operation)(op, [operation || m, a]);
}
return operation || m;
}
},

//
// An operand is anything that can be part of an operation,
Expand All @@ -1075,7 +1085,7 @@ less.Parser = function Parser(env) {
expression: function () {
var e, delim, entities = [], d;

while (e = $(this.addition) || $(this.entity)) {
while (e = $(this.bor) || $(this.entity)) {
entities.push(e);
}
if (entities.length > 0) {
Expand Down
5 changes: 3 additions & 2 deletions lib/less/tree/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,12 @@ tree.Color.prototype = {
//
operate: function (op, other) {
var result = [];

if (op == '||') {
return this||other;
}
if (! (other instanceof tree.Color)) {
other = other.toColor();
}

for (var c = 0; c < 3; c++) {
result[c] = tree.operate(op, this.rgb[c], other.rgb[c]);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/less/tree/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ tree.Import = function (path, imports) {
this.path = path.value.value || path.value;
}

this.css = /css$/.test(this.path);
this.css = /css$/.test(this.path) && !/less\.css$/.test(this.path);//precompile less.css and .less files!

// Only pre-compile .less files
if (! this.css) {
Expand Down
1 change: 1 addition & 0 deletions lib/less/tree/operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ tree.operate = function (op, a, b) {
case '-': return a - b;
case '*': return a * b;
case '/': return a / b;
case '||': return a || b;
}
};

Expand Down
1 change: 1 addition & 0 deletions lib/less/tree/ruleset.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ tree.Ruleset.prototype = {
else {
return this._variables = this.rules.reduce(function (hash, r) {
if (r instanceof tree.Rule && r.variable === true) {
r.prevValue = hash[r.name];
hash[r.name] = r;
}
return hash;
Expand Down
4 changes: 4 additions & 0 deletions lib/less/tree/value.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
tree.Value = function (value) {
this.value = value;
this.is = 'value';
if (value ===undefined) {
this.operate = function(op,other){return op=='||'?other:undefined;};
}
};

tree.Value.prototype = {
eval: function (env) {
if (this.value.length === 1) {
Expand Down
16 changes: 14 additions & 2 deletions lib/less/tree/variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,26 @@ tree.Variable.prototype = {

if (variable = tree.find(env.frames, function (frame) {
if (v = frame.variable(name)) {
return v.value.eval(env);
if (!v.isEvaluating) { //stop recursive evaluation by returning a special value
v.isEvaluating = true;
try {
return v.value.eval(env);
} finally {
v.isEvaluating = undefined;
}
} else if (v.prevValue) {
return v.prevValue.value.eval(env);
} else {
return new tree.Value(undefined);
}
}
})) { return variable }
})) { return variable; }
else {
throw { message: "variable " + name + " is undefined",
index: this.index };
}
}
};


})(require('less/tree'));