commit eaec21428869c7fbab2ce135ca2c83605e058a08
parent 27e17af20670fe47d0b34083b6819017ae25cac7
Author: Mark Striemer <mstriemer@mozilla.com>
Date: Thu, 27 Nov 2025 23:46:48 +0000
Bug 2001746 - Don't throw in moz-box-group on keypresses without a valid focus target r=akulyk
Differential Revision: https://phabricator.services.mozilla.com/D273692
Diffstat:
2 files changed, 59 insertions(+), 4 deletions(-)
diff --git a/toolkit/content/tests/widgets/test_moz_box_group.html b/toolkit/content/tests/widgets/test_moz_box_group.html
@@ -542,6 +542,57 @@
await SpecialPowers.popPrefEnv();
});
+ add_task(async function testMozBoxGroupKeydownOnWhitespace() {
+ // Test bug 2001746 - ignore keypresses that fire on the ul if whitespace
+ // was clicked in moz-box-group
+ let listTemplate = html`<moz-box-group type="list">
+ <moz-box-item label="item 1"></moz-box-item>
+ <moz-box-item label="item 2"></moz-box-item>
+ <moz-box-item label="item 3"></moz-box-item>
+ </moz-box-group>`;
+ let {
+ children: [boxGroup],
+ } = await testHelpers.renderTemplate(listTemplate);
+
+ let list = boxGroup.shadowRoot.querySelector("ul");
+ ok(list, "Box group has a list element.");
+
+ // Simulate clicking on the list container (whitespace) by focusing it
+ list.focus();
+ is(
+ boxGroup.shadowRoot.activeElement,
+ list,
+ "The list container can be focused."
+ );
+ is(
+ document.activeElement,
+ boxGroup,
+ "The moz-box-group is the activeElement."
+ );
+
+ // Press arrow keys - this should not throw an error
+ let errorThrown = false;
+ try {
+ synthesizeKey("KEY_ArrowDown");
+ await boxGroup.updateComplete;
+ synthesizeKey("KEY_ArrowUp");
+ await boxGroup.updateComplete;
+ } catch (e) {
+ errorThrown = true;
+ ok(false, `Unexpected error: ${e.message}`);
+ }
+
+ ok(
+ !errorThrown,
+ "No error thrown when pressing keys on whitespace in moz-box-group."
+ );
+ is(
+ document.activeElement,
+ boxGroup,
+ "Focus did not move after arrow key press."
+ );
+ });
+
add_task(async function testMozBoxGroupReorderable() {
let reorderableTemplate = html`<moz-box-group type="reorderable-list">
<moz-box-item label="item 1"></moz-box-item>
diff --git a/toolkit/content/widgets/moz-box-group/moz-box-group.mjs b/toolkit/content/widgets/moz-box-group/moz-box-group.mjs
@@ -121,10 +121,14 @@ export default class MozBoxGroup extends MozLitElement {
}
}
- let positionAttr =
- event.target.getAttribute("position") ??
- // handles the case where an interactive element is nested in a moz-box-item
- event.target.closest("[position]").getAttribute("position");
+ let positionElement = event.target.closest("[position]");
+ if (!positionElement) {
+ // If the user has clicked on the MozBoxGroup it may get keydown events
+ // even if there is no focused element within it. Then the event target
+ // will be the <ul> and we won't find an element with [position].
+ return;
+ }
+ let positionAttr = positionElement.getAttribute("position");
let currentPosition = parseInt(positionAttr);
switch (event.key) {