mirror of
https://github.com/less/less.js.git
synced 2026-02-08 22:15:04 -05:00
reworked guard comparison
This commit is contained in:
@@ -94,4 +94,44 @@ tree.outputRuleset = function (env, output, rules) {
|
||||
env.tabLevel--;
|
||||
};
|
||||
|
||||
tree.compare = function (a, b) {
|
||||
/* returns:
|
||||
-1: a < b
|
||||
0: a = b
|
||||
1: a > b
|
||||
and *any* other value for a != b (e.g. undefined, NaN, -2 etc.) */
|
||||
|
||||
if ((a.compare) &&
|
||||
// for "symmetric results" force toCSS-based comparison
|
||||
// of Quoted or Anonymous if either value is one of those
|
||||
!(b.type === "Quoted" || b.type === "Anonymous")) {
|
||||
return a.compare(b);
|
||||
} else if (b.compare) {
|
||||
return -b.compare(a);
|
||||
} else if (a.type !== b.type) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
a = a.value;
|
||||
b = b.value;
|
||||
if (!Array.isArray(a)) {
|
||||
return a === b ? 0 : undefined;
|
||||
}
|
||||
if (a.length !== b.length) {
|
||||
return undefined;
|
||||
}
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
if (tree.compare(a[i], b[i]) !== 0) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
};
|
||||
|
||||
tree.numericCompare = function (a, b) {
|
||||
return a < b ? -1
|
||||
: a === b ? 0
|
||||
: a > b ? 1 : undefined;
|
||||
};
|
||||
|
||||
})(require('./tree'));
|
||||
|
||||
@@ -12,19 +12,8 @@ tree.Anonymous.prototype = {
|
||||
eval: function () {
|
||||
return new tree.Anonymous(this.value, this.index, this.currentFileInfo, this.mapLines, this.rulesetLike);
|
||||
},
|
||||
compare: function (x) {
|
||||
if (!x.toCSS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
var left = this.toCSS(),
|
||||
right = x.toCSS();
|
||||
|
||||
if (left === right) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return left < right ? -1 : 1;
|
||||
compare: function (other) {
|
||||
return other.toCSS && this.toCSS() === other.toCSS() ? 0 : undefined;
|
||||
},
|
||||
isRulesetLike: function() {
|
||||
return this.rulesetLike;
|
||||
|
||||
@@ -150,14 +150,11 @@ tree.Color.prototype = {
|
||||
return toHex([this.alpha * 255].concat(this.rgb));
|
||||
},
|
||||
compare: function (x) {
|
||||
if (!x.rgb) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (x.rgb[0] === this.rgb[0] &&
|
||||
return (x.rgb &&
|
||||
x.rgb[0] === this.rgb[0] &&
|
||||
x.rgb[1] === this.rgb[1] &&
|
||||
x.rgb[2] === this.rgb[2] &&
|
||||
x.alpha === this.alpha) ? 0 : -1;
|
||||
x.alpha === this.alpha) ? 0 : undefined;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -14,34 +14,20 @@ tree.Condition.prototype = {
|
||||
this.rvalue = visitor.visit(this.rvalue);
|
||||
},
|
||||
eval: function (env) {
|
||||
var a = this.lvalue.eval(env),
|
||||
b = this.rvalue.eval(env);
|
||||
|
||||
var i = this.index, result;
|
||||
|
||||
result = (function (op) {
|
||||
var result = (function (op, a, b) {
|
||||
switch (op) {
|
||||
case 'and':
|
||||
return a && b;
|
||||
case 'or':
|
||||
return a || b;
|
||||
case 'and': return a && b;
|
||||
case 'or': return a || b;
|
||||
default:
|
||||
if (a.compare) {
|
||||
result = a.compare(b);
|
||||
} else if (b.compare) {
|
||||
result = b.compare(a);
|
||||
} else {
|
||||
throw { type: "Type",
|
||||
message: "Unable to perform comparison",
|
||||
index: i };
|
||||
}
|
||||
switch (result) {
|
||||
switch (tree.compare(a, b)) {
|
||||
case -1: return op === '<' || op === '=<' || op === '<=';
|
||||
case 0: return op === '=' || op === '>=' || op === '=<' || op === '<=';
|
||||
case 1: return op === '>' || op === '>=';
|
||||
default: return false;
|
||||
}
|
||||
}
|
||||
})(this.op);
|
||||
}) (this.op, this.lvalue.eval(env), this.rvalue.eval(env));
|
||||
|
||||
return this.negate ? !result : result;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -88,33 +88,24 @@ tree.Dimension.prototype = {
|
||||
},
|
||||
|
||||
compare: function (other) {
|
||||
if (other instanceof tree.Dimension) {
|
||||
var a, b,
|
||||
aValue, bValue;
|
||||
|
||||
if (this.unit.isEmpty() || other.unit.isEmpty()) {
|
||||
a = this;
|
||||
b = other;
|
||||
} else {
|
||||
a = this.unify();
|
||||
b = other.unify();
|
||||
if (a.unit.compare(b.unit) !== 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
aValue = a.value;
|
||||
bValue = b.value;
|
||||
|
||||
if (bValue > aValue) {
|
||||
return -1;
|
||||
} else if (bValue < aValue) {
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
var a, b;
|
||||
|
||||
if (!(other instanceof tree.Dimension)) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
if (this.unit.isEmpty() || other.unit.isEmpty()) {
|
||||
a = this;
|
||||
b = other;
|
||||
} else {
|
||||
a = this.unify();
|
||||
b = other.unify();
|
||||
if (a.unit.compare(b.unit) !== 0) {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
|
||||
return tree.numericCompare(a.value, b.value);
|
||||
},
|
||||
|
||||
unify: function () {
|
||||
@@ -220,7 +211,7 @@ tree.Unit.prototype = {
|
||||
},
|
||||
|
||||
compare: function (other) {
|
||||
return this.is(other.toString()) ? 0 : -1;
|
||||
return this.is(other.toString()) ? 0 : undefined;
|
||||
},
|
||||
|
||||
is: function (unitString) {
|
||||
|
||||
@@ -8,14 +8,7 @@ tree.Keyword.prototype = {
|
||||
if (this.value === '%') { throw { type: "Syntax", message: "Invalid % without number" }; }
|
||||
output.add(this.value);
|
||||
},
|
||||
toCSS: tree.toCSS,
|
||||
compare: function (other) {
|
||||
if (other instanceof tree.Keyword) {
|
||||
return other.value === this.value ? 0 : 1;
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
toCSS: tree.toCSS
|
||||
};
|
||||
|
||||
tree.True = new(tree.Keyword)('true');
|
||||
|
||||
@@ -29,27 +29,13 @@ tree.Quoted.prototype = {
|
||||
});
|
||||
return new(tree.Quoted)(this.quote + value + this.quote, value, this.escaped, this.index, this.currentFileInfo);
|
||||
},
|
||||
compare: function (x) {
|
||||
if (!x.toCSS) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
var left, right;
|
||||
|
||||
compare: function (other) {
|
||||
// when comparing quoted strings allow the quote to differ
|
||||
if (x.type === "Quoted" && !this.escaped && !x.escaped) {
|
||||
left = x.value;
|
||||
right = this.value;
|
||||
if (other.type === "Quoted" && !this.escaped && !other.escaped) {
|
||||
return tree.numericCompare(this.value, other.value);
|
||||
} else {
|
||||
left = this.toCSS();
|
||||
right = x.toCSS();
|
||||
return other.toCSS && this.toCSS() === other.toCSS() ? 0 : undefined;
|
||||
}
|
||||
|
||||
if (left === right) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return left < right ? -1 : 1;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user