/src/mozilla-central/dom/base/nsDOMAttributeMap.cpp
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 | | #include "nsDOMAttributeMap.h" |
12 | | |
13 | | #include "mozilla/MemoryReporting.h" |
14 | | #include "mozilla/dom/Attr.h" |
15 | | #include "mozilla/dom/Element.h" |
16 | | #include "mozilla/dom/NamedNodeMapBinding.h" |
17 | | #include "mozilla/dom/NodeInfoInlines.h" |
18 | | #include "nsAttrName.h" |
19 | | #include "nsContentUtils.h" |
20 | | #include "nsError.h" |
21 | | #include "nsIContentInlines.h" |
22 | | #include "nsIDocument.h" |
23 | | #include "nsNameSpaceManager.h" |
24 | | #include "nsNodeInfoManager.h" |
25 | | #include "nsUnicharUtils.h" |
26 | | #include "nsWrapperCacheInlines.h" |
27 | | |
28 | | using namespace mozilla; |
29 | | using namespace mozilla::dom; |
30 | | |
31 | | //---------------------------------------------------------------------- |
32 | | |
33 | | nsDOMAttributeMap::nsDOMAttributeMap(Element* aContent) |
34 | | : mContent(aContent) |
35 | 0 | { |
36 | 0 | // We don't add a reference to our content. If it goes away, |
37 | 0 | // we'll be told to drop our reference |
38 | 0 | } |
39 | | |
40 | | nsDOMAttributeMap::~nsDOMAttributeMap() |
41 | 0 | { |
42 | 0 | DropReference(); |
43 | 0 | } |
44 | | |
45 | | void |
46 | | nsDOMAttributeMap::DropReference() |
47 | 0 | { |
48 | 0 | for (auto iter = mAttributeCache.Iter(); !iter.Done(); iter.Next()) { |
49 | 0 | iter.Data()->SetMap(nullptr); |
50 | 0 | iter.Remove(); |
51 | 0 | } |
52 | 0 | mContent = nullptr; |
53 | 0 | } |
54 | | |
55 | | NS_IMPL_CYCLE_COLLECTION_CLASS(nsDOMAttributeMap) |
56 | | |
57 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMAttributeMap) |
58 | 0 | tmp->DropReference(); |
59 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER |
60 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK(mContent) |
61 | 0 | NS_IMPL_CYCLE_COLLECTION_UNLINK_END |
62 | | |
63 | | |
64 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMAttributeMap) |
65 | 0 | for (auto iter = tmp->mAttributeCache.Iter(); !iter.Done(); iter.Next()) { |
66 | 0 | cb.NoteXPCOMChild(static_cast<nsINode*>(iter.Data().get())); |
67 | 0 | } |
68 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mContent) |
69 | 0 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END |
70 | | |
71 | | NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(nsDOMAttributeMap) |
72 | | |
73 | 0 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsDOMAttributeMap) |
74 | 0 | if (tmp->HasKnownLiveWrapper()) { |
75 | 0 | if (tmp->mContent) { |
76 | 0 | // The map owns the element so we can mark it when the |
77 | 0 | // map itself is certainly alive. |
78 | 0 | mozilla::dom::FragmentOrElement::MarkNodeChildren(tmp->mContent); |
79 | 0 | } |
80 | 0 | return true; |
81 | 0 | } |
82 | 0 | if (tmp->mContent && |
83 | 0 | mozilla::dom::FragmentOrElement::CanSkip(tmp->mContent, true)) { |
84 | 0 | return true; |
85 | 0 | } |
86 | 0 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END |
87 | 0 |
|
88 | 0 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsDOMAttributeMap) |
89 | 0 | return tmp->HasKnownLiveWrapperAndDoesNotNeedTracing(tmp); |
90 | 0 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END |
91 | | |
92 | 0 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsDOMAttributeMap) |
93 | 0 | return tmp->HasKnownLiveWrapper(); |
94 | 0 | NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END |
95 | | |
96 | | // QueryInterface implementation for nsDOMAttributeMap |
97 | | |
98 | 0 | NS_INTERFACE_MAP_BEGIN(nsDOMAttributeMap) |
99 | 0 | NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY |
100 | 0 | NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(nsDOMAttributeMap) |
101 | 0 | NS_INTERFACE_MAP_ENTRY(nsISupports) |
102 | 0 | NS_INTERFACE_MAP_END |
103 | | |
104 | | NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMAttributeMap) |
105 | | NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMAttributeMap) |
106 | | |
107 | | nsresult |
108 | | nsDOMAttributeMap::SetOwnerDocument(nsIDocument* aDocument) |
109 | 0 | { |
110 | 0 | for (auto iter = mAttributeCache.Iter(); !iter.Done(); iter.Next()) { |
111 | 0 | nsresult rv = iter.Data()->SetOwnerDocument(aDocument); |
112 | 0 | NS_ENSURE_SUCCESS(rv, NS_ERROR_FAILURE); |
113 | 0 | } |
114 | 0 | return NS_OK; |
115 | 0 | } |
116 | | |
117 | | void |
118 | | nsDOMAttributeMap::DropAttribute(int32_t aNamespaceID, nsAtom* aLocalName) |
119 | 0 | { |
120 | 0 | nsAttrKey attr(aNamespaceID, aLocalName); |
121 | 0 | if (auto entry = mAttributeCache.Lookup(attr)) { |
122 | 0 | entry.Data()->SetMap(nullptr); // break link to map |
123 | 0 | entry.Remove(); |
124 | 0 | } |
125 | 0 | } |
126 | | |
127 | | Attr* |
128 | | nsDOMAttributeMap::GetAttribute(mozilla::dom::NodeInfo* aNodeInfo) |
129 | 0 | { |
130 | 0 | NS_ASSERTION(aNodeInfo, "GetAttribute() called with aNodeInfo == nullptr!"); |
131 | 0 |
|
132 | 0 | nsAttrKey attr(aNodeInfo->NamespaceID(), aNodeInfo->NameAtom()); |
133 | 0 |
|
134 | 0 | RefPtr<Attr>& entryValue = mAttributeCache.GetOrInsert(attr); |
135 | 0 | Attr* node = entryValue; |
136 | 0 | if (!node) { |
137 | 0 | // Newly inserted entry! |
138 | 0 | RefPtr<mozilla::dom::NodeInfo> ni = aNodeInfo; |
139 | 0 | entryValue = new Attr(this, ni.forget(), EmptyString()); |
140 | 0 | node = entryValue; |
141 | 0 | } |
142 | 0 |
|
143 | 0 | return node; |
144 | 0 | } |
145 | | |
146 | | Attr* |
147 | | nsDOMAttributeMap::NamedGetter(const nsAString& aAttrName, bool& aFound) |
148 | 0 | { |
149 | 0 | aFound = false; |
150 | 0 | NS_ENSURE_TRUE(mContent, nullptr); |
151 | 0 |
|
152 | 0 | RefPtr<mozilla::dom::NodeInfo> ni = mContent->GetExistingAttrNameFromQName(aAttrName); |
153 | 0 | if (!ni) { |
154 | 0 | return nullptr; |
155 | 0 | } |
156 | 0 | |
157 | 0 | aFound = true; |
158 | 0 | return GetAttribute(ni); |
159 | 0 | } |
160 | | |
161 | | void |
162 | | nsDOMAttributeMap::GetSupportedNames(nsTArray<nsString>& aNames) |
163 | 0 | { |
164 | 0 | // For HTML elements in HTML documents, only include names that are still the |
165 | 0 | // same after ASCII-lowercasing, since our named getter will end up |
166 | 0 | // ASCII-lowercasing the given string. |
167 | 0 | bool lowercaseNamesOnly = |
168 | 0 | mContent->IsHTMLElement() && mContent->IsInHTMLDocument(); |
169 | 0 |
|
170 | 0 | const uint32_t count = mContent->GetAttrCount(); |
171 | 0 | bool seenNonAtomName = false; |
172 | 0 | for (uint32_t i = 0; i < count; i++) { |
173 | 0 | const nsAttrName* name = mContent->GetAttrNameAt(i); |
174 | 0 | seenNonAtomName = seenNonAtomName || !name->IsAtom(); |
175 | 0 | nsString qualifiedName; |
176 | 0 | name->GetQualifiedName(qualifiedName); |
177 | 0 |
|
178 | 0 | if (lowercaseNamesOnly && |
179 | 0 | nsContentUtils::StringContainsASCIIUpper(qualifiedName)) { |
180 | 0 | continue; |
181 | 0 | } |
182 | 0 | |
183 | 0 | // Omit duplicates. We only need to do this check if we've seen a non-atom |
184 | 0 | // name, because that's the only way we can have two identical qualified |
185 | 0 | // names. |
186 | 0 | if (seenNonAtomName && aNames.Contains(qualifiedName)) { |
187 | 0 | continue; |
188 | 0 | } |
189 | 0 | |
190 | 0 | aNames.AppendElement(qualifiedName); |
191 | 0 | } |
192 | 0 | } |
193 | | |
194 | | Attr* |
195 | | nsDOMAttributeMap::GetNamedItem(const nsAString& aAttrName) |
196 | 0 | { |
197 | 0 | bool dummy; |
198 | 0 | return NamedGetter(aAttrName, dummy); |
199 | 0 | } |
200 | | |
201 | | already_AddRefed<Attr> |
202 | | nsDOMAttributeMap::SetNamedItemNS(Attr& aAttr, ErrorResult& aError) |
203 | 0 | { |
204 | 0 | NS_ENSURE_TRUE(mContent, nullptr); |
205 | 0 |
|
206 | 0 | // XXX should check same-origin between mContent and aAttr however |
207 | 0 | // nsContentUtils::CheckSameOrigin can't deal with attributenodes yet |
208 | 0 |
|
209 | 0 | // Check that attribute is not owned by somebody else |
210 | 0 | nsDOMAttributeMap* owner = aAttr.GetMap(); |
211 | 0 | if (owner) { |
212 | 0 | if (owner != this) { |
213 | 0 | aError.Throw(NS_ERROR_DOM_INUSE_ATTRIBUTE_ERR); |
214 | 0 | return nullptr; |
215 | 0 | } |
216 | 0 | |
217 | 0 | // setting a preexisting attribute is a no-op, just return the same |
218 | 0 | // node. |
219 | 0 | RefPtr<Attr> attribute = &aAttr; |
220 | 0 | return attribute.forget(); |
221 | 0 | } |
222 | 0 | |
223 | 0 | nsresult rv; |
224 | 0 | if (mContent->OwnerDoc() != aAttr.OwnerDoc()) { |
225 | 0 | DebugOnly<void*> adoptedNode = |
226 | 0 | mContent->OwnerDoc()->AdoptNode(aAttr, aError); |
227 | 0 | if (aError.Failed()) { |
228 | 0 | return nullptr; |
229 | 0 | } |
230 | 0 | |
231 | 0 | NS_ASSERTION(adoptedNode == &aAttr, "Uh, adopt node changed nodes?"); |
232 | 0 | } |
233 | 0 |
|
234 | 0 | // Get nodeinfo and preexisting attribute (if it exists) |
235 | 0 | RefPtr<NodeInfo> oldNi; |
236 | 0 |
|
237 | 0 | uint32_t i, count = mContent->GetAttrCount(); |
238 | 0 | for (i = 0; i < count; ++i) { |
239 | 0 | const nsAttrName* name = mContent->GetAttrNameAt(i); |
240 | 0 | int32_t attrNS = name->NamespaceID(); |
241 | 0 | nsAtom* nameAtom = name->LocalName(); |
242 | 0 |
|
243 | 0 | // we're purposefully ignoring the prefix. |
244 | 0 | if (aAttr.NodeInfo()->Equals(nameAtom, attrNS)) { |
245 | 0 | oldNi = mContent->NodeInfo()->NodeInfoManager()-> |
246 | 0 | GetNodeInfo(nameAtom, name->GetPrefix(), aAttr.NodeInfo()->NamespaceID(), |
247 | 0 | nsINode::ATTRIBUTE_NODE); |
248 | 0 | break; |
249 | 0 | } |
250 | 0 | } |
251 | 0 |
|
252 | 0 | RefPtr<Attr> oldAttr; |
253 | 0 |
|
254 | 0 | if (oldNi) { |
255 | 0 | oldAttr = GetAttribute(oldNi); |
256 | 0 |
|
257 | 0 | if (oldAttr == &aAttr) { |
258 | 0 | return oldAttr.forget(); |
259 | 0 | } |
260 | 0 | |
261 | 0 | if (oldAttr) { |
262 | 0 | // Just remove it from our hashtable. This has no side-effects, so we |
263 | 0 | // don't have to recheck anything after we do it. Then we'll add our new |
264 | 0 | // Attr to the hashtable and do the actual attr set on the element. This |
265 | 0 | // will make the whole thing look like a single attribute mutation (with |
266 | 0 | // the new attr node in place) as opposed to a removal and addition. |
267 | 0 | DropAttribute(oldNi->NamespaceID(), oldNi->NameAtom()); |
268 | 0 | } |
269 | 0 | } |
270 | 0 |
|
271 | 0 | nsAutoString value; |
272 | 0 | aAttr.GetValue(value); |
273 | 0 |
|
274 | 0 | RefPtr<NodeInfo> ni = aAttr.NodeInfo(); |
275 | 0 |
|
276 | 0 | // Add the new attribute to the attribute map before updating |
277 | 0 | // its value in the element. @see bug 364413. |
278 | 0 | nsAttrKey attrkey(ni->NamespaceID(), ni->NameAtom()); |
279 | 0 | mAttributeCache.Put(attrkey, &aAttr); |
280 | 0 | aAttr.SetMap(this); |
281 | 0 |
|
282 | 0 | rv = mContent->SetAttr(ni->NamespaceID(), ni->NameAtom(), |
283 | 0 | ni->GetPrefixAtom(), value, true); |
284 | 0 | if (NS_FAILED(rv)) { |
285 | 0 | DropAttribute(ni->NamespaceID(), ni->NameAtom()); |
286 | 0 | aError.Throw(rv); |
287 | 0 | return nullptr; |
288 | 0 | } |
289 | 0 | |
290 | 0 | return oldAttr.forget(); |
291 | 0 | } |
292 | | |
293 | | already_AddRefed<Attr> |
294 | | nsDOMAttributeMap::RemoveNamedItem(NodeInfo* aNodeInfo, ErrorResult& aError) |
295 | 0 | { |
296 | 0 | RefPtr<Attr> attribute = GetAttribute(aNodeInfo); |
297 | 0 | // This removes the attribute node from the attribute map. |
298 | 0 | aError = mContent->UnsetAttr(aNodeInfo->NamespaceID(), aNodeInfo->NameAtom(), true); |
299 | 0 | return attribute.forget(); |
300 | 0 | } |
301 | | |
302 | | already_AddRefed<Attr> |
303 | | nsDOMAttributeMap::RemoveNamedItem(const nsAString& aName, ErrorResult& aError) |
304 | 0 | { |
305 | 0 | if (!mContent) { |
306 | 0 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
307 | 0 | return nullptr; |
308 | 0 | } |
309 | 0 | |
310 | 0 | RefPtr<mozilla::dom::NodeInfo> ni = mContent->GetExistingAttrNameFromQName(aName); |
311 | 0 | if (!ni) { |
312 | 0 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
313 | 0 | return nullptr; |
314 | 0 | } |
315 | 0 | |
316 | 0 | return RemoveNamedItem(ni, aError); |
317 | 0 | } |
318 | | |
319 | | |
320 | | Attr* |
321 | | nsDOMAttributeMap::IndexedGetter(uint32_t aIndex, bool& aFound) |
322 | 0 | { |
323 | 0 | aFound = false; |
324 | 0 | NS_ENSURE_TRUE(mContent, nullptr); |
325 | 0 |
|
326 | 0 | const nsAttrName* name = mContent->GetAttrNameAt(aIndex); |
327 | 0 | NS_ENSURE_TRUE(name, nullptr); |
328 | 0 |
|
329 | 0 | aFound = true; |
330 | 0 | // Don't use the nodeinfo even if one exists since it can have the wrong |
331 | 0 | // owner document. |
332 | 0 | RefPtr<mozilla::dom::NodeInfo> ni = mContent->NodeInfo()->NodeInfoManager()-> |
333 | 0 | GetNodeInfo(name->LocalName(), name->GetPrefix(), name->NamespaceID(), |
334 | 0 | nsINode::ATTRIBUTE_NODE); |
335 | 0 | return GetAttribute(ni); |
336 | 0 | } |
337 | | |
338 | | Attr* |
339 | | nsDOMAttributeMap::Item(uint32_t aIndex) |
340 | 0 | { |
341 | 0 | bool dummy; |
342 | 0 | return IndexedGetter(aIndex, dummy); |
343 | 0 | } |
344 | | |
345 | | uint32_t |
346 | | nsDOMAttributeMap::Length() const |
347 | 0 | { |
348 | 0 | NS_ENSURE_TRUE(mContent, 0); |
349 | 0 |
|
350 | 0 | return mContent->GetAttrCount(); |
351 | 0 | } |
352 | | |
353 | | Attr* |
354 | | nsDOMAttributeMap::GetNamedItemNS(const nsAString& aNamespaceURI, |
355 | | const nsAString& aLocalName) |
356 | 0 | { |
357 | 0 | RefPtr<mozilla::dom::NodeInfo> ni = GetAttrNodeInfo(aNamespaceURI, aLocalName); |
358 | 0 | if (!ni) { |
359 | 0 | return nullptr; |
360 | 0 | } |
361 | 0 | |
362 | 0 | return GetAttribute(ni); |
363 | 0 | } |
364 | | |
365 | | already_AddRefed<mozilla::dom::NodeInfo> |
366 | | nsDOMAttributeMap::GetAttrNodeInfo(const nsAString& aNamespaceURI, |
367 | | const nsAString& aLocalName) |
368 | 0 | { |
369 | 0 | if (!mContent) { |
370 | 0 | return nullptr; |
371 | 0 | } |
372 | 0 | |
373 | 0 | int32_t nameSpaceID = kNameSpaceID_None; |
374 | 0 |
|
375 | 0 | if (!aNamespaceURI.IsEmpty()) { |
376 | 0 | nameSpaceID = |
377 | 0 | nsContentUtils::NameSpaceManager()->GetNameSpaceID(aNamespaceURI, |
378 | 0 | nsContentUtils::IsChromeDoc(mContent->OwnerDoc())); |
379 | 0 |
|
380 | 0 | if (nameSpaceID == kNameSpaceID_Unknown) { |
381 | 0 | return nullptr; |
382 | 0 | } |
383 | 0 | } |
384 | 0 | |
385 | 0 | uint32_t i, count = mContent->GetAttrCount(); |
386 | 0 | for (i = 0; i < count; ++i) { |
387 | 0 | const nsAttrName* name = mContent->GetAttrNameAt(i); |
388 | 0 | int32_t attrNS = name->NamespaceID(); |
389 | 0 | nsAtom* nameAtom = name->LocalName(); |
390 | 0 |
|
391 | 0 | // we're purposefully ignoring the prefix. |
392 | 0 | if (nameSpaceID == attrNS && |
393 | 0 | nameAtom->Equals(aLocalName)) { |
394 | 0 | RefPtr<mozilla::dom::NodeInfo> ni; |
395 | 0 | ni = mContent->NodeInfo()->NodeInfoManager()-> |
396 | 0 | GetNodeInfo(nameAtom, name->GetPrefix(), nameSpaceID, |
397 | 0 | nsINode::ATTRIBUTE_NODE); |
398 | 0 |
|
399 | 0 | return ni.forget(); |
400 | 0 | } |
401 | 0 | } |
402 | 0 |
|
403 | 0 | return nullptr; |
404 | 0 | } |
405 | | |
406 | | already_AddRefed<Attr> |
407 | | nsDOMAttributeMap::RemoveNamedItemNS(const nsAString& aNamespaceURI, |
408 | | const nsAString& aLocalName, |
409 | | ErrorResult& aError) |
410 | 0 | { |
411 | 0 | RefPtr<mozilla::dom::NodeInfo> ni = GetAttrNodeInfo(aNamespaceURI, aLocalName); |
412 | 0 | if (!ni) { |
413 | 0 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
414 | 0 | return nullptr; |
415 | 0 | } |
416 | 0 | |
417 | 0 | return RemoveNamedItem(ni, aError); |
418 | 0 | } |
419 | | |
420 | | uint32_t |
421 | | nsDOMAttributeMap::Count() const |
422 | 0 | { |
423 | 0 | return mAttributeCache.Count(); |
424 | 0 | } |
425 | | |
426 | | size_t |
427 | | nsDOMAttributeMap::SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const |
428 | 0 | { |
429 | 0 | size_t n = aMallocSizeOf(this); |
430 | 0 |
|
431 | 0 | n += mAttributeCache.ShallowSizeOfExcludingThis(aMallocSizeOf); |
432 | 0 | for (auto iter = mAttributeCache.ConstIter(); !iter.Done(); iter.Next()) { |
433 | 0 | n += aMallocSizeOf(iter.Data().get()); |
434 | 0 | } |
435 | 0 |
|
436 | 0 | // NB: mContent is non-owning and thus not counted. |
437 | 0 | return n; |
438 | 0 | } |
439 | | |
440 | | /* virtual */ JSObject* |
441 | | nsDOMAttributeMap::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) |
442 | 0 | { |
443 | 0 | return NamedNodeMap_Binding::Wrap(aCx, this, aGivenProto); |
444 | 0 | } |
445 | | |
446 | | DocGroup* |
447 | | nsDOMAttributeMap::GetDocGroup() const |
448 | 0 | { |
449 | 0 | return mContent ? mContent->OwnerDoc()->GetDocGroup() : nullptr; |
450 | 0 | } |