content/base/src/nsDOMAttributeMap.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7 * Implementation of the |attributes| property of DOM Core's Element object.
michael@0 8 */
michael@0 9
michael@0 10 #ifndef nsDOMAttributeMap_h
michael@0 11 #define nsDOMAttributeMap_h
michael@0 12
michael@0 13 #include "mozilla/MemoryReporting.h"
michael@0 14 #include "mozilla/dom/Attr.h"
michael@0 15 #include "mozilla/ErrorResult.h"
michael@0 16 #include "nsCycleCollectionParticipant.h"
michael@0 17 #include "nsIDOMMozNamedAttrMap.h"
michael@0 18 #include "nsRefPtrHashtable.h"
michael@0 19 #include "nsString.h"
michael@0 20 #include "nsWrapperCache.h"
michael@0 21
michael@0 22 class nsIAtom;
michael@0 23 class nsINodeInfo;
michael@0 24 class nsIDocument;
michael@0 25
michael@0 26 /**
michael@0 27 * Structure used as a key for caching Attrs in nsDOMAttributeMap's mAttributeCache.
michael@0 28 */
michael@0 29 class nsAttrKey
michael@0 30 {
michael@0 31 public:
michael@0 32 /**
michael@0 33 * The namespace of the attribute
michael@0 34 */
michael@0 35 int32_t mNamespaceID;
michael@0 36
michael@0 37 /**
michael@0 38 * The atom for attribute, weak ref. is fine as we only use it for the
michael@0 39 * hashcode, we never dereference it.
michael@0 40 */
michael@0 41 nsIAtom* mLocalName;
michael@0 42
michael@0 43 nsAttrKey(int32_t aNs, nsIAtom* aName)
michael@0 44 : mNamespaceID(aNs), mLocalName(aName) {}
michael@0 45
michael@0 46 nsAttrKey(const nsAttrKey& aAttr)
michael@0 47 : mNamespaceID(aAttr.mNamespaceID), mLocalName(aAttr.mLocalName) {}
michael@0 48 };
michael@0 49
michael@0 50 /**
michael@0 51 * PLDHashEntryHdr implementation for nsAttrKey.
michael@0 52 */
michael@0 53 class nsAttrHashKey : public PLDHashEntryHdr
michael@0 54 {
michael@0 55 public:
michael@0 56 typedef const nsAttrKey& KeyType;
michael@0 57 typedef const nsAttrKey* KeyTypePointer;
michael@0 58
michael@0 59 nsAttrHashKey(KeyTypePointer aKey) : mKey(*aKey) {}
michael@0 60 nsAttrHashKey(const nsAttrHashKey& aCopy) : mKey(aCopy.mKey) {}
michael@0 61 ~nsAttrHashKey() {}
michael@0 62
michael@0 63 KeyType GetKey() const { return mKey; }
michael@0 64 bool KeyEquals(KeyTypePointer aKey) const
michael@0 65 {
michael@0 66 return mKey.mLocalName == aKey->mLocalName &&
michael@0 67 mKey.mNamespaceID == aKey->mNamespaceID;
michael@0 68 }
michael@0 69
michael@0 70 static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
michael@0 71 static PLDHashNumber HashKey(KeyTypePointer aKey)
michael@0 72 {
michael@0 73 if (!aKey)
michael@0 74 return 0;
michael@0 75
michael@0 76 return mozilla::HashGeneric(aKey->mNamespaceID, aKey->mLocalName);
michael@0 77 }
michael@0 78 enum { ALLOW_MEMMOVE = true };
michael@0 79
michael@0 80 private:
michael@0 81 nsAttrKey mKey;
michael@0 82 };
michael@0 83
michael@0 84 // Helper class that implements the nsIDOMMozNamedAttrMap interface.
michael@0 85 class nsDOMAttributeMap : public nsIDOMMozNamedAttrMap
michael@0 86 , public nsWrapperCache
michael@0 87 {
michael@0 88 public:
michael@0 89 typedef mozilla::dom::Attr Attr;
michael@0 90 typedef mozilla::dom::Element Element;
michael@0 91 typedef mozilla::ErrorResult ErrorResult;
michael@0 92
michael@0 93 nsDOMAttributeMap(Element *aContent);
michael@0 94 virtual ~nsDOMAttributeMap();
michael@0 95
michael@0 96 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
michael@0 97 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsDOMAttributeMap)
michael@0 98
michael@0 99 // nsIDOMMozNamedAttrMap interface
michael@0 100 NS_DECL_NSIDOMMOZNAMEDATTRMAP
michael@0 101
michael@0 102 void DropReference();
michael@0 103
michael@0 104 Element* GetContent()
michael@0 105 {
michael@0 106 return mContent;
michael@0 107 }
michael@0 108
michael@0 109 /**
michael@0 110 * Called when mContent is moved into a new document.
michael@0 111 * Updates the nodeinfos of all owned nodes.
michael@0 112 */
michael@0 113 nsresult SetOwnerDocument(nsIDocument* aDocument);
michael@0 114
michael@0 115 /**
michael@0 116 * Drop an attribute from the map's cache (does not remove the attribute
michael@0 117 * from the node!)
michael@0 118 */
michael@0 119 void DropAttribute(int32_t aNamespaceID, nsIAtom* aLocalName);
michael@0 120
michael@0 121 /**
michael@0 122 * Returns the number of attribute nodes currently in the map.
michael@0 123 * Note: this is just the number of cached attribute nodes, not the number of
michael@0 124 * attributes in mContent.
michael@0 125 *
michael@0 126 * @return The number of attribute nodes in the map.
michael@0 127 */
michael@0 128 uint32_t Count() const;
michael@0 129
michael@0 130 typedef nsRefPtrHashtable<nsAttrHashKey, Attr> AttrCache;
michael@0 131
michael@0 132 /**
michael@0 133 * Enumerates over the attribute nodess in the map and calls aFunc for each
michael@0 134 * one. If aFunc returns PL_DHASH_STOP we'll stop enumerating at that point.
michael@0 135 *
michael@0 136 * @return The number of attribute nodes that aFunc was called for.
michael@0 137 */
michael@0 138 uint32_t Enumerate(AttrCache::EnumReadFunction aFunc, void *aUserArg) const;
michael@0 139
michael@0 140 Element* GetParentObject() const
michael@0 141 {
michael@0 142 return mContent;
michael@0 143 }
michael@0 144 virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
michael@0 145
michael@0 146 // WebIDL
michael@0 147 Attr* GetNamedItem(const nsAString& aAttrName);
michael@0 148 Attr* NamedGetter(const nsAString& aAttrName, bool& aFound);
michael@0 149 bool NameIsEnumerable(const nsAString& aName);
michael@0 150 already_AddRefed<Attr>
michael@0 151 SetNamedItem(Attr& aAttr, ErrorResult& aError)
michael@0 152 {
michael@0 153 return SetNamedItemInternal(aAttr, false, aError);
michael@0 154 }
michael@0 155 already_AddRefed<Attr>
michael@0 156 RemoveNamedItem(const nsAString& aName, ErrorResult& aError);
michael@0 157
michael@0 158 Attr* Item(uint32_t aIndex);
michael@0 159 Attr* IndexedGetter(uint32_t aIndex, bool& aFound);
michael@0 160 uint32_t Length() const;
michael@0 161
michael@0 162 Attr*
michael@0 163 GetNamedItemNS(const nsAString& aNamespaceURI,
michael@0 164 const nsAString& aLocalName);
michael@0 165 already_AddRefed<Attr>
michael@0 166 SetNamedItemNS(Attr& aNode, ErrorResult& aError)
michael@0 167 {
michael@0 168 return SetNamedItemInternal(aNode, true, aError);
michael@0 169 }
michael@0 170 already_AddRefed<Attr>
michael@0 171 RemoveNamedItemNS(const nsAString& aNamespaceURI, const nsAString& aLocalName,
michael@0 172 ErrorResult& aError);
michael@0 173
michael@0 174 void GetSupportedNames(unsigned, nsTArray<nsString>& aNames)
michael@0 175 {
michael@0 176 // No supported names we want to show up in iteration.
michael@0 177 }
michael@0 178
michael@0 179 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
michael@0 180
michael@0 181 private:
michael@0 182 nsCOMPtr<Element> mContent;
michael@0 183
michael@0 184 /**
michael@0 185 * Cache of Attrs.
michael@0 186 */
michael@0 187 AttrCache mAttributeCache;
michael@0 188
michael@0 189 /**
michael@0 190 * SetNamedItem() (aWithNS = false) and SetNamedItemNS() (aWithNS =
michael@0 191 * true) implementation.
michael@0 192 */
michael@0 193 already_AddRefed<Attr>
michael@0 194 SetNamedItemInternal(Attr& aNode, bool aWithNS, ErrorResult& aError);
michael@0 195
michael@0 196 already_AddRefed<nsINodeInfo>
michael@0 197 GetAttrNodeInfo(const nsAString& aNamespaceURI,
michael@0 198 const nsAString& aLocalName);
michael@0 199
michael@0 200 Attr* GetAttribute(nsINodeInfo* aNodeInfo, bool aNsAware);
michael@0 201
michael@0 202 /**
michael@0 203 * Remove an attribute, returns the removed node.
michael@0 204 */
michael@0 205 already_AddRefed<Attr> RemoveAttribute(nsINodeInfo* aNodeInfo);
michael@0 206 };
michael@0 207
michael@0 208 // XXX khuey yes this is crazy. The bindings code needs to see this include,
michael@0 209 // but if we pull it in at the top of the file we get a circular include
michael@0 210 // problem.
michael@0 211 #include "mozilla/dom/Element.h"
michael@0 212
michael@0 213 #endif /* nsDOMAttributeMap_h */

mercurial