1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/src/nsDOMAttributeMap.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,213 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * Implementation of the |attributes| property of DOM Core's Element object. 1.11 + */ 1.12 + 1.13 +#ifndef nsDOMAttributeMap_h 1.14 +#define nsDOMAttributeMap_h 1.15 + 1.16 +#include "mozilla/MemoryReporting.h" 1.17 +#include "mozilla/dom/Attr.h" 1.18 +#include "mozilla/ErrorResult.h" 1.19 +#include "nsCycleCollectionParticipant.h" 1.20 +#include "nsIDOMMozNamedAttrMap.h" 1.21 +#include "nsRefPtrHashtable.h" 1.22 +#include "nsString.h" 1.23 +#include "nsWrapperCache.h" 1.24 + 1.25 +class nsIAtom; 1.26 +class nsINodeInfo; 1.27 +class nsIDocument; 1.28 + 1.29 +/** 1.30 + * Structure used as a key for caching Attrs in nsDOMAttributeMap's mAttributeCache. 1.31 + */ 1.32 +class nsAttrKey 1.33 +{ 1.34 +public: 1.35 + /** 1.36 + * The namespace of the attribute 1.37 + */ 1.38 + int32_t mNamespaceID; 1.39 + 1.40 + /** 1.41 + * The atom for attribute, weak ref. is fine as we only use it for the 1.42 + * hashcode, we never dereference it. 1.43 + */ 1.44 + nsIAtom* mLocalName; 1.45 + 1.46 + nsAttrKey(int32_t aNs, nsIAtom* aName) 1.47 + : mNamespaceID(aNs), mLocalName(aName) {} 1.48 + 1.49 + nsAttrKey(const nsAttrKey& aAttr) 1.50 + : mNamespaceID(aAttr.mNamespaceID), mLocalName(aAttr.mLocalName) {} 1.51 +}; 1.52 + 1.53 +/** 1.54 + * PLDHashEntryHdr implementation for nsAttrKey. 1.55 + */ 1.56 +class nsAttrHashKey : public PLDHashEntryHdr 1.57 +{ 1.58 +public: 1.59 + typedef const nsAttrKey& KeyType; 1.60 + typedef const nsAttrKey* KeyTypePointer; 1.61 + 1.62 + nsAttrHashKey(KeyTypePointer aKey) : mKey(*aKey) {} 1.63 + nsAttrHashKey(const nsAttrHashKey& aCopy) : mKey(aCopy.mKey) {} 1.64 + ~nsAttrHashKey() {} 1.65 + 1.66 + KeyType GetKey() const { return mKey; } 1.67 + bool KeyEquals(KeyTypePointer aKey) const 1.68 + { 1.69 + return mKey.mLocalName == aKey->mLocalName && 1.70 + mKey.mNamespaceID == aKey->mNamespaceID; 1.71 + } 1.72 + 1.73 + static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; } 1.74 + static PLDHashNumber HashKey(KeyTypePointer aKey) 1.75 + { 1.76 + if (!aKey) 1.77 + return 0; 1.78 + 1.79 + return mozilla::HashGeneric(aKey->mNamespaceID, aKey->mLocalName); 1.80 + } 1.81 + enum { ALLOW_MEMMOVE = true }; 1.82 + 1.83 +private: 1.84 + nsAttrKey mKey; 1.85 +}; 1.86 + 1.87 +// Helper class that implements the nsIDOMMozNamedAttrMap interface. 1.88 +class nsDOMAttributeMap : public nsIDOMMozNamedAttrMap 1.89 + , public nsWrapperCache 1.90 +{ 1.91 +public: 1.92 + typedef mozilla::dom::Attr Attr; 1.93 + typedef mozilla::dom::Element Element; 1.94 + typedef mozilla::ErrorResult ErrorResult; 1.95 + 1.96 + nsDOMAttributeMap(Element *aContent); 1.97 + virtual ~nsDOMAttributeMap(); 1.98 + 1.99 + NS_DECL_CYCLE_COLLECTING_ISUPPORTS 1.100 + NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsDOMAttributeMap) 1.101 + 1.102 + // nsIDOMMozNamedAttrMap interface 1.103 + NS_DECL_NSIDOMMOZNAMEDATTRMAP 1.104 + 1.105 + void DropReference(); 1.106 + 1.107 + Element* GetContent() 1.108 + { 1.109 + return mContent; 1.110 + } 1.111 + 1.112 + /** 1.113 + * Called when mContent is moved into a new document. 1.114 + * Updates the nodeinfos of all owned nodes. 1.115 + */ 1.116 + nsresult SetOwnerDocument(nsIDocument* aDocument); 1.117 + 1.118 + /** 1.119 + * Drop an attribute from the map's cache (does not remove the attribute 1.120 + * from the node!) 1.121 + */ 1.122 + void DropAttribute(int32_t aNamespaceID, nsIAtom* aLocalName); 1.123 + 1.124 + /** 1.125 + * Returns the number of attribute nodes currently in the map. 1.126 + * Note: this is just the number of cached attribute nodes, not the number of 1.127 + * attributes in mContent. 1.128 + * 1.129 + * @return The number of attribute nodes in the map. 1.130 + */ 1.131 + uint32_t Count() const; 1.132 + 1.133 + typedef nsRefPtrHashtable<nsAttrHashKey, Attr> AttrCache; 1.134 + 1.135 + /** 1.136 + * Enumerates over the attribute nodess in the map and calls aFunc for each 1.137 + * one. If aFunc returns PL_DHASH_STOP we'll stop enumerating at that point. 1.138 + * 1.139 + * @return The number of attribute nodes that aFunc was called for. 1.140 + */ 1.141 + uint32_t Enumerate(AttrCache::EnumReadFunction aFunc, void *aUserArg) const; 1.142 + 1.143 + Element* GetParentObject() const 1.144 + { 1.145 + return mContent; 1.146 + } 1.147 + virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; 1.148 + 1.149 + // WebIDL 1.150 + Attr* GetNamedItem(const nsAString& aAttrName); 1.151 + Attr* NamedGetter(const nsAString& aAttrName, bool& aFound); 1.152 + bool NameIsEnumerable(const nsAString& aName); 1.153 + already_AddRefed<Attr> 1.154 + SetNamedItem(Attr& aAttr, ErrorResult& aError) 1.155 + { 1.156 + return SetNamedItemInternal(aAttr, false, aError); 1.157 + } 1.158 + already_AddRefed<Attr> 1.159 + RemoveNamedItem(const nsAString& aName, ErrorResult& aError); 1.160 + 1.161 + Attr* Item(uint32_t aIndex); 1.162 + Attr* IndexedGetter(uint32_t aIndex, bool& aFound); 1.163 + uint32_t Length() const; 1.164 + 1.165 + Attr* 1.166 + GetNamedItemNS(const nsAString& aNamespaceURI, 1.167 + const nsAString& aLocalName); 1.168 + already_AddRefed<Attr> 1.169 + SetNamedItemNS(Attr& aNode, ErrorResult& aError) 1.170 + { 1.171 + return SetNamedItemInternal(aNode, true, aError); 1.172 + } 1.173 + already_AddRefed<Attr> 1.174 + RemoveNamedItemNS(const nsAString& aNamespaceURI, const nsAString& aLocalName, 1.175 + ErrorResult& aError); 1.176 + 1.177 + void GetSupportedNames(unsigned, nsTArray<nsString>& aNames) 1.178 + { 1.179 + // No supported names we want to show up in iteration. 1.180 + } 1.181 + 1.182 + size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const; 1.183 + 1.184 +private: 1.185 + nsCOMPtr<Element> mContent; 1.186 + 1.187 + /** 1.188 + * Cache of Attrs. 1.189 + */ 1.190 + AttrCache mAttributeCache; 1.191 + 1.192 + /** 1.193 + * SetNamedItem() (aWithNS = false) and SetNamedItemNS() (aWithNS = 1.194 + * true) implementation. 1.195 + */ 1.196 + already_AddRefed<Attr> 1.197 + SetNamedItemInternal(Attr& aNode, bool aWithNS, ErrorResult& aError); 1.198 + 1.199 + already_AddRefed<nsINodeInfo> 1.200 + GetAttrNodeInfo(const nsAString& aNamespaceURI, 1.201 + const nsAString& aLocalName); 1.202 + 1.203 + Attr* GetAttribute(nsINodeInfo* aNodeInfo, bool aNsAware); 1.204 + 1.205 + /** 1.206 + * Remove an attribute, returns the removed node. 1.207 + */ 1.208 + already_AddRefed<Attr> RemoveAttribute(nsINodeInfo* aNodeInfo); 1.209 +}; 1.210 + 1.211 +// XXX khuey yes this is crazy. The bindings code needs to see this include, 1.212 +// but if we pull it in at the top of the file we get a circular include 1.213 +// problem. 1.214 +#include "mozilla/dom/Element.h" 1.215 + 1.216 +#endif /* nsDOMAttributeMap_h */