Bug 1056545 part.4 nsEditorEventListener should use early return style r=ehsan
authorMasayuki Nakano <masayuki@d-toybox.com>
Tue, 26 Aug 2014 09:57:46 +0900
changeset 223233 8b7c34d6dec5a22760138da5aefb3dba2a0a9241
parent 223232 c43e2df984a603ad888a14676e57f3bfe0f49a76
child 223234 71c5e8aa91dd7885a42dc36f018e655bf60a6ab9
push id3979
push userraliiev@mozilla.com
push dateMon, 13 Oct 2014 16:35:44 +0000
treeherdermozilla-beta@30f2cc610691 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1056545
milestone34.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1056545 part.4 nsEditorEventListener should use early return style r=ehsan
editor/libeditor/nsEditorEventListener.cpp
editor/libeditor/nsEditorEventListener.h
--- a/editor/libeditor/nsEditorEventListener.cpp
+++ b/editor/libeditor/nsEditorEventListener.cpp
@@ -485,53 +485,54 @@ bool IsCtrlShiftPressed(bool& isRTL)
 // This logic is mostly borrowed from Chromium's
 // RenderWidgetHostViewWin::OnKeyEvent.
 
 nsresult
 nsEditorEventListener::KeyUp(nsIDOMKeyEvent* aKeyEvent)
 {
   NS_ENSURE_TRUE(aKeyEvent, NS_OK);
 
-  if (mHaveBidiKeyboards) {
-    uint32_t keyCode = 0;
-    aKeyEvent->GetKeyCode(&keyCode);
-    if (keyCode == nsIDOMKeyEvent::DOM_VK_SHIFT ||
-        keyCode == nsIDOMKeyEvent::DOM_VK_CONTROL) {
-      if (mShouldSwitchTextDirection && mEditor->IsPlaintextEditor()) {
-        mEditor->SwitchTextDirectionTo(mSwitchToRTL ?
-          nsIPlaintextEditor::eEditorRightToLeft :
-          nsIPlaintextEditor::eEditorLeftToRight);
-        mShouldSwitchTextDirection = false;
-      }
-    }
+  if (!mHaveBidiKeyboards) {
+    return NS_OK;
   }
 
+  uint32_t keyCode = 0;
+  aKeyEvent->GetKeyCode(&keyCode);
+  if ((keyCode == nsIDOMKeyEvent::DOM_VK_SHIFT ||
+       keyCode == nsIDOMKeyEvent::DOM_VK_CONTROL) &&
+      mShouldSwitchTextDirection && mEditor->IsPlaintextEditor()) {
+    mEditor->SwitchTextDirectionTo(mSwitchToRTL ?
+      nsIPlaintextEditor::eEditorRightToLeft :
+      nsIPlaintextEditor::eEditorLeftToRight);
+    mShouldSwitchTextDirection = false;
+  }
   return NS_OK;
 }
 
 nsresult
 nsEditorEventListener::KeyDown(nsIDOMKeyEvent* aKeyEvent)
 {
   NS_ENSURE_TRUE(aKeyEvent, NS_OK);
 
-  if (mHaveBidiKeyboards) {
-    uint32_t keyCode = 0;
-    aKeyEvent->GetKeyCode(&keyCode);
-    if (keyCode == nsIDOMKeyEvent::DOM_VK_SHIFT) {
-      bool switchToRTL;
-      if (IsCtrlShiftPressed(switchToRTL)) {
-        mShouldSwitchTextDirection = true;
-        mSwitchToRTL = switchToRTL;
-      }
-    } else if (keyCode != nsIDOMKeyEvent::DOM_VK_CONTROL) {
-      // In case the user presses any other key besides Ctrl and Shift
-      mShouldSwitchTextDirection = false;
-    }
+  if (!mHaveBidiKeyboards) {
+    return NS_OK;
   }
 
+  uint32_t keyCode = 0;
+  aKeyEvent->GetKeyCode(&keyCode);
+  if (keyCode == nsIDOMKeyEvent::DOM_VK_SHIFT) {
+    bool switchToRTL;
+    if (IsCtrlShiftPressed(switchToRTL)) {
+      mShouldSwitchTextDirection = true;
+      mSwitchToRTL = switchToRTL;
+    }
+  } else if (keyCode != nsIDOMKeyEvent::DOM_VK_CONTROL) {
+    // In case the user presses any other key besides Ctrl and Shift
+    mShouldSwitchTextDirection = false;
+  }
   return NS_OK;
 }
 #endif
 
 nsresult
 nsEditorEventListener::KeyPress(nsIDOMKeyEvent* aKeyEvent)
 {
   NS_ENSURE_TRUE(aKeyEvent, NS_OK);
@@ -555,40 +556,41 @@ nsEditorEventListener::KeyPress(nsIDOMKe
   nsresult rv = mEditor->HandleKeyPressEvent(aKeyEvent);
   NS_ENSURE_SUCCESS(rv, rv);
 
   aKeyEvent->GetDefaultPrevented(&defaultPrevented);
   if (defaultPrevented) {
     return NS_OK;
   }
 
-  if (ShouldHandleNativeKeyBindings(aKeyEvent)) {
-    // Now, ask the native key bindings to handle the event.
-    WidgetKeyboardEvent* keyEvent =
-      aKeyEvent->GetInternalNSEvent()->AsKeyboardEvent();
-    MOZ_ASSERT(keyEvent,
-               "DOM key event's internal event must be WidgetKeyboardEvent");
-    nsIWidget* widget = keyEvent->widget;
-    // If the event is created by chrome script, the widget is always nullptr.
-    if (!widget) {
-      nsCOMPtr<nsIPresShell> ps = GetPresShell();
-      nsPresContext* pc = ps ? ps->GetPresContext() : nullptr;
-      widget = pc ? pc->GetNearestWidget() : nullptr;
-      NS_ENSURE_TRUE(widget, NS_OK);
-    }
-
-    nsCOMPtr<nsIDocument> doc = mEditor->GetDocument();
-    bool handled = widget->ExecuteNativeKeyBinding(
-                             nsIWidget::NativeKeyBindingsForRichTextEditor,
-                             *keyEvent, DoCommandCallback, doc);
-    if (handled) {
-      aKeyEvent->PreventDefault();
-    }
+  if (!ShouldHandleNativeKeyBindings(aKeyEvent)) {
+    return NS_OK;
   }
 
+  // Now, ask the native key bindings to handle the event.
+  WidgetKeyboardEvent* keyEvent =
+    aKeyEvent->GetInternalNSEvent()->AsKeyboardEvent();
+  MOZ_ASSERT(keyEvent,
+             "DOM key event's internal event must be WidgetKeyboardEvent");
+  nsIWidget* widget = keyEvent->widget;
+  // If the event is created by chrome script, the widget is always nullptr.
+  if (!widget) {
+    nsCOMPtr<nsIPresShell> ps = GetPresShell();
+    nsPresContext* pc = ps ? ps->GetPresContext() : nullptr;
+    widget = pc ? pc->GetNearestWidget() : nullptr;
+    NS_ENSURE_TRUE(widget, NS_OK);
+  }
+
+  nsCOMPtr<nsIDocument> doc = mEditor->GetDocument();
+  bool handled = widget->ExecuteNativeKeyBinding(
+                           nsIWidget::NativeKeyBindingsForRichTextEditor,
+                           *keyEvent, DoCommandCallback, doc);
+  if (handled) {
+    aKeyEvent->PreventDefault();
+  }
   return NS_OK;
 }
 
 nsresult
 nsEditorEventListener::MouseClick(nsIDOMMouseEvent* aMouseEvent)
 {
   NS_ENSURE_TRUE(aMouseEvent, NS_OK);
 
@@ -621,70 +623,79 @@ nsEditorEventListener::MouseClick(nsIDOM
   }
 
   // If we got a mouse down inside the editing area, we should force the 
   // IME to commit before we change the cursor position
   mEditor->ForceCompositionEnd();
 
   int16_t button = -1;
   aMouseEvent->GetButton(&button);
-  // middle-mouse click (paste);
   if (button == 1) {
-    if (Preferences::GetBool("middlemouse.paste", false)) {
-      // Set the selection to the point under the mouse cursor:
-      nsCOMPtr<nsIDOMNode> parent;
-      if (NS_FAILED(aMouseEvent->GetRangeParent(getter_AddRefs(parent)))) {
-        return NS_ERROR_NULL_POINTER;
-      }
-      int32_t offset = 0;
-      if (NS_FAILED(aMouseEvent->GetRangeOffset(&offset))) {
-        return NS_ERROR_NULL_POINTER;
-      }
+    return HandleMiddleClickPaste(aMouseEvent);
+  }
+  return NS_OK;
+}
 
-      nsCOMPtr<nsISelection> selection;
-      if (NS_SUCCEEDED(mEditor->GetSelection(getter_AddRefs(selection)))) {
-        selection->Collapse(parent, offset);
-      }
+nsresult
+nsEditorEventListener::HandleMiddleClickPaste(nsIDOMMouseEvent* aMouseEvent)
+{
+  if (!Preferences::GetBool("middlemouse.paste", false)) {
+    // Middle click paste isn't enabled.
+    return NS_OK;
+  }
 
-      // If the ctrl key is pressed, we'll do paste as quotation.
-      // Would've used the alt key, but the kde wmgr treats alt-middle specially. 
-      bool ctrlKey = false;
-      aMouseEvent->GetCtrlKey(&ctrlKey);
+  // Set the selection to the point under the mouse cursor:
+  nsCOMPtr<nsIDOMNode> parent;
+  if (NS_FAILED(aMouseEvent->GetRangeParent(getter_AddRefs(parent)))) {
+    return NS_ERROR_NULL_POINTER;
+  }
+  int32_t offset = 0;
+  if (NS_FAILED(aMouseEvent->GetRangeOffset(&offset))) {
+    return NS_ERROR_NULL_POINTER;
+  }
 
-      nsCOMPtr<nsIEditorMailSupport> mailEditor;
-      if (ctrlKey) {
-        mailEditor = do_QueryObject(mEditor);
-      }
+  nsCOMPtr<nsISelection> selection;
+  if (NS_SUCCEEDED(mEditor->GetSelection(getter_AddRefs(selection)))) {
+    selection->Collapse(parent, offset);
+  }
+
+  // If the ctrl key is pressed, we'll do paste as quotation.
+  // Would've used the alt key, but the kde wmgr treats alt-middle specially. 
+  bool ctrlKey = false;
+  aMouseEvent->GetCtrlKey(&ctrlKey);
 
-      int32_t clipboard = nsIClipboard::kGlobalClipboard;
-      nsCOMPtr<nsIClipboard> clipboardService =
-        do_GetService("@mozilla.org/widget/clipboard;1", &rv);
-      if (NS_SUCCEEDED(rv)) {
-        bool selectionSupported;
-        rv = clipboardService->SupportsSelectionClipboard(&selectionSupported);
-        if (NS_SUCCEEDED(rv) && selectionSupported) {
-          clipboard = nsIClipboard::kSelectionClipboard;
-        }
-      }
+  nsCOMPtr<nsIEditorMailSupport> mailEditor;
+  if (ctrlKey) {
+    mailEditor = do_QueryObject(mEditor);
+  }
 
-      if (mailEditor) {
-        mailEditor->PasteAsQuotation(clipboard);
-      } else {
-        mEditor->Paste(clipboard);
-      }
-
-      // Prevent the event from propagating up to be possibly handled
-      // again by the containing window:
-      aMouseEvent->StopPropagation();
-      aMouseEvent->PreventDefault();
-
-      // We processed the event, whether drop/paste succeeded or not
-      return NS_OK;
+  nsresult rv;
+  int32_t clipboard = nsIClipboard::kGlobalClipboard;
+  nsCOMPtr<nsIClipboard> clipboardService =
+    do_GetService("@mozilla.org/widget/clipboard;1", &rv);
+  if (NS_SUCCEEDED(rv)) {
+    bool selectionSupported;
+    rv = clipboardService->SupportsSelectionClipboard(&selectionSupported);
+    if (NS_SUCCEEDED(rv) && selectionSupported) {
+      clipboard = nsIClipboard::kSelectionClipboard;
     }
   }
+
+  if (mailEditor) {
+    mailEditor->PasteAsQuotation(clipboard);
+  } else {
+    mEditor->Paste(clipboard);
+  }
+
+  // Prevent the event from propagating up to be possibly handled
+  // again by the containing window:
+  aMouseEvent->StopPropagation();
+  aMouseEvent->PreventDefault();
+
+  // We processed the event, whether drop/paste succeeded or not
   return NS_OK;
 }
 
 nsresult
 nsEditorEventListener::MouseDown(nsIDOMMouseEvent* aMouseEvent)
 {
   NS_ENSURE_TRUE(aMouseEvent, NS_OK);
 
@@ -744,57 +755,62 @@ nsEditorEventListener::DragOver(nsIDOMDr
 
   aDragEvent->GetRangeParent(getter_AddRefs(parent));
   nsCOMPtr<nsIContent> dropParent = do_QueryInterface(parent);
   NS_ENSURE_TRUE(dropParent, NS_ERROR_FAILURE);
 
   if (dropParent->IsEditable() && CanDrop(aDragEvent)) {
     aDragEvent->PreventDefault(); // consumed
 
-    if (mCaret) {
-      int32_t offset = 0;
-      nsresult rv = aDragEvent->GetRangeOffset(&offset);
-      NS_ENSURE_SUCCESS(rv, rv);
-
-      mCaret->SetVisible(true);
-      mCaret->SetCaretPosition(parent, offset);
-    }
-  } else {
-    if (!IsFileControlTextBox()) {
-      // This is needed when dropping on an input, to prevent the editor for
-      // the editable parent from receiving the event.
-      aDragEvent->StopPropagation();
+    if (!mCaret) {
+      return NS_OK;
     }
 
-    if (mCaret) {
-      mCaret->SetVisible(false);
-    }
+    int32_t offset = 0;
+    nsresult rv = aDragEvent->GetRangeOffset(&offset);
+    NS_ENSURE_SUCCESS(rv, rv);
+
+    mCaret->SetVisible(true);
+    mCaret->SetCaretPosition(parent, offset);
+
+    return NS_OK;
   }
 
+  if (!IsFileControlTextBox()) {
+    // This is needed when dropping on an input, to prevent the editor for
+    // the editable parent from receiving the event.
+    aDragEvent->StopPropagation();
+  }
+
+  if (mCaret) {
+    mCaret->SetVisible(false);
+  }
   return NS_OK;
 }
 
 void
 nsEditorEventListener::CleanupDragDropCaret()
 {
-  if (mCaret) {
-    mCaret->SetVisible(false);    // hide it, so that it turns off its timer
+  if (!mCaret) {
+    return;
+  }
+
+  mCaret->SetVisible(false);    // hide it, so that it turns off its timer
 
-    nsCOMPtr<nsIPresShell> presShell = GetPresShell();
-    if (presShell) {
-      nsCOMPtr<nsISelectionController> selCon(do_QueryInterface(presShell));
-      if (selCon) {
-        selCon->SetCaretEnabled(false);
-      }
-      presShell->RestoreCaret();
+  nsCOMPtr<nsIPresShell> presShell = GetPresShell();
+  if (presShell) {
+    nsCOMPtr<nsISelectionController> selCon(do_QueryInterface(presShell));
+    if (selCon) {
+      selCon->SetCaretEnabled(false);
     }
+    presShell->RestoreCaret();
+  }
 
-    mCaret->Terminate();
-    mCaret = nullptr;
-  }
+  mCaret->Terminate();
+  mCaret = nullptr;
 }
 
 nsresult
 nsEditorEventListener::DragExit(nsIDOMDragEvent* aDragEvent)
 {
   NS_ENSURE_TRUE(aDragEvent, NS_OK);
 
   CleanupDragDropCaret();
@@ -877,58 +893,61 @@ nsEditorEventListener::CanDrop(nsIDOMDra
 
   nsCOMPtr<nsIDOMDocument> domdoc = mEditor->GetDOMDocument();
   NS_ENSURE_TRUE(domdoc, false);
 
   nsCOMPtr<nsIDOMDocument> sourceDoc;
   nsresult rv = sourceNode->GetOwnerDocument(getter_AddRefs(sourceDoc));
   NS_ENSURE_SUCCESS(rv, false);
 
-  // source and dest are the same document; disallow drops within the selection
-  if (domdoc == sourceDoc) {
-    nsCOMPtr<nsISelection> selection;
-    rv = mEditor->GetSelection(getter_AddRefs(selection));
-    if (NS_FAILED(rv) || !selection) {
-      return false;
+  // If the source and the dest are not same document, allow to drop it always.
+  if (domdoc != sourceDoc) {
+    return true;
+  }
+
+  nsCOMPtr<nsISelection> selection;
+  rv = mEditor->GetSelection(getter_AddRefs(selection));
+  if (NS_FAILED(rv) || !selection) {
+    return false;
+  }
+
+  // If selection is collapsed, allow to drop it always.
+  if (selection->Collapsed()) {
+    return true;
+  }
+
+  nsCOMPtr<nsIDOMNode> parent;
+  rv = aEvent->GetRangeParent(getter_AddRefs(parent));
+  if (NS_FAILED(rv) || !parent) {
+    return false;
+  }
+
+  int32_t offset = 0;
+  rv = aEvent->GetRangeOffset(&offset);
+  NS_ENSURE_SUCCESS(rv, false);
+
+  int32_t rangeCount;
+  rv = selection->GetRangeCount(&rangeCount);
+  NS_ENSURE_SUCCESS(rv, false);
+
+  for (int32_t i = 0; i < rangeCount; i++) {
+    nsCOMPtr<nsIDOMRange> range;
+    rv = selection->GetRangeAt(i, getter_AddRefs(range));
+    if (NS_FAILED(rv) || !range) {
+      // Don't bail yet, iterate through them all
+      continue;
     }
 
-    // Don't bother if collapsed - can always drop
-    if (!selection->Collapsed()) {
-      nsCOMPtr<nsIDOMNode> parent;
-      rv = aEvent->GetRangeParent(getter_AddRefs(parent));
-      if (NS_FAILED(rv) || !parent) {
-        return false;
-      }
-
-      int32_t offset = 0;
-      rv = aEvent->GetRangeOffset(&offset);
-      NS_ENSURE_SUCCESS(rv, false);
-
-      int32_t rangeCount;
-      rv = selection->GetRangeCount(&rangeCount);
-      NS_ENSURE_SUCCESS(rv, false);
-
-      for (int32_t i = 0; i < rangeCount; i++) {
-        nsCOMPtr<nsIDOMRange> range;
-        rv = selection->GetRangeAt(i, getter_AddRefs(range));
-        if (NS_FAILED(rv) || !range) {
-          // Don't bail yet, iterate through them all
-          continue;
-        }
-
-        bool inRange = true;
-        range->IsPointInRange(parent, offset, &inRange);
-        if (inRange) {
-          // Okay, now you can bail, we are over the orginal selection
-          return false;
-        }
-      }
+    bool inRange = true;
+    range->IsPointInRange(parent, offset, &inRange);
+    if (inRange) {
+      // Okay, now you can bail, we are over the orginal selection
+      return false;
     }
   }
-  
   return true;
 }
 
 nsresult
 nsEditorEventListener::HandleStartComposition(nsIDOMEvent* aCompositionEvent)
 {
   if (!mEditor->IsAcceptableInputEvent(aCompositionEvent)) {
     return NS_OK;
@@ -1013,21 +1032,19 @@ nsEditorEventListener::Blur(nsIDOMEvent*
 
   // check if something else is focused. If another element is focused, then
   // we should not change the selection.
   nsIFocusManager* fm = nsFocusManager::GetFocusManager();
   NS_ENSURE_TRUE(fm, NS_OK);
 
   nsCOMPtr<nsIDOMElement> element;
   fm->GetFocusedElement(getter_AddRefs(element));
-  if (element) {
-    return NS_OK;
+  if (!element) {
+    mEditor->FinalizeSelection();
   }
-
-  mEditor->FinalizeSelection();
   return NS_OK;
 }
 
 void
 nsEditorEventListener::SpellCheckIfNeeded()
 {
   // If the spell check skip flag is still enabled from creation time,
   // disable it because focused editors are allowed to spell check.
@@ -1038,25 +1055,25 @@ nsEditorEventListener::SpellCheckIfNeede
     mEditor->SetFlags(currentFlags);
   }
 }
 
 bool
 nsEditorEventListener::IsFileControlTextBox()
 {
   dom::Element* root = mEditor->GetRoot();
-  if (root && root->ChromeOnlyAccess()) {
-    nsIContent* parent = root->FindFirstNonChromeOnlyAccessContent();
-    if (parent && parent->IsHTML(nsGkAtoms::input)) {
-      nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(parent);
-      MOZ_ASSERT(formControl);
-      return formControl->GetType() == NS_FORM_INPUT_FILE;
-    }
+  if (!root || !root->ChromeOnlyAccess()) {
+    return false;
   }
-  return false;
+  nsIContent* parent = root->FindFirstNonChromeOnlyAccessContent();
+  if (!parent || !parent->IsHTML(nsGkAtoms::input)) {
+    return false;
+  }
+  nsCOMPtr<nsIFormControl> formControl = do_QueryInterface(parent);
+  return formControl->GetType() == NS_FORM_INPUT_FILE;
 }
 
 bool
 nsEditorEventListener::ShouldHandleNativeKeyBindings(nsIDOMKeyEvent* aKeyEvent)
 {
   // Only return true if the target of the event is a desendant of the active
   // editing host in order to match the similar decision made in
   // nsXBLWindowKeyHandler.
@@ -1086,9 +1103,8 @@ nsEditorEventListener::ShouldHandleNativ
 
   nsIContent* editingHost = htmlEditor->GetActiveEditingHost();
   if (!editingHost) {
     return false;
   }
 
   return nsContentUtils::ContentIsDescendantOf(targetContent, editingHost);
 }
-
--- a/editor/libeditor/nsEditorEventListener.h
+++ b/editor/libeditor/nsEditorEventListener.h
@@ -70,16 +70,17 @@ protected:
   nsresult Drop(nsIDOMDragEvent* aDragEvent);
   nsresult DragGesture(nsIDOMDragEvent* aDragEvent);
 
   bool CanDrop(nsIDOMDragEvent* aEvent);
   void CleanupDragDropCaret();
   already_AddRefed<nsIPresShell> GetPresShell();
   bool IsFileControlTextBox();
   bool ShouldHandleNativeKeyBindings(nsIDOMKeyEvent* aKeyEvent);
+  nsresult HandleMiddleClickPaste(nsIDOMMouseEvent* aMouseEvent);
 
   nsEditor* mEditor; // weak
   nsRefPtr<nsCaret> mCaret;
   bool mCommitText;
   bool mInTransaction;
 #ifdef HANDLE_NATIVE_TEXT_DIRECTION_SWITCH
   bool mHaveBidiKeyboards;
   bool mShouldSwitchTextDirection;