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     PassRefPtr<NodeList> tags(const String&);
     42 
     43     // Non-DOM API
     44     virtual bool hasNamedItem(const AtomicString& name) const;
     45     void namedItems(const AtomicString& name, Vector<RefPtr<Node> >&) const;
     46     bool isEmpty() const
     47     {
     48         if (isLengthCacheValid())
     49             return !cachedLength();
     50         if (isItemCacheValid())
     51             return !cachedItem();
     52         return !item(0);
     53     }
     54     bool hasExactlyOneItem() const
     55     {
     56         if (isLengthCacheValid())
     57             return cachedLength() == 1;
     58         if (isItemCacheValid())
     59             return cachedItem() && !cachedItemOffset() && !item(1);
     60         return item(0) && !item(1);
     61     }
     62 
     63     virtual Element* virtualItemAfter(unsigned& offsetInArray, Element*) const;
     64 
     65     Element* traverseFirstElement(unsigned& offsetInArray, ContainerNode* root) const;
     66     Element* traverseForwardToOffset(unsigned offset, Element* currentElement, unsigned& currentOffset, unsigned& offsetInArray, ContainerNode* root) const;
     67 
     68 protected:
     69     HTMLCollection(Node* base, CollectionType, ItemAfterOverrideType);
     70 
     71     virtual void updateNameCache() const;
     72 
     73     typedef HashMap<StringImpl*, OwnPtr<Vector<Element*> > > NodeCacheMap;
     74     Vector<Element*>* idCache(const AtomicString& name) const { return m_idCache.get(name.impl()); }
     75     Vector<Element*>* nameCache(const AtomicString& name) const { return m_nameCache.get(name.impl()); }
     76     void appendIdCache(const AtomicString& name, Element* element) const { append(m_idCache, name, element); }
     77     void appendNameCache(const AtomicString& name, Element* element) const { append(m_nameCache, name, element); }
     78 
     79 private:
     80     bool checkForNameMatch(Element*, bool checkName, const AtomicString& name) const;
     81     Element* traverseNextElement(unsigned& offsetInArray, Element* previous, ContainerNode* root) const;
     82 
     83     virtual bool isLiveNodeList() const OVERRIDE { ASSERT_NOT_REACHED(); return true; }
     84 
     85     static void append(NodeCacheMap&, const AtomicString&, Element*);
     86 
     87     mutable NodeCacheMap m_idCache;
     88     mutable NodeCacheMap m_nameCache;
     89     mutable unsigned m_cachedElementsArrayOffset;
     90 
     91     friend class LiveNodeListBase;
     92 };
     93 
     94 } // namespace
     95 
     96 #endif
     97