Unreviewed, rolling out r123184, r123195, and r123197.
[WebKit-https.git] / Source / WebCore / dom / NodeRareData.h
1 /*
2  * Copyright (C) 2008, 2010 Apple Inc. All rights reserved.
3  * Copyright (C) 2008 David Smith <catfish.man@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  */
21
22 #ifndef NodeRareData_h
23 #define NodeRareData_h
24
25 #include "ChildNodeList.h"
26 #include "DOMSettableTokenList.h"
27 #include "DynamicNodeList.h"
28 #include "MutationObserver.h"
29 #include "MutationObserverRegistration.h"
30 #include "QualifiedName.h"
31 #include "TagNodeList.h"
32 #include <wtf/HashSet.h>
33 #include <wtf/OwnPtr.h>
34 #include <wtf/PassOwnPtr.h>
35 #include <wtf/text/AtomicString.h>
36 #include <wtf/text/StringHash.h>
37
38 #if ENABLE(MICRODATA)
39 #include "HTMLPropertiesCollection.h"
40 #include "MicroDataItemList.h"
41 #endif
42
43 namespace WebCore {
44
45 class LabelsNodeList;
46 class RadioNodeList;
47 class TreeScope;
48
49 class NodeListsNodeData {
50     WTF_MAKE_NONCOPYABLE(NodeListsNodeData); WTF_MAKE_FAST_ALLOCATED;
51 public:
52     template <typename StringType>
53     struct NodeListCacheMapEntryHash {
54         static unsigned hash(const std::pair<unsigned char, StringType>& entry)
55         {
56             return DefaultHash<StringType>::Hash::hash(entry.second) + entry.first;
57         }
58         static bool equal(const std::pair<unsigned char, StringType>& a, const std::pair<unsigned char, StringType>& b) { return a == b; }
59         static const bool safeToCompareToEmptyOrDeleted = DefaultHash<StringType>::Hash::safeToCompareToEmptyOrDeleted;
60     };
61
62     typedef HashMap<std::pair<unsigned char, AtomicString>, DynamicSubtreeNodeList*, NodeListCacheMapEntryHash<AtomicString> > NodeListAtomicNameCacheMap;
63     typedef HashMap<std::pair<unsigned char, String>, DynamicSubtreeNodeList*, NodeListCacheMapEntryHash<String> > NodeListNameCacheMap;
64     typedef HashMap<QualifiedName, TagNodeList*> TagNodeListCacheNS;
65
66     template<typename T>
67     PassRefPtr<T> addCacheWithAtomicName(Node* node, DynamicNodeList::NodeListType listType, const AtomicString& name)
68     {
69         NodeListAtomicNameCacheMap::AddResult result = m_atomicNameCaches.add(namedNodeListKey(listType, name), 0);
70         if (!result.isNewEntry)
71             return static_cast<T*>(result.iterator->second);
72
73         RefPtr<T> list = T::create(node, name);
74         result.iterator->second = list.get();
75         return list.release();
76     }
77
78     template<typename T>
79     PassRefPtr<T> addCacheWithName(Node* node, DynamicNodeList::NodeListType listType, const String& name)
80     {
81         NodeListNameCacheMap::AddResult result = m_nameCaches.add(namedNodeListKey(listType, name), 0);
82         if (!result.isNewEntry)
83             return static_cast<T*>(result.iterator->second);
84
85         RefPtr<T> list = T::create(node, name);
86         result.iterator->second = list.get();
87         return list.release();
88     }
89
90     PassRefPtr<TagNodeList> addCacheWithQualifiedName(Node* node, const AtomicString& namespaceURI, const AtomicString& localName)
91     {
92         QualifiedName name(nullAtom, localName, namespaceURI);
93         TagNodeListCacheNS::AddResult result = m_tagNodeListCacheNS.add(name, 0);
94         if (!result.isNewEntry)
95             return result.iterator->second;
96
97         RefPtr<TagNodeList> list = TagNodeList::create(node, namespaceURI, localName);
98         result.iterator->second = list.get();
99         return list.release();
100     }
101
102     void removeCacheWithAtomicName(DynamicSubtreeNodeList* list, DynamicNodeList::NodeListType listType, const AtomicString& name)
103     {
104         ASSERT_UNUSED(list, list == m_atomicNameCaches.get(namedNodeListKey(listType, name)));
105         m_atomicNameCaches.remove(namedNodeListKey(listType, name));
106     }
107
108     void removeCacheWithName(DynamicSubtreeNodeList* list, DynamicNodeList::NodeListType listType, const String& name)
109     {
110         ASSERT_UNUSED(list, list == m_nameCaches.get(namedNodeListKey(listType, name)));
111         m_nameCaches.remove(namedNodeListKey(listType, name));
112     }
113
114     void removeCacheWithQualifiedName(DynamicSubtreeNodeList* list, const AtomicString& namespaceURI, const AtomicString& localName)
115     {
116         QualifiedName name(nullAtom, localName, namespaceURI);
117         ASSERT_UNUSED(list, list == m_tagNodeListCacheNS.get(name));
118         m_tagNodeListCacheNS.remove(name);
119     }
120
121     static PassOwnPtr<NodeListsNodeData> create()
122     {
123         return adoptPtr(new NodeListsNodeData);
124     }
125
126     void invalidateCaches(const QualifiedName* attrName = 0);
127     bool isEmpty() const
128     {
129         return m_atomicNameCaches.isEmpty() && m_nameCaches.isEmpty() && m_tagNodeListCacheNS.isEmpty();
130     }
131
132     void adoptTreeScope(Document* oldDocument, Document* newDocument)
133     {
134         invalidateCaches();
135
136         if (oldDocument != newDocument) {
137             NodeListAtomicNameCacheMap::const_iterator atomicNameCacheEnd = m_atomicNameCaches.end();
138             for (NodeListAtomicNameCacheMap::const_iterator it = m_atomicNameCaches.begin(); it != atomicNameCacheEnd; ++it) {
139                 DynamicSubtreeNodeList* list = it->second;
140                 if (list->isRootedAtDocument()) {
141                     oldDocument->unregisterNodeListCache(list);
142                     newDocument->registerNodeListCache(list);
143                 }
144             }
145
146             NodeListNameCacheMap::const_iterator nameCacheEnd = m_nameCaches.end();
147             for (NodeListNameCacheMap::const_iterator it = m_nameCaches.begin(); it != nameCacheEnd; ++it) {
148                 DynamicSubtreeNodeList* list = it->second;
149                 if (list->isRootedAtDocument()) {
150                     oldDocument->unregisterNodeListCache(list);
151                     newDocument->registerNodeListCache(list);
152                 }
153             }
154
155             TagNodeListCacheNS::const_iterator tagEnd = m_tagNodeListCacheNS.end();
156             for (TagNodeListCacheNS::const_iterator it = m_tagNodeListCacheNS.begin(); it != tagEnd; ++it) {
157                 DynamicSubtreeNodeList* list = it->second;
158                 ASSERT(!list->isRootedAtDocument());
159                 oldDocument->unregisterNodeListCache(list);
160                 newDocument->registerNodeListCache(list);
161             }
162         }
163     }
164
165 private:
166     NodeListsNodeData() { }
167
168     std::pair<unsigned char, AtomicString> namedNodeListKey(DynamicNodeList::NodeListType listType, const AtomicString& name)
169     {
170         return std::pair<unsigned char, AtomicString>(listType, name);
171     }
172
173     std::pair<unsigned char, String> namedNodeListKey(DynamicNodeList::NodeListType listType, const String& name)
174     {
175         return std::pair<unsigned char, String>(listType, name);
176     }
177
178     NodeListAtomicNameCacheMap m_atomicNameCaches;
179     NodeListNameCacheMap m_nameCaches;
180     TagNodeListCacheNS m_tagNodeListCacheNS;
181 };
182
183 class NodeRareData {
184     WTF_MAKE_NONCOPYABLE(NodeRareData); WTF_MAKE_FAST_ALLOCATED;
185 public:    
186     NodeRareData()
187         : m_treeScope(0)
188         , m_childNodeList(0)
189         , m_tabIndex(0)
190         , m_tabIndexWasSetExplicitly(false)
191         , m_isFocused(false)
192         , m_needsFocusAppearanceUpdateSoonAfterAttach(false)
193 #if ENABLE(STYLE_SCOPED)
194         , m_numberOfScopedHTMLStyleChildren(0)
195 #endif
196     {
197     }
198
199     virtual ~NodeRareData()
200     {
201     }
202
203     typedef HashMap<const Node*, NodeRareData*> NodeRareDataMap;
204     
205     static NodeRareDataMap& rareDataMap()
206     {
207         static NodeRareDataMap* dataMap = new NodeRareDataMap;
208         return *dataMap;
209     }
210     
211     static NodeRareData* rareDataFromMap(const Node* node)
212     {
213         return rareDataMap().get(node);
214     }
215
216     TreeScope* treeScope() const { return m_treeScope; }
217     void setTreeScope(TreeScope* treeScope) { m_treeScope = treeScope; }
218     
219     void clearNodeLists() { m_nodeLists.clear(); }
220     void setNodeLists(PassOwnPtr<NodeListsNodeData> lists) { m_nodeLists = lists; }
221     NodeListsNodeData* nodeLists() const { return m_nodeLists.get(); }
222     NodeListsNodeData* ensureNodeLists()
223     {
224         if (!m_nodeLists)
225             setNodeLists(NodeListsNodeData::create());
226         return m_nodeLists.get();
227     }
228     void clearChildNodeListCache()
229     {
230         if (m_childNodeList)
231             m_childNodeList->invalidateCache();
232     }
233
234     ChildNodeList* childNodeList() const { return m_childNodeList; }
235     void setChildNodeList(ChildNodeList* list) { m_childNodeList = list; }
236
237     short tabIndex() const { return m_tabIndex; }
238     void setTabIndexExplicitly(short index) { m_tabIndex = index; m_tabIndexWasSetExplicitly = true; }
239     bool tabIndexSetExplicitly() const { return m_tabIndexWasSetExplicitly; }
240     void clearTabIndexExplicitly() { m_tabIndex = 0; m_tabIndexWasSetExplicitly = false; }
241
242     EventTargetData* eventTargetData() { return m_eventTargetData.get(); }
243     EventTargetData* ensureEventTargetData()
244     {
245         if (!m_eventTargetData)
246             m_eventTargetData = adoptPtr(new EventTargetData);
247         return m_eventTargetData.get();
248     }
249
250 #if ENABLE(MUTATION_OBSERVERS)
251     Vector<OwnPtr<MutationObserverRegistration> >* mutationObserverRegistry() { return m_mutationObserverRegistry.get(); }
252     Vector<OwnPtr<MutationObserverRegistration> >* ensureMutationObserverRegistry()
253     {
254         if (!m_mutationObserverRegistry)
255             m_mutationObserverRegistry = adoptPtr(new Vector<OwnPtr<MutationObserverRegistration> >);
256         return m_mutationObserverRegistry.get();
257     }
258
259     HashSet<MutationObserverRegistration*>* transientMutationObserverRegistry() { return m_transientMutationObserverRegistry.get(); }
260     HashSet<MutationObserverRegistration*>* ensureTransientMutationObserverRegistry()
261     {
262         if (!m_transientMutationObserverRegistry)
263             m_transientMutationObserverRegistry = adoptPtr(new HashSet<MutationObserverRegistration*>);
264         return m_transientMutationObserverRegistry.get();
265     }
266 #endif
267
268 #if ENABLE(MICRODATA)
269     DOMSettableTokenList* itemProp() const
270     {
271         if (!m_itemProp)
272             m_itemProp = DOMSettableTokenList::create();
273
274         return m_itemProp.get();
275     }
276
277     void setItemProp(const String& value)
278     {
279         if (!m_itemProp)
280             m_itemProp = DOMSettableTokenList::create();
281
282         m_itemProp->setValue(value);
283     }
284
285     DOMSettableTokenList* itemRef() const
286     {
287         if (!m_itemRef)
288             m_itemRef = DOMSettableTokenList::create();
289
290         return m_itemRef.get();
291     }
292
293     void setItemRef(const String& value)
294     {
295         if (!m_itemRef)
296             m_itemRef = DOMSettableTokenList::create();
297
298         m_itemRef->setValue(value);
299     }
300
301     DOMSettableTokenList* itemType() const
302     {
303         if (!m_itemType)
304             m_itemType = DOMSettableTokenList::create();
305
306         return m_itemType.get();
307     }
308
309     void setItemType(const String& value)
310     {
311         if (!m_itemType)
312             m_itemType = DOMSettableTokenList::create();
313
314         m_itemType->setValue(value);
315     }
316 #endif
317
318 #if ENABLE(STYLE_SCOPED)
319     void registerScopedHTMLStyleChild()
320     {
321         ++m_numberOfScopedHTMLStyleChildren;
322     }
323
324     void unregisterScopedHTMLStyleChild()
325     {
326         ASSERT(m_numberOfScopedHTMLStyleChildren > 0);
327         if (m_numberOfScopedHTMLStyleChildren > 0)
328             --m_numberOfScopedHTMLStyleChildren;
329     }
330
331     bool hasScopedHTMLStyleChild() const
332     {
333         return m_numberOfScopedHTMLStyleChildren;
334     }
335
336     size_t numberOfScopedHTMLStyleChildren() const
337     {
338         return m_numberOfScopedHTMLStyleChildren;
339     }
340 #endif
341
342     bool isFocused() const { return m_isFocused; }
343     void setFocused(bool focused) { m_isFocused = focused; }
344
345 protected:
346     // for ElementRareData
347     bool needsFocusAppearanceUpdateSoonAfterAttach() const { return m_needsFocusAppearanceUpdateSoonAfterAttach; }
348     void setNeedsFocusAppearanceUpdateSoonAfterAttach(bool needs) { m_needsFocusAppearanceUpdateSoonAfterAttach = needs; }
349
350 private:
351
352     TreeScope* m_treeScope;
353     OwnPtr<NodeListsNodeData> m_nodeLists;
354     ChildNodeList* m_childNodeList;
355     OwnPtr<EventTargetData> m_eventTargetData;
356     short m_tabIndex;
357     bool m_tabIndexWasSetExplicitly : 1;
358     bool m_isFocused : 1;
359     bool m_needsFocusAppearanceUpdateSoonAfterAttach : 1;
360
361 #if ENABLE(MUTATION_OBSERVERS)
362     OwnPtr<Vector<OwnPtr<MutationObserverRegistration> > > m_mutationObserverRegistry;
363     OwnPtr<HashSet<MutationObserverRegistration*> > m_transientMutationObserverRegistry;
364 #endif
365
366 #if ENABLE(MICRODATA)
367     mutable RefPtr<DOMSettableTokenList> m_itemProp;
368     mutable RefPtr<DOMSettableTokenList> m_itemRef;
369     mutable RefPtr<DOMSettableTokenList> m_itemType;
370 #endif
371
372 #if ENABLE(STYLE_SCOPED)
373     size_t m_numberOfScopedHTMLStyleChildren;
374 #endif
375 };
376
377 } // namespace WebCore
378
379 #endif // NodeRareData_h