diff --git a/addons/web/static/src/legacy/js/fields/field_utils.js b/addons/web/static/src/legacy/js/fields/field_utils.js index f91c51fcb2c..aaec21709b9 100644 --- a/addons/web/static/src/legacy/js/fields/field_utils.js +++ b/addons/web/static/src/legacy/js/fields/field_utils.js @@ -23,6 +23,8 @@ var utils = require('web.utils'); var _t = core._t; +const NBSP = "\u00a0"; + //------------------------------------------------------------------------------ // Formatting //------------------------------------------------------------------------------ @@ -330,8 +332,12 @@ function formatX2Many(value) { * digits precision in the field. Note: if the currency defines a * precision, the currency's one is used. * @param {boolean} [options.forceString=false] - * if false, returns a string encoding the html formatted value (with - * whitespace encoded as ' ') + * if true, returns a string with regular whitespace. Otherwise it uses + * non-breaking whitespace unicode character. The option is presented for + * historical reason and will be removed in master. Previous + * implementation used html entity ` `, which doesn't work in html + * attributes. With new implementation we can always use the unicode + * character and the option is not needed anymore. * @returns {string | _Markup} */ function formatMonetary(value, field, options) { @@ -367,7 +373,7 @@ function formatMonetary(value, field, options) { if (options.forceString) { return val.join(' '); } - return utils.Markup(val.map((v) => _.escape(v)).join(' ')); + return utils.Markup(val.map((v) => _.escape(v)).join(NBSP)); } /** * Returns a string representing the given value (multiplied by 100) @@ -611,6 +617,9 @@ function parseFloat(value) { */ function parseMonetary(value, field, options) { var values = value.split(' '); + if (values.length === 1) { + values = value.split(NBSP); + } if (values.length === 1) { return parseFloat(value); } diff --git a/addons/web/static/tests/legacy/fields/basic_fields_tests.js b/addons/web/static/tests/legacy/fields/basic_fields_tests.js index 1b9cf556467..44cb9bb3980 100644 --- a/addons/web/static/tests/legacy/fields/basic_fields_tests.js +++ b/addons/web/static/tests/legacy/fields/basic_fields_tests.js @@ -58,6 +58,7 @@ QUnit.module('Legacy basic_fields', { txt: {string: "txt", type: "text", default: "My little txt Value\nHo-ho-hoooo Merry Christmas"}, int_field: {string: "int_field", type: "integer", sortable: true, searchable: true}, qux: {string: "Qux", type: "float", digits: [16,1], searchable: true}, + monetary: {string: "MMM", type: "monetary", digits: [16,1], searchable: true}, p: {string: "one2many field", type: "one2many", relation: 'partner', searchable: true}, trululu: {string: "Trululu", type: "many2one", relation: 'partner', searchable: true}, timmy: {string: "pokemon", type: "many2many", relation: 'partner_type', searchable: true}, @@ -107,7 +108,17 @@ QUnit.module('Legacy basic_fields', { selection: 'done', }, {id: 3, bar: true, foo: "gnap", int_field: 80, qux: -3.89859}, - {id: 5, bar: false, foo: "blop", int_field: -4, qux: 9.1, currency_id: 1}], + {id: 5, bar: false, foo: "blop", int_field: -4, qux: 9.1, currency_id: 1, monetary: 99.9}], + onchanges: {}, + }, + team: { + fields: { + partner_ids: { string: "Partner", type: "one2many", relation: 'partner' }, + }, + records: [{ + id: 1, + partner_ids: [5], + }], onchanges: {}, }, product: { @@ -5771,6 +5782,46 @@ QUnit.module('Legacy basic_fields', { form.destroy(); }); + QUnit.test('monetary field in the lines of a form view', async function (assert) { + assert.expect(4); + + var form = await createView({ + View: FormView, + model: 'team', + data: this.data, + arch:'
', + res_id: 1, + session: { + currencies: _.indexBy(this.data.currency.records, 'id'), + }, + }); + + // Non-breaking space between the currency and the amount + assert.strictEqual(form.$('.o_list_table .o_list_number').first().text(), '$\u00a099.9', + 'The value should be displayed properly.'); + assert.hasAttrValue(form.$('.o_list_table .o_list_number').first(), 'title', '$\u00a099.9', + 'The title value should be displayed properly.'); + + await testUtils.form.clickEdit(form); + + // Non-breaking space between the currency and the amount + assert.strictEqual(form.$('.o_list_table .o_list_number').first().text(), '$\u00a099.9', + 'The value should be displayed properly.'); + assert.hasAttrValue(form.$('.o_list_table .o_list_number').first(), 'title', '$\u00a099.9', + 'The title value should be displayed properly.'); + + form.destroy(); + }); + QUnit.test('monetary field rounding using formula in form view', async function (assert) { assert.expect(1);