Bug 1415445 - part 2: EditorBase::CreateTxnForCreateElement() should take EditorRawDOMPoint for insertion point r?m_kato draft
authorMasayuki Nakano <masayuki@d-toybox.com>
Wed, 08 Nov 2017 17:23:28 +0900
changeset 695542 995baa1558a8896a03bb109f47bd88aff42b6a46
parent 695541 856200f104449a1e6c47bc3ba96161df15ac4f3c
child 695543 907b989894a9c9e454962fb86bf9e928b31aec98
push id88462
push usermasayuki@d-toybox.com
push dateThu, 09 Nov 2017 12:16:29 +0000
reviewersm_kato
bugs1415445
milestone58.0a1
Bug 1415445 - part 2: EditorBase::CreateTxnForCreateElement() should take EditorRawDOMPoint for insertion point r?m_kato The constructor of CreateElementTransaction now takes EditorRawDOMPoint instead of a set of container node and offset in it. So, its only user, EditorBase::CreateTxnForCreateElement(), should take EditorRawDOMPoint too. MozReview-Commit-ID: A8QfPM3LRii
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorBase.h
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -1420,44 +1420,58 @@ EditorBase::SetSpellcheckUserOverride(bo
 already_AddRefed<Element>
 EditorBase::CreateNode(nsAtom* aTag,
                        nsINode* aParent,
                        int32_t aPosition,
                        nsIContent* aChildAtPosition)
 {
   MOZ_ASSERT(aTag && aParent);
 
+  EditorRawDOMPoint pointToInsert;
+  if (aPosition == -1) {
+    pointToInsert.Set(aParent, aParent->Length());
+  } else if (aChildAtPosition) {
+    pointToInsert.Set(aChildAtPosition);
+    // Ensure pointToInsert has offset for sending same offset for both
+    // WillCreateNode() and DidCreateNode().
+    Unused << pointToInsert.Offset();
+  } else {
+    pointToInsert.Set(aParent, aPosition);
+  }
+
   AutoRules beginRulesSniffing(this, EditAction::createNode, nsIEditor::eNext);
 
   {
     AutoActionListenerArray listeners(mActionListeners);
     for (auto& listener : listeners) {
       listener->WillCreateNode(nsDependentAtomString(aTag),
-                               GetAsDOMNode(aParent), aPosition);
+                               GetAsDOMNode(pointToInsert.Container()),
+                               pointToInsert.Offset());
     }
   }
 
   nsCOMPtr<Element> ret;
 
   RefPtr<CreateElementTransaction> transaction =
-    CreateTxnForCreateElement(*aTag, *aParent, aPosition,
-                              aChildAtPosition);
+    CreateTxnForCreateElement(*aTag, pointToInsert);
   nsresult rv = DoTransaction(transaction);
   if (NS_SUCCEEDED(rv)) {
     ret = transaction->GetNewNode();
     MOZ_ASSERT(ret);
   }
 
-  mRangeUpdater.SelAdjCreateNode(aParent, aPosition);
+  mRangeUpdater.SelAdjCreateNode(pointToInsert.Container(),
+                                 pointToInsert.Offset());
 
   {
     AutoActionListenerArray listeners(mActionListeners);
     for (auto& listener : listeners) {
       listener->DidCreateNode(nsDependentAtomString(aTag), GetAsDOMNode(ret),
-                              GetAsDOMNode(aParent), aPosition, rv);
+                              GetAsDOMNode(pointToInsert.Container()),
+                              pointToInsert.Offset(), rv);
     }
   }
 
   return ret.forget();
 }
 
 NS_IMETHODIMP
 EditorBase::InsertNode(nsIDOMNode* aNode,
@@ -4363,30 +4377,20 @@ EditorBase::CreateTxnForRemoveAttribute(
   RefPtr<ChangeAttributeTransaction> transaction =
     new ChangeAttributeTransaction(aElement, aAttribute, nullptr);
 
   return transaction.forget();
 }
 
 already_AddRefed<CreateElementTransaction>
 EditorBase::CreateTxnForCreateElement(nsAtom& aTag,
-                                      nsINode& aParent,
-                                      int32_t aPosition,
-                                      nsIContent* aChildAtPosition)
-{
-  EditorRawDOMPoint pointToInsert;
-  if (aPosition == -1) {
-    pointToInsert.Set(&aParent, aParent.Length());
-  } else if (aChildAtPosition) {
-    pointToInsert.Set(aChildAtPosition);
-  } else {
-    pointToInsert.Set(&aParent, aPosition);
-  }
+                                      const EditorRawDOMPoint& aPointToInsert)
+{
   RefPtr<CreateElementTransaction> transaction =
-    new CreateElementTransaction(*this, aTag, pointToInsert);
+    new CreateElementTransaction(*this, aTag, aPointToInsert);
 
   return transaction.forget();
 }
 
 
 already_AddRefed<InsertNodeTransaction>
 EditorBase::CreateTxnForInsertNode(nsIContent& aNode,
                                    nsINode& aParent,
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -389,23 +389,31 @@ protected:
   /**
    * Create a transaction for removing aAttribute on aElement.  Never returns
    * null.
    */
   already_AddRefed<ChangeAttributeTransaction>
     CreateTxnForRemoveAttribute(Element& aElement, nsAtom& aAttribute);
 
   /**
-   * Create a transaction for creating a new child node of aParent of type aTag.
+   * Create a transaction for creating a new child node of the container of
+   * aPointToInsert of type aTag.
+   *
+   * @param aTag            The element name to create.
+   * @param aPointToInsert  The insertion point of new element.  If this refers
+   *                        end of the container or after, the transaction
+   *                        will append the element to the container.
+   *                        Otherwise, will insert the element before the
+   *                        child node referred by this.
+   * @return                A CreateElementTransaction which are initialized
+   *                        with the arguments.
    */
   already_AddRefed<CreateElementTransaction>
     CreateTxnForCreateElement(nsAtom& aTag,
-                              nsINode& aParent,
-                              int32_t aPosition,
-                              nsIContent* aChildAtPosition);
+                              const EditorRawDOMPoint& aPointToInsert);
 
   already_AddRefed<Element> CreateNode(nsAtom* aTag, nsINode* aParent,
                                        int32_t aPosition,
                                        nsIContent* aChildAtPosition = nullptr);
 
   /**
    * Create a transaction for inserting aNode as a child of aParent.
    */