From 030a8c8f023c7b7d580c0a7d92bf8d569b2d52aa Mon Sep 17 00:00:00 2001 From: "Walid HANNICHE (waha)" Date: Fri, 4 Nov 2022 12:05:06 +0000 Subject: [PATCH] [FIX] web: parse numebr (separator not set) Steps to reproduce: - select a language where thousands separator is not (e.g. portugese) - try editing any number input field Bug: error when parsing the new value when thousands separator is not defined Fix: fixed default value (only fallback to commas if it's not the decimal Point separator) opw-3041135 closes odoo/odoo#105312 X-original-commit: 71712acdf259aca7745a8a0b41a022d3abdcb047 Signed-off-by: Aaron Bohy (aab) --- addons/web/static/src/views/fields/parsers.js | 8 ++++---- addons/web/static/tests/views/fields/parsers_tests.js | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/addons/web/static/src/views/fields/parsers.js b/addons/web/static/src/views/fields/parsers.js index 51451cff7de..6a5eadbbf38 100644 --- a/addons/web/static/src/views/fields/parsers.js +++ b/addons/web/static/src/views/fields/parsers.js @@ -33,8 +33,8 @@ function evaluateMathematicalExpression(expr, context = {}) { * * @param {string} value * @param {Object} options - additional options - * @param {string|RegExp} [options.thousandsSep] - the thousands separator used in the value - * @param {string|RegExp} [options.decimalPoint] - the decimal point used in the value + * @param {string|RegExp} options.thousandsSep - the thousands separator used in the value + * @param {string|RegExp} options.decimalPoint - the decimal point used in the value * @returns {number} */ function parseNumber(value, options = {}) { @@ -66,7 +66,7 @@ export class InvalidNumberError extends Error {} * @returns {number} a float */ export function parseFloat(value) { - const thousandsSepRegex = localization.thousandsSep; + const thousandsSepRegex = localization.thousandsSep || ""; const decimalPointRegex = localization.decimalPoint; let parsed = parseNumber(value, { thousandsSep: thousandsSepRegex, @@ -116,7 +116,7 @@ export function parseFloatTime(value) { * @returns {number} an integer */ export function parseInteger(value) { - const thousandsSepRegex = localization.thousandsSep; + const thousandsSepRegex = localization.thousandsSep || ""; const decimalPointRegex = localization.decimalPoint; let parsed = parseNumber(value, { thousandsSep: thousandsSepRegex, diff --git a/addons/web/static/tests/views/fields/parsers_tests.js b/addons/web/static/tests/views/fields/parsers_tests.js index 2662fb02db7..95710685404 100644 --- a/addons/web/static/tests/views/fields/parsers_tests.js +++ b/addons/web/static/tests/views/fields/parsers_tests.js @@ -39,6 +39,9 @@ QUnit.module("Fields", (hooks) => { patchWithCleanup(localization, { decimalPoint: ",", thousandsSep: "." }); assert.strictEqual(parseFloat("1.234,567"), 1234.567); + + patchWithCleanup(localization, { decimalPoint: ",", thousandsSep: false }); + assert.strictEqual(parseFloat("1234,567"), 1234.567); }); QUnit.test("parseFloatTime", function (assert) { @@ -73,6 +76,9 @@ QUnit.module("Fields", (hooks) => { expectInvalidNumberError(assert, parseInteger, "1.234,567"); // fallback to en localization assert.strictEqual(parseInteger("1,000,000"), 1000000); + + patchWithCleanup(localization, { decimalPoint: ",", thousandsSep: false }); + assert.strictEqual(parseInteger("1000000"), 1000000); }); QUnit.test("parsePercentage", function (assert) {