Bug 1852754: part 9) Add tests for dynamically loading <link rel="prefetch"> elements...
[gecko.git] / dom / base / nsDOMAttributeMap.h
blob5ed1e96544ab740eaa341b596d862bb993988cff
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/. */
7 /*
8 * Implementation of the |attributes| property of DOM Core's Element object.
9 */
11 #ifndef nsDOMAttributeMap_h
12 #define nsDOMAttributeMap_h
14 #include "mozilla/MemoryReporting.h"
15 #include "nsCycleCollectionParticipant.h"
16 #include "nsRefPtrHashtable.h"
17 #include "nsString.h"
18 #include "nsWrapperCache.h"
20 class nsAtom;
21 class nsINode;
23 namespace mozilla {
24 class ErrorResult;
26 namespace dom {
27 class Attr;
28 class DocGroup;
29 class Document;
30 class Element;
31 class NodeInfo;
32 } // namespace dom
33 } // namespace mozilla
35 /**
36 * Structure used as a key for caching Attrs in nsDOMAttributeMap's
37 * mAttributeCache.
39 class nsAttrKey {
40 public:
41 /**
42 * The namespace of the attribute
44 int32_t mNamespaceID;
46 /**
47 * The atom for attribute, stored as void*, to make sure that we only use it
48 * for the hashcode, and we can never dereference it.
50 void* mLocalName;
52 nsAttrKey(int32_t aNs, nsAtom* aName)
53 : mNamespaceID(aNs), mLocalName(aName) {}
55 nsAttrKey(const nsAttrKey& aAttr) = default;
58 /**
59 * PLDHashEntryHdr implementation for nsAttrKey.
61 class nsAttrHashKey : public PLDHashEntryHdr {
62 public:
63 using KeyType = const nsAttrKey&;
64 using KeyTypePointer = const nsAttrKey*;
66 explicit nsAttrHashKey(KeyTypePointer aKey) : mKey(*aKey) {}
67 nsAttrHashKey(const nsAttrHashKey& aCopy)
68 : PLDHashEntryHdr{}, mKey(aCopy.mKey) {}
69 ~nsAttrHashKey() = default;
71 KeyType GetKey() const { return mKey; }
72 bool KeyEquals(KeyTypePointer aKey) const {
73 return mKey.mLocalName == aKey->mLocalName &&
74 mKey.mNamespaceID == aKey->mNamespaceID;
77 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
78 static PLDHashNumber HashKey(KeyTypePointer aKey) {
79 if (!aKey) return 0;
81 return mozilla::HashGeneric(aKey->mNamespaceID, aKey->mLocalName);
83 enum { ALLOW_MEMMOVE = true };
85 private:
86 nsAttrKey mKey;
89 class nsDOMAttributeMap final : public nsISupports, public nsWrapperCache {
90 public:
91 using Attr = mozilla::dom::Attr;
92 using DocGroup = mozilla::dom::DocGroup;
93 using Document = mozilla::dom::Document;
94 using Element = mozilla::dom::Element;
95 using ErrorResult = mozilla::ErrorResult;
97 explicit nsDOMAttributeMap(Element* aContent);
99 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
100 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_WRAPPERCACHE_CLASS(nsDOMAttributeMap)
102 void DropReference();
104 Element* GetContent() { return mContent; }
107 * Called when mContent is moved into a new document.
108 * Updates the nodeinfos of all owned nodes.
110 nsresult SetOwnerDocument(Document* aDocument);
113 * Drop an attribute from the map's cache (does not remove the attribute
114 * from the node!)
116 void DropAttribute(int32_t aNamespaceID, nsAtom* aLocalName);
119 * Returns the number of attribute nodes currently in the map.
120 * Note: this is just the number of cached attribute nodes, not the number of
121 * attributes in mContent.
123 * @return The number of attribute nodes in the map.
125 uint32_t Count() const;
127 using AttrCache = nsRefPtrHashtable<nsAttrHashKey, Attr>;
129 static void BlastSubtreeToPieces(nsINode* aNode);
131 Element* GetParentObject() const { return mContent; }
132 virtual JSObject* WrapObject(JSContext* aCx,
133 JS::Handle<JSObject*> aGivenProto) override;
134 DocGroup* GetDocGroup() const;
136 // WebIDL
137 Attr* GetNamedItem(const nsAString& aAttrName);
138 Attr* NamedGetter(const nsAString& aAttrName, bool& aFound);
139 already_AddRefed<Attr> RemoveNamedItem(mozilla::dom::NodeInfo* aNodeInfo,
140 ErrorResult& aError);
141 already_AddRefed<Attr> RemoveNamedItem(const nsAString& aName,
142 ErrorResult& aError);
144 Attr* Item(uint32_t aIndex);
145 Attr* IndexedGetter(uint32_t aIndex, bool& aFound);
146 uint32_t Length() const;
148 Attr* GetNamedItemNS(const nsAString& aNamespaceURI,
149 const nsAString& aLocalName);
150 already_AddRefed<Attr> SetNamedItemNS(Attr& aNode, ErrorResult& aError);
151 already_AddRefed<Attr> RemoveNamedItemNS(const nsAString& aNamespaceURI,
152 const nsAString& aLocalName,
153 ErrorResult& aError);
155 void GetSupportedNames(nsTArray<nsString>& aNames);
157 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
159 protected:
160 virtual ~nsDOMAttributeMap();
162 private:
163 nsCOMPtr<Element> mContent;
166 * Cache of Attrs.
168 AttrCache mAttributeCache;
170 already_AddRefed<mozilla::dom::NodeInfo> GetAttrNodeInfo(
171 const nsAString& aNamespaceURI, const nsAString& aLocalName);
173 Attr* GetAttribute(mozilla::dom::NodeInfo* aNodeInfo);
176 #endif /* nsDOMAttributeMap_h */