Bug 1190172 - Back out for now, requested by Masayuki at bug 1191356 comment 23
authorAryeh Gregor <ayg@aryeh.name>
Wed, 20 Apr 2016 20:44:30 +0300
changeset 317910 63704b357cc2b6f707561e09151fd6ed48e34276
parent 317909 89bcf18a1f4d32436acb4f21d60b57b74f30c437
child 317911 412dcc9441b2d77535d85867c65ec28672704252
push id9480
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 17:12:58 +0000
treeherdermozilla-aurora@0d6a91c76a9e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs1190172, 1191356
milestone48.0a1
Bug 1190172 - Back out for now, requested by Masayuki at bug 1191356 comment 23
editor/libeditor/nsHTMLEditRules.cpp
editor/libeditor/nsHTMLEditRules.h
editor/libeditor/nsHTMLEditor.h
editor/libeditor/nsHTMLEditorStyle.cpp
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -1279,20 +1279,21 @@ nsHTMLEditRules::WillInsertText(EditActi
   WillInsert(*aSelection, aCancel);
   // initialize out param
   // we want to ignore result of WillInsert()
   *aCancel = false;
 
   // we need to get the doc
   NS_ENSURE_STATE(mHTMLEditor);
   nsCOMPtr<nsIDocument> doc = mHTMLEditor->GetDocument();
-  NS_ENSURE_STATE(doc);
+  nsCOMPtr<nsIDOMDocument> domDoc = mHTMLEditor->GetDOMDocument();
+  NS_ENSURE_TRUE(doc && domDoc, NS_ERROR_NOT_INITIALIZED);
 
   // for every property that is set, insert a new inline style node
-  res = CreateStyleForInsertText(*aSelection, *doc);
+  res = CreateStyleForInsertText(aSelection, domDoc);
   NS_ENSURE_SUCCESS(res, res);
 
   // get the (collapsed) selection location
   NS_ENSURE_STATE(mHTMLEditor);
   NS_ENSURE_STATE(aSelection->GetRangeAt(0));
   nsCOMPtr<nsINode> selNode = aSelection->GetRangeAt(0)->GetStartParent();
   int32_t selOffset = aSelection->GetRangeAt(0)->StartOffset();
   NS_ENSURE_STATE(selNode);
@@ -4377,26 +4378,26 @@ nsHTMLEditRules::ConvertListType(Element
 
 
 ///////////////////////////////////////////////////////////////////////////
 // CreateStyleForInsertText:  take care of clearing and setting appropriate
 //                            style nodes for text insertion.
 //
 //
 nsresult
-nsHTMLEditRules::CreateStyleForInsertText(Selection& aSelection,
-                                          nsIDocument& aDoc)
-{
-  MOZ_ASSERT(mHTMLEditor->mTypeInState);
+nsHTMLEditRules::CreateStyleForInsertText(Selection* aSelection,
+                                          nsIDOMDocument *aDoc)
+{
+  MOZ_ASSERT(aSelection && aDoc && mHTMLEditor->mTypeInState);
 
   nsresult res;
   bool weDidSomething = false;
-  NS_ENSURE_STATE(aSelection.GetRangeAt(0));
-  nsCOMPtr<nsINode> node = aSelection.GetRangeAt(0)->GetStartParent();
-  int32_t offset = aSelection.GetRangeAt(0)->StartOffset();
+  NS_ENSURE_STATE(aSelection->GetRangeAt(0));
+  nsCOMPtr<nsINode> node = aSelection->GetRangeAt(0)->GetStartParent();
+  int32_t offset = aSelection->GetRangeAt(0)->StartOffset();
 
   // next examine our present style and make sure default styles are either
   // present or explicitly overridden.  If neither, add the default style to
   // the TypeInState
   int32_t length = mHTMLEditor->mDefaultStyles.Length();
   for (int32_t j = 0; j < length; j++) {
     PropItem* propItem = mHTMLEditor->mDefaultStyles[j];
     MOZ_ASSERT(propItem);
@@ -4419,22 +4420,23 @@ nsHTMLEditRules::CreateStyleForInsertTex
 
     if (!bAny) {
       // no style set for this prop/attr
       mHTMLEditor->mTypeInState->SetProp(propItem->tag, propItem->attr,
                                          propItem->value);
     }
   }
 
-  nsCOMPtr<Element> rootElement = aDoc.GetRootElement();
-  NS_ENSURE_STATE(rootElement);
+  nsCOMPtr<nsIDOMElement> rootElement;
+  res = aDoc->GetDocumentElement(getter_AddRefs(rootElement));
+  NS_ENSURE_SUCCESS(res, res);
 
   // process clearing any styles first
   nsAutoPtr<PropItem> item(mHTMLEditor->mTypeInState->TakeClearProperty());
-  while (item && node != rootElement) {
+  while (item && GetAsDOMNode(node) != rootElement) {
     NS_ENSURE_STATE(mHTMLEditor);
     res = mHTMLEditor->ClearStyle(address_of(node), &offset,
                                   item->tag, &item->attr);
     NS_ENSURE_SUCCESS(res, res);
     item = mHTMLEditor->mTypeInState->TakeClearProperty();
     weDidSomething = true;
   }
 
@@ -4450,46 +4452,51 @@ nsHTMLEditRules::CreateStyleForInsertTex
       NS_ENSURE_STATE(mHTMLEditor);
       offset = mHTMLEditor->SplitNodeDeep(*text, *text, offset);
       NS_ENSURE_STATE(offset != -1);
       node = node->GetParentNode();
     }
     if (!mHTMLEditor->IsContainer(node)) {
       return NS_OK;
     }
-    OwningNonNull<Text> newNode = aDoc.CreateTextNode(EmptyString());
+    nsCOMPtr<nsIContent> newNode;
+    nsCOMPtr<nsIDOMText> nodeAsText;
+    res = aDoc->CreateTextNode(EmptyString(), getter_AddRefs(nodeAsText));
+    NS_ENSURE_SUCCESS(res, res);
+    NS_ENSURE_TRUE(nodeAsText, NS_ERROR_NULL_POINTER);
+    newNode = do_QueryInterface(nodeAsText);
     NS_ENSURE_STATE(mHTMLEditor);
-    res = mHTMLEditor->InsertNode(newNode, *node, offset);
+    res = mHTMLEditor->InsertNode(*newNode, *node, offset);
     NS_ENSURE_SUCCESS(res, res);
     node = newNode;
     offset = 0;
     weDidSomething = true;
 
     if (relFontSize) {
       // dir indicated bigger versus smaller.  1 = bigger, -1 = smaller
-      nsHTMLEditor::FontSize dir = relFontSize > 0 ?
-        nsHTMLEditor::FontSize::incr : nsHTMLEditor::FontSize::decr;
+      int32_t dir = relFontSize > 0 ? 1 : -1;
       for (int32_t j = 0; j < DeprecatedAbs(relFontSize); j++) {
         NS_ENSURE_STATE(mHTMLEditor);
-        res = mHTMLEditor->RelativeFontChangeOnTextNode(dir, newNode, 0, -1);
+        res = mHTMLEditor->RelativeFontChangeOnTextNode(dir, nodeAsText,
+                                                        0, -1);
         NS_ENSURE_SUCCESS(res, res);
       }
     }
 
     while (item) {
       NS_ENSURE_STATE(mHTMLEditor);
       res = mHTMLEditor->SetInlinePropertyOnNode(*node->AsContent(),
                                                  *item->tag, &item->attr,
                                                  item->value);
       NS_ENSURE_SUCCESS(res, res);
       item = mHTMLEditor->mTypeInState->TakeSetProperty();
     }
   }
   if (weDidSomething) {
-    return aSelection.Collapse(node, offset);
+    return aSelection->Collapse(node, offset);
   }
 
   return NS_OK;
 }
 
 
 ///////////////////////////////////////////////////////////////////////////
 // IsEmptyBlock: figure out if aNode is (or is inside) an empty block.
--- a/editor/libeditor/nsHTMLEditRules.h
+++ b/editor/libeditor/nsHTMLEditRules.h
@@ -240,18 +240,18 @@ protected:
                            nsCOMPtr<nsIDOMNode>* outList,
                            nsIAtom* aListType,
                            nsIAtom* aItemType);
   nsresult ConvertListType(mozilla::dom::Element* aList,
                            mozilla::dom::Element** aOutList,
                            nsIAtom* aListType,
                            nsIAtom* aItemType);
 
-  nsresult CreateStyleForInsertText(mozilla::dom::Selection& aSelection,
-                                    nsIDocument& aDoc);
+  nsresult CreateStyleForInsertText(mozilla::dom::Selection* aSelection,
+                                    nsIDOMDocument* aDoc);
   nsresult IsEmptyBlock(nsIDOMNode *aNode,
                         bool *outIsEmptyBlock,
                         bool aMozBRDoesntCount = false,
                         bool aListItemsNotEmpty = false);
   nsresult CheckForEmptyBlock(nsINode* aStartNode,
                               mozilla::dom::Element* aBodyNode,
                               mozilla::dom::Selection* aSelection,
                               nsIEditor::EDirection aAction,
--- a/editor/libeditor/nsHTMLEditor.h
+++ b/editor/libeditor/nsHTMLEditor.h
@@ -635,20 +635,20 @@ protected:
   nsresult MakeDefinitionItem(const nsAString & aItemType);
   nsresult InsertBasicBlock(const nsAString & aBlockType);
 
   /* increase/decrease the font size of selection */
   enum class FontSize { incr, decr };
   nsresult RelativeFontChange(FontSize aDir);
 
   /* helper routines for font size changing */
-  nsresult RelativeFontChangeOnTextNode(FontSize aDir,
-                                        mozilla::dom::Text& aTextNode,
-                                        int32_t aStartOffset,
-                                        int32_t aEndOffset);
+  nsresult RelativeFontChangeOnTextNode( int32_t aSizeChange,
+                                         nsIDOMCharacterData *aTextNode,
+                                         int32_t aStartOffset,
+                                         int32_t aEndOffset);
   nsresult RelativeFontChangeOnNode(int32_t aSizeChange, nsIContent* aNode);
   nsresult RelativeFontChangeHelper(int32_t aSizeChange, nsINode* aNode);
 
   /* helper routines for inline style */
   nsresult SetInlinePropertyOnTextNode(mozilla::dom::Text& aData,
                                        int32_t aStartOffset,
                                        int32_t aEndOffset,
                                        nsIAtom& aProperty,
--- a/editor/libeditor/nsHTMLEditorStyle.cpp
+++ b/editor/libeditor/nsHTMLEditorStyle.cpp
@@ -1404,19 +1404,19 @@ nsHTMLEditor::RelativeFontChange(FontSiz
     // Adjust range to include any ancestors with entirely selected children
     nsresult res = PromoteInlineRange(range);
     NS_ENSURE_SUCCESS(res, res);
 
     // Check for easy case: both range endpoints in same text node
     nsCOMPtr<nsINode> startNode = range->GetStartParent();
     nsCOMPtr<nsINode> endNode = range->GetEndParent();
     if (startNode == endNode && IsTextNode(startNode)) {
-      res = RelativeFontChangeOnTextNode(aDir, *startNode->GetAsText(),
-                                         range->StartOffset(),
-                                         range->EndOffset());
+      res = RelativeFontChangeOnTextNode(aDir == FontSize::incr ? +1 : -1,
+          static_cast<nsIDOMCharacterData*>(startNode->AsDOMNode()),
+          range->StartOffset(), range->EndOffset());
       NS_ENSURE_SUCCESS(res, res);
     } else {
       // Not the easy case.  Range not contained in single text node.  There
       // are up to three phases here.  There are all the nodes reported by the
       // subtree iterator to be processed.  And there are potentially a
       // starting textnode and an ending textnode which are only partially
       // contained by the range.
 
@@ -1446,88 +1446,96 @@ nsHTMLEditor::RelativeFontChange(FontSiz
                                          node);
           NS_ENSURE_SUCCESS(res, res);
         }
       }
       // Now check the start and end parents of the range to see if they need
       // to be separately handled (they do if they are text nodes, due to how
       // the subtree iterator works - it will not have reported them).
       if (IsTextNode(startNode) && IsEditable(startNode)) {
-        res = RelativeFontChangeOnTextNode(aDir, *startNode->GetAsText(),
-                                           range->StartOffset(),
-                                           startNode->Length());
+        res = RelativeFontChangeOnTextNode(aDir == FontSize::incr ? +1 : -1,
+            static_cast<nsIDOMCharacterData*>(startNode->AsDOMNode()),
+            range->StartOffset(), startNode->Length());
         NS_ENSURE_SUCCESS(res, res);
       }
       if (IsTextNode(endNode) && IsEditable(endNode)) {
-        res = RelativeFontChangeOnTextNode(aDir, *endNode->GetAsText(), 0,
-                                           range->EndOffset());
+        res = RelativeFontChangeOnTextNode(aDir == FontSize::incr ? +1 : -1,
+            static_cast<nsIDOMCharacterData*>(endNode->AsDOMNode()),
+            0, range->EndOffset());
         NS_ENSURE_SUCCESS(res, res);
       }
     }
   }
 
   return NS_OK;
 }
 
 nsresult
-nsHTMLEditor::RelativeFontChangeOnTextNode(FontSize aDir,
-                                           Text& aTextNode,
-                                           int32_t aStartOffset,
-                                           int32_t aEndOffset)
+nsHTMLEditor::RelativeFontChangeOnTextNode( int32_t aSizeChange,
+                                            nsIDOMCharacterData *aTextNode,
+                                            int32_t aStartOffset,
+                                            int32_t aEndOffset)
 {
-  // Don't need to do anything if no characters actually selected
-  if (aStartOffset == aEndOffset) {
-    return NS_OK;
-  }
+  // Can only change font size by + or - 1
+  if ( !( (aSizeChange==1) || (aSizeChange==-1) ) )
+    return NS_ERROR_ILLEGAL_VALUE;
+  nsCOMPtr<nsIContent> textNode = do_QueryInterface(aTextNode);
+  NS_ENSURE_TRUE(textNode, NS_ERROR_NULL_POINTER);
 
-  if (!aTextNode.GetParentNode() ||
-      !CanContainTag(*aTextNode.GetParentNode(), *nsGkAtoms::big)) {
+  // don't need to do anything if no characters actually selected
+  if (aStartOffset == aEndOffset) return NS_OK;
+
+  if (!textNode->GetParentNode() ||
+      !CanContainTag(*textNode->GetParentNode(), *nsGkAtoms::big)) {
     return NS_OK;
   }
 
-  OwningNonNull<nsIContent> node = aTextNode;
+  nsCOMPtr<nsIDOMNode> tmp;
+  nsCOMPtr<nsIContent> node = do_QueryInterface(aTextNode);
+  NS_ENSURE_STATE(node);
 
-  // Do we need to split the text node?
+  // do we need to split the text node?
+  uint32_t textLen;
+  aTextNode->GetLength(&textLen);
 
   // -1 is a magic value meaning to the end of node
-  if (aEndOffset == -1) {
-    aEndOffset = aTextNode.Length();
+  if (aEndOffset == -1) aEndOffset = textLen;
+
+  nsresult res = NS_OK;
+  if ( (uint32_t)aEndOffset != textLen )
+  {
+    // we need to split off back of text node
+    res = SplitNode(GetAsDOMNode(node), aEndOffset, getter_AddRefs(tmp));
+    NS_ENSURE_SUCCESS(res, res);
+    // remember left node
+    node = do_QueryInterface(tmp);
+  }
+  if ( aStartOffset )
+  {
+    // we need to split off front of text node
+    res = SplitNode(GetAsDOMNode(node), aStartOffset, getter_AddRefs(tmp));
+    NS_ENSURE_SUCCESS(res, res);
   }
 
-  ErrorResult rv;
-  if ((uint32_t)aEndOffset != aTextNode.Length()) {
-    // We need to split off back of text node
-    node = SplitNode(node, aEndOffset, rv);
-    NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
-  }
-  if (aStartOffset) {
-    // We need to split off front of text node
-    SplitNode(node, aStartOffset, rv);
-    NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
-  }
-
-  // Look for siblings that are correct type of node
-  nsIAtom* nodeType = aDir == FontSize::incr ? nsGkAtoms::big
-                                             : nsGkAtoms::small;
+  // look for siblings that are correct type of node
+  nsIAtom* nodeType = aSizeChange == 1 ? nsGkAtoms::big : nsGkAtoms::small;
   nsCOMPtr<nsIContent> sibling = GetPriorHTMLSibling(node);
   if (sibling && sibling->IsHTMLElement(nodeType)) {
-    // Previous sib is already right kind of inline node; slide this over
-    nsresult res = MoveNode(node, sibling, -1);
-    NS_ENSURE_SUCCESS(res, res);
-    return NS_OK;
+    // previous sib is already right kind of inline node; slide this over into it
+    res = MoveNode(node, sibling, -1);
+    return res;
   }
   sibling = GetNextHTMLSibling(node);
   if (sibling && sibling->IsHTMLElement(nodeType)) {
-    // Following sib is already right kind of inline node; slide this over
-    nsresult res = MoveNode(node, sibling, 0);
-    NS_ENSURE_SUCCESS(res, res);
-    return NS_OK;
+    // following sib is already right kind of inline node; slide this over into it
+    res = MoveNode(node, sibling, 0);
+    return res;
   }
 
-  // Else reparent the node inside font node with appropriate relative size
+  // else reparent the node inside font node with appropriate relative size
   nsCOMPtr<Element> newElement = InsertContainerAbove(node, nodeType);
   NS_ENSURE_STATE(newElement);
 
   return NS_OK;
 }
 
 
 nsresult