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*/ `