OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) | 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) |
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) | 3 * (C) 1999 Antti Koivisto (koivisto@kde.org) |
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All r
ights reserved. | 4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All r
ights reserved. |
5 * Copyright (C) 2014 Samsung Electronics. All rights reserved. | 5 * Copyright (C) 2014 Samsung Electronics. All rights reserved. |
6 * | 6 * |
7 * This library is free software; you can redistribute it and/or | 7 * This library is free software; you can redistribute it and/or |
8 * modify it under the terms of the GNU Library General Public | 8 * modify it under the terms of the GNU Library General Public |
9 * License as published by the Free Software Foundation; either | 9 * License as published by the Free Software Foundation; either |
10 * version 2 of the License, or (at your option) any later version. | 10 * version 2 of the License, or (at your option) any later version. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 | 58 |
59 // CollectionIndexCache API. | 59 // CollectionIndexCache API. |
60 bool canTraverseBackward() const { return !overridesItemAfter(); } | 60 bool canTraverseBackward() const { return !overridesItemAfter(); } |
61 Element* itemBefore(const Element* previousItem) const; | 61 Element* itemBefore(const Element* previousItem) const; |
62 Element* traverseToFirstElement(const ContainerNode& root) const; | 62 Element* traverseToFirstElement(const ContainerNode& root) const; |
63 Element* traverseForwardToOffset(unsigned offset, Element& currentElement, u
nsigned& currentOffset, const ContainerNode& root) const; | 63 Element* traverseForwardToOffset(unsigned offset, Element& currentElement, u
nsigned& currentOffset, const ContainerNode& root) const; |
64 | 64 |
65 protected: | 65 protected: |
66 HTMLCollection(ContainerNode& base, CollectionType, ItemAfterOverrideType); | 66 HTMLCollection(ContainerNode& base, CollectionType, ItemAfterOverrideType); |
67 | 67 |
| 68 class NamedItemCache { |
| 69 public: |
| 70 Vector<Element*>* getElementsById(const AtomicString& id) const { return
m_idCache.get(id.impl()); } |
| 71 Vector<Element*>* getElementsByName(const AtomicString& name) const { re
turn m_nameCache.get(name.impl()); } |
| 72 void addElementWithId(const AtomicString& id, Element* element) { addEle
mentToMap(m_idCache, id, element); } |
| 73 void addElementWithName(const AtomicString& name, Element* element) { ad
dElementToMap(m_nameCache, name, element); } |
| 74 |
| 75 private: |
| 76 typedef HashMap<StringImpl*, OwnPtr<Vector<Element*> > > StringToElement
sMap; |
| 77 static void addElementToMap(StringToElementsMap& map, const AtomicString
& key, Element* element) |
| 78 { |
| 79 OwnPtr<Vector<Element*> >& vector = map.add(key.impl(), nullptr).sto
redValue->value; |
| 80 if (!vector) |
| 81 vector = adoptPtr(new Vector<Element*>); |
| 82 vector->append(element); |
| 83 } |
| 84 |
| 85 StringToElementsMap m_idCache; |
| 86 StringToElementsMap m_nameCache; |
| 87 }; |
| 88 |
68 bool overridesItemAfter() const { return m_overridesItemAfter; } | 89 bool overridesItemAfter() const { return m_overridesItemAfter; } |
69 virtual Element* virtualItemAfter(Element*) const; | 90 virtual Element* virtualItemAfter(Element*) const; |
70 bool shouldOnlyIncludeDirectChildren() const { return m_shouldOnlyIncludeDir
ectChildren; } | 91 bool shouldOnlyIncludeDirectChildren() const { return m_shouldOnlyIncludeDir
ectChildren; } |
71 virtual void supportedPropertyNames(Vector<String>& names); | 92 virtual void supportedPropertyNames(Vector<String>& names); |
72 | 93 |
73 virtual void updateIdNameCache() const; | 94 virtual void updateIdNameCache() const; |
74 bool hasValidIdNameCache() const { return m_hasValidIdNameCache; } | 95 bool hasValidIdNameCache() const { return m_namedItemCache; } |
75 void setHasValidIdNameCache() const | 96 |
| 97 NamedItemCache& createNamedItemCache() const |
76 { | 98 { |
77 ASSERT(!m_hasValidIdNameCache); | 99 ASSERT(!m_namedItemCache); |
78 m_hasValidIdNameCache = true; | |
79 document().incrementNodeListWithIdNameCacheCount(); | 100 document().incrementNodeListWithIdNameCacheCount(); |
| 101 m_namedItemCache = adoptPtr(new NamedItemCache); |
| 102 return *m_namedItemCache; |
80 } | 103 } |
81 | 104 NamedItemCache& namedItemCache() const |
82 typedef HashMap<StringImpl*, OwnPtr<Vector<Element*> > > NodeCacheMap; | 105 { |
83 Vector<Element*>* idCache(const AtomicString& name) const { return m_idCache
.get(name.impl()); } | 106 ASSERT(m_namedItemCache); |
84 Vector<Element*>* nameCache(const AtomicString& name) const { return m_nameC
ache.get(name.impl()); } | 107 return *m_namedItemCache; |
85 void appendIdCache(const AtomicString& name, Element* element) const { appen
d(m_idCache, name, element); } | 108 } |
86 void appendNameCache(const AtomicString& name, Element* element) const { app
end(m_nameCache, name, element); } | |
87 | 109 |
88 private: | 110 private: |
89 Element* traverseNextElement(Element& previous, const ContainerNode& root) c
onst; | 111 Element* traverseNextElement(Element& previous, const ContainerNode& root) c
onst; |
90 | 112 |
91 static void append(NodeCacheMap&, const AtomicString&, Element*); | |
92 void invalidateIdNameCacheMaps(Document* oldDocument = 0) const | 113 void invalidateIdNameCacheMaps(Document* oldDocument = 0) const |
93 { | 114 { |
94 if (!m_hasValidIdNameCache) | 115 if (!hasValidIdNameCache()) |
95 return; | 116 return; |
96 | 117 |
97 // Make sure we decrement the NodeListWithIdNameCache count from | 118 // Make sure we decrement the NodeListWithIdNameCache count from |
98 // the old document instead of the new one in the case the collection | 119 // the old document instead of the new one in the case the collection |
99 // is moved to a new document. | 120 // is moved to a new document. |
100 unregisterIdNameCacheFromDocument(oldDocument ? *oldDocument : document(
)); | 121 unregisterIdNameCacheFromDocument(oldDocument ? *oldDocument : document(
)); |
101 | 122 |
102 m_idCache.clear(); | 123 m_namedItemCache.clear(); |
103 m_nameCache.clear(); | |
104 m_hasValidIdNameCache = false; | |
105 } | 124 } |
106 | 125 |
107 void unregisterIdNameCacheFromDocument(Document& document) const | 126 void unregisterIdNameCacheFromDocument(Document& document) const |
108 { | 127 { |
109 ASSERT(m_hasValidIdNameCache); | 128 ASSERT(hasValidIdNameCache()); |
110 document.decrementNodeListWithIdNameCacheCount(); | 129 document.decrementNodeListWithIdNameCacheCount(); |
111 } | 130 } |
112 | 131 |
113 const unsigned m_overridesItemAfter : 1; | 132 const unsigned m_overridesItemAfter : 1; |
114 const unsigned m_shouldOnlyIncludeDirectChildren : 1; | 133 const unsigned m_shouldOnlyIncludeDirectChildren : 1; |
115 mutable unsigned m_hasValidIdNameCache : 1; | 134 mutable OwnPtr<NamedItemCache> m_namedItemCache; |
116 mutable NodeCacheMap m_idCache; | |
117 mutable NodeCacheMap m_nameCache; | |
118 mutable CollectionIndexCache<HTMLCollection, Element> m_collectionIndexCache
; | 135 mutable CollectionIndexCache<HTMLCollection, Element> m_collectionIndexCache
; |
119 | 136 |
120 friend class LiveNodeListBase; | 137 friend class LiveNodeListBase; |
121 }; | 138 }; |
122 | 139 |
123 } // namespace | 140 } // namespace |
124 | 141 |
125 #endif | 142 #endif |
OLD | NEW |