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