Bug 1476906 - Change nsIHTMLEditor::ReplaceHeadContentsWithHTML() to a non-virtual method of HTMLEditor r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Thu, 19 Jul 2018 21:31:19 +0900
changeset 485494 bd51af94b40860c028013d53c44d81dddfc2608b
parent 485493 867794e06da1b1c19e20b7e74b5e560559162d9e
child 485495 19fe41443952b92975fbfb6854f1bf73c7b2f4a2
push id1815
push userffxbld-merge
push dateMon, 15 Oct 2018 10:40:45 +0000
treeherdermozilla-release@18d4c09e9378 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1476906
milestone63.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 1476906 - Change nsIHTMLEditor::ReplaceHeadContentsWithHTML() to a non-virtual method of HTMLEditor r=m_kato nsIHTMLEditor::ReplaceHeadContentsWithHTML() is used only by HTMLEditor internally. So, it shouldn't be an nsIHTMLEditor's method. This patch changes it as a non-virtual method of HTMLEditor and rename it to ReplaceHeadContentsWithSourceWithTransaction() for consistency with other methods. MozReview-Commit-ID: GT3maKEbZuP
editor/libeditor/HTMLEditor.cpp
editor/libeditor/HTMLEditor.h
editor/nsIHTMLEditor.idl
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -1209,24 +1209,19 @@ HTMLEditor::CollapseSelectionToDeepestNo
       break;
     }
     node = child;
   }
 
   selection->Collapse(node, 0);
 }
 
-
-/**
- * This is mostly like InsertHTMLWithCharsetAndContext, but we can't use that
- * because it is selection-based and the rules code won't let us edit under the
- * <head> node
- */
-NS_IMETHODIMP
-HTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
+nsresult
+HTMLEditor::ReplaceHeadContentsWithSourceWithTransaction(
+              const nsAString& aSourceToInsert)
 {
   // don't do any post processing, rules get confused
   AutoTopLevelEditSubActionNotifier
     maybeTopLevelEditSubAction(*this,
                                EditSubAction::eReplaceHeadWithHTMLSource,
                                nsIEditor::eNone);
   RefPtr<Selection> selection = GetSelection();
   NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
@@ -1239,65 +1234,73 @@ HTMLEditor::ReplaceHeadContentsWithHTML(
   // XXX We're using AutoTopLevelEditSubActionNotifier above...
   nsCOMPtr<nsIDocument> document = GetDocument();
   if (NS_WARN_IF(!document)) {
     return NS_ERROR_NOT_INITIALIZED;
   }
 
   RefPtr<nsContentList> nodeList =
     document->GetElementsByTagName(NS_LITERAL_STRING("head"));
-  NS_ENSURE_TRUE(nodeList, NS_ERROR_NULL_POINTER);
+  if (NS_WARN_IF(!nodeList)) {
+    return NS_ERROR_FAILURE;
+  }
 
   nsCOMPtr<nsIContent> headNode = nodeList->Item(0);
-  NS_ENSURE_TRUE(headNode, NS_ERROR_NULL_POINTER);
+  if (NS_WARN_IF(!headNode)) {
+    return NS_ERROR_FAILURE;
+  }
 
   // First, make sure there are no return chars in the source.  Bad things
   // happen if you insert returns (instead of dom newlines, \n) into an editor
   // document.
-  nsAutoString inputString (aSourceToInsert);  // hope this does copy-on-write
+  nsAutoString inputString(aSourceToInsert);
 
   // Windows linebreaks: Map CRLF to LF:
-  inputString.ReplaceSubstring(u"\r\n", u"\n");
+  inputString.ReplaceSubstring(NS_LITERAL_STRING("\r\n"),
+                               NS_LITERAL_STRING("\n"));
 
   // Mac linebreaks: Map any remaining CR to LF:
-  inputString.ReplaceSubstring(u"\r", u"\n");
+  inputString.ReplaceSubstring(NS_LITERAL_STRING("\r"),
+                               NS_LITERAL_STRING("\n"));
 
   AutoPlaceholderBatch beginBatching(this);
 
   // Get the first range in the selection, for context:
   RefPtr<nsRange> range = selection->GetRangeAt(0);
-  NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
+  if (NS_WARN_IF(!range)) {
+    return NS_ERROR_FAILURE;
+  }
 
   ErrorResult err;
-  RefPtr<DocumentFragment> docfrag =
+  RefPtr<DocumentFragment> documentFragment =
     range->CreateContextualFragment(inputString, err);
 
   // XXXX BUG 50965: This is not returning the text between <title>...</title>
   // Special code is needed in JS to handle title anyway, so it doesn't matter!
 
   if (err.Failed()) {
-#ifdef DEBUG
-    printf("Couldn't create contextual fragment: error was %X\n",
-           err.ErrorCodeAsInt());
-#endif
     return err.StealNSResult();
   }
-  NS_ENSURE_TRUE(docfrag, NS_ERROR_NULL_POINTER);
+  if (NS_WARN_IF(!documentFragment)) {
+    return NS_ERROR_FAILURE;
+  }
 
   // First delete all children in head
   while (nsCOMPtr<nsIContent> child = headNode->GetFirstChild()) {
     nsresult rv = DeleteNodeWithTransaction(*child);
-    NS_ENSURE_SUCCESS(rv, rv);
+    if (NS_WARN_IF(NS_FAILED(rv))) {
+      return rv;
+    }
   }
 
   // Now insert the new nodes
   int32_t offsetOfNewNode = 0;
 
   // Loop over the contents of the fragment and move into the document
-  while (nsCOMPtr<nsIContent> child = docfrag->GetFirstChild()) {
+  while (nsCOMPtr<nsIContent> child = documentFragment->GetFirstChild()) {
     nsresult rv =
       InsertNodeWithTransaction(*child,
                                 EditorRawDOMPoint(headNode, offsetOfNewNode++));
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
   }
 
@@ -1357,57 +1360,62 @@ HTMLEditor::RebuildDocumentFromSource(co
   AutoPlaceholderBatch beginBatching(this);
 
   nsReadingIterator<char16_t> endtotal;
   aSourceString.EndReading(endtotal);
 
   if (foundhead) {
     if (foundclosehead) {
       nsresult rv =
-        ReplaceHeadContentsWithHTML(Substring(beginhead, beginclosehead));
+        ReplaceHeadContentsWithSourceWithTransaction(
+          Substring(beginhead, beginclosehead));
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     } else if (foundbody) {
       nsresult rv =
-        ReplaceHeadContentsWithHTML(Substring(beginhead, beginbody));
+        ReplaceHeadContentsWithSourceWithTransaction(
+          Substring(beginhead, beginbody));
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     } else {
       // XXX Without recourse to some parser/content sink/docshell hackery we
       // don't really know where the head ends and the body begins so we assume
       // that there is no body
-      nsresult rv = ReplaceHeadContentsWithHTML(Substring(beginhead, endtotal));
+      nsresult rv =
+        ReplaceHeadContentsWithSourceWithTransaction(
+          Substring(beginhead, endtotal));
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     }
   } else {
     nsReadingIterator<char16_t> begintotal;
     aSourceString.BeginReading(begintotal);
     NS_NAMED_LITERAL_STRING(head, "<head>");
     if (foundclosehead) {
       nsresult rv =
-        ReplaceHeadContentsWithHTML(head + Substring(begintotal,
-                                                     beginclosehead));
+        ReplaceHeadContentsWithSourceWithTransaction(
+          head + Substring(begintotal, beginclosehead));
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     } else if (foundbody) {
-      nsresult rv = ReplaceHeadContentsWithHTML(head + Substring(begintotal,
-                                                                 beginbody));
+      nsresult rv =
+        ReplaceHeadContentsWithSourceWithTransaction(
+          head + Substring(begintotal, beginbody));
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     } else {
       // XXX Without recourse to some parser/content sink/docshell hackery we
       // don't really know where the head ends and the body begins so we assume
       // that there is no head
-      nsresult rv = ReplaceHeadContentsWithHTML(head);
+      nsresult rv = ReplaceHeadContentsWithSourceWithTransaction(head);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
     }
   }
 
   nsresult rv = SelectAll();
   NS_ENSURE_SUCCESS(rv, rv);
--- a/editor/libeditor/HTMLEditor.h
+++ b/editor/libeditor/HTMLEditor.h
@@ -778,16 +778,27 @@ protected: // Shouldn't be used by frien
    * InsertBrElementAtSelectionWithTransaction() inserts a new <br> element at
    * selection.  If there is non-collapsed selection ranges, the selected
    * ranges is deleted first.
    */
   nsresult InsertBrElementAtSelectionWithTransaction();
 
   nsresult LoadHTML(const nsAString& aInputString);
 
+  /**
+   * ReplaceHeadContentsWithSourceWithTransaction() replaces all children of
+   * <head> element with given source code.  This is undoable.
+   *
+   * @param aSourceToInsert     HTML source fragment to replace the children
+   *                            of <head> element.
+   */
+  nsresult
+  ReplaceHeadContentsWithSourceWithTransaction(
+    const nsAString& aSourceToInsert);
+
   nsresult GetCSSBackgroundColorState(bool* aMixed, nsAString& aOutColor,
                                       bool aBlockLevel);
   nsresult GetHTMLBackgroundColorState(bool* aMixed, nsAString& outColor);
 
   nsresult GetLastCellInRow(nsINode* aRowNode,
                             nsINode** aCellNode);
 
   nsresult GetCellFromRange(nsRange* aRange, Element** aCell);
--- a/editor/nsIHTMLEditor.idl
+++ b/editor/nsIHTMLEditor.idl
@@ -343,21 +343,16 @@ interface nsIHTMLEditor : nsISupports
    *      (an "A" tag with the "href" attribute set)
    *    Use "anchor" or "namedanchor" to get a named anchor node
    *      (an "A" tag with the "name" attribute set)
    * @return the element as described above
    */
   nsISupports getSelectedElement(in AString aTagName);
 
   /**
-   * Replace all children of <HEAD> with string of HTML source
-   */
-  void replaceHeadContentsWithHTML(in AString aSourceToInsert);
-
-  /**
    * Return a new element with default attribute values
    *
    * This does not rely on the selection, and is not sensitive to context.
    *
    * Used primarily to supply new element for various insert element dialogs
    *  (Image, Link, NamedAnchor, Table, and HorizontalRule
    *   are the only returned elements as of 7/25/99)
    *