Bug 1337718 part.1 Make EditorBase::HandleKeyPressEvent() take WidgetKeyboardEvent* instead of nsIDOMKeyEvent* r?m_kato
MozReview-Commit-ID: 8QUiHPRf9AH
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -60,17 +60,16 @@
#include "nsIDOMAttr.h" // for nsIDOMAttr
#include "nsIDOMCharacterData.h" // for nsIDOMCharacterData
#include "nsIDOMDocument.h" // for nsIDOMDocument
#include "nsIDOMElement.h" // for nsIDOMElement
#include "nsIDOMEvent.h" // for nsIDOMEvent
#include "nsIDOMEventListener.h" // for nsIDOMEventListener
#include "nsIDOMEventTarget.h" // for nsIDOMEventTarget
#include "nsIDOMHTMLElement.h" // for nsIDOMHTMLElement
-#include "nsIDOMKeyEvent.h" // for nsIDOMKeyEvent, etc.
#include "nsIDOMMozNamedAttrMap.h" // for nsIDOMMozNamedAttrMap
#include "nsIDOMMouseEvent.h" // for nsIDOMMouseEvent
#include "nsIDOMNode.h" // for nsIDOMNode, etc.
#include "nsIDOMNodeList.h" // for nsIDOMNodeList
#include "nsIDOMText.h" // for nsIDOMText
#include "nsIDocument.h" // for nsIDocument
#include "nsIDocumentStateListener.h" // for nsIDocumentStateListener
#include "nsIEditActionListener.h" // for nsIEditActionListener
@@ -4631,68 +4630,68 @@ EditorBase::RemoveAttributeOrEquivalent(
if (NS_WARN_IF(!element)) {
return NS_ERROR_NULL_POINTER;
}
nsCOMPtr<nsIAtom> attribute = NS_Atomize(aAttribute);
return RemoveAttributeOrEquivalent(element, attribute, aSuppressTransaction);
}
nsresult
-EditorBase::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent)
+EditorBase::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent)
{
// NOTE: When you change this method, you should also change:
// * editor/libeditor/tests/test_texteditor_keyevent_handling.html
// * editor/libeditor/tests/test_htmleditor_keyevent_handling.html
//
// And also when you add new key handling, you need to change the subclass's
// HandleKeyPressEvent()'s switch statement.
- WidgetKeyboardEvent* nativeKeyEvent =
- aKeyEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
- NS_ENSURE_TRUE(nativeKeyEvent, NS_ERROR_UNEXPECTED);
- NS_ASSERTION(nativeKeyEvent->mMessage == eKeyPress,
- "HandleKeyPressEvent gets non-keypress event");
+ if (NS_WARN_IF(!aKeyboardEvent)) {
+ return NS_ERROR_UNEXPECTED;
+ }
+ MOZ_ASSERT(aKeyboardEvent->mMessage == eKeyPress,
+ "HandleKeyPressEvent gets non-keypress event");
// if we are readonly or disabled, then do nothing.
if (IsReadonly() || IsDisabled()) {
// consume backspace for disabled and readonly textfields, to prevent
// back in history, which could be confusing to users
- if (nativeKeyEvent->mKeyCode == NS_VK_BACK) {
- aKeyEvent->AsEvent()->PreventDefault();
+ if (aKeyboardEvent->mKeyCode == NS_VK_BACK) {
+ aKeyboardEvent->PreventDefault();
}
return NS_OK;
}
- switch (nativeKeyEvent->mKeyCode) {
+ switch (aKeyboardEvent->mKeyCode) {
case NS_VK_META:
case NS_VK_WIN:
case NS_VK_SHIFT:
case NS_VK_CONTROL:
case NS_VK_ALT:
- aKeyEvent->AsEvent()->PreventDefault(); // consumed
+ aKeyboardEvent->PreventDefault(); // consumed
return NS_OK;
case NS_VK_BACK:
- if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() ||
- nativeKeyEvent->IsMeta() || nativeKeyEvent->IsOS()) {
+ if (aKeyboardEvent->IsControl() || aKeyboardEvent->IsAlt() ||
+ aKeyboardEvent->IsMeta() || aKeyboardEvent->IsOS()) {
return NS_OK;
}
DeleteSelection(nsIEditor::ePrevious, nsIEditor::eStrip);
- aKeyEvent->AsEvent()->PreventDefault(); // consumed
+ aKeyboardEvent->PreventDefault(); // consumed
return NS_OK;
case NS_VK_DELETE:
// on certain platforms (such as windows) the shift key
// modifies what delete does (cmd_cut in this case).
// bailing here to allow the keybindings to do the cut.
- if (nativeKeyEvent->IsShift() || nativeKeyEvent->IsControl() ||
- nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta() ||
- nativeKeyEvent->IsOS()) {
+ if (aKeyboardEvent->IsShift() || aKeyboardEvent->IsControl() ||
+ aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
+ aKeyboardEvent->IsOS()) {
return NS_OK;
}
DeleteSelection(nsIEditor::eNext, nsIEditor::eStrip);
- aKeyEvent->AsEvent()->PreventDefault(); // consumed
+ aKeyboardEvent->PreventDefault(); // consumed
return NS_OK;
}
return NS_OK;
}
nsresult
EditorBase::HandleInlineSpellCheck(EditAction action,
Selection* aSelection,
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -29,17 +29,16 @@
#include "nscore.h" // for nsresult, nsAString, etc.
class nsIAtom;
class nsIContent;
class nsIDOMDocument;
class nsIDOMEvent;
class nsIDOMEventListener;
class nsIDOMEventTarget;
-class nsIDOMKeyEvent;
class nsIDOMNode;
class nsIDocument;
class nsIDocumentStateListener;
class nsIEditActionListener;
class nsIEditorObserver;
class nsIInlineSpellChecker;
class nsINode;
class nsIPresShell;
@@ -743,17 +742,17 @@ public:
nsresult GetString(const nsAString& name, nsAString& value);
void BeginUpdateViewBatch();
virtual nsresult EndUpdateViewBatch();
bool GetShouldTxnSetSelection();
- virtual nsresult HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent);
+ virtual nsresult HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent);
nsresult HandleInlineSpellCheck(EditAction action,
Selection* aSelection,
nsIDOMNode* previousSelectedNode,
int32_t previousSelectedOffset,
nsIDOMNode* aStartNode,
int32_t aStartOffset,
nsIDOMNode* aEndNode,
--- a/editor/libeditor/EditorEventListener.cpp
+++ b/editor/libeditor/EditorEventListener.cpp
@@ -610,17 +610,17 @@ EditorEventListener::KeyPress(nsIDOMKeyE
aKeyEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
MOZ_ASSERT(keypressEvent,
"DOM key event's internal event must be WidgetKeyboardEvent");
if (!editorBase->IsAcceptableInputEvent(keypressEvent) ||
DetachedFromEditorOrDefaultPrevented(keypressEvent)) {
return NS_OK;
}
- nsresult rv = editorBase->HandleKeyPressEvent(aKeyEvent);
+ nsresult rv = editorBase->HandleKeyPressEvent(keypressEvent);
NS_ENSURE_SUCCESS(rv, rv);
if (DetachedFromEditorOrDefaultPrevented(keypressEvent)) {
return NS_OK;
}
if (!ShouldHandleNativeKeyBindings(aKeyEvent)) {
return NS_OK;
}
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -23,17 +23,16 @@
#include "nsIDOMText.h"
#include "nsIDOMMozNamedAttrMap.h"
#include "nsIDOMNodeList.h"
#include "nsIDOMDocument.h"
#include "nsIDOMAttr.h"
#include "nsIDocumentInlines.h"
#include "nsIDOMEventTarget.h"
-#include "nsIDOMKeyEvent.h"
#include "nsIDOMMouseEvent.h"
#include "nsIDOMHTMLAnchorElement.h"
#include "nsISelectionController.h"
#include "nsIDOMHTMLDocument.h"
#include "nsILinkHandler.h"
#include "nsIInlineSpellChecker.h"
#include "mozilla/css/Loader.h"
@@ -588,57 +587,57 @@ HTMLEditor::BeginningOfDocument()
selOffset = curOffset;
done = true;
}
}
return selection->Collapse(selNode, selOffset);
}
nsresult
-HTMLEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent)
+HTMLEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent)
{
// NOTE: When you change this method, you should also change:
// * editor/libeditor/tests/test_htmleditor_keyevent_handling.html
if (IsReadonly() || IsDisabled()) {
// When we're not editable, the events are handled on EditorBase, so, we can
// bypass TextEditor.
- return EditorBase::HandleKeyPressEvent(aKeyEvent);
- }
-
- WidgetKeyboardEvent* nativeKeyEvent =
- aKeyEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
- NS_ENSURE_TRUE(nativeKeyEvent, NS_ERROR_UNEXPECTED);
- NS_ASSERTION(nativeKeyEvent->mMessage == eKeyPress,
- "HandleKeyPressEvent gets non-keypress event");
-
- switch (nativeKeyEvent->mKeyCode) {
+ return EditorBase::HandleKeyPressEvent(aKeyboardEvent);
+ }
+
+ if (NS_WARN_IF(!aKeyboardEvent)) {
+ return NS_ERROR_UNEXPECTED;
+ }
+ MOZ_ASSERT(aKeyboardEvent->mMessage == eKeyPress,
+ "HandleKeyPressEvent gets non-keypress event");
+
+ switch (aKeyboardEvent->mKeyCode) {
case NS_VK_META:
case NS_VK_WIN:
case NS_VK_SHIFT:
case NS_VK_CONTROL:
case NS_VK_ALT:
case NS_VK_BACK:
case NS_VK_DELETE:
// These keys are handled on EditorBase, so, we can bypass
// TextEditor.
- return EditorBase::HandleKeyPressEvent(aKeyEvent);
+ return EditorBase::HandleKeyPressEvent(aKeyboardEvent);
case NS_VK_TAB: {
if (IsPlaintextEditor()) {
// If this works as plain text editor, e.g., mail editor for plain
// text, should be handled on TextEditor.
- return TextEditor::HandleKeyPressEvent(aKeyEvent);
+ return TextEditor::HandleKeyPressEvent(aKeyboardEvent);
}
if (IsTabbable()) {
return NS_OK; // let it be used for focus switching
}
- if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() ||
- nativeKeyEvent->IsMeta() || nativeKeyEvent->IsOS()) {
+ if (aKeyboardEvent->IsControl() || aKeyboardEvent->IsAlt() ||
+ aKeyboardEvent->IsMeta() || aKeyboardEvent->IsOS()) {
return NS_OK;
}
RefPtr<Selection> selection = GetSelection();
NS_ENSURE_TRUE(selection && selection->RangeCount(), NS_ERROR_FAILURE);
nsCOMPtr<nsINode> node = selection->GetRangeAt(0)->GetStartParent();
MOZ_ASSERT(node);
@@ -647,60 +646,61 @@ HTMLEditor::HandleKeyPressEvent(nsIDOMKe
if (!blockParent) {
break;
}
bool handled = false;
nsresult rv = NS_OK;
if (HTMLEditUtils::IsTableElement(blockParent)) {
- rv = TabInTable(nativeKeyEvent->IsShift(), &handled);
+ rv = TabInTable(aKeyboardEvent->IsShift(), &handled);
if (handled) {
ScrollSelectionIntoView(false);
}
} else if (HTMLEditUtils::IsListItem(blockParent)) {
- rv = Indent(nativeKeyEvent->IsShift()
+ rv = Indent(aKeyboardEvent->IsShift()
? NS_LITERAL_STRING("outdent")
: NS_LITERAL_STRING("indent"));
handled = true;
}
NS_ENSURE_SUCCESS(rv, rv);
if (handled) {
- return aKeyEvent->AsEvent()->PreventDefault(); // consumed
+ aKeyboardEvent->PreventDefault(); // consumed
+ return NS_OK;
}
- if (nativeKeyEvent->IsShift()) {
+ if (aKeyboardEvent->IsShift()) {
return NS_OK; // don't type text for shift tabs
}
- aKeyEvent->AsEvent()->PreventDefault();
+ aKeyboardEvent->PreventDefault();
return TypedText(NS_LITERAL_STRING("\t"), eTypedText);
}
case NS_VK_RETURN:
- if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() ||
- nativeKeyEvent->IsMeta() || nativeKeyEvent->IsOS()) {
+ if (aKeyboardEvent->IsControl() || aKeyboardEvent->IsAlt() ||
+ aKeyboardEvent->IsMeta() || aKeyboardEvent->IsOS()) {
return NS_OK;
}
- aKeyEvent->AsEvent()->PreventDefault(); // consumed
- if (nativeKeyEvent->IsShift() && !IsPlaintextEditor()) {
+ aKeyboardEvent->PreventDefault(); // consumed
+ if (aKeyboardEvent->IsShift() && !IsPlaintextEditor()) {
// only inserts a br node
return TypedText(EmptyString(), eTypedBR);
}
// uses rules to figure out what to insert
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 (!aKeyboardEvent->mCharCode || aKeyboardEvent->IsControl() ||
+ aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
+ aKeyboardEvent->IsOS()) {
// we don't PreventDefault() here or keybindings like control-x won't work
return NS_OK;
}
- aKeyEvent->AsEvent()->PreventDefault();
- nsAutoString str(nativeKeyEvent->mCharCode);
+ aKeyboardEvent->PreventDefault();
+ nsAutoString str(aKeyboardEvent->mCharCode);
return TypedText(str, eTypedText);
}
static void
AssertParserServiceIsCorrect(nsIAtom* aTag, bool aIsBlock)
{
#ifdef DEBUG
// Check this against what we would have said with the old code:
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -33,17 +33,16 @@
#include "nsIHTMLObjectResizer.h"
#include "nsISelectionListener.h"
#include "nsITableEditor.h"
#include "nsPoint.h"
#include "nsStubMutationObserver.h"
#include "nsTArray.h"
class nsDocumentFragment;
-class nsIDOMKeyEvent;
class nsITransferable;
class nsIClipboard;
class nsILinkHandler;
class nsTableWrapperFrame;
class nsIDOMRange;
class nsRange;
namespace mozilla {
@@ -103,17 +102,18 @@ public:
bool GetReturnInParagraphCreatesNewParagraph();
Element* GetSelectionContainer();
// nsIEditor overrides
NS_IMETHOD GetPreferredIMEState(widget::IMEState* aState) override;
// TextEditor overrides
NS_IMETHOD BeginningOfDocument() override;
- virtual nsresult HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent) override;
+ virtual nsresult HandleKeyPressEvent(
+ WidgetKeyboardEvent* aKeyboardEvent) override;
virtual already_AddRefed<nsIContent> GetFocusedContent() override;
virtual already_AddRefed<nsIContent> GetFocusedContentForIME() override;
virtual bool IsActiveInDOMWindow() override;
virtual already_AddRefed<dom::EventTarget> GetDOMEventTarget() override;
virtual Element* GetEditorRoot() override;
virtual already_AddRefed<nsIContent> FindSelectionRoot(
nsINode *aNode) override;
virtual bool IsAcceptableInputEvent(WidgetGUIEvent* aGUIEvent) override;
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -30,17 +30,16 @@
#include "nsError.h"
#include "nsGkAtoms.h"
#include "nsIClipboard.h"
#include "nsIContent.h"
#include "nsIContentIterator.h"
#include "nsIDOMDocument.h"
#include "nsIDOMElement.h"
#include "nsIDOMEventTarget.h"
-#include "nsIDOMKeyEvent.h"
#include "nsIDOMNode.h"
#include "nsIDOMNodeList.h"
#include "nsIDocumentEncoder.h"
#include "nsIEditRules.h"
#include "nsINode.h"
#include "nsIPresShell.h"
#include "nsISelectionController.h"
#include "nsISupportsPrimitives.h"
@@ -325,81 +324,81 @@ TextEditor::InitRules()
if (!mRules) {
// instantiate the rules for this text editor
mRules = new TextEditRules();
}
return mRules->Init(this);
}
nsresult
-TextEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent)
+TextEditor::HandleKeyPressEvent(WidgetKeyboardEvent* aKeyboardEvent)
{
// NOTE: When you change this method, you should also change:
// * editor/libeditor/tests/test_texteditor_keyevent_handling.html
// * editor/libeditor/tests/test_htmleditor_keyevent_handling.html
//
// And also when you add new key handling, you need to change the subclass's
// HandleKeyPressEvent()'s switch statement.
if (IsReadonly() || IsDisabled()) {
// When we're not editable, the events handled on EditorBase.
- return EditorBase::HandleKeyPressEvent(aKeyEvent);
+ return EditorBase::HandleKeyPressEvent(aKeyboardEvent);
}
- WidgetKeyboardEvent* nativeKeyEvent =
- aKeyEvent->AsEvent()->WidgetEventPtr()->AsKeyboardEvent();
- NS_ENSURE_TRUE(nativeKeyEvent, NS_ERROR_UNEXPECTED);
- NS_ASSERTION(nativeKeyEvent->mMessage == eKeyPress,
- "HandleKeyPressEvent gets non-keypress event");
+ if (NS_WARN_IF(!aKeyboardEvent)) {
+ return NS_ERROR_UNEXPECTED;
+ }
+ MOZ_ASSERT(aKeyboardEvent->mMessage == eKeyPress,
+ "HandleKeyPressEvent gets non-keypress event");
- switch (nativeKeyEvent->mKeyCode) {
+ switch (aKeyboardEvent->mKeyCode) {
case NS_VK_META:
case NS_VK_WIN:
case NS_VK_SHIFT:
case NS_VK_CONTROL:
case NS_VK_ALT:
case NS_VK_BACK:
case NS_VK_DELETE:
// These keys are handled on EditorBase
- return EditorBase::HandleKeyPressEvent(aKeyEvent);
+ return EditorBase::HandleKeyPressEvent(aKeyboardEvent);
case NS_VK_TAB: {
if (IsTabbable()) {
return NS_OK; // let it be used for focus switching
}
- if (nativeKeyEvent->IsShift() || nativeKeyEvent->IsControl() ||
- nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta() ||
- nativeKeyEvent->IsOS()) {
+ if (aKeyboardEvent->IsShift() || aKeyboardEvent->IsControl() ||
+ aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
+ aKeyboardEvent->IsOS()) {
return NS_OK;
}
// else we insert the tab straight through
- aKeyEvent->AsEvent()->PreventDefault();
+ aKeyboardEvent->PreventDefault();
return TypedText(NS_LITERAL_STRING("\t"), eTypedText);
}
case NS_VK_RETURN:
- if (IsSingleLineEditor() || nativeKeyEvent->IsControl() ||
- nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta() ||
- nativeKeyEvent->IsOS()) {
+ if (IsSingleLineEditor() || aKeyboardEvent->IsControl() ||
+ aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
+ aKeyboardEvent->IsOS()) {
return NS_OK;
}
- aKeyEvent->AsEvent()->PreventDefault();
+ aKeyboardEvent->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 (!aKeyboardEvent->mCharCode || aKeyboardEvent->IsControl() ||
+ aKeyboardEvent->IsAlt() || aKeyboardEvent->IsMeta() ||
+ aKeyboardEvent->IsOS()) {
// we don't PreventDefault() here or keybindings like control-x won't work
return NS_OK;
}
- aKeyEvent->AsEvent()->PreventDefault();
- nsAutoString str(nativeKeyEvent->mCharCode);
+ aKeyboardEvent->PreventDefault();
+ nsAutoString str(aKeyboardEvent->mCharCode);
return TypedText(str, eTypedText);
}
/* This routine is needed to provide a bottleneck for typing for logging
purposes. Can't use HandleKeyPress() (above) for that since it takes
a nsIDOMKeyEvent* parameter. So instead we pass enough info through
to TypedText() to determine what action to take, but without passing
an event.
--- a/editor/libeditor/TextEditor.h
+++ b/editor/libeditor/TextEditor.h
@@ -14,17 +14,16 @@
#include "nsIPlaintextEditor.h"
#include "nsISupportsImpl.h"
#include "nscore.h"
class nsIContent;
class nsIDOMDocument;
class nsIDOMElement;
class nsIDOMEvent;
-class nsIDOMKeyEvent;
class nsIDOMNode;
class nsIDocumentEncoder;
class nsIEditRules;
class nsIOutputStream;
class nsISelectionController;
class nsITransferable;
namespace mozilla {
@@ -122,17 +121,18 @@ public:
*/
NS_IMETHOD EndOperation() override;
/**
* Make the given selection span the entire document.
*/
virtual nsresult SelectEntireDocument(Selection* aSelection) override;
- virtual nsresult HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent) override;
+ virtual nsresult HandleKeyPressEvent(
+ WidgetKeyboardEvent* aKeyboardEvent) override;
virtual already_AddRefed<dom::EventTarget> GetDOMEventTarget() override;
virtual nsresult BeginIMEComposition(WidgetCompositionEvent* aEvent) override;
virtual nsresult UpdateIMEComposition(
WidgetCompositionEvent* aCompositionChangeEvet) override;
virtual already_AddRefed<nsIContent> GetInputEventTargetContent() override;