From e8efa6e0335bcbc59eed323c1b365cafef5d2834 Mon Sep 17 00:00:00 2001 From: jurcovicovam Date: Mon, 20 Jul 2015 14:31:53 +0200 Subject: [PATCH] Percentage should work the same way as other math functions (round, floor, etc) and throw error on NaN. #2553 --- lib/less/functions/math-helper.js | 16 +++++++++++++++ lib/less/functions/math.js | 20 ++++--------------- lib/less/functions/number.js | 9 +++++++-- .../percentage-non-number-argument.less | 3 +++ .../errors/percentage-non-number-argument.txt | 4 ++++ 5 files changed, 34 insertions(+), 18 deletions(-) create mode 100644 lib/less/functions/math-helper.js create mode 100644 test/less/errors/percentage-non-number-argument.less create mode 100644 test/less/errors/percentage-non-number-argument.txt diff --git a/lib/less/functions/math-helper.js b/lib/less/functions/math-helper.js new file mode 100644 index 000000000..440017c65 --- /dev/null +++ b/lib/less/functions/math-helper.js @@ -0,0 +1,16 @@ +var Dimension = require("../tree/dimension"); + +var MathHelper = function() { +}; +MathHelper._math = function (fn, unit, n) { + if (!(n instanceof Dimension)) { + throw { type: "Argument", message: "argument must be a number" }; + } + if (unit == null) { + unit = n.unit; + } else { + n = n.unify(); + } + return new Dimension(fn(parseFloat(n.value)), unit); +}; +module.exports = MathHelper; \ No newline at end of file diff --git a/lib/less/functions/math.js b/lib/less/functions/math.js index 069ad6b58..fc50423a6 100644 --- a/lib/less/functions/math.js +++ b/lib/less/functions/math.js @@ -1,5 +1,5 @@ -var Dimension = require("../tree/dimension"), - functionRegistry = require("./function-registry"); +var functionRegistry = require("./function-registry"), + mathHelper = require("./math-helper.js"); var mathFunctions = { // name, unit @@ -15,27 +15,15 @@ var mathFunctions = { acos: "rad" }; -function _math(fn, unit, n) { - if (!(n instanceof Dimension)) { - throw { type: "Argument", message: "argument must be a number" }; - } - if (unit == null) { - unit = n.unit; - } else { - n = n.unify(); - } - return new Dimension(fn(parseFloat(n.value)), unit); -} - for (var f in mathFunctions) { if (mathFunctions.hasOwnProperty(f)) { - mathFunctions[f] = _math.bind(null, Math[f], mathFunctions[f]); + mathFunctions[f] = mathHelper._math.bind(null, Math[f], mathFunctions[f]); } } mathFunctions.round = function (n, f) { var fraction = typeof f === "undefined" ? 0 : f.value; - return _math(function(num) { return num.toFixed(fraction); }, null, n); + return mathHelper._math(function(num) { return num.toFixed(fraction); }, null, n); }; functionRegistry.addMultiple(mathFunctions); diff --git a/lib/less/functions/number.js b/lib/less/functions/number.js index d5490347a..359d1725e 100644 --- a/lib/less/functions/number.js +++ b/lib/less/functions/number.js @@ -1,6 +1,7 @@ var Dimension = require("../tree/dimension"), Anonymous = require("../tree/anonymous"), - functionRegistry = require("./function-registry"); + functionRegistry = require("./function-registry"), + mathHelper = require("./math-helper.js"); var minMax = function (isMin, args) { args = Array.prototype.slice.call(args); @@ -71,6 +72,10 @@ functionRegistry.addMultiple({ return new Dimension(Math.pow(x.value, y.value), x.unit); }, percentage: function (n) { - return new Dimension(n.value * 100, '%'); + var result = mathHelper._math(function(num) { + return num * 100; + }, '%', n); + + return result; } }); diff --git a/test/less/errors/percentage-non-number-argument.less b/test/less/errors/percentage-non-number-argument.less new file mode 100644 index 000000000..d6c6cf64c --- /dev/null +++ b/test/less/errors/percentage-non-number-argument.less @@ -0,0 +1,3 @@ +div { + percentage: percentage(16/17); +} diff --git a/test/less/errors/percentage-non-number-argument.txt b/test/less/errors/percentage-non-number-argument.txt new file mode 100644 index 000000000..f01a1e546 --- /dev/null +++ b/test/less/errors/percentage-non-number-argument.txt @@ -0,0 +1,4 @@ +ArgumentError: error evaluating function `percentage`: argument must be a number in {path}percentage-non-number-argument.less on line 2, column 15: +1 div { +2 percentage: percentage(16/17); +3 }