From bdabc55280e78025ea29299095cb526b9297c086 Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Thu, 18 Aug 2022 05:44:30 +0000 Subject: [PATCH] [FIX] web: fix label with empty string rendering with default label In legacy, when you have a label with a string attribute that is empty, that label is rendered as empty. In the new form view, when the string attribute was empty we would fall back to the default label for that field, which is incorrect. This commit fixes that by not rendering labels that have an empty string at all. closes odoo/odoo#98237 Signed-off-by: Mathieu Duckerts-Antoine --- .../static/src/views/form/form_compiler.js | 15 +++- .../settings_form_compiler.js | 2 +- .../static/tests/legacy/views/form_tests.js | 23 ++++++ .../tests/views/form/form_compiler_tests.js | 11 +++ .../tests/views/form/form_view_tests.js | 73 ++++++++++++++++++- .../settings_form_view_tests.js | 53 ++++++++++++++ 6 files changed, 173 insertions(+), 4 deletions(-) diff --git a/addons/web/static/src/views/form/form_compiler.js b/addons/web/static/src/views/form/form_compiler.js index a95ce979535..cfdc71d85dd 100644 --- a/addons/web/static/src/views/form/form_compiler.js +++ b/addons/web/static/src/views/form/form_compiler.js @@ -79,6 +79,9 @@ export class FormCompiler extends ViewCompiler { fieldInfo: `props.archInfo.fieldNodes['${fieldId}']`, className: `"${label.className}"`, }; + if (label.hasAttribute("data-no-label")) { + return; + } let labelText = label.textContent || fieldString; labelText = labelText ? toStringExpression(labelText) @@ -173,7 +176,11 @@ export class FormCompiler extends ViewCompiler { label, params ); - label.replaceWith(formLabel); + if (formLabel) { + label.replaceWith(formLabel); + } else { + label.remove(); + } return formLabel; }; for (const label of labels) { @@ -405,6 +412,8 @@ export class FormCompiler extends ViewCompiler { const string = el.getAttribute("string"); if (string) { append(label, createTextNode(string)); + } else if (string === "") { + label.setAttribute("data-no-label", "true"); } if (this.encounteredFields[forAttr]) { label = this.encounteredFields[forAttr](label); @@ -461,7 +470,9 @@ export class FormCompiler extends ViewCompiler { for (const anchor of child.querySelectorAll("[href^=\\#]")) { const anchorValue = CSS.escape(anchor.getAttribute("href").substring(1)); - if (!anchorValue.length) continue; + if (!anchorValue.length) { + continue; + } pageAnchors.push(anchorValue); noteBookAnchors[anchorValue] = { origin: `'${pageId}'`, diff --git a/addons/web/static/src/webclient/settings_form_view/settings_form_compiler.js b/addons/web/static/src/webclient/settings_form_view/settings_form_compiler.js index 3af48890c34..a861bda7262 100644 --- a/addons/web/static/src/webclient/settings_form_view/settings_form_compiler.js +++ b/addons/web/static/src/webclient/settings_form_view/settings_form_compiler.js @@ -247,7 +247,7 @@ export class SettingsFormCompiler extends FormCompiler { label.textContent = labelweak.textContent; } const res = super.createLabelFromField(fieldId, fieldName, fieldString, label, params); - if (labelweak) { + if (labelweak || label.hasAttribute("data-no-label")) { // the work of pushing the label in the search structure is already done return res; } diff --git a/addons/web/static/tests/legacy/views/form_tests.js b/addons/web/static/tests/legacy/views/form_tests.js index 9480ebef5ec..d3504e29266 100644 --- a/addons/web/static/tests/legacy/views/form_tests.js +++ b/addons/web/static/tests/legacy/views/form_tests.js @@ -1419,6 +1419,29 @@ QUnit.module('LegacyViews', { form.destroy(); }); + QUnit.test("label ignores the content of the label when present", async function (assert) { + await createView({ + View: FormView, + model: 'partner', + data: this.data, + arch: ` +
+ + + +
+ +
+
+
+
`, + res_id: 2, + }); + + assert.containsOnce(target, "label.o_form_label"); + assert.strictEqual(target.querySelector("label.o_form_label").textContent, "Bar"); + }); + QUnit.test('input ids for multiple occurrences of fields in form view', async function (assert) { // A same field can occur several times in the view, but its id must be // unique by occurrence, otherwise there is a warning in the console (in diff --git a/addons/web/static/tests/views/form/form_compiler_tests.js b/addons/web/static/tests/views/form/form_compiler_tests.js index 7f4c7b4c00a..3f4a09d60fe 100644 --- a/addons/web/static/tests/views/form/form_compiler_tests.js +++ b/addons/web/static/tests/views/form/form_compiler_tests.js @@ -41,6 +41,17 @@ QUnit.module("Form Compiler", () => { assert.areEquivalent(compileTemplate(arch), expected); }); + QUnit.test("label with empty string is not rendered", async (assert) => { + const arch = /*xml*/ `