From 53595b93267c29ddea812751be8a12080632dec9 Mon Sep 17 00:00:00 2001 From: "Simon Genin (ges)" Date: Thu, 23 Jun 2022 11:40:15 +0000 Subject: [PATCH] [IMP] web: number parsers: fallback on english localization With this commit, if the user types something in a numeric field input that doesn't match its localization, we try to parse it with the english localization. If it works, we considerer that value. Otherwise, we throw an error. al request Part-of: odoo/odoo#92475 --- addons/web/static/src/views/fields/parsers.js | 19 +++++++++++++++++++ .../tests/search/custom_filter_item_tests.js | 4 ++-- .../tests/views/fields/parsers_tests.js | 10 ++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/views/fields/parsers.js b/addons/web/static/src/views/fields/parsers.js index 32238b9e4cf..2fe442b27e3 100644 --- a/addons/web/static/src/views/fields/parsers.js +++ b/addons/web/static/src/views/fields/parsers.js @@ -72,6 +72,15 @@ export function parseFloat(value) { thousandsSep: thousandsSepRegex, decimalPoint: decimalPointRegex, }); + if (isNaN(parsed)) { + parsed = parseNumber(value, { + thousandsSep: ",", + decimalPoint: ".", + }); + if (isNaN(parsed)) { + throw new InvalidNumberError(`"${value}" is not a correct number`); + } + } return parsed; } @@ -114,6 +123,16 @@ export function parseInteger(value) { decimalPoint: decimalPointRegex, truncate: true, }); + if (!Number.isInteger(parsed)) { + parsed = parseNumber(value, { + thousandsSep: ",", + decimalPoint: ".", + truncate: true, + }); + if (!Number.isInteger(parsed)) { + throw new InvalidNumberError(`"${value}" is not a correct number`); + } + } return parsed; } diff --git a/addons/web/static/tests/search/custom_filter_item_tests.js b/addons/web/static/tests/search/custom_filter_item_tests.js index 32355bf3890..694683ebf5b 100644 --- a/addons/web/static/tests/search/custom_filter_item_tests.js +++ b/addons/web/static/tests/search/custom_filter_item_tests.js @@ -622,12 +622,12 @@ QUnit.module("Search", (hooks) => { assert.strictEqual(floatInput.value, "4,2"); await editConditionValue(target, 0, "4,2,"); - assert.strictEqual(floatInput.value, "4,2"); + assert.strictEqual(floatInput.value, "42,0"); // because of the en localization fallback in parsers. await editConditionValue(target, 0, "DefinitelyValidFloat"); // The input here is a string, resulting in a parsing error instead of 0 - assert.strictEqual(floatInput.value, "4,2"); + assert.strictEqual(floatInput.value, "42,0"); }); QUnit.test("add custom filter with multiple values", async function (assert) { diff --git a/addons/web/static/tests/views/fields/parsers_tests.js b/addons/web/static/tests/views/fields/parsers_tests.js index 530d1ec67d6..2662fb02db7 100644 --- a/addons/web/static/tests/views/fields/parsers_tests.js +++ b/addons/web/static/tests/views/fields/parsers_tests.js @@ -127,4 +127,14 @@ QUnit.module("Fields", (hooks) => { assert.throws(() => parseMonetary("1$\u00a01", { currencyId: 1 })); assert.throws(() => parseMonetary("$\u00a012.00\u00a034", { currencyId: 3 })); }); + + QUnit.test("parsers fallback on english localisation", function (assert) { + patchWithCleanup(localization, { + decimalPoint: ",", + thousandsSep: ".", + }); + + assert.strictEqual(parseInteger("1,000,000"), 1000000); + assert.strictEqual(parseFloat("1,000,000.50"), 1000000.5); + }); });