Bug 1726454 - Make nsGenericHTMLElement::HandleKeyboardActivation handle spacebar key press correctly. r=masayuki

This prevents <summary> from scrolling with the space bar. Add a test
for that.

Differential Revision: https://phabricator.services.mozilla.com/D158517
This commit is contained in:
Emilio Cobos Álvarez 2022-10-04 10:15:20 +00:00
Родитель 399f0690f9
Коммит 7ddd987dbe
3 изменённых файлов: 73 добавлений и 6 удалений

Просмотреть файл

@ -2262,19 +2262,19 @@ void nsGenericHTMLElement::HandleKeyboardActivation(
bool shouldActivate = false;
switch (message) {
case eKeyDown:
if (keyEvent->mKeyCode == NS_VK_SPACE) {
if (keyEvent->ShouldWorkAsSpaceKey()) {
SetFlags(HTML_ELEMENT_ACTIVE_FOR_KEYBOARD);
}
return;
case eKeyPress:
shouldActivate = keyEvent->mKeyCode == NS_VK_RETURN;
if (keyEvent->mKeyCode == NS_VK_SPACE) {
if (keyEvent->ShouldWorkAsSpaceKey()) {
// Consume 'space' key to prevent scrolling the page down.
aVisitor.mEventStatus = nsEventStatus_eConsumeNoDefault;
}
break;
case eKeyUp:
shouldActivate = keyEvent->mKeyCode == NS_VK_SPACE &&
shouldActivate = keyEvent->ShouldWorkAsSpaceKey() &&
HasFlag(HTML_ELEMENT_ACTIVE_FOR_KEYBOARD);
if (shouldActivate) {
UnsetFlags(HTML_ELEMENT_ACTIVE_FOR_KEYBOARD);

Просмотреть файл

@ -0,0 +1,55 @@
<!doctype html>
<meta charset="utf-8">
<title>Details activation with space bar</title>
<link rel="author" href="mailto:emilio@crisal.io" title="Emilio Cobos Álvarez">
<link rel="author" href="https://mozilla.org" title="Mozilla">
<link rel="help" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1726454">
<script src=/resources/testharness.js></script>
<script src=/resources/testharnessreport.js></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-actions.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<style>
:root {
scroll-behavior: instant;
}
.spacer {
height: 200vh;
}
</style>
<div class="spacer"></div>
<details>
<summary>Activate me with the <kbd>Space</kbd> key</summary>
<p>Summary</p>
</details>
<div class="spacer"></div>
<script>
function tick() {
return new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
}
promise_test(async t => {
const details = document.querySelector("details");
const summary = details.querySelector("summary");
summary.focus();
assert_equals(document.activeElement, summary, "Summary should be focusable");
assert_false(details.open, "Details should be closed");
await tick();
const oldScrollY = window.scrollY;
assert_true(isFinite(oldScrollY), "Should be able to get vertical scroll position");
window.addEventListener("scroll", t.unreached_func("Unexpected scroll event"));
await test_driver.send_keys(summary, " ");
assert_true(details.open, "Space bar on summary should open details");
assert_equals(window.scrollY, oldScrollY, "Scroll position shouldn't change");
await tick();
assert_equals(window.scrollY, oldScrollY, "Scroll position shouldn't change");
});
</script>

Просмотреть файл

@ -124,7 +124,7 @@ struct IgnoreModifierState {
* mozilla::WidgetKeyboardEvent
******************************************************************************/
class WidgetKeyboardEvent : public WidgetInputEvent {
class WidgetKeyboardEvent final : public WidgetInputEvent {
private:
friend class dom::PBrowserParent;
friend class dom::PBrowserChild;
@ -150,7 +150,7 @@ class WidgetKeyboardEvent : public WidgetInputEvent {
mEditCommandsForRichTextEditorInitialized(false) {}
public:
virtual WidgetKeyboardEvent* AsKeyboardEvent() override { return this; }
WidgetKeyboardEvent* AsKeyboardEvent() override { return this; }
WidgetKeyboardEvent(bool aIsTrusted, EventMessage aMessage,
nsIWidget* aWidget,
@ -252,7 +252,7 @@ class WidgetKeyboardEvent : public WidgetInputEvent {
(MODIFIER_ALT | MODIFIER_META | MODIFIER_OS | MODIFIER_SHIFT));
}
virtual WidgetEvent* Duplicate() const override {
WidgetEvent* Duplicate() const override {
MOZ_ASSERT(mClass == eKeyboardEventClass,
"Duplicate() must be overridden by sub class");
// Not copying widget, it is a weak reference.
@ -294,6 +294,18 @@ class WidgetKeyboardEvent : public WidgetInputEvent {
IsAccel()));
}
[[nodiscard]] bool ShouldWorkAsSpaceKey() const {
if (mKeyCode == NS_VK_SPACE) {
return true;
}
// Additionally, if the code value is "Space" and the key is not mapped to
// a function key (i.e., not a printable key), we should treat it as space
// key because the active keyboard layout may input different character
// from the ASCII white space (U+0020). For example, NBSP (U+00A0).
return mKeyNameIndex == KEY_NAME_INDEX_USE_STRING &&
mCodeNameIndex == CODE_NAME_INDEX_Space;
}
/**
* CanTreatAsUserInput() returns true if the key is pressed for perhaps
* doing something on the web app or our UI. This means that when this