Reviewed by Richard.
authormjs <mjs@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 22 Nov 2004 04:31:39 +0000 (04:31 +0000)
committermjs <mjs@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Mon, 22 Nov 2004 04:31:39 +0000 (04:31 +0000)
<rdar://problem/3889655> HTMLCollectionImpl should use traverseNextNode to improve speed and save recursion

* khtml/html/html_miscimpl.cpp:
        (HTMLCollectionImpl::calcLength):
        (HTMLCollectionImpl::getItem):
        (HTMLCollectionImpl::item):
        (HTMLCollectionImpl::nextItem):
        (HTMLCollectionImpl::getNamedItem):
        (HTMLCollectionImpl::namedItem):
        (HTMLCollectionImpl::nextNamedItemInternal):
        (HTMLFormCollectionImpl::nextNamedItemInternal):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@8060 268f45cc-cd09-0410-ab3c-d52691b4dbfc

WebCore/ChangeLog-2005-08-23
WebCore/khtml/html/html_miscimpl.cpp

index 18eb248..254d7ca 100644 (file)
@@ -1,3 +1,19 @@
+2004-11-21  Maciej Stachowiak  <mjs@apple.com>
+
+        Reviewed by Richard.
+
+       <rdar://problem/3889655> HTMLCollectionImpl should use traverseNextNode to improve speed and save recursion
+        
+       * khtml/html/html_miscimpl.cpp:
+        (HTMLCollectionImpl::calcLength):
+        (HTMLCollectionImpl::getItem):
+        (HTMLCollectionImpl::item):
+        (HTMLCollectionImpl::nextItem):
+        (HTMLCollectionImpl::getNamedItem):
+        (HTMLCollectionImpl::namedItem):
+        (HTMLCollectionImpl::nextNamedItemInternal):
+        (HTMLFormCollectionImpl::nextNamedItemInternal):
+
 2004-11-19  Maciej Stachowiak  <mjs@apple.com>
 
         Reviewed by Darin.
index 15af794..335233a 100644 (file)
@@ -153,10 +153,12 @@ unsigned long HTMLCollectionImpl::calcLength(NodeImpl *current) const
             default:
                 kdDebug( 6030 ) << "Error in HTMLCollection, wrong tagId!" << endl;
             }
-            if(deep && current->firstChild())
-                len += calcLength(current->firstChild());
+            if(deep) {
+                current = current->traverseNextNode(base);
+                continue;
+            }
         }
-        current = current->nextSibling();
+        current = current->traverseNextSibling(base);
     }
     return len;
 }
@@ -247,12 +249,12 @@ NodeImpl *HTMLCollectionImpl::getItem(NodeImpl *current, int index, int &len) co
                 kdDebug( 6030 ) << "Error in HTMLCollection, wrong tagId!" << endl;
             }
             if(len == (index + 1)) return current;
-            NodeImpl *retval=0;
-            if(deep && current->firstChild())
-                retval = getItem(current->firstChild(), index, len);
-            if(retval) return retval;
+            if (deep) {
+                current = current->traverseNextNode(base);
+                continue;
+            } 
         }
-        current = current->nextSibling();
+        current = current->traverseNextSibling(base);
     }
     return 0;
 }
@@ -274,12 +276,6 @@ NodeImpl *HTMLCollectionImpl::item( unsigned long index ) const
      }
      int pos = (int) info->position;
      NodeImpl *node = getItem(info->current, index, pos);
-     while (!node && info->current->parentNode() && info->current->parentNode() != base) {
-         info->current = info->current->parentNode();
-         if (info->current->nextSibling())
-             node = getItem(info->current->nextSibling(), index, pos);
-         
-     }
      info->current = node;
      info->position = index;
      return info->current;
@@ -295,28 +291,11 @@ NodeImpl *HTMLCollectionImpl::nextItem() const
      resetCollectionInfo();
      int pos = 0;
  
-     info->position = ~0;  // no position
      // Look for the 'second' item. The first one is currentItem, already given back.
      NodeImpl *retval = getItem(info->current, 1, pos);
-     if (retval)
-     {
-         info->current = retval;
-         return retval;
-     }
-     // retval was 0, means we have to go up
-     while( !retval && info->current->parentNode()
-            && info->current->parentNode() != base )
-     {
-         info->current = info->current->parentNode();
-         if (info->current->nextSibling())
-         {
-             // ... and to take the first one from there
-             pos = 0;
-             retval = getItem(info->current->nextSibling(), 0, pos);
-         }
-      }
+
      info->current = retval;
-     return info->current;
+     return retval;
 }
 
 NodeImpl *HTMLCollectionImpl::getNamedItem( NodeImpl *current, int attr_id,
@@ -409,16 +388,12 @@ NodeImpl *HTMLCollectionImpl::getNamedItem( NodeImpl *current, int attr_id,
                     return current;
                 }
             }
-            NodeImpl *retval = 0;
-            if(deep && current->firstChild())
-                retval = getNamedItem(current->firstChild(), attr_id, name, caseSensitive);
-            if(retval)
-            {
-                //kdDebug( 6030 ) << "got a return value " << retval << endl;
-                return retval;
-            }
+            if (deep) {
+                current = current->traverseNextNode(base);
+                continue;
+            } 
         }
-        current = current->nextSibling();
+        current = current->traverseNextSibling(base);
     }
     return 0;
 }
@@ -430,8 +405,7 @@ NodeImpl *HTMLCollectionImpl::namedItem( const DOMString &name, bool caseSensiti
     // attribute. If a match is not found, the method then searches for an
     // object with a matching name attribute, but only on those elements
     // that are allowed a name attribute.
-     resetCollectionInfo();
-     info->position = ~0;  // no position
+    resetCollectionInfo();
     idsDone = false;
     info->current = getNamedItem(base->firstChild(), ATTR_ID, name);
     if(info->current)
@@ -482,24 +456,6 @@ NodeImpl *HTMLCollectionImpl::nextNamedItemInternal( const DOMString &name ) con
             info->current = retval;
             return retval;
         }
-
-        // retval was 0, means we have to go up
-        while( !retval && info->current->parentNode()
-               && info->current->parentNode() != base )
-        {
-            info->current = info->current->parentNode();
-            if (info->current->nextSibling())
-            {
-                // ... and to take the first one from there
-                retval = getNamedItem(info->current->nextSibling(), idsDone ? ATTR_NAME : ATTR_ID, name);
-            }
-        }
-        if ( retval )
-        {
-            //kdDebug() << "*HTMLCollectionImpl::nextNamedItem found after going up " << retval << endl;
-            info->current = retval;
-            return info->current;
-        }
     }
 
     if ( idsDone )
@@ -648,7 +604,7 @@ NodeImpl * HTMLFormCollectionImpl::nextItem() const
 
 NodeImpl * HTMLFormCollectionImpl::nextNamedItemInternal( const DOMString &name ) const
 {
-    NodeImpl *retval = getNamedFormItem( idsDone ? ATTR_NAME : ATTR_ID, name, ++currentPos, true );
+    NodeImpl *retval = getNamedFormItem( idsDone ? ATTR_NAME : ATTR_ID, name, ++info->position, true );
     if ( retval )
         return retval;
     if ( idsDone ) // we're done