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