diff options
author | Moonchild <moonchild@palemoon.org> | 2022-10-23 21:45:26 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2022-10-23 21:45:26 +0000 |
commit | 2d752c925976cc983885d8f96b58a45dd5e31133 (patch) | |
tree | b5202eb4add0c638c56fb867ac30b489c027f7b7 | |
parent | 528ae6aeb0fd69994ee5aed950492c42bede0aa3 (diff) | |
parent | 7ac438f6ad7abca71e044582d7fca96cf1f84d20 (diff) | |
download | uxp-2d752c925976cc983885d8f96b58a45dd5e31133.tar.gz |
Merge pull request 'Align keypress events in content with the web' (#2020) from keypress-event-work into master
Reviewed-on: https://repo.palemoon.org/MoonchildProductions/UXP/pulls/2020
-rw-r--r-- | editor/libeditor/HTMLEditor.cpp | 9 | ||||
-rw-r--r-- | editor/libeditor/TextEditor.cpp | 10 | ||||
-rw-r--r-- | extensions/spellcheck/src/mozInlineSpellChecker.cpp | 10 | ||||
-rw-r--r-- | extensions/spellcheck/src/mozInlineSpellChecker.h | 2 | ||||
-rw-r--r-- | modules/libpref/init/all.js | 4 | ||||
-rw-r--r-- | toolkit/components/satchel/nsFormFillController.cpp | 76 | ||||
-rw-r--r-- | toolkit/content/widgets/autocomplete.xml | 2 | ||||
-rw-r--r-- | widget/TextEventDispatcher.cpp | 12 | ||||
-rw-r--r-- | widget/TextEventDispatcher.h | 4 | ||||
-rw-r--r-- | widget/TextEvents.h | 24 |
10 files changed, 103 insertions, 50 deletions
diff --git a/editor/libeditor/HTMLEditor.cpp b/editor/libeditor/HTMLEditor.cpp index 6a630cb1c4..130b033bd1 100644 --- a/editor/libeditor/HTMLEditor.cpp +++ b/editor/libeditor/HTMLEditor.cpp @@ -664,8 +664,7 @@ HTMLEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent) return TypedText(NS_LITERAL_STRING("\t"), eTypedText); } case NS_VK_RETURN: - if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() || - nativeKeyEvent->IsMeta() || nativeKeyEvent->IsOS()) { + if (!nativeKeyEvent->IsInputtingLineBreak()) { return NS_OK; } aKeyEvent->AsEvent()->PreventDefault(); // consumed @@ -677,11 +676,7 @@ HTMLEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent) return TypedText(EmptyString(), eTypedBreak); } - // NOTE: On some keyboard layout, some characters are inputted with Control - // key or Alt key, but at that time, widget sets FALSE to these keys. - if (!nativeKeyEvent->mCharCode || nativeKeyEvent->IsControl() || - nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta() || - nativeKeyEvent->IsOS()) { + if (!nativeKeyEvent->IsInputtingText()) { // we don't PreventDefault() here or keybindings like control-x won't work return NS_OK; } diff --git a/editor/libeditor/TextEditor.cpp b/editor/libeditor/TextEditor.cpp index 3bee7843ce..4b26eff9cd 100644 --- a/editor/libeditor/TextEditor.cpp +++ b/editor/libeditor/TextEditor.cpp @@ -397,20 +397,14 @@ TextEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent) return TypedText(NS_LITERAL_STRING("\t"), eTypedText); } case NS_VK_RETURN: - if (IsSingleLineEditor() || nativeKeyEvent->IsControl() || - nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta() || - nativeKeyEvent->IsOS()) { + if (IsSingleLineEditor() || !nativeKeyEvent->IsInputtingLineBreak()) { return NS_OK; } aKeyEvent->AsEvent()->PreventDefault(); return TypedText(EmptyString(), eTypedBreak); } - // NOTE: On some keyboard layout, some characters are inputted with Control - // key or Alt key, but at that time, widget sets FALSE to these keys. - if (!nativeKeyEvent->mCharCode || nativeKeyEvent->IsControl() || - nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta() || - nativeKeyEvent->IsOS()) { + if (!nativeKeyEvent->IsInputtingText()) { // we don't PreventDefault() here or keybindings like control-x won't work return NS_OK; } diff --git a/extensions/spellcheck/src/mozInlineSpellChecker.cpp b/extensions/spellcheck/src/mozInlineSpellChecker.cpp index 6ca17885da..37898c818e 100644 --- a/extensions/spellcheck/src/mozInlineSpellChecker.cpp +++ b/extensions/spellcheck/src/mozInlineSpellChecker.cpp @@ -714,7 +714,7 @@ mozInlineSpellChecker::RegisterEventListeners() true, false); piTarget->AddEventListener(NS_LITERAL_STRING("click"), this, false, false); - piTarget->AddEventListener(NS_LITERAL_STRING("keypress"), this, + piTarget->AddEventListener(NS_LITERAL_STRING("keydown"), this, false, false); return NS_OK; } @@ -738,7 +738,7 @@ mozInlineSpellChecker::UnregisterEventListeners() piTarget->RemoveEventListener(NS_LITERAL_STRING("blur"), this, true); piTarget->RemoveEventListener(NS_LITERAL_STRING("click"), this, false); - piTarget->RemoveEventListener(NS_LITERAL_STRING("keypress"), this, false); + piTarget->RemoveEventListener(NS_LITERAL_STRING("keydown"), this, false); return NS_OK; } @@ -1916,8 +1916,8 @@ NS_IMETHODIMP mozInlineSpellChecker::HandleEvent(nsIDOMEvent* aEvent) if (eventType.EqualsLiteral("click")) { return MouseClick(aEvent); } - if (eventType.EqualsLiteral("keypress")) { - return KeyPress(aEvent); + if (eventType.EqualsLiteral("keydown")) { + return KeyDown(aEvent); } return NS_OK; @@ -1943,7 +1943,7 @@ nsresult mozInlineSpellChecker::MouseClick(nsIDOMEvent *aMouseEvent) return NS_OK; } -nsresult mozInlineSpellChecker::KeyPress(nsIDOMEvent* aKeyEvent) +nsresult mozInlineSpellChecker::KeyDown(nsIDOMEvent* aKeyEvent) { nsCOMPtr<nsIDOMKeyEvent>keyEvent = do_QueryInterface(aKeyEvent); NS_ENSURE_TRUE(keyEvent, NS_OK); diff --git a/extensions/spellcheck/src/mozInlineSpellChecker.h b/extensions/spellcheck/src/mozInlineSpellChecker.h index 52261f22be..251b5f6f27 100644 --- a/extensions/spellcheck/src/mozInlineSpellChecker.h +++ b/extensions/spellcheck/src/mozInlineSpellChecker.h @@ -197,7 +197,7 @@ public: nsresult Blur(nsIDOMEvent* aEvent); nsresult MouseClick(nsIDOMEvent* aMouseEvent); - nsresult KeyPress(nsIDOMEvent* aKeyEvent); + nsresult KeyDown(nsIDOMEvent* aKeyEvent); mozInlineSpellChecker(); diff --git a/modules/libpref/init/all.js b/modules/libpref/init/all.js index b0c064fc0f..bac789c96b 100644 --- a/modules/libpref/init/all.js +++ b/modules/libpref/init/all.js @@ -223,6 +223,10 @@ pref("dom.keyboardevent.code.enabled", true); // even if this is true). pref("dom.keyboardevent.dispatch_during_composition", false); +// If this is true, TextEventDispatcher dispatches keypress events +// for the input of non-printable characters (content only). +pref("dom.keyboardevent.keypress.dispatch_non_printable_in_content", false); + // Whether URL,Location,Link::GetHash should be percent encoded // in setter and percent decoded in getter (old behaviour = true) pref("dom.url.encode_decode_hash", true); 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); ]]></handler> - <handler event="keypress" phase="capturing" + <handler event="keypress" phase="capturing" group="system" action="return this.onKeyPress(event);"/> <handler event="compositionstart" phase="capturing" diff --git a/widget/TextEventDispatcher.cpp b/widget/TextEventDispatcher.cpp index 6c54376a48..2b058ba9c3 100644 --- a/widget/TextEventDispatcher.cpp +++ b/widget/TextEventDispatcher.cpp @@ -21,6 +21,7 @@ namespace widget { *****************************************************************************/ bool TextEventDispatcher::sDispatchKeyEventsDuringComposition = false; +bool TextEventDispatcher::sDispatchKeyPressEventNonPrintableInContent = false; TextEventDispatcher::TextEventDispatcher(nsIWidget* aWidget) : mWidget(aWidget) @@ -36,6 +37,10 @@ TextEventDispatcher::TextEventDispatcher(nsIWidget* aWidget) &sDispatchKeyEventsDuringComposition, "dom.keyboardevent.dispatch_during_composition", false); + Preferences::AddBoolVarCache( + &sDispatchKeyPressEventNonPrintableInContent, + "dom.keyboardevent.keypress.dispatch_non_printable_in_content", + false); sInitialized = true; } } @@ -531,6 +536,13 @@ TextEventDispatcher::DispatchKeyboardEventInternal( } } + if (!sDispatchKeyPressEventNonPrintableInContent && + keyEvent.mMessage == eKeyPress && + !keyEvent.IsInputtingText() && + !keyEvent.IsInputtingLineBreak()) { + keyEvent.mFlags.mOnlySystemGroupDispatchInContent = true; + } + DispatchInputEvent(mWidget, keyEvent, aStatus); return true; } diff --git a/widget/TextEventDispatcher.h b/widget/TextEventDispatcher.h index 1bed15a3bf..5d26127c71 100644 --- a/widget/TextEventDispatcher.h +++ b/widget/TextEventDispatcher.h @@ -399,6 +399,10 @@ private: // is a composition. static bool sDispatchKeyEventsDuringComposition; + // If this is true, keypress events for non-printable keys are dispatched to
+ // event listeners of the system event group in web content.
+ static bool sDispatchKeyPressEventNonPrintableInContent; + nsresult BeginInputTransactionInternal( TextEventDispatcherListener* aListener, InputTransactionType aType); diff --git a/widget/TextEvents.h b/widget/TextEvents.h index 6c29341144..59aa91b2e0 100644 --- a/widget/TextEvents.h +++ b/widget/TextEvents.h @@ -183,6 +183,30 @@ public: return IsKeyEventOnPlugin(mMessage); } + bool IsInputtingText() const + { + // NOTE: On some keyboard layouts, some characters are put in with Control + // or Alt keys, but at that time, widget unsets the modifier flag + // from the eKeyPress event, so it does not count as a modifier in + // this check. + return mMessage == eKeyPress && + mCharCode && + !(mModifiers & (MODIFIER_ALT | + MODIFIER_CONTROL | + MODIFIER_META | + MODIFIER_OS)); + } + + bool IsInputtingLineBreak() const + { + return mMessage == eKeyPress && + mKeyNameIndex == KEY_NAME_INDEX_Enter && + !(mModifiers & (MODIFIER_ALT | + MODIFIER_CONTROL | + MODIFIER_META | + MODIFIER_OS)); + } + virtual WidgetEvent* Duplicate() const override { MOZ_ASSERT(mClass == eKeyboardEventClass, |