Bug 628938 - Treewalker not working properly in Mozilla Treewalker Demo; r=sicking,smaug a=jst
authorCraig Topper <craig.topper@gmail.com>
Wed, 23 Feb 2011 15:58:55 -0800
changeset 63034 8b140daba936efb5dba83ce26fb6d3baa09c9ce7
parent 63033 7b6be2af0613624f8984438dc5014d22f8b7370a
child 63035 1c9cd44cff88310a3303844476a9adea88c52fd8
push id1
push userroot
push dateTue, 10 Dec 2013 15:46:25 +0000
reviewerssicking, smaug, jst
bugs628938
milestone2.0b13pre
Bug 628938 - Treewalker not working properly in Mozilla Treewalker Demo; r=sicking,smaug a=jst
content/base/src/nsTreeWalker.cpp
--- a/content/base/src/nsTreeWalker.cpp
+++ b/content/base/src/nsTreeWalker.cpp
@@ -384,64 +384,56 @@ nsresult nsTreeWalker::FirstChildInterna
 /*
  * Implements NextSibling and PreviousSibling which only vary in which
  * direction they search.
  * @param aReversed Controls whether we search forwards or backwards
  * @param _retval   Returned node. Null if no child is found
  * @returns         Errorcode
  */
 nsresult nsTreeWalker::NextSiblingInternal(PRBool aReversed, nsIDOMNode **_retval)
-
 {
     nsresult rv;
     PRInt16 filtered;
 
     *_retval = nsnull;
 
     nsCOMPtr<nsINode> node = mCurrentNode;
 
     if (node == mRoot)
         return NS_OK;
 
     while (1) {
-        nsCOMPtr<nsINode> sibling = aReversed ? node->GetPreviousSibling()
-                                              : node->GetNextSibling();
+        nsINode* sibling = aReversed ? node->GetPreviousSibling()
+                                     : node->GetNextSibling();
 
         while (sibling) {
-            rv = TestNode(sibling, &filtered);
+            node = sibling;
+
+            rv = TestNode(node, &filtered);
             NS_ENSURE_SUCCESS(rv, rv);
 
-            switch (filtered) {
-                case nsIDOMNodeFilter::FILTER_ACCEPT:
-                    // Node found
-                    mCurrentNode = sibling;
-                    return CallQueryInterface(sibling, _retval);
-                case nsIDOMNodeFilter::FILTER_SKIP: {
-                        nsINode *firstChild = aReversed ? sibling->GetLastChild()
-                                                        : sibling->GetFirstChild();
-                        if (firstChild) {
-                            sibling = firstChild;
-                            continue;
-                        }
-                    }
-                    break;
-                case nsIDOMNodeFilter::FILTER_REJECT:
-                    // Keep searching
-                    break;
+            if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT) {
+                // Node found
+                mCurrentNode.swap(node);
+                return CallQueryInterface(mCurrentNode, _retval);
             }
-            sibling = aReversed ? sibling->GetPreviousSibling()
-                                : sibling->GetNextSibling();
+
+            // If rejected or no children, try a sibling
+            if (filtered == nsIDOMNodeFilter::FILTER_REJECT ||
+                !(sibling = aReversed ? node->GetLastChild()
+                                      : node->GetFirstChild())) {
+                sibling = aReversed ? node->GetPreviousSibling()
+                                    : node->GetNextSibling();
+            }
         }
 
         node = node->GetNodeParent();
 
         if (!node || node == mRoot)
-            break;
+            return NS_OK;
 
         // Is parent transparent in filtered view?
         rv = TestNode(node, &filtered);
         NS_ENSURE_SUCCESS(rv, rv);
         if (filtered == nsIDOMNodeFilter::FILTER_ACCEPT)
-            break;
+            return NS_OK;
     }
-
-    return NS_OK;
 }