Bug 1451672 - part 18: Rename EditorBase::CloneAttributes() to EditorBase::CloneAttributesWithTransaction() r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 13 Apr 2018 18:44:08 +0900
changeset 468371 907f07168fedfb1d6ec03c07f881c1327ae56c35
parent 468370 a6d341deddb0e73c1bba5109df5ec84b7e616020
child 468372 9617921cebc374474b9fb42f6543841041d4cfda
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1451672
milestone61.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 1451672 - part 18: Rename EditorBase::CloneAttributes() to EditorBase::CloneAttributesWithTransaction() r=m_kato MozReview-Commit-ID: 5tL31gRDVc9
editor/libeditor/EditorBase.cpp
editor/libeditor/EditorBase.h
editor/libeditor/HTMLEditor.cpp
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -1675,17 +1675,17 @@ EditorBase::ReplaceContainerWithTransact
   RefPtr<Element> newContainer = CreateHTMLContent(&aTagName);
   if (NS_WARN_IF(!newContainer)) {
     return nullptr;
   }
 
   // Set or clone attribute if needed.
   if (aCloneAllAttributes) {
     MOZ_ASSERT(&aAttribute == nsGkAtoms::_empty);
-    CloneAttributes(newContainer, &aOldContainer);
+    CloneAttributesWithTransaction(*newContainer, aOldContainer);
   } else if (&aAttribute != nsGkAtoms::_empty) {
     nsresult rv =
       newContainer->SetAttr(kNameSpaceID_None, &aAttribute, aAttributeValue,
                             true);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return nullptr;
     }
   }
@@ -2537,67 +2537,73 @@ EditorBase::CloneAttributeWithTransactio
   return RemoveAttributeWithTransaction(aDestElement, aAttribute);
 }
 
 /**
  * @param aDest     Must be a DOM element.
  * @param aSource   Must be a DOM element.
  */
 NS_IMETHODIMP
-EditorBase::CloneAttributes(nsIDOMNode* aDest,
-                            nsIDOMNode* aSource)
-{
-  NS_ENSURE_TRUE(aDest && aSource, NS_ERROR_NULL_POINTER);
-
-  nsCOMPtr<Element> dest = do_QueryInterface(aDest);
-  nsCOMPtr<Element> source = do_QueryInterface(aSource);
-  NS_ENSURE_TRUE(dest && source, NS_ERROR_NO_INTERFACE);
-
-  CloneAttributes(dest, source);
+EditorBase::CloneAttributes(nsIDOMNode* aDestDOMElement,
+                            nsIDOMNode* aSourceDOMElement)
+{
+  nsCOMPtr<Element> destElement = do_QueryInterface(aDestDOMElement);
+  nsCOMPtr<Element> sourceElement = do_QueryInterface(aSourceDOMElement);
+  if (NS_WARN_IF(!destElement) || NS_WARN_IF(!sourceElement)) {
+    return NS_ERROR_INVALID_ARG;
+  }
+
+  CloneAttributesWithTransaction(*destElement, *sourceElement);
 
   return NS_OK;
 }
 
 void
-EditorBase::CloneAttributes(Element* aDest,
-                            Element* aSource)
-{
-  MOZ_ASSERT(aDest && aSource);
-
+EditorBase::CloneAttributesWithTransaction(Element& aDestElement,
+                                           Element& aSourceElement)
+{
   AutoPlaceholderBatch beginBatching(this);
 
   // Use transaction system for undo only if destination is already in the
   // document
-  NS_ENSURE_TRUE(GetRoot(), );
-  bool destInBody = GetRoot()->Contains(aDest);
+  Element* rootElement = GetRoot();
+  if (NS_WARN_IF(!rootElement)) {
+    return;
+  }
+
+  OwningNonNull<Element> destElement(aDestElement);
+  OwningNonNull<Element> sourceElement(aDestElement);
+  bool isDestElementInBody = rootElement->Contains(destElement);
 
   // Clear existing attributes
-  RefPtr<nsDOMAttributeMap> destAttributes = aDest->Attributes();
+  RefPtr<nsDOMAttributeMap> destAttributes = destElement->Attributes();
   while (RefPtr<Attr> attr = destAttributes->Item(0)) {
-    if (destInBody) {
-      RemoveAttributeWithTransaction(*aDest, *attr->NodeInfo()->NameAtom());
+    if (isDestElementInBody) {
+      RemoveAttributeWithTransaction(destElement,
+                                     *attr->NodeInfo()->NameAtom());
     } else {
-      aDest->UnsetAttr(kNameSpaceID_None, attr->NodeInfo()->NameAtom(), true);
+      destElement->UnsetAttr(kNameSpaceID_None, attr->NodeInfo()->NameAtom(),
+                             true);
     }
   }
 
   // Set just the attributes that the source element has
-  RefPtr<nsDOMAttributeMap> sourceAttributes = aSource->Attributes();
+  RefPtr<nsDOMAttributeMap> sourceAttributes = sourceElement->Attributes();
   uint32_t sourceCount = sourceAttributes->Length();
   for (uint32_t i = 0; i < sourceCount; i++) {
     RefPtr<Attr> attr = sourceAttributes->Item(i);
     nsAutoString value;
     attr->GetValue(value);
-    if (destInBody) {
-      SetAttributeOrEquivalent(aDest, attr->NodeInfo()->NameAtom(), value,
+    if (isDestElementInBody) {
+      SetAttributeOrEquivalent(destElement, attr->NodeInfo()->NameAtom(), value,
                                false);
     } else {
       // The element is not inserted in the document yet, we don't want to put
       // a transaction on the UndoStack
-      SetAttributeOrEquivalent(aDest, attr->NodeInfo()->NameAtom(), value,
+      SetAttributeOrEquivalent(destElement, attr->NodeInfo()->NameAtom(), value,
                                true);
     }
   }
 }
 
 nsresult
 EditorBase::ScrollSelectionIntoView(bool aScrollToAnchor)
 {
--- a/editor/libeditor/EditorBase.h
+++ b/editor/libeditor/EditorBase.h
@@ -420,17 +420,23 @@ public:
                                   nsAtom& aAttribute,
                                   const nsAString& aAttributeValue)
   {
     return ReplaceContainerWithTransactionInternal(aOldContainer, aTagName,
                                                    aAttribute,
                                                    aAttributeValue, false);
   }
 
-  void CloneAttributes(Element* aDest, Element* aSource);
+  /**
+   * CloneAttributesWithTransaction() clones all attributes from
+   * aSourceElement to aDestElement after removing all attributes in
+   * aDestElement.
+   */
+  void CloneAttributesWithTransaction(Element& aDestElement,
+                                      Element& aSourceElement);
 
   /**
    * RemoveContainerWithTransaction() removes aElement from the DOM tree and
    * moves all its children to the parent of aElement.
    *
    * @param aElement            The element to be removed.
    */
   nsresult RemoveContainerWithTransaction(Element& aElement);
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -1283,18 +1283,20 @@ HTMLEditor::ReplaceHeadContentsWithHTML(
 NS_IMETHODIMP
 HTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
 {
   CommitComposition();
 
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
 
-  nsCOMPtr<Element> bodyElement = GetRoot();
-  NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER);
+  RefPtr<Element> rootElement = GetRoot();
+  if (NS_WARN_IF(!rootElement)) {
+    return NS_ERROR_NULL_POINTER;
+  }
 
   // Find where the <body> tag starts.
   nsReadingIterator<char16_t> beginbody;
   nsReadingIterator<char16_t> endbody;
   aSourceString.BeginReading(beginbody);
   aSourceString.EndReading(endbody);
   bool foundbody = CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<body"),
                                                  beginbody, endbody);
@@ -1405,21 +1407,22 @@ HTMLEditor::RebuildDocumentFromSource(co
     } else {
       // assume there is no head, the entire source is body
       nsresult rv = LoadHTML(body + aSourceString);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     }
 
-    nsCOMPtr<Element> divElement =
+    RefPtr<Element> divElement =
       CreateElementWithDefaults(NS_LITERAL_STRING("div"));
-    NS_ENSURE_TRUE(divElement, NS_ERROR_FAILURE);
-
-    CloneAttributes(bodyElement, divElement);
+    if (NS_WARN_IF(!divElement)) {
+      return NS_ERROR_FAILURE;
+    }
+    CloneAttributesWithTransaction(*rootElement, *divElement);
 
     return BeginningOfDocument();
   }
 
   rv = LoadHTML(Substring(beginbody, endtotal));
   NS_ENSURE_SUCCESS(rv, rv);
 
   // Now we must copy attributes user might have edited on the <body> tag
@@ -1448,17 +1451,17 @@ HTMLEditor::RebuildDocumentFromSource(co
     range->CreateContextualFragment(bodyTag, erv);
   NS_ENSURE_TRUE(!erv.Failed(), erv.StealNSResult());
   NS_ENSURE_TRUE(docfrag, NS_ERROR_NULL_POINTER);
 
   nsCOMPtr<nsIContent> child = docfrag->GetFirstChild();
   NS_ENSURE_TRUE(child && child->IsElement(), NS_ERROR_NULL_POINTER);
 
   // Copy all attributes from the div child to current body element
-  CloneAttributes(bodyElement, child->AsElement());
+  CloneAttributesWithTransaction(*rootElement, *child->AsElement());
 
   // place selection at first editable content
   return BeginningOfDocument();
 }
 
 EditorRawDOMPoint
 HTMLEditor::GetBetterInsertionPointFor(nsINode& aNodeToInsert,
                                        const EditorRawDOMPoint& aPointToInsert)
@@ -4557,17 +4560,17 @@ HTMLEditor::CopyLastEditableChildStyles(
         EditorRawDOMPoint atStartOfNewBlock(newBlock, 0);
         deepestStyle = newStyles =
           CreateNodeWithTransaction(*childElement->NodeInfo()->NameAtom(),
                                     atStartOfNewBlock);
         if (NS_WARN_IF(!newStyles)) {
           return NS_ERROR_FAILURE;
         }
       }
-      CloneAttributes(newStyles, childElement);
+      CloneAttributesWithTransaction(*newStyles, *childElement);
     }
     childElement = childElement->GetParentElement();
   }
   if (deepestStyle) {
     RefPtr<Element> retVal = CreateBR(EditorRawDOMPoint(deepestStyle, 0));
     retVal.forget(aOutBrNode);
     NS_ENSURE_STATE(*aOutBrNode);
   }