Skip to content

Commit de2e398

Browse files
bmeurerDevtools-frontend LUCI CQ
authored andcommitted
[ui] Make TreeOutline listen to "keyup" instead of "keydown".
The primary motivation for this is to allow the processing of "click" events generated from "Enter" key presses in elements within a TreeOutline. Before this CL, the "keydown" handler in the TreeOutline would always consume the "Enter" key before the child element gets a chance to turn that into a "click" event. There's a secondary reason however, which is that up-events should be preferred for accessibility reasons, see [^1] which describes this in the context of so-called pointer device buttons, but the same applies to keyboards as well. [^1]: https://www.w3.org/WAI/WCAG21/Understanding/pointer-cancellation Fixed: 373168872 Change-Id: I3b82e1fde221383279ee6a690af3e436fcc98033 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5939709 Auto-Submit: Benedikt Meurer <[email protected]> Reviewed-by: Changhao Han <[email protected]> Commit-Queue: Changhao Han <[email protected]>
1 parent b6af601 commit de2e398

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

front_end/testing/DOMHelpers.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,17 @@ export function dispatchKeyDownEvent<T extends Element>(element: T, options: Key
163163
}
164164
}
165165

166+
/**
167+
* Dispatches a keyup event. Errors if the event was not dispatched successfully.
168+
*/
169+
export function dispatchKeyUpEvent<T extends Element>(element: T, options: KeyboardEventInit = {}) {
170+
const clickEvent = new KeyboardEvent('keyup', options);
171+
const success = element.dispatchEvent(clickEvent);
172+
if (!success) {
173+
assert.fail('Failed to trigger keydown event successfully.');
174+
}
175+
}
176+
166177
export function dispatchInputEvent<T extends Element>(element: T, options: InputEventInit = {}) {
167178
const inputEvent = new InputEvent('input', options);
168179
element.dispatchEvent(inputEvent);

front_end/ui/legacy/Treeoutline.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// found in the LICENSE file.
44

55
import * as Platform from '../../core/platform/platform.js';
6-
import {dispatchKeyDownEvent, renderElementIntoDOM} from '../../testing/DOMHelpers.js';
6+
import {dispatchKeyUpEvent, renderElementIntoDOM} from '../../testing/DOMHelpers.js';
77

88
import * as UI from './legacy.js';
99

@@ -18,7 +18,7 @@ describe('TreeOutline', () => {
1818
tree.appendChild(parent);
1919
parent.select();
2020

21-
dispatchKeyDownEvent(tree.contentElement, {bubbles: true, key: 'Enter'});
21+
dispatchKeyUpEvent(tree.contentElement, {bubbles: true, key: 'Enter'});
2222
assert.isTrue(parent.expanded, 'Enter key was supposed to expand the parent node');
2323
});
2424

@@ -32,7 +32,7 @@ describe('TreeOutline', () => {
3232
parent.select();
3333
parent.expand();
3434

35-
dispatchKeyDownEvent(tree.contentElement, {bubbles: true, key: 'Enter'});
35+
dispatchKeyUpEvent(tree.contentElement, {bubbles: true, key: 'Enter'});
3636
assert.isFalse(parent.expanded, 'Enter key was supposed to collapse the parent node');
3737
});
3838
});
@@ -106,7 +106,7 @@ describe('TreeOutline', () => {
106106

107107
function sendKey(key: string) {
108108
const deepActiveElement = Platform.DOMUtilities.deepActiveElement(document);
109-
deepActiveElement!.dispatchEvent(new KeyboardEvent('keydown', {bubbles: true, cancelable: true, key}));
109+
deepActiveElement!.dispatchEvent(new KeyboardEvent('keyup', {bubbles: true, cancelable: true, key}));
110110
}
111111
});
112112
});

front_end/ui/legacy/Treeoutline.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ export class TreeOutline extends Common.ObjectWrapper.ObjectWrapper<EventTypes>
9797
this.comparator = null;
9898

9999
this.contentElement = this.rootElementInternal.childrenListNode;
100-
this.contentElement.addEventListener('keydown', this.treeKeyDown.bind(this), false);
100+
this.contentElement.addEventListener('keyup', this.treeKeyUp.bind(this), false);
101101

102102
this.preventTabOrder = false;
103103
this.showSelectionOnKeyboardFocus = false;
@@ -293,7 +293,7 @@ export class TreeOutline extends Common.ObjectWrapper.ObjectWrapper<EventTypes>
293293
return true;
294294
}
295295

296-
private treeKeyDown(event: KeyboardEvent): void {
296+
private treeKeyUp(event: KeyboardEvent): void {
297297
if (event.shiftKey || event.metaKey || event.ctrlKey || isEditing()) {
298298
return;
299299
}

0 commit comments

Comments
 (0)