Bug 1482019 - part 3: Minimize some scope of auto varaiables in HTMLEditor::GetSelectedNode() r=m_kato
authorMasayuki Nakano <masayuki@d-toybox.com>
Fri, 10 Aug 2018 17:41:58 +0900
changeset 486972 f6053ad1d49aa8924c4ede25f0247c9c2a86d607
parent 486971 40fbaeccbd895386d7f64ba330608576d2296cef
child 486973 66e9a30a6b1a192e6afdb9bae386243cec9a2127
push id9719
push userffxbld-merge
push dateFri, 24 Aug 2018 17:49:46 +0000
treeherdermozilla-beta@719ec98fba77 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersm_kato
bugs1482019
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 1482019 - part 3: Minimize some scope of auto varaiables in HTMLEditor::GetSelectedNode() r=m_kato Some variables in HTMLEditor::GetSelectedNode() are declared very large block they are not used and/or referred. So, we can get rid of some variables or move smaller block.
editor/libeditor/HTMLEditor.cpp
--- a/editor/libeditor/HTMLEditor.cpp
+++ b/editor/libeditor/HTMLEditor.cpp
@@ -2652,20 +2652,16 @@ HTMLEditor::GetSelectedElement(const nsA
 
 already_AddRefed<nsINode>
 HTMLEditor::GetSelectedNode(Selection& aSelection,
                             const nsAtom* aTagName,
                             ErrorResult& aRv)
 {
   MOZ_ASSERT(!aRv.Failed());
 
-  bool isCollapsed = aSelection.IsCollapsed();
-
-  // nullptr indicates we should look for any element.
-  bool anyTag = !aTagName;
   bool isLinkTag = aTagName && IsLinkTag(*aTagName);
   bool isNamedAnchorTag = aTagName && IsNamedAnchorTag(*aTagName);
 
   RefPtr<nsRange> range = aSelection.GetRangeAt(0);
   if (NS_WARN_IF(!range)) {
     aRv.Throw(NS_ERROR_FAILURE);
     return nullptr;
   }
@@ -2677,126 +2673,119 @@ HTMLEditor::GetSelectedNode(Selection& a
   nsIContent* endNode = range->GetChildAtEndOffset();
 
   // Optimization for a single selected element
   if (startContainer && startContainer == endContainer &&
       startNode && endNode && startNode->GetNextSibling() == endNode) {
     nsCOMPtr<nsINode> selectedNode = startNode;
     if (selectedNode) {
       // Test for appropriate node type requested
-      if (anyTag || aTagName == selectedNode->NodeInfo()->NameAtom() ||
+      if (!aTagName || aTagName == selectedNode->NodeInfo()->NameAtom() ||
           (isLinkTag && HTMLEditUtils::IsLink(selectedNode)) ||
           (isNamedAnchorTag && HTMLEditUtils::IsNamedAnchor(selectedNode))) {
         return selectedNode.forget();
       }
     }
   }
 
-  bool bNodeFound = false;
-  nsCOMPtr<Element> selectedElement;
+  RefPtr<Element> selectedElement;
   if (isLinkTag) {
     // Link tag is a special case - we return the anchor node
     //  found for any selection that is totally within a link,
     //  included a collapsed selection (just a caret in a link)
     const RangeBoundary& anchor = aSelection.AnchorRef();
     const RangeBoundary& focus = aSelection.FocusRef();
     // Link node must be the same for both ends of selection
     if (anchor.IsSet()) {
       RefPtr<Element> parentLinkOfAnchor =
         GetElementOrParentByTagName(NS_LITERAL_STRING("href"),
                                     anchor.Container());
       // XXX: ERROR_HANDLING  can parentLinkOfAnchor be null?
       if (parentLinkOfAnchor) {
-        if (isCollapsed) {
-          // We have just a caret in the link
-          bNodeFound = true;
-        } else if (focus.IsSet()) {
+        if (aSelection.IsCollapsed()) {
+          // We have just a caret in the link.
+          return parentLinkOfAnchor.forget();
+        }
+        if (focus.IsSet()) {
           // Link node must be the same for both ends of selection.
           RefPtr<Element> parentLinkOfFocus =
             GetElementOrParentByTagName(NS_LITERAL_STRING("href"),
                                         focus.Container());
           if (parentLinkOfFocus == parentLinkOfAnchor) {
-            bNodeFound = true;
+            return parentLinkOfAnchor.forget();
           }
         }
-
-        // We found a link node parent
-        if (bNodeFound) {
-          return parentLinkOfAnchor.forget();
-        }
       } else if (anchor.GetChildAtOffset() && focus.GetChildAtOffset()) {
         // Check if link node is the only thing selected
         if (HTMLEditUtils::IsLink(anchor.GetChildAtOffset()) &&
             anchor.Container() == focus.Container() &&
             focus.GetChildAtOffset() ==
               anchor.GetChildAtOffset()->GetNextSibling()) {
-          selectedElement = do_QueryInterface(anchor.GetChildAtOffset());
-          bNodeFound = true;
+          selectedElement = Element::FromNodeOrNull(anchor.GetChildAtOffset());
         }
       }
     }
   }
 
-  if (!isCollapsed) {
+  if (!aSelection.IsCollapsed()) {
     RefPtr<nsRange> currange = aSelection.GetRangeAt(0);
     if (currange) {
       nsresult rv;
       nsCOMPtr<nsIContentIterator> iter =
         do_CreateInstance("@mozilla.org/content/post-content-iterator;1",
                           &rv);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         aRv.Throw(rv);
         return nullptr;
       }
 
+      bool found = !!selectedElement;
       const nsAtom* tagNameLookingFor = aTagName;
       iter->Init(currange);
       // loop through the content iterator for each content node
       while (!iter->IsDone()) {
         // Query interface to cast nsIContent to Element
         //  then get tagType to compare to  aTagName
         // Clone node of each desired type and append it to the aDomFrag
         selectedElement = Element::FromNodeOrNull(iter->GetCurrentNode());
         if (selectedElement) {
           // If we already found a node, then we have another element,
           //  thus there's not just one element selected
-          if (bNodeFound) {
-            bNodeFound = false;
+          if (found) {
             break;
           }
 
-          if (anyTag) {
+          if (!tagNameLookingFor) {
             // Get name of first selected element
             tagNameLookingFor = selectedElement->NodeInfo()->NameAtom();
-            anyTag = false;
           }
 
           // The "A" tag is a pain,
           //  used for both link(href is set) and "Named Anchor"
           if ((isLinkTag &&
                HTMLEditUtils::IsLink(selectedElement)) ||
               (isNamedAnchorTag &&
                HTMLEditUtils::IsNamedAnchor(selectedElement))) {
-            bNodeFound = true;
+            found = true;
           }
           // All other tag names are handled here.
           else if (tagNameLookingFor ==
                      selectedElement->NodeInfo()->NameAtom()) {
-            bNodeFound = true;
+            found = true;
           }
-          if (!bNodeFound) {
+
+          if (!found) {
             // Check if node we have is really part of the selection???
             break;
           }
         }
         iter->Next();
       }
     } else {
       // Should never get here?
-      isCollapsed = true;
       NS_WARNING("isCollapsed was FALSE, but no elements found in selection\n");
     }
   }
 
   return selectedElement.forget();
 }
 
 already_AddRefed<Element>