From 202bcab8d2bd8e2ae01c25388809ae62112d6465 Mon Sep 17 00:00:00 2001 From: Bart Schuller Date: Mon, 14 Feb 2011 23:47:41 +0100 Subject: [PATCH] Added new round(n) function. Rationale: Using unrounded computed widths leads to pixel errors. --- lib/less/functions.js | 12 ++++++++++++ test/css/functions.css | 2 ++ test/less/functions.less | 2 ++ 3 files changed, 16 insertions(+) diff --git a/lib/less/functions.js b/lib/less/functions.js index 5d2d89c0..5853b2f9 100644 --- a/lib/less/functions.js +++ b/lib/less/functions.js @@ -130,6 +130,18 @@ tree.functions = { } str = str.replace(/%%/g, '%'); return new(tree.Quoted)('"' + str + '"', str); + }, + round: function (n) { + if (n instanceof tree.Dimension) { + return new(tree.Dimension)(Math.round(number(n)), n.unit); + } else if (typeof(n) === 'number') { + return Math.round(n); + } else { + throw { + error: "RuntimeError", + message: "math functions take numbers as parameters" + }; + } } }; diff --git a/test/css/functions.css b/test/css/functions.css index fc839179..d6d0ea84 100644 --- a/test/css/functions.css +++ b/test/css/functions.css @@ -20,6 +20,8 @@ hue: 98; saturation: 12%; lightness: 95%; + rounded: 11; + roundedpx: 3px; } #alpha { alpha: rgba(153, 94, 51, 0.6); diff --git a/test/less/functions.less b/test/less/functions.less index 1e67aeb5..634a65b8 100644 --- a/test/less/functions.less +++ b/test/less/functions.less @@ -24,6 +24,8 @@ hue: hue(hsl(98, 12%, 95%)); saturation: saturation(hsl(98, 12%, 95%)); lightness: lightness(hsl(98, 12%, 95%)); + rounded: round(@r/3); + roundedpx: round(10px / 3); } #alpha {