• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 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 <wtf/RefCounted.h>
27 #include <wtf/Forward.h>
28 #include <wtf/HashMap.h>
29 #include <wtf/Vector.h>
30 
31 namespace WebCore {
32 
33 class AtomicString;
34 class AtomicStringImpl;
35 class Element;
36 class Node;
37 class NodeList;
38 class String;
39 
40 class HTMLCollection : public RefCounted<HTMLCollection> {
41 public:
42     enum Type {
43         // unnamed collection types cached in the document
44 
45         DocImages,    // all <img> elements in the document
46         DocApplets,   // all <object> and <applet> elements
47         DocEmbeds,    // all <embed> elements
48         DocObjects,   // all <object> elements
49         DocForms,     // all <form> elements
50         DocLinks,     // all <a> _and_ <area> elements with a value for href
51         DocAnchors,   // all <a> elements with a value for name
52         DocScripts,   // all <script> elements
53 
54         DocAll,       // "all" elements (IE)
55         NodeChildren, // first-level children (IE)
56 
57         // named collection types cached in the document
58 
59         WindowNamedItems,
60         DocumentNamedItems,
61 
62         // types not cached in the document; these are types that can't be used on a document
63 
64         TableTBodies, // all <tbody> elements in this table
65         TSectionRows, // all row elements in this table section
66         TRCells,      // all cells in this row
67         SelectOptions,
68         MapAreas,
69 
70         Other
71     };
72 
73     static const Type FirstUnnamedDocumentCachedType = DocImages;
74     static const unsigned NumUnnamedDocumentCachedTypes = NodeChildren - DocImages + 1;
75 
76     static const Type FirstNamedDocumentCachedType = WindowNamedItems;
77     static const unsigned NumNamedDocumentCachedTypes = DocumentNamedItems - WindowNamedItems + 1;
78 
79     static PassRefPtr<HTMLCollection> create(PassRefPtr<Node> base, Type);
80     virtual ~HTMLCollection();
81 
82     unsigned length() const;
83 
84     virtual Node* item(unsigned index) const;
85     virtual Node* nextItem() const;
86 
87     virtual Node* namedItem(const AtomicString& name) const;
88     virtual Node* nextNamedItem(const AtomicString& name) const; // In case of multiple items named the same way
89 
90     Node* firstItem() const;
91 
92     void namedItems(const AtomicString& name, Vector<RefPtr<Node> >&) const;
93 
94     PassRefPtr<NodeList> tags(const String&);
95 
base()96     Node* base() const { return m_base.get(); }
type()97     Type type() const { return m_type; }
98 
99     // FIXME: This class name is a bad in two ways. First, "info" is much too vague,
100     // and doesn't convey the job of this class (caching collection state).
101     // Second, since this is a member of HTMLCollection, it doesn't need "collection"
102     // in its name.
103     struct CollectionInfo {
104         CollectionInfo();
105         CollectionInfo(const CollectionInfo&);
106         CollectionInfo& operator=(const CollectionInfo& other)
107         {
108             CollectionInfo tmp(other);
109             swap(tmp);
110             return *this;
111         }
112         ~CollectionInfo();
113 
114         void reset();
115         void swap(CollectionInfo&);
116 
117         typedef HashMap<AtomicStringImpl*, Vector<Element*>*> NodeCacheMap;
118 
119         unsigned version;
120         Element* current;
121         unsigned position;
122         unsigned length;
123         int elementsArrayPosition;
124         NodeCacheMap idCache;
125         NodeCacheMap nameCache;
126         bool hasLength;
127         bool hasNameCache;
128 
129     private:
130         static void copyCacheMap(NodeCacheMap&, const NodeCacheMap&);
131     };
132 
133 protected:
134     HTMLCollection(PassRefPtr<Node> base, Type, CollectionInfo*);
135 
info()136     CollectionInfo* info() const { return m_info; }
137     virtual void resetCollectionInfo() const;
138 
139     mutable bool m_idsDone; // for nextNamedItem()
140 
141 private:
142     HTMLCollection(PassRefPtr<Node> base, Type);
143 
144     virtual Element* itemAfter(Element*) const;
145     virtual unsigned calcLength() const;
146     virtual void updateNameCache() const;
147 
148     bool checkForNameMatch(Element*, bool checkName, const AtomicString& name) const;
149 
150     RefPtr<Node> m_base;
151     Type m_type;
152 
153     mutable CollectionInfo* m_info;
154     mutable bool m_ownsInfo;
155 };
156 
157 } // namespace
158 
159 #endif
160