commit ba199a3da738aadd2cd5312643876c6fac473fa6
parent 3f346719e60d7ce37c38f0e30e2731a2076e6bde
Author: Anna Kulyk <akulyk@mozilla.com>
Date: Fri, 3 Oct 2025 15:15:19 +0000
Bug 1987527 - Part 1: Setting value should always get set on its setting control when it changes r=mkennedy
Differential Revision: https://phabricator.services.mozilla.com/D267021
Diffstat:
2 files changed, 40 insertions(+), 22 deletions(-)
diff --git a/browser/components/preferences/tests/chrome/test_setting_control_checkbox.html b/browser/components/preferences/tests/chrome/test_setting_control_checkbox.html
@@ -131,6 +131,31 @@
);
});
+ add_task(async function testSettingSameControlValue() {
+ const SETTING = "setting-control-same-value";
+ Preferences.addSetting({
+ id: SETTING,
+ get: () => false,
+ set: () => false,
+ });
+ let itemConfig = {
+ l10nId: LABEL_L10N_ID,
+ id: SETTING,
+ };
+ let setting = Preferences.getSetting(SETTING);
+ let control = await renderTemplate(itemConfig);
+ ok(control, "Got a control");
+ let checkbox = control.controlEl;
+ is(checkbox.localName, "moz-checkbox", "moz-checkbox is rendered");
+ is(checkbox.checked, false, "checkbox is unchecked on initial render");
+
+ let settingChanged = waitForSettingChange(setting);
+ synthesizeMouseAtCenter(checkbox, {});
+ setting.emit("change");
+ await settingChanged;
+ is(checkbox.checked, false, "checkbox stays unchecked after click");
+ });
+
add_task(async function testSupportLinkCheckbox() {
const SETTING = "setting-control-support-link";
Preferences.addSetting({
diff --git a/browser/components/preferences/widgets/setting-control/setting-control.mjs b/browser/components/preferences/widgets/setting-control/setting-control.mjs
@@ -101,17 +101,6 @@ class SpreadDirective extends Directive {
const spread = directive(SpreadDirective);
/**
- * @type Map<string, HTMLElement>
- */
-const controlInstances = new Map();
-function getControlInstance(control = "moz-checkbox") {
- if (!controlInstances.has(control)) {
- controlInstances.set(control, document.createElement(control));
- }
- return controlInstances.get(control);
-}
-
-/**
* Mapping of parent control tag names to the literal tag name for their
* expected children. eg. "moz-radio-group"->literal`moz-radio`.
* @type Map<string, literal>
@@ -216,7 +205,21 @@ export class SettingControl extends MozLitElement {
}
updated() {
- this.controlRef?.value?.requestUpdate();
+ const control = this.controlRef?.value;
+ if (!control) {
+ return;
+ }
+
+ // Set the value based on the control's API.
+ if ("checked" in control) {
+ control.checked = this.value;
+ } else if ("pressed" in control) {
+ control.pressed = this.value;
+ } else if ("value" in control) {
+ control.value = this.value;
+ }
+
+ control.requestUpdate();
}
/**
@@ -264,16 +267,6 @@ export class SettingControl extends MozLitElement {
this.setting.locked ||
this.isControlledByExtension();
- // Set the value based on the control's API.
- let instance = getControlInstance(config.control);
- if ("checked" in instance) {
- props[".checked"] = this.value;
- } else if ("pressed" in instance) {
- props[".pressed"] = this.value;
- } else if ("value" in instance) {
- props[".value"] = this.value;
- }
-
return props;
}