/src/mozilla-central/dom/base/nsDOMAttributeMap.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | /* |
8 | | * Implementation of the |attributes| property of DOM Core's Element object. |
9 | | */ |
10 | | |
11 | | #ifndef nsDOMAttributeMap_h |
12 | | #define nsDOMAttributeMap_h |
13 | | |
14 | | #include "mozilla/MemoryReporting.h" |
15 | | #include "mozilla/dom/Attr.h" |
16 | | #include "mozilla/ErrorResult.h" |
17 | | #include "nsCycleCollectionParticipant.h" |
18 | | #include "nsRefPtrHashtable.h" |
19 | | #include "nsString.h" |
20 | | #include "nsWrapperCache.h" |
21 | | |
22 | | class nsAtom; |
23 | | class nsIDocument; |
24 | | namespace mozilla { |
25 | | namespace dom { |
26 | | class DocGroup; |
27 | | } // namespace dom |
28 | | } // namespace mozilla |
29 | | |
30 | | /** |
31 | | * Structure used as a key for caching Attrs in nsDOMAttributeMap's mAttributeCache. |
32 | | */ |
33 | | class nsAttrKey |
34 | | { |
35 | | public: |
36 | | /** |
37 | | * The namespace of the attribute |
38 | | */ |
39 | | int32_t mNamespaceID; |
40 | | |
41 | | /** |
42 | | * The atom for attribute, stored as void*, to make sure that we only use it |
43 | | * for the hashcode, and we can never dereference it. |
44 | | */ |
45 | | void* mLocalName; |
46 | | |
47 | | nsAttrKey(int32_t aNs, nsAtom* aName) |
48 | 0 | : mNamespaceID(aNs), mLocalName(aName) {} |
49 | | |
50 | | nsAttrKey(const nsAttrKey& aAttr) |
51 | 0 | : mNamespaceID(aAttr.mNamespaceID), mLocalName(aAttr.mLocalName) {} |
52 | | }; |
53 | | |
54 | | /** |
55 | | * PLDHashEntryHdr implementation for nsAttrKey. |
56 | | */ |
57 | | class nsAttrHashKey : public PLDHashEntryHdr |
58 | | { |
59 | | public: |
60 | | typedef const nsAttrKey& KeyType; |
61 | | typedef const nsAttrKey* KeyTypePointer; |
62 | | |
63 | 0 | explicit nsAttrHashKey(KeyTypePointer aKey) : mKey(*aKey) {} |
64 | 0 | nsAttrHashKey(const nsAttrHashKey& aCopy) : mKey(aCopy.mKey) {} |
65 | 0 | ~nsAttrHashKey() {} |
66 | | |
67 | 0 | KeyType GetKey() const { return mKey; } |
68 | | bool KeyEquals(KeyTypePointer aKey) const |
69 | 0 | { |
70 | 0 | return mKey.mLocalName == aKey->mLocalName && |
71 | 0 | mKey.mNamespaceID == aKey->mNamespaceID; |
72 | 0 | } |
73 | | |
74 | 0 | static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } |
75 | | static PLDHashNumber HashKey(KeyTypePointer aKey) |
76 | 0 | { |
77 | 0 | if (!aKey) |
78 | 0 | return 0; |
79 | 0 | |
80 | 0 | return mozilla::HashGeneric(aKey->mNamespaceID, aKey->mLocalName); |
81 | 0 | } |
82 | | enum { ALLOW_MEMMOVE = true }; |
83 | | |
84 | | private: |
85 | | nsAttrKey mKey; |
86 | | }; |
87 | | |
88 | | class nsDOMAttributeMap final : public nsISupports |
89 | | , public nsWrapperCache |
90 | | { |
91 | | public: |
92 | | typedef mozilla::dom::Attr Attr; |
93 | | typedef mozilla::dom::DocGroup DocGroup; |
94 | | typedef mozilla::dom::Element Element; |
95 | | typedef mozilla::ErrorResult ErrorResult; |
96 | | |
97 | | explicit nsDOMAttributeMap(Element *aContent); |
98 | | |
99 | | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
100 | | NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsDOMAttributeMap) |
101 | | |
102 | | void DropReference(); |
103 | | |
104 | | Element* GetContent() |
105 | 0 | { |
106 | 0 | return mContent; |
107 | 0 | } |
108 | | |
109 | | /** |
110 | | * Called when mContent is moved into a new document. |
111 | | * Updates the nodeinfos of all owned nodes. |
112 | | */ |
113 | | nsresult SetOwnerDocument(nsIDocument* aDocument); |
114 | | |
115 | | /** |
116 | | * Drop an attribute from the map's cache (does not remove the attribute |
117 | | * from the node!) |
118 | | */ |
119 | | void DropAttribute(int32_t aNamespaceID, nsAtom* aLocalName); |
120 | | |
121 | | /** |
122 | | * Returns the number of attribute nodes currently in the map. |
123 | | * Note: this is just the number of cached attribute nodes, not the number of |
124 | | * attributes in mContent. |
125 | | * |
126 | | * @return The number of attribute nodes in the map. |
127 | | */ |
128 | | uint32_t Count() const; |
129 | | |
130 | | typedef nsRefPtrHashtable<nsAttrHashKey, Attr> AttrCache; |
131 | | |
132 | | static void BlastSubtreeToPieces(nsINode *aNode); |
133 | | |
134 | | Element* GetParentObject() const |
135 | 0 | { |
136 | 0 | return mContent; |
137 | 0 | } |
138 | | virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; |
139 | | DocGroup* GetDocGroup() const; |
140 | | |
141 | | // WebIDL |
142 | | Attr* GetNamedItem(const nsAString& aAttrName); |
143 | | Attr* NamedGetter(const nsAString& aAttrName, bool& aFound); |
144 | | already_AddRefed<Attr> |
145 | | RemoveNamedItem(mozilla::dom::NodeInfo* aNodeInfo, ErrorResult& aError); |
146 | | already_AddRefed<Attr> |
147 | | RemoveNamedItem(const nsAString& aName, ErrorResult& aError); |
148 | | |
149 | | Attr* Item(uint32_t aIndex); |
150 | | Attr* IndexedGetter(uint32_t aIndex, bool& aFound); |
151 | | uint32_t Length() const; |
152 | | |
153 | | Attr* |
154 | | GetNamedItemNS(const nsAString& aNamespaceURI, |
155 | | const nsAString& aLocalName); |
156 | | already_AddRefed<Attr> |
157 | | SetNamedItemNS(Attr& aNode, ErrorResult& aError); |
158 | | already_AddRefed<Attr> |
159 | | RemoveNamedItemNS(const nsAString& aNamespaceURI, const nsAString& aLocalName, |
160 | | ErrorResult& aError); |
161 | | |
162 | | void |
163 | | GetSupportedNames(nsTArray<nsString>& aNames); |
164 | | |
165 | | size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; |
166 | | |
167 | | protected: |
168 | | virtual ~nsDOMAttributeMap(); |
169 | | |
170 | | private: |
171 | | nsCOMPtr<Element> mContent; |
172 | | |
173 | | /** |
174 | | * Cache of Attrs. |
175 | | */ |
176 | | AttrCache mAttributeCache; |
177 | | |
178 | | already_AddRefed<mozilla::dom::NodeInfo> |
179 | | GetAttrNodeInfo(const nsAString& aNamespaceURI, |
180 | | const nsAString& aLocalName); |
181 | | |
182 | | Attr* GetAttribute(mozilla::dom::NodeInfo* aNodeInfo); |
183 | | }; |
184 | | |
185 | | // XXX khuey yes this is crazy. The bindings code needs to see this include, |
186 | | // but if we pull it in at the top of the file we get a circular include |
187 | | // problem. |
188 | | #include "mozilla/dom/Element.h" |
189 | | |
190 | | #endif /* nsDOMAttributeMap_h */ |