NodeInfo.h (9775B)
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 * Class that represents a prefix/namespace/localName triple; a single 9 * nodeinfo is shared by all elements in a document that have that 10 * prefix, namespace, and localName. 11 * 12 * nsNodeInfoManagers are internal objects that manage a list of 13 * NodeInfos, every document object should hold a strong reference to 14 * a nsNodeInfoManager and every NodeInfo also holds a strong reference 15 * to their owning manager. When a NodeInfo is no longer used it will 16 * automatically remove itself from its owner manager, and when all 17 * NodeInfos have been removed from a nsNodeInfoManager and all external 18 * references are released the nsNodeInfoManager deletes itself. 19 */ 20 21 #ifndef mozilla_dom_NodeInfo_h___ 22 #define mozilla_dom_NodeInfo_h___ 23 24 #include "mozilla/Attributes.h" 25 #include "mozilla/Maybe.h" 26 #include "mozilla/dom/NameSpaceConstants.h" 27 #include "nsAtom.h" 28 #include "nsCycleCollectionParticipant.h" 29 #include "nsHashKeys.h" 30 #include "nsString.h" 31 32 class nsNodeInfoManager; 33 34 namespace mozilla::dom { 35 36 class Document; 37 38 class NodeInfo final { 39 public: 40 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(NodeInfo) 41 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_NATIVE_CLASS_WITH_CUSTOM_DELETE(NodeInfo) 42 43 /* 44 * Get the name from this node as a string, this does not include the prefix. 45 * 46 * For the HTML element "<body>" this will return "body" and for the XML 47 * element "<html:body>" this will return "body". 48 */ 49 void GetName(nsAString& aName) const; 50 51 /* 52 * Get the name from this node as an atom, this does not include the prefix. 53 * This function never returns a null atom. 54 * 55 * For the HTML element "<body>" this will return the "body" atom and for 56 * the XML element "<html:body>" this will return the "body" atom. 57 */ 58 nsAtom* NameAtom() const { return mInner.mName; } 59 60 /* 61 * Get the qualified name from this node as a string, the qualified name 62 * includes the prefix, if one exists. 63 * 64 * For the HTML element "<body>" this will return "body" and for the XML 65 * element "<html:body>" this will return "html:body". 66 */ 67 const nsString& QualifiedName() const { return mQualifiedName; } 68 69 /* 70 * Returns the node's nodeName as defined in DOM Core 71 */ 72 const nsString& NodeName() const { return mNodeName; } 73 74 /* 75 * Returns the node's localName as defined in DOM Core 76 */ 77 const nsString& LocalName() const { return mLocalName; } 78 79 /* 80 * Get the prefix from this node as a string. 81 * 82 * For the HTML element "<body>" this will return a null string and for 83 * the XML element "<html:body>" this will return the string "html". 84 */ 85 void GetPrefix(nsAString& aPrefix) const; 86 87 /* 88 * Get the prefix from this node as an atom. 89 * 90 * For the HTML element "<body>" this will return a null atom and for 91 * the XML element "<html:body>" this will return the "html" atom. 92 */ 93 nsAtom* GetPrefixAtom() const { return mInner.mPrefix; } 94 95 /* 96 * Get the namespace URI for a node, if the node has a namespace URI. 97 */ 98 void GetNamespaceURI(nsAString& aNameSpaceURI) const; 99 100 /* 101 * Get the namespace ID for a node if the node has a namespace, if not this 102 * returns kNameSpaceID_None. 103 */ 104 int32_t NamespaceID() const { return mInner.mNamespaceID; } 105 106 /* 107 * Get the nodetype for the node. Returns the values specified in Node 108 * for Node.nodeType 109 */ 110 uint16_t NodeType() const { return mInner.mNodeType; } 111 112 /* 113 * Get the extra name, used by PIs and DocTypes, for the node. 114 */ 115 nsAtom* GetExtraName() const { return mInner.mExtraName; } 116 117 /** 118 * Get the owning node info manager. Only to be used inside Gecko, you can't 119 * really do anything with the pointer outside Gecko anyway. 120 */ 121 nsNodeInfoManager* NodeInfoManager() const { return mOwnerManager; } 122 123 /* 124 * Utility functions that can be used to check if a nodeinfo holds a specific 125 * name, name and prefix, name and prefix and namespace ID, or just 126 * namespace ID. 127 */ 128 inline bool Equals(NodeInfo* aNodeInfo) const; 129 130 bool NameAndNamespaceEquals(NodeInfo* aNodeInfo) const; 131 132 bool Equals(const nsAtom* aNameAtom) const { 133 return mInner.mName == aNameAtom; 134 } 135 136 bool Equals(const nsAtom* aNameAtom, const nsAtom* aPrefixAtom) const { 137 return (mInner.mName == aNameAtom) && (mInner.mPrefix == aPrefixAtom); 138 } 139 140 bool Equals(const nsAtom* aNameAtom, int32_t aNamespaceID) const { 141 return ((mInner.mName == aNameAtom) && 142 (mInner.mNamespaceID == aNamespaceID)); 143 } 144 145 bool Equals(const nsAtom* aNameAtom, const nsAtom* aPrefixAtom, 146 int32_t aNamespaceID) const { 147 return ((mInner.mName == aNameAtom) && (mInner.mPrefix == aPrefixAtom) && 148 (mInner.mNamespaceID == aNamespaceID)); 149 } 150 151 bool NamespaceEquals(int32_t aNamespaceID) const { 152 return mInner.mNamespaceID == aNamespaceID; 153 } 154 155 inline bool Equals(const nsAString& aName) const; 156 157 inline bool Equals(const nsAString& aName, const nsAString& aPrefix) const; 158 159 inline bool Equals(const nsAString& aName, int32_t aNamespaceID) const; 160 161 inline bool Equals(const nsAString& aName, const nsAString& aPrefix, 162 int32_t aNamespaceID) const; 163 164 bool NamespaceEquals(const nsAString& aNamespaceURI) const; 165 166 inline bool QualifiedNameEquals(const nsAtom* aNameAtom) const; 167 168 bool QualifiedNameEquals(const nsAString& aQualifiedName) const { 169 return mQualifiedName == aQualifiedName; 170 } 171 172 /* 173 * Retrieve a pointer to the document that owns this node info. 174 */ 175 Document* GetDocument() const { return mDocument; } 176 177 private: 178 NodeInfo() = delete; 179 NodeInfo(const NodeInfo& aOther) = delete; 180 181 // NodeInfo is only constructed by nsNodeInfoManager which is a friend class. 182 // aName and aOwnerManager may not be null. 183 NodeInfo(nsAtom* aName, nsAtom* aPrefix, int32_t aNamespaceID, 184 uint16_t aNodeType, nsAtom* aExtraName, 185 nsNodeInfoManager* aOwnerManager); 186 187 ~NodeInfo(); 188 189 public: 190 bool CanSkip(); 191 192 /** 193 * This method gets called by the cycle collector when it's time to delete 194 * this object. 195 */ 196 void DeleteCycleCollectable(); 197 198 protected: 199 /* 200 * NodeInfoInner is used for two things: 201 * 202 * 1. as a member in nsNodeInfo for holding the name, prefix and 203 * namespace ID 204 * 2. as the hash key in the hash table in nsNodeInfoManager 205 * 206 * NodeInfoInner does not do any kind of reference counting, 207 * that's up to the user of this class. Since NodeInfoInner is 208 * typically used as a member of NodeInfo, the hash table doesn't 209 * need to delete the keys. When the value (NodeInfo) is deleted 210 * the key is automatically deleted. 211 */ 212 213 class NodeInfoInner { 214 public: 215 NodeInfoInner() 216 : mName(nullptr), 217 mPrefix(nullptr), 218 mNamespaceID(kNameSpaceID_Unknown), 219 mNodeType(0), 220 mNameString(nullptr), 221 mExtraName(nullptr), 222 mHash() {} 223 NodeInfoInner(nsAtom* aName, nsAtom* aPrefix, int32_t aNamespaceID, 224 uint16_t aNodeType, nsAtom* aExtraName) 225 : mName(aName), 226 mPrefix(aPrefix), 227 mNamespaceID(aNamespaceID), 228 mNodeType(aNodeType), 229 mNameString(nullptr), 230 mExtraName(aExtraName), 231 mHash() {} 232 NodeInfoInner(const nsAString& aTmpName, nsAtom* aPrefix, 233 int32_t aNamespaceID, uint16_t aNodeType) 234 : mName(nullptr), 235 mPrefix(aPrefix), 236 mNamespaceID(aNamespaceID), 237 mNodeType(aNodeType), 238 mNameString(&aTmpName), 239 mExtraName(nullptr), 240 mHash() {} 241 242 bool operator==(const NodeInfoInner& aOther) const { 243 if (mPrefix != aOther.mPrefix || mNamespaceID != aOther.mNamespaceID || 244 mNodeType != aOther.mNodeType || mExtraName != aOther.mExtraName) { 245 return false; 246 } 247 248 if (mName) { 249 if (aOther.mName) { 250 return mName == aOther.mName; 251 } 252 return mName->Equals(*(aOther.mNameString)); 253 } 254 255 if (aOther.mName) { 256 return aOther.mName->Equals(*(mNameString)); 257 } 258 259 return mNameString->Equals(*(aOther.mNameString)); 260 } 261 262 uint32_t Hash() const { 263 if (!mHash) { 264 mHash.emplace(mName ? mName->hash() 265 : mozilla::HashString(*mNameString)); 266 } 267 return mHash.value(); 268 } 269 270 nsAtom* const MOZ_OWNING_REF mName; 271 nsAtom* MOZ_OWNING_REF mPrefix; 272 int32_t mNamespaceID; 273 uint16_t mNodeType; // As defined by Node.nodeType 274 const nsAString* const mNameString; 275 nsAtom* MOZ_OWNING_REF mExtraName; // Only used by PIs and DocTypes 276 mutable mozilla::Maybe<const uint32_t> mHash; 277 }; 278 279 // nsNodeInfoManager needs to pass mInner to the hash table. 280 friend class ::nsNodeInfoManager; 281 282 // This is a non-owning reference, but it's safe since it's set to nullptr 283 // by nsNodeInfoManager::DropDocumentReference when the document is destroyed. 284 Document* MOZ_NON_OWNING_REF mDocument; // Cache of mOwnerManager->mDocument 285 286 NodeInfoInner mInner; 287 288 RefPtr<nsNodeInfoManager> mOwnerManager; 289 290 /* 291 * Members for various functions of mName+mPrefix that we can be 292 * asked to compute. 293 */ 294 295 // Qualified name 296 nsString mQualifiedName; 297 298 // nodeName for the node. 299 nsString mNodeName; 300 301 // localName for the node. This is either equal to mInner.mName, or a 302 // void string, depending on mInner.mNodeType. 303 nsString mLocalName; 304 }; 305 306 } // namespace mozilla::dom 307 308 #endif /* mozilla_dom_NodeInfo_h___ */