From 08e613709e93eed8d36b08574f3df9cd1bdb3ebd Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Fri, 10 Jan 2014 22:48:37 +0530 Subject: [PATCH 1/9] 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()); From bf2a49a12e79ac168b59c23174e96c3e8e511722 Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Sat, 11 Jan 2014 17:43:24 +0530 Subject: [PATCH 2/9] Improved min/max function, 2 new Built in function max(0, 1em, 2, 4px) //returns 4px on the basis of first enter basis. Previous calculation would result in max(2, 1em, 4px). Here, 2 and 1em is compared, 2 is returned. then 2 and 4px is compared. Resulting in 4px. max(0, 1em, 1, 4px) //max(1em, 4px) --- lib/less/functions.js | 23 +++-------------------- 1 file changed, 3 insertions(+), 20 deletions(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index cbddee5e..85145b7e 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -276,29 +276,12 @@ tree.functions = { 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]; + unit = currentUnified.unit.toString() === "" && unitStatic !== undefined ? unitStatic : currentUnified.unit.toString(); + unitStatic = unit !== "" && unitStatic === undefined || unit !== "" && order[0].unify().unit.toString() === "" ? unit : unitStatic; + values[unit] = values[""] !== undefined && unit !== "" && unit === unitStatic ? values[""] : 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; } From 0f2926b4d8f8481e28e28faf3c09744d2648cae5 Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Sat, 11 Jan 2014 17:52:25 +0530 Subject: [PATCH 3/9] Update functions.js max(0, 1em, 2, 4px) //returns max(2, 4px) on the basis of first enter basis, as 2 gets em unit. Now em and px are incompatible till latest version of LESS. --- lib/less/functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 85145b7e..c1d99581 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -277,7 +277,7 @@ tree.functions = { } currentUnified = current.unify(); unit = currentUnified.unit.toString() === "" && unitStatic !== undefined ? unitStatic : currentUnified.unit.toString(); - unitStatic = unit !== "" && unitStatic === undefined || unit !== "" && order[0].unify().unit.toString() === "" ? unit : unitStatic; + unitStatic = unit !== "" && unitStatic === undefined ? unit : unitStatic; values[unit] = values[""] !== undefined && unit !== "" && unit === unitStatic ? values[""] : values[unit]; j = values[unit]; if (j === undefined) { From b77b02091aa01609412f6b4d5e43d88002623d82 Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Sat, 11 Jan 2014 17:53:57 +0530 Subject: [PATCH 4/9] Improved min/max function, 2 new Built in function max(0, 1em, 2, 4px) //returns max(2, 4px) on the basis of first enter basis, as 2 gets em unit. Now em and px are incompatible till latest version of LESS. --- lib/less/functions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index c1d99581..68d93a30 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -269,7 +269,7 @@ tree.functions = { 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. + // value is the index into the order array for (i = 0; i < args.length; i++) { current = args[i]; if (!(current instanceof tree.Dimension)) { From 7fc12014f281fa23b3757faff0d579930a51c335 Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Sun, 12 Jan 2014 10:47:55 +0530 Subject: [PATCH 5/9] Error handlers for min/max functions --- lib/less/functions.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/less/functions.js b/lib/less/functions.js index 68d93a30..c6ab2340 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -281,6 +281,9 @@ tree.functions = { values[unit] = values[""] !== undefined && unit !== "" && unit === unitStatic ? values[""] : values[unit]; j = values[unit]; if (j === undefined) { + if(unitStatic !== undefined && unit !== unitStatic) { + throw{ type: "Argument", message: "incompatible types" }; + } values[unit] = order.length; order.push(current); continue; From e6b94f9c45c0a2cc309ba2598e783c1fd670f98a Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Mon, 13 Jan 2014 16:43:46 +0530 Subject: [PATCH 6/9] Pixel conversion added. --- lib/less/tree/dimension.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/less/tree/dimension.js b/lib/less/tree/dimension.js index c64cd89a..7012c41a 100644 --- a/lib/less/tree/dimension.js +++ b/lib/less/tree/dimension.js @@ -161,6 +161,7 @@ tree.UnitConversions = { 'cm': 0.01, 'mm': 0.001, 'in': 0.0254, + 'px': 0.0254 / 96, 'pt': 0.0254 / 72, 'pc': 0.0254 / 72 * 12 }, From 82ab17c08217a44f46270695e4201756b6548d2c Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Mon, 13 Jan 2014 18:20:43 +0530 Subject: [PATCH 7/9] Consistency fixes --- lib/less/functions.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/less/functions.js b/lib/less/functions.js index c6ab2340..74c15e6a 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -273,6 +273,9 @@ tree.functions = { for (i = 0; i < args.length; i++) { current = args[i]; if (!(current instanceof tree.Dimension)) { + if(Array.isArray(args[i].value)) { + Array.prototype.push.apply(args, Array.prototype.slice.call(args[i].value)); + } continue; } currentUnified = current.unify(); From 7bbf7cef971582fc2dc09247e209df62234b6daa Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Wed, 15 Jan 2014 17:46:17 +0530 Subject: [PATCH 8/9] max/min Bugfix max(0, 1cm)// 0 ... 0 assumed unit "m". on comparison 0.01m was less than 0 --- lib/less/functions.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 74c15e6a..7df372e9 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -266,10 +266,10 @@ tree.functions = { switch(args.length) { case 0: throw { type: "Argument", message: "one or more arguments required" }; } - var i, j, current, currentUnified, referenceUnified, unit, unitStatic, + var i, j, current, currentUnified, referenceUnified, unit, unitStatic, unitClone, 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 + // value is the index into the order array. for (i = 0; i < args.length; i++) { current = args[i]; if (!(current instanceof tree.Dimension)) { @@ -278,11 +278,11 @@ tree.functions = { } continue; } - currentUnified = current.unify(); + currentUnified = current.unit.toString() === "" && unitClone !== undefined ? new(tree.Dimension)(current.value, unitClone).unify() : current.unify(); unit = currentUnified.unit.toString() === "" && unitStatic !== undefined ? unitStatic : currentUnified.unit.toString(); - unitStatic = unit !== "" && unitStatic === undefined ? unit : unitStatic; - values[unit] = values[""] !== undefined && unit !== "" && unit === unitStatic ? values[""] : values[unit]; - j = values[unit]; + unitStatic = unit !== "" && unitStatic === undefined || unit !== "" && order[0].unify().unit.toString() === "" ? unit : unitStatic; + unitClone = unit !== "" && unitClone === undefined ? current.unit.toString() : unitClone; + j = values[""] !== undefined && unit !== "" && unit === unitStatic ? values[""] : values[unit]; if (j === undefined) { if(unitStatic !== undefined && unit !== unitStatic) { throw{ type: "Argument", message: "incompatible types" }; @@ -291,7 +291,7 @@ tree.functions = { order.push(current); continue; } - referenceUnified = order[j].unify(); + referenceUnified = order[j].unit.toString() === "" && unitClone !== undefined ? new(tree.Dimension)(order[j].value, unitClone).unify() : order[j].unify(); if ( isMin && currentUnified.value < referenceUnified.value || !isMin && currentUnified.value > referenceUnified.value) { order[j] = current; From 8ccadb85938b880876dbf3528b1078815682c0ff Mon Sep 17 00:00:00 2001 From: deviprsd21 Date: Wed, 15 Jan 2014 18:28:34 +0530 Subject: [PATCH 9/9] Removal of zero, unitonly changed to obtain --- lib/less/functions.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/lib/less/functions.js b/lib/less/functions.js index 7df372e9..d23bd3ce 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -309,10 +309,7 @@ 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) { + obtain: function (n) { return new(tree.Anonymous)(n.unit); }, argb: function (color) {