Bug 1191354 part 6 - Clean up IsBlockNode/IsInlineNode in nsHTMLEditRules.cpp; r=ehsan
authorAryeh Gregor <ayg@aryeh.name>
Sun, 01 May 2016 16:14:51 +0300
changeset 295644 14fb48ed9c8d2961095277c15637b3df31d1cd51
parent 295643 47f7ab7dc09b3bd045c7f3357d11b4246ebb50ad
child 295645 4724402baace28203770e6589a7be500a8dde4c5
push id19015
push usercbook@mozilla.com
push dateMon, 02 May 2016 09:39:23 +0000
treeherderfx-team@2080375bc69d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1191354
milestone49.0a1
Bug 1191354 part 6 - Clean up IsBlockNode/IsInlineNode in nsHTMLEditRules.cpp; r=ehsan
editor/libeditor/nsHTMLEditRules.cpp
--- a/editor/libeditor/nsHTMLEditRules.cpp
+++ b/editor/libeditor/nsHTMLEditRules.cpp
@@ -70,24 +70,22 @@ enum
   kNextSib = 2,
   kBothSibs = 3
 };
 
 /********************************************************
  *  first some helpful functors we will use
  ********************************************************/
 
-static bool IsBlockNode(nsIDOMNode* node)
-{
-  bool isBlock (false);
-  nsHTMLEditor::NodeIsBlockStatic(node, &isBlock);
-  return isBlock;
-}
-
-static bool IsInlineNode(nsIDOMNode* node)
+static bool IsBlockNode(const nsINode& node)
+{
+  return nsHTMLEditor::NodeIsBlockStatic(&node);
+}
+
+static bool IsInlineNode(const nsINode& node)
 {
   return !IsBlockNode(node);
 }
 
 static bool
 IsStyleCachePreservingAction(EditAction action)
 {
   return action == EditAction::deleteSelection ||
@@ -1051,18 +1049,17 @@ nsHTMLEditRules::GetParagraphState(bool 
 
   // post process list.  We need to replace any block nodes that are not format
   // nodes with their content.  This is so we only have to look "up" the hierarchy
   // to find format nodes, instead of both up and down.
   for (int32_t i = arrayOfNodes.Length() - 1; i >= 0; i--) {
     auto& curNode = arrayOfNodes[i];
     nsAutoString format;
     // if it is a known format node we have it easy
-    if (IsBlockNode(GetAsDOMNode(curNode)) &&
-        !nsHTMLEditUtils::IsFormatNode(curNode)) {
+    if (IsBlockNode(curNode) && !nsHTMLEditUtils::IsFormatNode(curNode)) {
       // arrayOfNodes.RemoveObject(curNode);
       res = AppendInnerFormatNodes(arrayOfNodes, curNode);
       NS_ENSURE_SUCCESS(res, res);
     }
   }
 
   // we might have an empty node list.  if so, find selection parent
   // and put that on the list
@@ -1085,17 +1082,17 @@ nsHTMLEditRules::GetParagraphState(bool 
   NS_ENSURE_TRUE(rootElem, NS_ERROR_NULL_POINTER);
 
   // loop through the nodes in selection and examine their paragraph format
   for (auto& curNode : Reversed(arrayOfNodes)) {
     nsAutoString format;
     // if it is a known format node we have it easy
     if (nsHTMLEditUtils::IsFormatNode(curNode)) {
       GetFormatString(GetAsDOMNode(curNode), format);
-    } else if (IsBlockNode(GetAsDOMNode(curNode))) {
+    } else if (IsBlockNode(curNode)) {
       // this is a div or some other non-format block.
       // we should ignore it.  Its children were appended to this list
       // by AppendInnerFormatNodes() call above.  We will get needed
       // info when we examine them instead.
       continue;
     }
     else
     {
@@ -1144,17 +1141,17 @@ nsHTMLEditRules::AppendInnerFormatNodes(
   // we only need to place any one inline inside this node onto
   // the list.  They are all the same for purposes of determining
   // paragraph style.  We use foundInline to track this as we are
   // going through the children in the loop below.
   bool foundInline = false;
   for (nsIContent* child = aNode->GetFirstChild();
        child;
        child = child->GetNextSibling()) {
-    bool isBlock = IsBlockNode(child->AsDOMNode());
+    bool isBlock = IsBlockNode(*child);
     bool isFormat = nsHTMLEditUtils::IsFormatNode(child);
     if (isBlock && !isFormat) {
       // if it's a div, etc, recurse
       AppendInnerFormatNodes(aArray, child);
     } else if (isFormat) {
       aArray.AppendElement(*child);
     } else if (!foundInline) {
       // if this is the first inline we've found, use it
@@ -1669,17 +1666,17 @@ nsHTMLEditRules::StandardBreakImpl(nsINo
     // SetInterlinePosition(true) means we want the caret to stick to the
     // content on the "right".  We want the caret to stick to whatever is past
     // the break.  This is because the break is on the same line we were on,
     // but the next content will be on the following line.
 
     // An exception to this is if the break has a next sibling that is a block
     // node.  Then we stick to the left to avoid an uber caret.
     nsCOMPtr<nsIContent> siblingNode = brNode->GetNextSibling();
-    if (siblingNode && IsBlockNode(GetAsDOMNode(siblingNode))) {
+    if (siblingNode && IsBlockNode(*siblingNode)) {
       aSelection.SetInterlinePosition(false);
     } else {
       aSelection.SetInterlinePosition(true);
     }
     res = aSelection.Collapse(node, offset + 1);
     NS_ENSURE_SUCCESS(res, res);
   }
   return NS_OK;
@@ -1745,17 +1742,17 @@ nsHTMLEditRules::SplitMailCites(Selectio
     // want selection before the break, and on same line
     aSelection->SetInterlinePosition(true);
     res = aSelection->Collapse(selNode, newOffset);
     NS_ENSURE_SUCCESS(res, res);
     // if citeNode wasn't a block, we might also want another break before it.
     // We need to examine the content both before the br we just added and also
     // just after it.  If we don't have another br or block boundary adjacent,
     // then we will need a 2nd br added to achieve blank line that user expects.
-    if (IsInlineNode(GetAsDOMNode(citeNode))) {
+    if (IsInlineNode(*citeNode)) {
       NS_ENSURE_STATE(mHTMLEditor);
       nsWSRunObject wsObj(mHTMLEditor, selNode, newOffset);
       nsCOMPtr<nsINode> visNode;
       int32_t visOffset=0;
       WSType wsType;
       wsObj.PriorVisibleNode(selNode, newOffset, address_of(visNode),
                              &visOffset, &wsType);
       if (wsType == WSType::normalWS || wsType == WSType::text ||
@@ -2481,17 +2478,17 @@ nsHTMLEditRules::InsertBRIfNeeded(Select
   // get selection
   nsCOMPtr<nsINode> node;
   int32_t offset;
   nsresult res = mEditor->GetStartNodeAndOffset(aSelection, getter_AddRefs(node), &offset);
   NS_ENSURE_SUCCESS(res, res);
   NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
 
   // inline elements don't need any br
-  if (!IsBlockNode(GetAsDOMNode(node))) {
+  if (!IsBlockNode(*node)) {
     return NS_OK;
   }
 
   // examine selection
   NS_ENSURE_STATE(mHTMLEditor);
   nsWSRunObject wsObj(mHTMLEditor, node, offset);
   if (((wsObj.mStartReason & WSType::block) ||
        (wsObj.mStartReason & WSType::br)) &&
@@ -2819,17 +2816,17 @@ nsHTMLEditRules::MoveBlock(nsIDOMNode *a
   nsTArray<OwningNonNull<nsINode>> arrayOfNodes;
   // GetNodesFromPoint is the workhorse that figures out what we wnat to move.
   nsresult res = GetNodesFromPoint(::DOMPoint(aRightBlock,aRightOffset),
                                    EditAction::makeList, arrayOfNodes,
                                    TouchContent::yes);
   NS_ENSURE_SUCCESS(res, res);
   for (auto& curNode : arrayOfNodes) {
     // get the node to act on
-    if (IsBlockNode(GetAsDOMNode(curNode))) {
+    if (IsBlockNode(curNode)) {
       // For block nodes, move their contents only, then delete block.
       res = MoveContents(GetAsDOMNode(curNode), aLeftBlock, &aLeftOffset);
       NS_ENSURE_SUCCESS(res, res);
       NS_ENSURE_STATE(mHTMLEditor);
       res = mHTMLEditor->DeleteNode(curNode);
     }
     else
     {
@@ -3226,17 +3223,17 @@ nsHTMLEditRules::WillMakeList(Selection*
       mNewBlock = GetAsDOMNode(curList);
       // curList is now the correct thing to put curNode in
       prevListItem = 0;
     }
 
     // if curNode isn't a list item, we must wrap it in one
     nsCOMPtr<Element> listItem;
     if (!nsHTMLEditUtils::IsListItem(curNode)) {
-      if (IsInlineNode(GetAsDOMNode(curNode)) && prevListItem) {
+      if (IsInlineNode(curNode) && prevListItem) {
         // this is a continuation of some inline nodes that belong together in
         // the same list item.  use prevListItem
         NS_ENSURE_STATE(mHTMLEditor);
         res = mHTMLEditor->MoveNode(curNode, prevListItem, -1);
         NS_ENSURE_SUCCESS(res, res);
       } else {
         // don't wrap li around a paragraph.  instead replace paragraph with li
         if (curNode->IsHTMLElement(nsGkAtoms::p)) {
@@ -3244,17 +3241,17 @@ nsHTMLEditRules::WillMakeList(Selection*
           listItem = mHTMLEditor->ReplaceContainer(curNode->AsElement(),
                                                    itemType);
           NS_ENSURE_STATE(listItem);
         } else {
           NS_ENSURE_STATE(mHTMLEditor);
           listItem = mHTMLEditor->InsertContainerAbove(curNode, itemType);
           NS_ENSURE_STATE(listItem);
         }
-        if (IsInlineNode(GetAsDOMNode(curNode))) {
+        if (IsInlineNode(curNode)) {
           prevListItem = listItem;
         } else {
           prevListItem = nullptr;
         }
       }
     } else {
       listItem = curNode->AsElement();
     }
@@ -3694,17 +3691,17 @@ nsHTMLEditRules::WillCSSIndent(Selection
       uint32_t listLen = curList->Length();
       NS_ENSURE_STATE(mHTMLEditor);
       res = mHTMLEditor->MoveNode(curNode, curList, listLen);
       NS_ENSURE_SUCCESS(res, res);
     }
 
     else // not a list item
     {
-      if (IsBlockNode(curNode->AsDOMNode())) {
+      if (curNode && IsBlockNode(*curNode)) {
         RelativeChangeIndentationOfElementNode(curNode->AsDOMNode(), +1);
         curQuote = nullptr;
       }
       else {
         if (!curQuote)
         {
           // First, check that our element can contain a div.
           if (!mEditor->CanContainTag(*curParent, *nsGkAtoms::div)) {
@@ -4019,17 +4016,17 @@ nsHTMLEditRules::WillOutdent(Selection* 
           curBlockQuoteIsIndentedWithCSS = false;
         }
         NS_ENSURE_STATE(mHTMLEditor);
         res = mHTMLEditor->RemoveBlockContainer(GetAsDOMNode(curNode));
         NS_ENSURE_SUCCESS(res, res);
         continue;
       }
       // is it a block with a 'margin' property?
-      if (useCSS && IsBlockNode(GetAsDOMNode(curNode))) {
+      if (useCSS && IsBlockNode(curNode)) {
         NS_ENSURE_STATE(mHTMLEditor);
         nsIAtom& marginProperty =
           MarginPropertyAtomForIndent(*mHTMLEditor->mHTMLCSSUtils, curNode);
         nsAutoString value;
         NS_ENSURE_STATE(mHTMLEditor);
         mHTMLEditor->mHTMLCSSUtils->GetSpecifiedProperty(curNode,
                                                          marginProperty,
                                                          value);
@@ -4534,17 +4531,17 @@ nsHTMLEditRules::CreateStyleForInsertTex
 nsresult
 nsHTMLEditRules::IsEmptyBlock(Element& aNode,
                               bool* aOutIsEmptyBlock,
                               MozBRCounts aMozBRCounts)
 {
   MOZ_ASSERT(aOutIsEmptyBlock);
   *aOutIsEmptyBlock = true;
 
-  NS_ENSURE_TRUE(IsBlockNode(aNode.AsDOMNode()), NS_ERROR_NULL_POINTER);
+  NS_ENSURE_TRUE(IsBlockNode(aNode), NS_ERROR_NULL_POINTER);
 
   return mHTMLEditor->IsEmptyNode(aNode.AsDOMNode(), aOutIsEmptyBlock,
                                   aMozBRCounts == MozBRCounts::yes ? false
                                                                    : true);
 }
 
 
 nsresult
@@ -4630,17 +4627,17 @@ nsHTMLEditRules::WillAlign(Selection& aS
     nsCOMPtr<nsIContent> brContent =
       mHTMLEditor->GetNextHTMLNode(parent, offset);
     if (brContent && nsTextEditUtils::IsBreak(brContent)) {
       // Making use of html structure... if next node after where we are
       // putting our div is not a block, then the br we found is in same block
       // we are, so it's safe to consume it.
       nsCOMPtr<nsIContent> sibling = mHTMLEditor->GetNextHTMLSibling(parent,
                                                                      offset);
-      if (!IsBlockNode(GetAsDOMNode(sibling))) {
+      if (sibling && !IsBlockNode(*sibling)) {
         rv = mHTMLEditor->DeleteNode(brContent);
         NS_ENSURE_SUCCESS(rv, rv);
       }
     }
     nsCOMPtr<Element> div = mHTMLEditor->CreateNode(nsGkAtoms::div, parent,
                                                     offset);
     NS_ENSURE_STATE(div);
     // Remember our new block for postprocessing
@@ -4857,17 +4854,17 @@ nsHTMLEditRules::AlignBlockContents(nsID
 nsresult
 nsHTMLEditRules::CheckForEmptyBlock(nsINode* aStartNode,
                                     Element* aBodyNode,
                                     Selection* aSelection,
                                     nsIEditor::EDirection aAction,
                                     bool* aHandled)
 {
   // If the editing host is an inline element, bail out early.
-  if (IsInlineNode(GetAsDOMNode(aBodyNode))) {
+  if (aBodyNode && IsInlineNode(*aBodyNode)) {
     return NS_OK;
   }
   NS_ENSURE_STATE(mHTMLEditor);
   nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
 
   // If we are inside an empty block, delete it.  Note: do NOT delete table
   // elements this way.
   nsCOMPtr<Element> block = mHTMLEditor->GetBlock(*aStartNode);
@@ -5393,17 +5390,17 @@ nsHTMLEditRules::GetPromotedPoint(RulesE
     // look back through any further inline nodes that aren't across a <br>
     // from us, and that are enclosed in the same block.
     NS_ENSURE_TRUE(mHTMLEditor, /* void */);
     nsCOMPtr<nsINode> priorNode =
       mHTMLEditor->GetPriorHTMLNode(node, offset, true);
 
     while (priorNode && priorNode->GetParentNode() &&
            mHTMLEditor && !mHTMLEditor->IsVisBreak(priorNode->AsDOMNode()) &&
-           !IsBlockNode(priorNode->AsDOMNode())) {
+           !IsBlockNode(*priorNode)) {
       offset = priorNode->GetParentNode()->IndexOf(priorNode);
       node = priorNode->GetParentNode();
       NS_ENSURE_TRUE(mHTMLEditor, /* void */);
       priorNode = mHTMLEditor->GetPriorHTMLNode(node, offset, true);
     }
 
     // finding the real start for this point.  look up the tree for as long as
     // we are the first node in the container, and as long as we haven't hit
@@ -5464,18 +5461,17 @@ nsHTMLEditRules::GetPromotedPoint(RulesE
   }
 
   // look ahead through any further inline nodes that aren't across a <br> from
   // us, and that are enclosed in the same block.
   NS_ENSURE_TRUE(mHTMLEditor, /* void */);
   nsCOMPtr<nsIContent> nextNode =
     mHTMLEditor->GetNextHTMLNode(node, offset, true);
 
-  while (nextNode && !IsBlockNode(nextNode->AsDOMNode()) &&
-         nextNode->GetParentNode()) {
+  while (nextNode && !IsBlockNode(*nextNode) && nextNode->GetParentNode()) {
     offset = 1 + nextNode->GetParentNode()->IndexOf(nextNode);
     node = nextNode->GetParentNode();
     NS_ENSURE_TRUE(mHTMLEditor, /* void */);
     if (mHTMLEditor->IsVisBreak(nextNode->AsDOMNode())) {
       break;
     }
 
     // Check for newlines in pre-formatted text nodes.
@@ -5781,19 +5777,18 @@ nsHTMLEditRules::GetNodesForOperation(ns
   if (aOperationType == EditAction::makeBasicBlock ||
       aOperationType == EditAction::makeList ||
       aOperationType == EditAction::align ||
       aOperationType == EditAction::setAbsolutePosition ||
       aOperationType == EditAction::indent ||
       aOperationType == EditAction::outdent) {
     for (int32_t i = aOutArrayOfNodes.Length() - 1; i >= 0; i--) {
       OwningNonNull<nsINode> node = aOutArrayOfNodes[i];
-      if (aTouchContent == TouchContent::yes &&
-          IsInlineNode(GetAsDOMNode(node)) && mHTMLEditor->IsContainer(node) &&
-          !mHTMLEditor->IsTextNode(node)) {
+      if (aTouchContent == TouchContent::yes && IsInlineNode(node) &&
+          mHTMLEditor->IsContainer(node) && !mHTMLEditor->IsTextNode(node)) {
         nsTArray<OwningNonNull<nsINode>> arrayOfInlines;
         res = BustUpInlinesAtBRs(*node->AsContent(), arrayOfInlines);
         NS_ENSURE_SUCCESS(res, res);
 
         // Put these nodes in aOutArrayOfNodes, replacing the current node
         aOutArrayOfNodes.RemoveElementAt(i);
         aOutArrayOfNodes.InsertElementsAt(i, arrayOfInlines);
       }
@@ -6103,22 +6098,22 @@ nsHTMLEditRules::BustUpInlinesAtBRs(nsIC
   }
   return NS_OK;
 }
 
 
 nsIContent*
 nsHTMLEditRules::GetHighestInlineParent(nsINode& aNode)
 {
-  if (!aNode.IsContent() || IsBlockNode(aNode.AsDOMNode())) {
+  if (!aNode.IsContent() || IsBlockNode(aNode)) {
     return nullptr;
   }
   OwningNonNull<nsIContent> node = *aNode.AsContent();
 
-  while (node->GetParent() && IsInlineNode(node->GetParent()->AsDOMNode())) {
+  while (node->GetParent() && IsInlineNode(*node->GetParent())) {
     node = *node->GetParent();
   }
   return node;
 }
 
 
 ///////////////////////////////////////////////////////////////////////////////
 // GetNodesFromPoint: Given a particular operation, construct a list of nodes
@@ -6764,17 +6759,17 @@ nsHTMLEditRules::RemoveBlockStyle(nsTArr
         NS_ENSURE_SUCCESS(res, res);
         firstNode = lastNode = curBlock = nullptr;
       }
       // Recursion time
       nsTArray<OwningNonNull<nsINode>> childArray;
       GetChildNodesForOperation(*curNode, childArray);
       res = RemoveBlockStyle(childArray);
       NS_ENSURE_SUCCESS(res, res);
-    } else if (IsInlineNode(GetAsDOMNode(curNode))) {
+    } else if (IsInlineNode(curNode)) {
       if (curBlock) {
         // If so, is this node a descendant?
         if (nsEditorUtils::IsDescendantOf(curNode, curBlock)) {
           // Then we don't need to do anything different for this node
           lastNode = curNode->AsContent();
           continue;
         } else {
           // Otherwise, we have progressed beyond end of curBlock, so let's
@@ -6901,17 +6896,17 @@ nsHTMLEditRules::ApplyBlockStyle(nsTArra
         curBlock = mHTMLEditor->CreateNode(&aBlockTag, curParent, offset);
         NS_ENSURE_STATE(curBlock);
         // Remember our new block for postprocessing
         mNewBlock = curBlock->AsDOMNode();
         // Note: doesn't matter if we set mNewBlock multiple times.
         res = mHTMLEditor->MoveNode(curNode->AsContent(), curBlock, -1);
         NS_ENSURE_SUCCESS(res, res);
       }
-    } else if (IsInlineNode(GetAsDOMNode(curNode))) {
+    } else if (IsInlineNode(curNode)) {
       // If curNode is inline, pull it into curBlock.  Note: it's assumed that
       // consecutive inline nodes in aNodeArray are actually members of the
       // same block parent.  This happens to be true now as a side effect of
       // how aNodeArray is contructed, but some additional logic should be
       // added here if that should change
       //
       // If curNode is a non editable, drop it if we are going to <pre>.
       if (&aBlockTag == nsGkAtoms::pre && !mHTMLEditor->IsEditable(curNode)) {
@@ -7317,24 +7312,24 @@ nsHTMLEditRules::CheckInterlinePosition(
     mHTMLEditor->GetPriorHTMLNode(selNode, selOffset, true);
   if (node && node->IsHTMLElement(nsGkAtoms::br)) {
     aSelection.SetInterlinePosition(true);
     return;
   }
 
   // Are we after a block?  If so try set caret to following content
   node = mHTMLEditor->GetPriorHTMLSibling(selNode, selOffset);
-  if (node && IsBlockNode(node->AsDOMNode())) {
+  if (node && IsBlockNode(*node)) {
     aSelection.SetInterlinePosition(true);
     return;
   }
 
   // Are we before a block?  If so try set caret to prior content
   node = mHTMLEditor->GetNextHTMLSibling(selNode, selOffset);
-  if (node && IsBlockNode(node->AsDOMNode())) {
+  if (node && IsBlockNode(*node)) {
     aSelection.SetInterlinePosition(false);
   }
 }
 
 nsresult
 nsHTMLEditRules::AdjustSelection(Selection* aSelection,
                                  nsIEditor::EDirection aAction)
 {
@@ -7780,17 +7775,17 @@ nsHTMLEditRules::SelectionEndpointInNode
  * IsEmptyInline: Return true if aNode is an empty inline container
  */
 bool
 nsHTMLEditRules::IsEmptyInline(nsINode& aNode)
 {
   NS_ENSURE_TRUE(mHTMLEditor, false);
   nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
 
-  if (IsInlineNode(aNode.AsDOMNode()) && mHTMLEditor->IsContainer(&aNode)) {
+  if (IsInlineNode(aNode) && mHTMLEditor->IsContainer(&aNode)) {
     bool isEmpty = true;
     mHTMLEditor->IsEmptyNode(&aNode, &isEmpty);
     return isEmpty;
   }
   return false;
 }
 
 
@@ -8057,17 +8052,17 @@ nsHTMLEditRules::UpdateDocChangeRange(ns
     }
   }
   return res;
 }
 
 nsresult
 nsHTMLEditRules::InsertMozBRIfNeeded(nsINode& aNode)
 {
-  if (!IsBlockNode(aNode.AsDOMNode())) {
+  if (!IsBlockNode(aNode)) {
     return NS_OK;
   }
 
   bool isEmpty;
   NS_ENSURE_STATE(mHTMLEditor);
   nsresult res = mHTMLEditor->IsEmptyNode(&aNode, &isEmpty);
   NS_ENSURE_SUCCESS(res, res);
   if (!isEmpty) {
@@ -8464,18 +8459,17 @@ nsHTMLEditRules::MakeSureElemStartsOrEnd
   res = MakeSureElemStartsOrEndsOnCR(aNode, true);
   return res;
 }
 
 nsresult
 nsHTMLEditRules::AlignBlock(Element& aElement, const nsAString& aAlignType,
                             ContentsOnly aContentsOnly)
 {
-  if (!IsBlockNode(aElement.AsDOMNode()) &&
-      !aElement.IsHTMLElement(nsGkAtoms::hr)) {
+  if (!IsBlockNode(aElement) && !aElement.IsHTMLElement(nsGkAtoms::hr)) {
     // We deal only with blocks; early way out
     return NS_OK;
   }
 
   NS_ENSURE_STATE(mHTMLEditor);
   nsCOMPtr<nsIEditor> kungFuDeathGrip(mHTMLEditor);
 
   nsresult res = RemoveAlignment(aElement.AsDOMNode(), aAlignType,