Use HashMap<OwnPtr> in CollectionCache
https://bugs.webkit.org/show_bug.cgi?id=73784

Reviewed by Andreas Kling.

* html/CollectionCache.cpp:
(WebCore::CollectionCache::copyCacheMap): Use adoptPtr.
(WebCore::CollectionCache::reset): Removed now-unneeded calls to deleteAllValues.
(WebCore::append): Added. Helper function for appending elements to the maps from
the collection cache.

* html/CollectionCache.h: Changed mapped type in NodeCacheMap to OwnPtr.
Added append function.

* html/HTMLCollection.cpp:
(WebCore::nameShouldBeVisibleInDocumentAll): Added, to factor out common code in
two functions below.
(WebCore::HTMLCollection::checkForNameMatch): Changed to call nameShouldBeVisibleInDocumentAll.
(WebCore::HTMLCollection::updateNameCache): Ditto. Also updated cache code to use the append
function, so it will work with OwnPtr. Also eliminated an unneeded get call before
each hash table add; we do both at once in the new append function.
* html/HTMLFormCollection.cpp:
(WebCore::HTMLFormCollection::updateNameCache): More of the same.


git-svn-id: http://svn.webkit.org/repository/webkit/trunk@102147 268f45cc-cd09-0410-ab3c-d52691b4dbfc
diff --git a/Source/WebCore/html/HTMLCollection.cpp b/Source/WebCore/html/HTMLCollection.cpp
index 4718eaa..3f2d0e1 100644
--- a/Source/WebCore/html/HTMLCollection.cpp
+++ b/Source/WebCore/html/HTMLCollection.cpp
@@ -1,7 +1,7 @@
 /*
  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
- * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved.
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011 Apple Inc. All rights reserved.
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Library General Public
@@ -266,6 +266,19 @@
      return retval;
 }
 
+static inline bool nameShouldBeVisibleInDocumentAll(HTMLElement* element)
+{
+    // The document.all collection returns only certain types of elements by name,
+    // although it returns any type of element by id.
+    return element->hasLocalName(appletTag)
+        || element->hasLocalName(embedTag)
+        || element->hasLocalName(formTag)
+        || element->hasLocalName(imgTag)
+        || element->hasLocalName(inputTag)
+        || element->hasLocalName(objectTag)
+        || element->hasLocalName(selectTag);
+}
+
 bool HTMLCollection::checkForNameMatch(Element* element, bool checkName, const AtomicString& name) const
 {
     if (!element->isHTMLElement())
@@ -275,13 +288,7 @@
     if (!checkName)
         return e->getIdAttribute() == name;
 
-    // document.all returns only images, forms, applets, objects and embeds
-    // by name (though everything by id)
-    if (m_type == DocAll && 
-        !(e->hasLocalName(imgTag) || e->hasLocalName(formTag) ||
-          e->hasLocalName(appletTag) || e->hasLocalName(objectTag) ||
-          e->hasLocalName(embedTag) || e->hasLocalName(inputTag) ||
-          e->hasLocalName(selectTag)))
+    if (m_type == DocAll && !nameShouldBeVisibleInDocumentAll(e))
         return false;
 
     return e->getAttribute(nameAttr) == name && e->getIdAttribute() != name;
@@ -325,29 +332,10 @@
         HTMLElement* e = toHTMLElement(element);
         const AtomicString& idAttrVal = e->getIdAttribute();
         const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
-        if (!idAttrVal.isEmpty()) {
-            // add to id cache
-            Vector<Element*>* idVector = m_info->idCache.get(idAttrVal.impl());
-            if (!idVector) {
-                idVector = new Vector<Element*>;
-                m_info->idCache.add(idAttrVal.impl(), idVector);
-            }
-            idVector->append(e);
-        }
-        if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal
-            && (m_type != DocAll || 
-                (e->hasLocalName(imgTag) || e->hasLocalName(formTag) ||
-                 e->hasLocalName(appletTag) || e->hasLocalName(objectTag) ||
-                 e->hasLocalName(embedTag) || e->hasLocalName(inputTag) ||
-                 e->hasLocalName(selectTag)))) {
-            // add to name cache
-            Vector<Element*>* nameVector = m_info->nameCache.get(nameAttrVal.impl());
-            if (!nameVector) {
-                nameVector = new Vector<Element*>;
-                m_info->nameCache.add(nameAttrVal.impl(), nameVector);
-            }
-            nameVector->append(e);
-        }
+        if (!idAttrVal.isEmpty())
+            append(m_info->idCache, idAttrVal, e);
+        if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && (m_type != DocAll || nameShouldBeVisibleInDocumentAll(e)))
+            append(m_info->nameCache, nameAttrVal, e);
     }
 
     m_info->hasNameCache = true;