From 6df4e4897681dae1668d582bbb64bfebf0cbc3e3 Mon Sep 17 00:00:00 2001 From: Kevin Gillette Date: Fri, 14 Jun 2013 01:06:16 -0600 Subject: [PATCH] Added min and max builtins. --- lib/less/functions.js | 35 +++++++++++++++++++++++++++++++++++ test/css/functions.css | 6 ++++++ test/less/functions.less | 6 ++++++ 3 files changed, 47 insertions(+) diff --git a/lib/less/functions.js b/lib/less/functions.js index 361be6f4..016e8a96 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -266,6 +266,41 @@ tree.functions = { throw { type: "Argument", message: "argument must be a number" }; } }, + _minmax: function (name, 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]; + } + var isMin = name == 'min'; + function f (a, b) { + if(!(a instanceof tree.Dimension && b instanceof tree.Dimension)) { + return null; + } + var ua = a.unify(); + var ub = b.unify(); + if(ua.unit.compare(ub.unit) != 0) { + return null; + } + var val = ua.compare(ub); + if(isMin && val > 0 || !isMin && val < 0) { + return b; + } + return a; + } + var val = args.reduce(f); + if(val !== null) { + return val; + } + return new(tree.Anonymous)(name + + "(" + args.map(function (a) { return a.toCSS(); }).join(', ') + ")"); + }, + min: function () { + return this._minmax('min', arguments); + }, + max: function () { + return this._minmax('max', arguments); + }, argb: function (color) { return new(tree.Anonymous)(color.toARGB()); diff --git a/test/css/functions.css b/test/css/functions.css index d1392c35..12a831a0 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -81,6 +81,12 @@ pow: 64px; pow: 64; pow: 27; + min: min(3, 3em); + min: min(3pt, 3em); + min: 1%; + min: min(1%, 5px); + min: 3pt; + max: 3; percentage: 20%; color: #ff0011; tint: #898989; diff --git a/test/less/functions.less b/test/less/functions.less index 337e9eb6..929595b6 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -87,6 +87,12 @@ pow: pow(8px, 2); pow: pow(4, 3); pow: pow(3, 3em); + min: min(3, 3em); + min: min(3pt, 3em); + min: min(1%, 5%); + min: min(1%, 5px); + min: min(1pc, 3pt); + max: max(1, 3); percentage: percentage((10px / 50)); color: color("#ff0011"); tint: tint(#777777, 13);