From 808332c2c123cf0cd8ca84bf4016b8fb6354898d Mon Sep 17 00:00:00 2001 From: Moonchild Date: Tue, 18 Oct 2022 12:48:23 +0000 Subject: [PATCH] Issue #2019 - Follow-up: Make autocomplete and satchel listen to keypress events in the system event group The autocomplete module listens to keypress events for both printable keys and non-printable keys a lot. However, we're stopping dispatching keypress events for non-printable keys in the default event group of web content. This means that autocomplete should listen to keypress events in the system event group. Note that it's difficult to globally change keypress event listeners to keydown event listeners because if we stop keypress events at preceding keydown event in autocomplete or satchel modules, some other modules fail to handle keydown or keypress events before autocomplete, and it's not easy to investigate which keypress event listener in which modules should be changed to a keydown event listener. Therefore, this patch doesn't do that, and uses the event group approach. --- .../satchel/nsFormFillController.cpp | 76 ++++++++++++------- toolkit/content/widgets/autocomplete.xml | 2 +- 2 files changed, 49 insertions(+), 29 deletions(-) diff --git a/toolkit/components/satchel/nsFormFillController.cpp b/toolkit/components/satchel/nsFormFillController.cpp index a89c138fab..aac1870490 100644 --- a/toolkit/components/satchel/nsFormFillController.cpp +++ b/toolkit/components/satchel/nsFormFillController.cpp @@ -5,6 +5,7 @@ #include "nsFormFillController.h" +#include "mozilla/EventListenerManager.h" #include "mozilla/dom/Element.h" #include "mozilla/dom/Event.h" // for nsIDOMEvent::InternalDOMEvent() #include "nsIFormAutoComplete.h" @@ -40,6 +41,7 @@ #include "nsIScriptSecurityManager.h" #include "nsFocusManager.h" +using namespace mozilla; using namespace mozilla::dom; NS_IMPL_CYCLE_COLLECTION(nsFormFillController, @@ -1188,23 +1190,29 @@ nsFormFillController::AddWindowListeners(nsPIDOMWindowOuter* aWindow) if (!target) return; - target->AddEventListener(NS_LITERAL_STRING("focus"), this, - true, false); - target->AddEventListener(NS_LITERAL_STRING("blur"), this, - true, false); - target->AddEventListener(NS_LITERAL_STRING("pagehide"), this, - true, false); - target->AddEventListener(NS_LITERAL_STRING("mousedown"), this, - true, false); - target->AddEventListener(NS_LITERAL_STRING("input"), this, - true, false); - target->AddEventListener(NS_LITERAL_STRING("keypress"), this, true, false); - target->AddEventListener(NS_LITERAL_STRING("compositionstart"), this, - true, false); - target->AddEventListener(NS_LITERAL_STRING("compositionend"), this, - true, false); - target->AddEventListener(NS_LITERAL_STRING("contextmenu"), this, - true, false); + EventListenerManager* elm = target->GetOrCreateListenerManager(); + if (NS_WARN_IF(!elm)) { + return; + } + + elm->AddEventListenerByType(this, NS_LITERAL_STRING("focus"), + TrustedEventsAtCapture()); + elm->AddEventListenerByType(this, NS_LITERAL_STRING("blur"), + TrustedEventsAtCapture()); + elm->AddEventListenerByType(this, NS_LITERAL_STRING("pagehide"), + TrustedEventsAtCapture()); + elm->AddEventListenerByType(this, NS_LITERAL_STRING("mousedown"), + TrustedEventsAtCapture()); + elm->AddEventListenerByType(this, NS_LITERAL_STRING("input"), + TrustedEventsAtCapture()); + elm->AddEventListenerByType(this, NS_LITERAL_STRING("keypress"), + TrustedEventsAtSystemGroupCapture()); + elm->AddEventListenerByType(this, NS_LITERAL_STRING("compositionstart"), + TrustedEventsAtCapture()); + elm->AddEventListenerByType(this, NS_LITERAL_STRING("compositionend"), + TrustedEventsAtCapture()); + elm->AddEventListenerByType(this, NS_LITERAL_STRING("contextmenu"), + TrustedEventsAtCapture()); // Note that any additional listeners added should ensure that they ignore // untrusted events, which might be sent by content that's up to no good. @@ -1226,17 +1234,29 @@ nsFormFillController::RemoveWindowListeners(nsPIDOMWindowOuter* aWindow) if (!target) return; - target->RemoveEventListener(NS_LITERAL_STRING("focus"), this, true); - target->RemoveEventListener(NS_LITERAL_STRING("blur"), this, true); - target->RemoveEventListener(NS_LITERAL_STRING("pagehide"), this, true); - target->RemoveEventListener(NS_LITERAL_STRING("mousedown"), this, true); - target->RemoveEventListener(NS_LITERAL_STRING("input"), this, true); - target->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, true); - target->RemoveEventListener(NS_LITERAL_STRING("compositionstart"), this, - true); - target->RemoveEventListener(NS_LITERAL_STRING("compositionend"), this, - true); - target->RemoveEventListener(NS_LITERAL_STRING("contextmenu"), this, true); + EventListenerManager* elm = target->GetOrCreateListenerManager(); + if (NS_WARN_IF(!elm)) { + return; + } + + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("focus"), + TrustedEventsAtCapture()); + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("blur"), + TrustedEventsAtCapture()); + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("pagehide"), + TrustedEventsAtCapture()); + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("mousedown"), + TrustedEventsAtCapture()); + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("input"), + TrustedEventsAtCapture()); + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("keypress"), + TrustedEventsAtSystemGroupCapture()); + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("compositionstart"), + TrustedEventsAtCapture()); + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("compositionend"), + TrustedEventsAtCapture()); + elm->RemoveEventListenerByType(this, NS_LITERAL_STRING("contextmenu"), + TrustedEventsAtCapture()); } void diff --git a/toolkit/content/widgets/autocomplete.xml b/toolkit/content/widgets/autocomplete.xml index 885eb2eab4..d9160956c8 100644 --- a/toolkit/content/widgets/autocomplete.xml +++ b/toolkit/content/widgets/autocomplete.xml @@ -648,7 +648,7 @@ this.onInput(event); ]]> -