diff --git a/toolkit/content/tests/widgets/test_moz_base_input_element.html b/toolkit/content/tests/widgets/test_moz_base_input_element.html index 34f561683ad6..fd9d78458ac2 100644 --- a/toolkit/content/tests/widgets/test_moz_base_input_element.html +++ b/toolkit/content/tests/widgets/test_moz_base_input_element.html @@ -93,6 +93,29 @@ "support link created" ); }); + + add_task(async function testLabelChange() { + let el = await renderTemplate(); + + let labelEl = el.shadowRoot.querySelector("label"); + is(labelEl.innerText.trim(), "Example label!", "Label is correct"); + is(labelEl.getAttribute("shownaccesskey"), "x", "Accesskey is shown"); + + // Test changing the label text, this could happen when the locale changes. + el.label = "Example new label!"; + await el.updateComplete; + + is( + labelEl.innerText.trim(), + "Example new label!", + "Label is updated correctly" + ); + is( + labelEl.getAttribute("shownaccesskey"), + "x", + "Accesskey is still shown" + ); + }); diff --git a/toolkit/content/tests/widgets/test_moz_label.html b/toolkit/content/tests/widgets/test_moz_label.html index 8d76d1743b0a..7cc3b65e6443 100644 --- a/toolkit/content/tests/widgets/test_moz_label.html +++ b/toolkit/content/tests/widgets/test_moz_label.html @@ -48,6 +48,13 @@ Shownaccesskey highlights the key + + + diff --git a/toolkit/content/widgets/lit-utils.mjs b/toolkit/content/widgets/lit-utils.mjs index 6fb31e633bfe..747020beb4ae 100644 --- a/toolkit/content/widgets/lit-utils.mjs +++ b/toolkit/content/widgets/lit-utils.mjs @@ -419,7 +419,12 @@ export class MozBaseInputElement extends MozLitElement { if (!this.label) { return ""; } - return html`${this.iconTemplate()}${this.label}`; + return html`${this.iconTemplate()}`; } descriptionTemplate() { diff --git a/toolkit/content/widgets/moz-label/moz-label.mjs b/toolkit/content/widgets/moz-label/moz-label.mjs index 56c925ad6a05..d7f5787c7be2 100644 --- a/toolkit/content/widgets/moz-label/moz-label.mjs +++ b/toolkit/content/widgets/moz-label/moz-label.mjs @@ -65,19 +65,39 @@ class MozTextLabel extends HTMLLabelElement { } } + #startMutationObserver() { + if (!this.#observer) { + return; + } + this.#observer.observe(this, { + characterData: true, + childList: true, + subtree: true, + }); + } + + #stopMutationObserver() { + if (!this.#observer) { + return; + } + this.#observer.disconnect(); + } + connectedCallback() { this.#setStyles(); this.formatAccessKey(); if (!this.#observer) { this.#observer = new MutationObserver(() => { + this.#lastFormattedAccessKey = null; this.formatAccessKey(); - }).observe(this, { characterData: true, childList: true, subtree: true }); + }); + this.#startMutationObserver(); } } disconnectedCallback() { if (this.#observer) { - this.#observer.disconnect(); + this.#stopMutationObserver(); this.#observer = null; } } @@ -185,6 +205,15 @@ class MozTextLabel extends HTMLLabelElement { ) { return; } + this.#stopMutationObserver(); + try { + this.#formatAccessKey(accessKey); + } finally { + queueMicrotask(() => this.#startMutationObserver()); + } + } + + #formatAccessKey(accessKey) { this.#lastFormattedAccessKey = accessKey; if (this.accessKeySpan) { // Clear old accesskey