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, 2011, 2012 Apple Inc. All rights reserved.
5 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
24 #ifndef HTMLCollection_h
25 #define HTMLCollection_h
27 #include "core/dom/LiveNodeListBase.h"
28 #include "core/html/CollectionItemsCache.h"
29 #include "core/html/CollectionType.h"
30 #include "wtf/Forward.h"
34 class HTMLCollection : public RefCountedWillBeGarbageCollectedFinalized<HTMLCollection>, public ScriptWrappable, public LiveNodeListBase {
35 DEFINE_WRAPPERTYPEINFO();
36 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(HTMLCollection);
38 enum ItemAfterOverrideType {
40 DoesNotOverrideItemAfter,
43 static PassRefPtrWillBeRawPtr<HTMLCollection> create(ContainerNode& base, CollectionType);
44 virtual ~HTMLCollection();
45 virtual void invalidateCache(Document* oldDocument = 0) const override;
46 void invalidateCacheForAttribute(const QualifiedName*) const;
49 unsigned length() const;
50 Element* item(unsigned offset) const;
51 virtual Element* namedItem(const AtomicString& name) const;
52 bool namedPropertyQuery(const AtomicString&, ExceptionState&);
53 void namedPropertyEnumerator(Vector<String>& names, ExceptionState&);
56 void namedItems(const AtomicString& name, WillBeHeapVector<RefPtrWillBeMember<Element>>&) const;
57 bool isEmpty() const { return m_collectionItemsCache.isEmpty(*this); }
58 bool hasExactlyOneItem() const { return m_collectionItemsCache.hasExactlyOneNode(*this); }
59 bool elementMatches(const Element&) const;
61 // CollectionIndexCache API.
62 bool canTraverseBackward() const { return !overridesItemAfter(); }
63 Element* traverseToFirst() const;
64 Element* traverseToLast() const;
65 Element* traverseForwardToOffset(unsigned offset, Element& currentElement, unsigned& currentOffset) const;
66 Element* traverseBackwardToOffset(unsigned offset, Element& currentElement, unsigned& currentOffset) const;
68 virtual void trace(Visitor*);
71 HTMLCollection(ContainerNode& base, CollectionType, ItemAfterOverrideType);
73 class NamedItemCache final : public NoBaseWillBeGarbageCollected<NamedItemCache> {
75 static PassOwnPtrWillBeRawPtr<NamedItemCache> create()
77 return adoptPtrWillBeNoop(new NamedItemCache);
80 WillBeHeapVector<RawPtrWillBeMember<Element>>* getElementsById(const AtomicString& id) const { return m_idCache.get(id.impl()); }
81 WillBeHeapVector<RawPtrWillBeMember<Element>>* getElementsByName(const AtomicString& name) const { return m_nameCache.get(name.impl()); }
82 void addElementWithId(const AtomicString& id, Element* element) { addElementToMap(m_idCache, id, element); }
83 void addElementWithName(const AtomicString& name, Element* element) { addElementToMap(m_nameCache, name, element); }
85 void trace(Visitor* visitor)
88 visitor->trace(m_idCache);
89 visitor->trace(m_nameCache);
95 typedef WillBeHeapHashMap<StringImpl*, OwnPtrWillBeMember<WillBeHeapVector<RawPtrWillBeMember<Element>>>> StringToElementsMap;
96 static void addElementToMap(StringToElementsMap& map, const AtomicString& key, Element* element)
98 OwnPtrWillBeMember<WillBeHeapVector<RawPtrWillBeMember<Element>>>& vector = map.add(key.impl(), nullptr).storedValue->value;
100 vector = adoptPtrWillBeNoop(new WillBeHeapVector<RawPtrWillBeMember<Element>>);
101 vector->append(element);
104 StringToElementsMap m_idCache;
105 StringToElementsMap m_nameCache;
108 bool overridesItemAfter() const { return m_overridesItemAfter; }
109 virtual Element* virtualItemAfter(Element*) const;
110 bool shouldOnlyIncludeDirectChildren() const { return m_shouldOnlyIncludeDirectChildren; }
111 virtual void supportedPropertyNames(Vector<String>& names);
113 virtual void updateIdNameCache() const;
114 bool hasValidIdNameCache() const { return m_namedItemCache; }
116 void setNamedItemCache(PassOwnPtrWillBeRawPtr<NamedItemCache> cache) const
118 ASSERT(!m_namedItemCache);
119 document().registerNodeListWithIdNameCache(this);
120 m_namedItemCache = cache;
123 NamedItemCache& namedItemCache() const
125 ASSERT(m_namedItemCache);
126 return *m_namedItemCache;
130 void invalidateIdNameCacheMaps(Document* oldDocument = 0) const
132 if (!hasValidIdNameCache())
135 // Make sure we decrement the NodeListWithIdNameCache count from
136 // the old document instead of the new one in the case the collection
137 // is moved to a new document.
138 unregisterIdNameCacheFromDocument(oldDocument ? *oldDocument : document());
140 m_namedItemCache.clear();
143 void unregisterIdNameCacheFromDocument(Document& document) const
145 ASSERT(hasValidIdNameCache());
146 document.unregisterNodeListWithIdNameCache(this);
149 const unsigned m_overridesItemAfter : 1;
150 const unsigned m_shouldOnlyIncludeDirectChildren : 1;
151 mutable OwnPtrWillBeMember<NamedItemCache> m_namedItemCache;
152 mutable CollectionItemsCache<HTMLCollection, Element> m_collectionItemsCache;
155 DEFINE_TYPE_CASTS(HTMLCollection, LiveNodeListBase, collection, isHTMLCollectionType(collection->type()), isHTMLCollectionType(collection.type()));
157 inline void HTMLCollection::invalidateCacheForAttribute(const QualifiedName* attrName) const
159 if (!attrName || shouldInvalidateTypeOnAttributeChange(invalidationType(), *attrName))
161 else if (*attrName == HTMLNames::idAttr || *attrName == HTMLNames::nameAttr)
162 invalidateIdNameCacheMaps();
167 #endif // HTMLCollection_h