From 08e613709e93eed8d36b08574f3df9cd1bdb3ebd Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Fri, 10 Jan 2014 22:48:37 +0530 Subject: [PATCH] Improved min/max function, 2 new Built in function --- lib/less/functions.js | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 3f58ec29..cbddee5e 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -264,24 +264,41 @@ tree.functions = { _minmax: function (isMin, args) { args = Array.prototype.slice.call(args); switch(args.length) { - case 0: throw { type: "Argument", message: "one or more arguments required" }; - case 1: return args[0]; + case 0: throw { type: "Argument", message: "one or more arguments required" }; } - var i, j, current, currentUnified, referenceUnified, unit, + var i, j, current, currentUnified, referenceUnified, unit, unitStatic, order = [], // elems only contains original argument values. values = {}; // key is the unit.toString() for unified tree.Dimension values, // value is the index into the order array. for (i = 0; i < args.length; i++) { current = args[i]; if (!(current instanceof tree.Dimension)) { - order.push(current); continue; } currentUnified = current.unify(); unit = currentUnified.unit.toString(); + if(unit === "" && unitStatic !== undefined) { + unit = unitStatic; + } else if(unit === "") { + unit = "variable"; + } + unitStatic = unit !== "variable" && unitStatic === undefined ? unit : unitStatic; + values[unit] = values["variable"] !== undefined && unit !== "variable" && unit === unitStatic ? values["variable"] : values[unit]; j = values[unit]; if (j === undefined) { values[unit] = order.length; + //error handling for incompatible types + if((values["px"] !== undefined && values["em"] !== undefined) || (values["px"] !== undefined && values["%"] !== undefined) || + (values["px"] !== undefined && values["m"] !== undefined) || (values["px"] !== undefined && values["s"] !== undefined) || + (values["px"] !== undefined && values["rad"] !== undefined) || (values["em"] !== undefined && values["%"] !== undefined) || + (values["em"] !== undefined && values["m"] !== undefined) || (values["em"] !== undefined && values["s"] !== undefined) || + (values["em"] !== undefined && values["rad"] !== undefined) || (values["%"] !== undefined && values["m"] !== undefined) || + (values["%"] !== undefined && values["s"] !== undefined) || (values["%"] !== undefined && values["rad"] !== undefined) || + (values["m"] !== undefined && values["s"] !== undefined) || (values["m"] !== undefined && values["rad"] !== undefined) || + (values["s"] !== undefined && values["rad"] !== undefined)) + { + throw { type: "Argument", message: "incompatible types" }; + } order.push(current); continue; } @@ -294,8 +311,7 @@ tree.functions = { if (order.length == 1) { return order[0]; } - args = order.map(function (a) { return a.toCSS(this.env); }) - .join(this.env.compress ? "," : ", "); + args = order.map(function (a) { return a.toCSS(this.env); }).join(this.env.compress ? "," : ", "); return new(tree.Anonymous)((isMin ? "min" : "max") + "(" + args + ")"); }, min: function () { @@ -303,6 +319,12 @@ tree.functions = { }, max: function () { return this._minmax(false, arguments); + }, + zero: function (n) { + return new(tree.Dimension)(n.value, n.value ? n.unit : ''); + }, + unitonly: function (n) { + return new(tree.Anonymous)(n.unit); }, argb: function (color) { return new(tree.Anonymous)(color.toARGB());