Title: [122345] trunk/Source/WebCore
Revision
122345
Author
[email protected]
Date
2012-07-11 11:13:12 -0700 (Wed, 11 Jul 2012)

Log Message

HTMLFormCollection::item and HTMLPropertiesCollection::item should share code
https://bugs.webkit.org/show_bug.cgi?id=90932

Reviewed by Anders Carlsson.

Merged HTMLFormCollection::item and HTMLPropertiesCollection::item as HTMLCollectionWithArrayStorage::item,
which can be merged into HTMLCollection::item in a follow up patch.

Also moved the call to invalidateCacheIfNeeded into HTMLCollection::updateNameCache() as done in
HTMLPropertiesCollection.

In addition, moved the early bail out for when the base element doesn't have itemscope attribute from individual
functions to updateRefElements so that HTMLCollectionWithArrayStorage::item doesn't need to have this check.

* html/HTMLCollection.cpp:
(WebCore::HTMLCollectionWithArrayStorage::item): Added. It's based on HTMLPropertiesCollection::item but it only
has the single loop (as supposed to nested loops) as HTMLFormCollection doesn't have multiple items per element
in the array unlike HTMLPropertiesCollection. In addition, offsetInArray (was i in HTMLPropertiesCollection::item) is
incremented in each itemAfter due to this semantic difference in each itemAfter.
(WebCore::HTMLCollection::updateNameCache):
(WebCore::HTMLCollection::hasNamedItem):
(WebCore::HTMLCollection::namedItems):
* html/HTMLCollection.h:
(HTMLCollectionWithArrayStorage):
(WebCore::HTMLCollectionWithArrayStorage::HTMLCollectionWithArrayStorage):
* html/HTMLFormCollection.cpp:
(WebCore::HTMLFormCollection::HTMLFormCollection):
(WebCore::HTMLFormCollection::calcLength): Merged numberOfFormControlElements since it was only called here.
(WebCore::HTMLFormCollection::itemAfter): Added. HTMLFormCollection has exactly one item per element in the array
so we increment the offset in each iteration. Note that when we're continuing a search, we need to increment
the offset in order to avoid returning the same item.
(WebCore::HTMLFormCollection::updateNameCache):
* html/HTMLFormCollection.h:
(HTMLFormCollection):
* html/HTMLPropertiesCollection.cpp:
(WebCore::HTMLPropertiesCollection::HTMLPropertiesCollection):
(WebCore::HTMLPropertiesCollection::updateRefElements): Set m_hasItemRefElements true upfront since we never fail to
update ref elements. Bail out as soon as we cleared the m_itemRefElements when the base element doesn't have itemscope
content attribute.
(WebCore::HTMLPropertiesCollection::itemAfter): Added. We reset previousItem to null because the existing itemAfter
requires previousItem be null when we're moving to a new entry in m_itemRefElements.
(WebCore::HTMLPropertiesCollection::calcLength):
(WebCore::HTMLPropertiesCollection::cacheFirstItem):
(WebCore::HTMLPropertiesCollection::updateNameCache): Merged findProperties since this was the only caller.
(WebCore::HTMLPropertiesCollection::names):
(WebCore::HTMLPropertiesCollection::namedItem):
(WebCore::HTMLPropertiesCollection::hasNamedItem):
* html/HTMLPropertiesCollection.h:
(HTMLPropertiesCollection): Made updateRefElements public as it's called in HTMLCollectionWithArrayStorage::item.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (122344 => 122345)


--- trunk/Source/WebCore/ChangeLog	2012-07-11 18:10:06 UTC (rev 122344)
+++ trunk/Source/WebCore/ChangeLog	2012-07-11 18:13:12 UTC (rev 122345)
@@ -1,3 +1,55 @@
+2012-07-10  Ryosuke Niwa  <[email protected]>
+
+        HTMLFormCollection::item and HTMLPropertiesCollection::item should share code
+        https://bugs.webkit.org/show_bug.cgi?id=90932
+
+        Reviewed by Anders Carlsson.
+
+        Merged HTMLFormCollection::item and HTMLPropertiesCollection::item as HTMLCollectionWithArrayStorage::item,
+        which can be merged into HTMLCollection::item in a follow up patch.
+
+        Also moved the call to invalidateCacheIfNeeded into HTMLCollection::updateNameCache() as done in
+        HTMLPropertiesCollection.
+
+        In addition, moved the early bail out for when the base element doesn't have itemscope attribute from individual
+        functions to updateRefElements so that HTMLCollectionWithArrayStorage::item doesn't need to have this check.
+
+        * html/HTMLCollection.cpp:
+        (WebCore::HTMLCollectionWithArrayStorage::item): Added. It's based on HTMLPropertiesCollection::item but it only
+        has the single loop (as supposed to nested loops) as HTMLFormCollection doesn't have multiple items per element
+        in the array unlike HTMLPropertiesCollection. In addition, offsetInArray (was i in HTMLPropertiesCollection::item) is
+        incremented in each itemAfter due to this semantic difference in each itemAfter.
+        (WebCore::HTMLCollection::updateNameCache):
+        (WebCore::HTMLCollection::hasNamedItem):
+        (WebCore::HTMLCollection::namedItems):
+        * html/HTMLCollection.h:
+        (HTMLCollectionWithArrayStorage):
+        (WebCore::HTMLCollectionWithArrayStorage::HTMLCollectionWithArrayStorage):
+        * html/HTMLFormCollection.cpp:
+        (WebCore::HTMLFormCollection::HTMLFormCollection):
+        (WebCore::HTMLFormCollection::calcLength): Merged numberOfFormControlElements since it was only called here.
+        (WebCore::HTMLFormCollection::itemAfter): Added. HTMLFormCollection has exactly one item per element in the array
+        so we increment the offset in each iteration. Note that when we're continuing a search, we need to increment
+        the offset in order to avoid returning the same item.
+        (WebCore::HTMLFormCollection::updateNameCache):
+        * html/HTMLFormCollection.h:
+        (HTMLFormCollection):
+        * html/HTMLPropertiesCollection.cpp:
+        (WebCore::HTMLPropertiesCollection::HTMLPropertiesCollection):
+        (WebCore::HTMLPropertiesCollection::updateRefElements): Set m_hasItemRefElements true upfront since we never fail to
+        update ref elements. Bail out as soon as we cleared the m_itemRefElements when the base element doesn't have itemscope
+        content attribute.
+        (WebCore::HTMLPropertiesCollection::itemAfter): Added. We reset previousItem to null because the existing itemAfter
+        requires previousItem be null when we're moving to a new entry in m_itemRefElements.
+        (WebCore::HTMLPropertiesCollection::calcLength):
+        (WebCore::HTMLPropertiesCollection::cacheFirstItem):
+        (WebCore::HTMLPropertiesCollection::updateNameCache): Merged findProperties since this was the only caller.
+        (WebCore::HTMLPropertiesCollection::names):
+        (WebCore::HTMLPropertiesCollection::namedItem):
+        (WebCore::HTMLPropertiesCollection::hasNamedItem):
+        * html/HTMLPropertiesCollection.h:
+        (HTMLPropertiesCollection): Made updateRefElements public as it's called in HTMLCollectionWithArrayStorage::item.
+
 2012-07-11  Sami Kyostila  <[email protected]>
 
         RenderView layer is marked as fixed position container in the scrolling tree if page scale != 1

Modified: trunk/Source/WebCore/html/HTMLCollection.cpp (122344 => 122345)


--- trunk/Source/WebCore/html/HTMLCollection.cpp	2012-07-11 18:10:06 UTC (rev 122344)
+++ trunk/Source/WebCore/html/HTMLCollection.cpp	2012-07-11 18:13:12 UTC (rev 122345)
@@ -236,6 +236,47 @@
     return cachedItem();
 }
 
+Node* HTMLCollectionWithArrayStorage::item(unsigned index) const
+{
+    invalidateCacheIfNeeded();
+    if (isItemCacheValid() && cachedItemOffset() == index)
+        return cachedItem();
+
+    if (isLengthCacheValid() && cachedLength() <= index)
+        return 0;
+
+#if ENABLE(MICRODATA)
+    if (type() == ItemProperties)
+        static_cast<const HTMLPropertiesCollection*>(this)->updateRefElements();
+#endif
+
+    if (!isItemCacheValid() || cachedItemOffset() > index) {
+        unsigned offsetInArray = 0;
+        setItemCache(itemAfter(offsetInArray, 0), 0, 0);
+        ASSERT(isItemCacheValid());
+        if (!cachedItem() || cachedItemOffset() == index)
+            return cachedItem();
+    }
+
+    unsigned currentIndex = cachedItemOffset();
+    ASSERT(cachedItem()->isHTMLElement());
+    HTMLElement* currentItem = toHTMLElement(cachedItem());
+    ASSERT(currentIndex < index);
+
+    unsigned offsetInArray = cachedElementsArrayOffset();
+    while ((currentItem = itemAfter(offsetInArray, currentItem))) {
+        currentIndex++;
+        if (currentIndex == index) {
+            setItemCache(currentItem, currentIndex, offsetInArray);
+            return cachedItem();
+        }
+    }
+
+    setLengthCache(currentIndex);
+
+    return 0;
+}
+
 static inline bool nameShouldBeVisibleInDocumentAll(HTMLElement* element)
 {
     // The document.all collection returns only certain types of elements by name,
@@ -296,6 +337,7 @@
 
 void HTMLCollection::updateNameCache() const
 {
+    invalidateCacheIfNeeded();
     if (hasNameCache())
         return;
 
@@ -319,7 +361,6 @@
     if (name.isEmpty())
         return false;
 
-    invalidateCacheIfNeeded();
     updateNameCache();
 
     if (Vector<Element*>* cache = idCache(name)) {
@@ -341,7 +382,6 @@
     if (name.isEmpty())
         return;
 
-    invalidateCacheIfNeeded();
     updateNameCache();
 
     Vector<Element*>* idResults = idCache(name);

Modified: trunk/Source/WebCore/html/HTMLCollection.h (122344 => 122345)


--- trunk/Source/WebCore/html/HTMLCollection.h	2012-07-11 18:10:06 UTC (rev 122344)
+++ trunk/Source/WebCore/html/HTMLCollection.h	2012-07-11 18:13:12 UTC (rev 122345)
@@ -155,6 +155,18 @@
     RefPtr<Node> m_base;
 };
 
+class HTMLCollectionWithArrayStorage : public HTMLCollection {
+public:
+    HTMLCollectionWithArrayStorage(Node* base, CollectionType type)
+        : HTMLCollection(base, type)
+    { }
+
+    virtual Node* item(unsigned index) const OVERRIDE;
+
+private:
+    virtual HTMLElement* itemAfter(unsigned& offsetInArray, HTMLElement* previousItem) const = 0;
+};
+
 } // namespace
 
 #endif

Modified: trunk/Source/WebCore/html/HTMLFormCollection.cpp (122344 => 122345)


--- trunk/Source/WebCore/html/HTMLFormCollection.cpp	2012-07-11 18:10:06 UTC (rev 122344)
+++ trunk/Source/WebCore/html/HTMLFormCollection.cpp	2012-07-11 18:13:12 UTC (rev 122345)
@@ -37,7 +37,7 @@
 // calculation every time if anything has changed.
 
 HTMLFormCollection::HTMLFormCollection(Element* base)
-    : HTMLCollection(base, FormControls)
+    : HTMLCollectionWithArrayStorage(base, FormControls)
 {
     ASSERT(base->hasTagName(formTag) || base->hasTagName(fieldsetTag));
 }
@@ -67,48 +67,25 @@
     return static_cast<HTMLFormElement*>(base())->imageElements();
 }
 
-unsigned HTMLFormCollection::numberOfFormControlElements() const
+unsigned HTMLFormCollection::calcLength() const
 {
-    ASSERT(base());
     ASSERT(base()->hasTagName(formTag) || base()->hasTagName(fieldsetTag));
     if (base()->hasTagName(formTag))
         return static_cast<HTMLFormElement*>(base())->length();
     return static_cast<HTMLFieldSetElement*>(base())->length();
 }
 
-unsigned HTMLFormCollection::calcLength() const
+HTMLElement* HTMLFormCollection::itemAfter(unsigned& offset, HTMLElement* previousItem) const
 {
-    return numberOfFormControlElements();
-}
-
-Node* HTMLFormCollection::item(unsigned index) const
-{
-    invalidateCacheIfNeeded();
-
-    if (isItemCacheValid() && cachedItemOffset() == index)
-        return cachedItem();
-
-    if (isLengthCacheValid() && cachedLength() <= index)
-        return 0;
-
-    if (!isItemCacheValid() || cachedItemOffset() > index)
-        setItemCache(0, 0, 0);
-
     const Vector<FormAssociatedElement*>& elementsArray = formControlElements();
-    unsigned currentIndex = cachedItemOffset();
-
-    for (unsigned i = cachedElementsArrayOffset(); i < elementsArray.size(); i++) {
-        if (elementsArray[i]->isEnumeratable()) {
-            HTMLElement* element = toHTMLElement(elementsArray[i]);
-            if (index == currentIndex) {
-                setItemCache(element, index, i);
-                return element;
-            }
-
-            currentIndex++;
-        }
+    if (previousItem)
+        offset++;
+    while (offset < elementsArray.size()) {
+        FormAssociatedElement* element = elementsArray[offset];
+        if (element->isEnumeratable())
+            return toHTMLElement(element);
+        offset++;
     }
-
     return 0;
 }
 
@@ -152,6 +129,7 @@
 
 void HTMLFormCollection::updateNameCache() const
 {
+    invalidateCacheIfNeeded();
     if (hasNameCache())
         return;
 

Modified: trunk/Source/WebCore/html/HTMLFormCollection.h (122344 => 122345)


--- trunk/Source/WebCore/html/HTMLFormCollection.h	2012-07-11 18:10:06 UTC (rev 122344)
+++ trunk/Source/WebCore/html/HTMLFormCollection.h	2012-07-11 18:13:12 UTC (rev 122345)
@@ -34,13 +34,12 @@
 // This class is just a big hack to find form elements even in malformed HTML elements.
 // The famous <table><tr><form><td> problem.
 
-class HTMLFormCollection : public HTMLCollection {
+class HTMLFormCollection : public HTMLCollectionWithArrayStorage {
 public:
     static PassRefPtr<HTMLFormCollection> create(Element*);
 
     virtual ~HTMLFormCollection();
 
-    virtual Node* item(unsigned index) const;
     virtual Node* namedItem(const AtomicString& name) const;
 
 private:
@@ -51,7 +50,7 @@
 
     const Vector<FormAssociatedElement*>& formControlElements() const;
     const Vector<HTMLImageElement*>& formImageElements() const;
-    unsigned numberOfFormControlElements() const;
+    HTMLElement* itemAfter(unsigned& offset, HTMLElement* previousItem) const OVERRIDE;
 };
 
 } //namespace

Modified: trunk/Source/WebCore/html/HTMLPropertiesCollection.cpp (122344 => 122345)


--- trunk/Source/WebCore/html/HTMLPropertiesCollection.cpp	2012-07-11 18:10:06 UTC (rev 122344)
+++ trunk/Source/WebCore/html/HTMLPropertiesCollection.cpp	2012-07-11 18:13:12 UTC (rev 122345)
@@ -51,7 +51,7 @@
 }
 
 HTMLPropertiesCollection::HTMLPropertiesCollection(Node* itemNode)
-    : HTMLCollection(itemNode, ItemProperties)
+    : HTMLCollectionWithArrayStorage(itemNode, ItemProperties)
     , m_hasPropertyNameCache(false)
     , m_hasItemRefElements(false)
 {
@@ -69,10 +69,13 @@
     HTMLElement* baseElement = toHTMLElement(base());
 
     m_itemRefElements.clear();
+    m_hasItemRefElements = true;
 
+    if (!baseElement->fastHasAttribute(itemscopeAttr))
+        return;
+
     if (!baseElement->fastHasAttribute(itemrefAttr)) {
         m_itemRefElements.append(baseElement);
-        m_hasItemRefElements = true;
         return;
     }
 
@@ -97,7 +100,6 @@
                 m_itemRefElements.append(element);
         }
     }
-    m_hasItemRefElements = true;
 }
 
 static Node* nextNodeWithProperty(Node* base, Node* node)
@@ -110,8 +112,19 @@
             ? node->traverseNextNode(base) : node->traverseNextSibling(base);
 }
 
-Element* HTMLPropertiesCollection::itemAfter(Element* base, Node* previous) const
+HTMLElement* HTMLPropertiesCollection::itemAfter(unsigned& offsetInArray, HTMLElement* previousItem) const
 {
+    while (offsetInArray < m_itemRefElements.size()) {
+        if (HTMLElement* next = itemAfter(m_itemRefElements[offsetInArray], previousItem))
+            return next;
+        offsetInArray++;
+        previousItem = 0;
+    }
+    return 0;
+}
+
+HTMLElement* HTMLPropertiesCollection::itemAfter(HTMLElement* base, HTMLElement* previous) const
+{
     Node* current;
     current = previous ? nextNodeWithProperty(base, previous) : base;
 
@@ -129,14 +142,11 @@
 
 unsigned HTMLPropertiesCollection::calcLength() const
 {
-    if (!toHTMLElement(base())->fastHasAttribute(itemscopeAttr))
-        return 0;
-
     unsigned length = 0;
     updateRefElements();
 
     for (unsigned i = 0; i < m_itemRefElements.size(); ++i) {
-        for (Element* element = itemAfter(m_itemRefElements[i], 0); element; element = itemAfter(m_itemRefElements[i], element))
+        for (HTMLElement* element = itemAfter(m_itemRefElements[i], 0); element; element = itemAfter(m_itemRefElements[i], element))
             ++length;
     }
 
@@ -146,64 +156,12 @@
 void HTMLPropertiesCollection::cacheFirstItem() const
 {
     for (unsigned i = 0; i < m_itemRefElements.size(); ++i) {
-        if (Element* element = itemAfter(m_itemRefElements[i], 0))
+        if (HTMLElement* element = itemAfter(m_itemRefElements[i], 0))
             return setItemCache(element, 0, i);
     }
     setItemCache(0, 0, 0);
 }
 
-Node* HTMLPropertiesCollection::item(unsigned index) const
-{
-    if (!toHTMLElement(base())->fastHasAttribute(itemscopeAttr))
-        return 0;
-
-    invalidateCacheIfNeeded();
-    if (isItemCacheValid() && cachedItemOffset() == index)
-        return cachedItem();
-
-    if (isLengthCacheValid() && cachedLength() <= index)
-        return 0;
-
-    updateRefElements();
-    if (!isItemCacheValid() || cachedItemOffset() > index) {
-        cacheFirstItem();
-        ASSERT(isItemCacheValid());
-        if (!cachedItem() || cachedItemOffset() == index)
-            return cachedItem();
-    }
-
-    unsigned currentPosition = cachedItemOffset();
-    Node* element = cachedItem();
-    ASSERT(currentPosition != index);
-
-    for (unsigned i = cachedElementsArrayOffset(); i < m_itemRefElements.size(); ++i) {
-        while (currentPosition < index) {
-            element = itemAfter(m_itemRefElements[i], element);
-            if (!element)
-                break;
-            currentPosition++;
-
-            if (currentPosition == index) {
-                setItemCache(element, currentPosition, i);
-                return cachedItem();
-            }
-        }
-    }
-
-    setLengthCache(currentPosition);
-
-    return 0;
-}
-
-void HTMLPropertiesCollection::findProperties(Element* base) const
-{
-    for (Element* element = itemAfter(base, 0); element; element = itemAfter(base, element)) {
-        DOMSettableTokenList* itemProperty = element->itemProp();
-        for (unsigned i = 0; i < itemProperty->length(); ++i)
-            updatePropertyCache(element, itemProperty->item(i));
-    }
-}
-
 void HTMLPropertiesCollection::updateNameCache() const
 {
     invalidateCacheIfNeeded();
@@ -212,31 +170,31 @@
 
     updateRefElements();
 
-    for (unsigned i = 0; i < m_itemRefElements.size(); ++i)
-        findProperties(m_itemRefElements[i]);
+    for (unsigned i = 0; i < m_itemRefElements.size(); ++i) {
+        HTMLElement* refElement = m_itemRefElements[i];
+        for (HTMLElement* element = itemAfter(refElement, 0); element; element = itemAfter(refElement, element)) {
+            DOMSettableTokenList* itemProperty = element->itemProp();
+            for (unsigned propertyIndex = 0; propertyIndex < itemProperty->length(); ++propertyIndex)
+                updatePropertyCache(element, itemProperty->item(propertyIndex));
+        }
+    }
 
     m_hasPropertyNameCache = true;
 }
 
 PassRefPtr<DOMStringList> HTMLPropertiesCollection::names() const
 {
-    if (!toHTMLElement(base())->fastHasAttribute(itemscopeAttr))
-        return DOMStringList::create();
-
     updateNameCache();
-
+    if (!m_propertyNames)
+        m_propertyNames = DOMStringList::create();
     return m_propertyNames;
 }
 
 PassRefPtr<NodeList> HTMLPropertiesCollection::namedItem(const String& name) const
 {
-    if (!toHTMLElement(base())->fastHasAttribute(itemscopeAttr))
-      return 0;
-
-    Vector<RefPtr<Node> > namedItems;
-
     updateNameCache();
 
+    Vector<RefPtr<Node> > namedItems;
     Vector<Element*>* propertyResults = m_propertyCache.get(AtomicString(name).impl());
     for (unsigned i = 0; propertyResults && i < propertyResults->size(); ++i)
         namedItems.append(propertyResults->at(i));
@@ -247,9 +205,6 @@
 
 bool HTMLPropertiesCollection::hasNamedItem(const AtomicString& name) const
 {
-    if (!toHTMLElement(base())->fastHasAttribute(itemscopeAttr))
-        return false;
-
     updateNameCache();
 
     if (Vector<Element*>* propertyCache = m_propertyCache.get(name.impl())) {

Modified: trunk/Source/WebCore/html/HTMLPropertiesCollection.h (122344 => 122345)


--- trunk/Source/WebCore/html/HTMLPropertiesCollection.h	2012-07-11 18:10:06 UTC (rev 122344)
+++ trunk/Source/WebCore/html/HTMLPropertiesCollection.h	2012-07-11 18:13:12 UTC (rev 122345)
@@ -40,15 +40,14 @@
 
 class DOMStringList;
 
-class HTMLPropertiesCollection : public HTMLCollection {
+class HTMLPropertiesCollection : public HTMLCollectionWithArrayStorage {
 public:
     static PassRefPtr<HTMLPropertiesCollection> create(Node*);
     virtual ~HTMLPropertiesCollection();
 
-    virtual Node* item(unsigned) const OVERRIDE;
+    void updateRefElements() const;
 
     PassRefPtr<DOMStringList> names() const;
-
     virtual PassRefPtr<NodeList> namedItem(const String&) const OVERRIDE;
     virtual bool hasNamedItem(const AtomicString&) const OVERRIDE;
 
@@ -65,15 +64,14 @@
     HTMLPropertiesCollection(Node*);
 
     virtual unsigned calcLength() const OVERRIDE;
-    void findProperties(Element* base) const;
 
     Node* findRefElements(Node* previous) const;
 
     void cacheFirstItem() const;
-    Element* itemAfter(Element* base, Node* previous) const;
+    virtual HTMLElement* itemAfter(unsigned& offsetInArray, HTMLElement* previousItemInSameArrayElement) const OVERRIDE;
+    HTMLElement* itemAfter(HTMLElement* base, HTMLElement* previous) const;
 
     void updateNameCache() const;
-    void updateRefElements() const;
 
     void updatePropertyCache(Element* element, const AtomicString& propertyName) const
     {
@@ -88,7 +86,7 @@
             append(m_propertyCache, propertyName, element);
     }
 
-    mutable Vector<Element*> m_itemRefElements;
+    mutable Vector<HTMLElement*> m_itemRefElements;
     mutable RefPtr<DOMStringList> m_propertyNames;
     mutable NodeCacheMap m_propertyCache;
 
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to