Bug 1332984 - Clean up usage of nsIDOMCharacterData into editor. r?masayuki draft
authorMakoto Kato <m_kato@ga2.so-net.ne.jp>
Mon, 23 Jan 2017 14:14:30 +0900
changeset 464866 ba9c3c665cf7d91efef5b377b2d8872183c481b3
parent 464862 d5e37c0a776f1f2c21ddac4612529f819e13733b
child 543021 da37286a7ed81c4c4eb311dff0730055dbd20894
push id42461
push userm_kato@ga2.so-net.ne.jp
push dateMon, 23 Jan 2017 05:21:11 +0000
reviewersmasayuki
bugs1332984
milestone53.0a1
Bug 1332984 - Clean up usage of nsIDOMCharacterData into editor. r?masayuki Some uses nsIDOMCharacterData to get the attribute of text node. But, by using Text object, we don't need nsIDOMCharacter. So we should use Text object instead of nsIDOMCharacterData instead if possible. MozReview-Commit-ID: 1cwTUcecFj3
editor/libeditor/CreateElementTransaction.cpp
editor/libeditor/DeleteRangeTransaction.cpp
editor/libeditor/EditorBase.cpp
editor/libeditor/HTMLEditRules.cpp
editor/libeditor/TextEditor.cpp
--- a/editor/libeditor/CreateElementTransaction.cpp
+++ b/editor/libeditor/CreateElementTransaction.cpp
@@ -14,17 +14,16 @@
 #include "mozilla/Casting.h"
 #include "mozilla/EditorBase.h"
 
 #include "nsAlgorithm.h"
 #include "nsAString.h"
 #include "nsDebug.h"
 #include "nsError.h"
 #include "nsIContent.h"
-#include "nsIDOMCharacterData.h"
 #include "nsIEditor.h"
 #include "nsINode.h"
 #include "nsISupportsUtils.h"
 #include "nsMemory.h"
 #include "nsReadableUtils.h"
 #include "nsStringFwd.h"
 #include "nsString.h"
 
--- a/editor/libeditor/DeleteRangeTransaction.cpp
+++ b/editor/libeditor/DeleteRangeTransaction.cpp
@@ -11,17 +11,16 @@
 #include "mozilla/EditorBase.h"
 #include "mozilla/dom/Selection.h"
 #include "mozilla/mozalloc.h"
 #include "nsCOMPtr.h"
 #include "nsDebug.h"
 #include "nsError.h"
 #include "nsIContent.h"
 #include "nsIContentIterator.h"
-#include "nsIDOMCharacterData.h"
 #include "nsINode.h"
 #include "nsAString.h"
 
 namespace mozilla {
 
 using namespace dom;
 
 // note that aEditorBase is not refcounted
--- a/editor/libeditor/EditorBase.cpp
+++ b/editor/libeditor/EditorBase.cpp
@@ -2904,25 +2904,23 @@ EditorBase::JoinNodesImpl(nsINode* aNode
       }
 
       savedRanges.AppendElement(range);
     }
   }
 
   // OK, ready to do join now.
   // If it's a text node, just shuffle around some text.
-  nsCOMPtr<nsIDOMCharacterData> keepNodeAsText( do_QueryInterface(aNodeToKeep) );
-  nsCOMPtr<nsIDOMCharacterData> joinNodeAsText( do_QueryInterface(aNodeToJoin) );
-  if (keepNodeAsText && joinNodeAsText) {
+  if (IsTextNode(aNodeToKeep) && IsTextNode(aNodeToJoin)) {
     nsAutoString rightText;
     nsAutoString leftText;
-    keepNodeAsText->GetData(rightText);
-    joinNodeAsText->GetData(leftText);
+    aNodeToKeep->GetAsText()->GetData(rightText);
+    aNodeToJoin->GetAsText()->GetData(leftText);
     leftText += rightText;
-    keepNodeAsText->SetData(leftText);
+    aNodeToKeep->GetAsText()->SetData(leftText);
   } else {
     // Otherwise it's an interior node, so shuffle around the children.
     nsCOMPtr<nsINodeList> childNodes = aNodeToJoin->ChildNodes();
     MOZ_ASSERT(childNodes);
 
     // Remember the first child in aNodeToKeep, we'll insert all the children of aNodeToJoin in front of it
     // GetFirstChild returns nullptr firstNode if aNodeToKeep has no children, that's OK.
     nsCOMPtr<nsIContent> firstNode = aNodeToKeep->GetFirstChild();
--- a/editor/libeditor/HTMLEditRules.cpp
+++ b/editor/libeditor/HTMLEditRules.cpp
@@ -7430,19 +7430,19 @@ HTMLEditRules::AdjustSelection(Selection
       }
 
       // we know we can skip the rest of this routine given the cirumstance
       return CreateMozBR(GetAsDOMNode(selNode), selOffset);
     }
   }
 
   // are we in a text node?
-  nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(selNode);
-  if (textNode)
+  if (EditorBase::IsTextNode(selNode)) {
     return NS_OK; // we LIKE it when we are in a text node.  that RULZ
+  }
 
   // do we need to insert a special mozBR?  We do if we are:
   // 1) prior node is in same block where selection is AND
   // 2) prior node is a br AND
   // 3) that br is not visible
 
   NS_ENSURE_STATE(mHTMLEditor);
   nsCOMPtr<nsIContent> nearNode =
--- a/editor/libeditor/TextEditor.cpp
+++ b/editor/libeditor/TextEditor.cpp
@@ -27,17 +27,16 @@
 #include "nsCopySupport.h"
 #include "nsDebug.h"
 #include "nsDependentSubstring.h"
 #include "nsError.h"
 #include "nsGkAtoms.h"
 #include "nsIClipboard.h"
 #include "nsIContent.h"
 #include "nsIContentIterator.h"
-#include "nsIDOMCharacterData.h"
 #include "nsIDOMDocument.h"
 #include "nsIDOMElement.h"
 #include "nsIDOMEventTarget.h"
 #include "nsIDOMKeyEvent.h"
 #include "nsIDOMNode.h"
 #include "nsIDOMNodeList.h"
 #include "nsIDocumentEncoder.h"
 #include "nsIEditRules.h"
@@ -458,53 +457,56 @@ TextEditor::CreateBRImpl(nsCOMPtr<nsIDOM
                          int32_t* aInOutOffset,
                          nsCOMPtr<nsIDOMNode>* outBRNode,
                          EDirection aSelect)
 {
   NS_ENSURE_TRUE(aInOutParent && *aInOutParent && aInOutOffset && outBRNode, NS_ERROR_NULL_POINTER);
   *outBRNode = nullptr;
 
   // we need to insert a br.  unfortunately, we may have to split a text node to do it.
-  nsCOMPtr<nsIDOMNode> node = *aInOutParent;
+  nsCOMPtr<nsINode> node = do_QueryInterface(*aInOutParent);
   int32_t theOffset = *aInOutOffset;
-  nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(node);
-  NS_NAMED_LITERAL_STRING(brType, "br");
-  nsCOMPtr<nsIDOMNode> brNode;
-  if (nodeAsText) {
+  RefPtr<Element> brNode;
+  if (IsTextNode(node)) {
     int32_t offset;
-    uint32_t len;
-    nodeAsText->GetLength(&len);
-    nsCOMPtr<nsIDOMNode> tmp = GetNodeLocation(node, &offset);
+    nsCOMPtr<nsINode> tmp = GetNodeLocation(node, &offset);
     NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
     if (!theOffset) {
       // we are already set to go
-    } else if (theOffset == (int32_t)len) {
+    } else if (theOffset == static_cast<int32_t>(node->Length())) {
       // update offset to point AFTER the text node
       offset++;
     } else {
       // split the text node
-      nsresult rv = SplitNode(node, theOffset, getter_AddRefs(tmp));
-      NS_ENSURE_SUCCESS(rv, rv);
+      ErrorResult rv;
+      SplitNode(*node->AsContent(), theOffset, rv);
+      if (NS_WARN_IF(rv.Failed())) {
+        return rv.StealNSResult();
+      }
       tmp = GetNodeLocation(node, &offset);
     }
     // create br
-    nsresult rv = CreateNode(brType, tmp, offset, getter_AddRefs(brNode));
-    NS_ENSURE_SUCCESS(rv, rv);
-    *aInOutParent = tmp;
+    brNode = CreateNode(nsGkAtoms::br, tmp, offset);
+    if (NS_WARN_IF(!brNode)) {
+      return NS_ERROR_FAILURE;
+    }
+    *aInOutParent = GetAsDOMNode(tmp);
     *aInOutOffset = offset+1;
   } else {
-    nsresult rv = CreateNode(brType, node, theOffset, getter_AddRefs(brNode));
-    NS_ENSURE_SUCCESS(rv, rv);
+    brNode = CreateNode(nsGkAtoms::br, node, theOffset);
+    if (NS_WARN_IF(!brNode)) {
+      return NS_ERROR_FAILURE;
+    }
     (*aInOutOffset)++;
   }
 
-  *outBRNode = brNode;
+  *outBRNode = GetAsDOMNode(brNode);
   if (*outBRNode && (aSelect != eNone)) {
     int32_t offset;
-    nsCOMPtr<nsIDOMNode> parent = GetNodeLocation(*outBRNode, &offset);
+    nsCOMPtr<nsINode> parent = GetNodeLocation(brNode, &offset);
 
     RefPtr<Selection> selection = GetSelection();
     NS_ENSURE_STATE(selection);
     if (aSelect == eNext) {
       // position selection after br
       selection->SetInterlinePosition(true);
       selection->Collapse(parent, offset + 1);
     } else if (aSelect == ePrevious) {
@@ -593,39 +595,33 @@ TextEditor::ExtendSelectionForDelete(Sel
         // Don't set aAction to eNone (see Bug 502259)
         break;
       case ePrevious: {
         // Only extend the selection where the selection is after a UTF-16
         // surrogate pair or a variation selector.
         // For other cases we don't want to do that, in order
         // to make sure that pressing backspace will only delete the last
         // typed character.
-        nsCOMPtr<nsIDOMNode> node;
+        nsCOMPtr<nsINode> node;
         int32_t offset;
         rv = GetStartNodeAndOffset(aSelection, getter_AddRefs(node), &offset);
         NS_ENSURE_SUCCESS(rv, rv);
         NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
 
         // node might be anonymous DIV, so we find better text node
         FindBetterInsertionPoint(node, offset);
 
         if (IsTextNode(node)) {
-          nsCOMPtr<nsIDOMCharacterData> charData = do_QueryInterface(node);
-          if (charData) {
-            nsAutoString data;
-            rv = charData->GetData(data);
-            NS_ENSURE_SUCCESS(rv, rv);
-
-            if ((offset > 1 &&
-                 NS_IS_LOW_SURROGATE(data[offset - 1]) &&
-                 NS_IS_HIGH_SURROGATE(data[offset - 2])) ||
-                (offset > 0 &&
-                 gfxFontUtils::IsVarSelector(data[offset - 1]))) {
-              rv = selCont->CharacterExtendForBackspace();
-            }
+          const nsTextFragment* data = node->GetAsText()->GetText();
+          if ((offset > 1 &&
+               NS_IS_LOW_SURROGATE(data->CharAt(offset - 1)) &&
+               NS_IS_HIGH_SURROGATE(data->CharAt(offset - 2))) ||
+              (offset > 0 &&
+               gfxFontUtils::IsVarSelector(data->CharAt(offset - 1)))) {
+            rv = selCont->CharacterExtendForBackspace();
           }
         }
         break;
       }
       case eToBeginningOfLine:
         selCont->IntraLineMove(true, false);          // try to move to end
         rv = selCont->IntraLineMove(false, true); // select to beginning
         *aAction = eNone;
@@ -934,22 +930,19 @@ TextEditor::GetTextLength(int32_t* aCoun
 
   nsCOMPtr<nsIContentIterator> iter =
     do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   uint32_t totalLength = 0;
   iter->Init(rootElement);
   for (; !iter->IsDone(); iter->Next()) {
-    nsCOMPtr<nsIDOMNode> currentNode = do_QueryInterface(iter->GetCurrentNode());
-    nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(currentNode);
-    if (textNode && IsEditable(currentNode)) {
-      uint32_t length;
-      textNode->GetLength(&length);
-      totalLength += length;
+    nsCOMPtr<nsINode> currentNode = iter->GetCurrentNode();
+    if (IsTextNode(currentNode) && IsEditable(currentNode)) {
+      totalLength += currentNode->Length();
     }
   }
 
   *aCount = totalLength;
   return NS_OK;
 }
 
 NS_IMETHODIMP