Bug 1156062 part 11 - Clean up nsHTMLEditRules::Before/AfterEdit; r=masayuki
authorAryeh Gregor <ayg@aryeh.name>
Tue, 19 Apr 2016 06:53:00 +0200
changeset 331763 13755b9cd5d9bfdc8e7467fd4879251c44be3a86
parent 331762 54f11856b7db8a64ee3f51736d1a154f219e20e7
child 331764 3732da92e8fccb4cc5dc7cdb615877c07e4dcbb9
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmasayuki
bugs1156062
milestone48.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 1156062 part 11 - Clean up nsHTMLEditRules::Before/AfterEdit; r=masayuki
editor/libeditor/nsHTMLEditRules.cpp
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -307,128 +307,133 @@ nsHTMLEditRules::DetachEditor()
   mHTMLEditor = nullptr;
   return nsTextEditRules::DetachEditor();
 }
 
 NS_IMETHODIMP
 nsHTMLEditRules::BeforeEdit(EditAction action,
                             nsIEditor::EDirection aDirection)
 {
-  if (mLockRulesSniffing) return NS_OK;
-
-  nsAutoLockRulesSniffing lockIt((nsTextEditRules*)this);
+  if (mLockRulesSniffing) {
+    return NS_OK;
+  }
+
+  NS_ENSURE_STATE(mHTMLEditor);
+  nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
+
+  nsAutoLockRulesSniffing lockIt(this);
   mDidExplicitlySetInterline = false;
 
-  if (!mActionNesting++)
-  {
-    // clear our flag about if just deleted a range
+  if (!mActionNesting) {
+    mActionNesting++;
+
+    // Clear our flag about if just deleted a range
     mDidRangedDelete = false;
 
-    // remember where our selection was before edit action took place:
-
-    // get selection
-    NS_ENSURE_STATE(mHTMLEditor);
+    // Remember where our selection was before edit action took place:
+
+    // Get selection
     RefPtr<Selection> selection = mHTMLEditor->GetSelection();
 
-    // get the selection location
+    // Get the selection location
     if (!selection->RangeCount()) {
       return NS_ERROR_UNEXPECTED;
     }
     mRangeItem->startNode = selection->GetRangeAt(0)->GetStartParent();
     mRangeItem->startOffset = selection->GetRangeAt(0)->StartOffset();
     mRangeItem->endNode = selection->GetRangeAt(0)->GetEndParent();
     mRangeItem->endOffset = selection->GetRangeAt(0)->EndOffset();
-    nsCOMPtr<nsIDOMNode> selStartNode = GetAsDOMNode(mRangeItem->startNode);
-    nsCOMPtr<nsIDOMNode> selEndNode = GetAsDOMNode(mRangeItem->endNode);
-
-    // register this range with range updater to track this as we perturb the doc
-    NS_ENSURE_STATE(mHTMLEditor);
+    nsCOMPtr<nsINode> selStartNode = mRangeItem->startNode;
+    nsCOMPtr<nsINode> selEndNode = mRangeItem->endNode;
+
+    // Register with range updater to track this as we perturb the doc
     (mHTMLEditor->mRangeUpdater).RegisterRangeItem(mRangeItem);
 
-    // clear deletion state bool
+    // Clear deletion state bool
     mDidDeleteSelection = false;
 
-    // clear out mDocChangeRange and mUtilRange
-    if(mDocChangeRange)
-    {
-      // clear out our accounting of what changed
+    // Clear out mDocChangeRange and mUtilRange
+    if (mDocChangeRange) {
+      // Clear out our accounting of what changed
       mDocChangeRange->Reset();
     }
-    if(mUtilRange)
-    {
-      // ditto for mUtilRange.
+    if (mUtilRange) {
+      // Ditto for mUtilRange.
       mUtilRange->Reset();
     }
 
-    // remember current inline styles for deletion and normal insertion operations
+    // Remember current inline styles for deletion and normal insertion ops
     if (action == EditAction::insertText ||
         action == EditAction::insertIMEText ||
         action == EditAction::deleteSelection ||
         IsStyleCachePreservingAction(action)) {
-      nsCOMPtr<nsIDOMNode> selNode = selStartNode;
-      if (aDirection == nsIEditor::eNext)
-        selNode = selEndNode;
-      nsresult res = CacheInlineStyles(selNode);
-      NS_ENSURE_SUCCESS(res, res);
+      nsCOMPtr<nsINode> selNode =
+        aDirection == nsIEditor::eNext ? selEndNode : selStartNode;
+      nsresult rv = CacheInlineStyles(GetAsDOMNode(selNode));
+      NS_ENSURE_SUCCESS(rv, rv);
     }
 
     // Stabilize the document against contenteditable count changes
-    NS_ENSURE_STATE(mHTMLEditor);
     nsCOMPtr<nsIDOMDocument> doc = mHTMLEditor->GetDOMDocument();
     NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
     nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc);
     NS_ENSURE_TRUE(htmlDoc, NS_ERROR_FAILURE);
     if (htmlDoc->GetEditingState() == nsIHTMLDocument::eContentEditable) {
       htmlDoc->ChangeContentEditableCount(nullptr, +1);
       mRestoreContentEditableCount = true;
     }
 
-    // check that selection is in subtree defined by body node
+    // Check that selection is in subtree defined by body node
     ConfirmSelectionInBody();
-    // let rules remember the top level action
+    // Let rules remember the top level action
     mTheAction = action;
   }
   return NS_OK;
 }
 
 
 NS_IMETHODIMP
 nsHTMLEditRules::AfterEdit(EditAction action,
                            nsIEditor::EDirection aDirection)
 {
-  if (mLockRulesSniffing) return NS_OK;
+  if (mLockRulesSniffing) {
+    return NS_OK;
+  }
+
+  NS_ENSURE_STATE(mHTMLEditor);
+  nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
 
   nsAutoLockRulesSniffing lockIt(this);
 
-  NS_PRECONDITION(mActionNesting>0, "bad action nesting!");
-  nsresult res = NS_OK;
-  if (!--mActionNesting)
-  {
-    // do all the tricky stuff
-    res = AfterEditInner(action, aDirection);
-
-    // free up selectionState range item
-    NS_ENSURE_STATE(mHTMLEditor);
+  MOZ_ASSERT(mActionNesting > 0);
+  nsresult rv = NS_OK;
+  mActionNesting--;
+  if (!mActionNesting) {
+    // Do all the tricky stuff
+    rv = AfterEditInner(action, aDirection);
+
+    // Free up selectionState range item
     (mHTMLEditor->mRangeUpdater).DropRangeItem(mRangeItem);
 
     // Reset the contenteditable count to its previous value
     if (mRestoreContentEditableCount) {
-      NS_ENSURE_STATE(mHTMLEditor);
       nsCOMPtr<nsIDOMDocument> doc = mHTMLEditor->GetDOMDocument();
       NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
       nsCOMPtr<nsIHTMLDocument> htmlDoc = do_QueryInterface(doc);
       NS_ENSURE_TRUE(htmlDoc, NS_ERROR_FAILURE);
       if (htmlDoc->GetEditingState() == nsIHTMLDocument::eContentEditable) {
         htmlDoc->ChangeContentEditableCount(nullptr, -1);
       }
       mRestoreContentEditableCount = false;
     }
   }
 
-  return res;
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  return NS_OK;
 }
 
 
 nsresult
 nsHTMLEditRules::AfterEditInner(EditAction action,
                                 nsIEditor::EDirection aDirection)
 {
   ConfirmSelectionInBody();