Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
      5  *
      6  * This library is free software; you can redistribute it and/or
      7  * modify it under the terms of the GNU Library General Public
      8  * License as published by the Free Software Foundation; either
      9  * version 2 of the License, or (at your option) any later version.
     10  *
     11  * This library is distributed in the hope that it will be useful,
     12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14  * Library General Public License for more details.
     15  *
     16  * You should have received a copy of the GNU Library General Public License
     17  * along with this library; see the file COPYING.LIB.  If not, write to
     18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     19  * Boston, MA 02110-1301, USA.
     20  *
     21  */
     22 
     23 #ifndef HTMLCollection_h
     24 #define HTMLCollection_h
     25 
     26 #include "core/dom/LiveNodeList.h"
     27 #include "core/html/CollectionType.h"
     28 #include "wtf/Forward.h"
     29 #include "wtf/HashMap.h"
     30 #include "wtf/Vector.h"
     31 
     32 namespace WebCore {
     33 
     34 class HTMLCollection : public LiveNodeListBase {
     35 public:
     36     static PassRefPtr<HTMLCollection> create(Node* base, CollectionType);
     37     virtual ~HTMLCollection();
     38 
     39     // DOM API
     40     virtual Node* namedItem(const AtomicString& name) const;
     41 
     42     // Non-DOM API
     43     virtual bool hasNamedItem(const AtomicString& name) const;
     44     void namedItems(const AtomicString& name, Vector<RefPtr<Node> >&) const;
     45     bool isEmpty() const
     46     {
     47         if (isLengthCacheValid())
     48             return !cachedLength();
     49         if (isItemCacheValid())
     50             return !cachedItem();
     51         return !item(0);
     52     }
     53     bool hasExactlyOneItem() const
     54     {
     55         if (isLengthCacheValid())
     56             return cachedLength() == 1;
     57         if (isItemCacheValid())
     58             return cachedItem() && !cachedItemOffset() && !item(1);
     59         return item(0) && !item(1);
     60     }
     61 
     62     virtual Element* virtualItemAfter(unsigned& offsetInArray, Element*) const;
     63 
     64     Element* traverseFirstElement(unsigned& offsetInArray, ContainerNode& root) const;
     65     Element* traverseForwardToOffset(unsigned offset, Element& currentElement, unsigned& currentOffset, unsigned& offsetInArray, ContainerNode* root) const;
     66 
     67 protected:
     68     HTMLCollection(Node* base, CollectionType, ItemAfterOverrideType);
     69 
     70     virtual void updateNameCache() const;
     71 
     72     typedef HashMap<StringImpl*, OwnPtr<Vector<Element*> > > NodeCacheMap;
     73     Vector<Element*>* idCache(const AtomicString& name) const { return m_idCache.get(name.impl()); }
     74     Vector<Element*>* nameCache(const AtomicString& name) const { return m_nameCache.get(name.impl()); }
     75     void appendIdCache(const AtomicString& name, Element* element) const { append(m_idCache, name, element); }
     76     void appendNameCache(const AtomicString& name, Element* element) const { append(m_nameCache, name, element); }
     77 
     78 private:
     79     bool checkForNameMatch(Element*, bool checkName, const AtomicString& name) const;
     80     Element* traverseNextElement(unsigned& offsetInArray, Element& previous, ContainerNode* root) const;
     81 
     82     virtual bool isLiveNodeList() const OVERRIDE { ASSERT_NOT_REACHED(); return true; }
     83 
     84     static void append(NodeCacheMap&, const AtomicString&, Element*);
     85 
     86     mutable NodeCacheMap m_idCache;
     87     mutable NodeCacheMap m_nameCache;
     88     mutable unsigned m_cachedElementsArrayOffset;
     89 
     90     friend class LiveNodeListBase;
     91 };
     92 
     93 } // namespace
     94 
     95 #endif
     96