Bug 1854550 - pt 10. Allow LOG() with zero extra arguments r=glandium
[gecko.git] / dom / base / NodeInfo.h
blob3060597ffb306f9064f817888cc9a695accfa311
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/. */
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.
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.
21 #ifndef mozilla_dom_NodeInfo_h___
22 #define mozilla_dom_NodeInfo_h___
24 #include "nsCycleCollectionParticipant.h"
25 #include "mozilla/dom/NameSpaceConstants.h"
26 #include "nsString.h"
27 #include "mozilla/Attributes.h"
28 #include "mozilla/Maybe.h"
29 #include "nsAtom.h"
30 #include "nsHashKeys.h"
32 class nsNodeInfoManager;
34 namespace mozilla::dom {
36 class Document;
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)
44 * Get the name from this node as a string, this does not include the prefix.
46 * For the HTML element "<body>" this will return "body" and for the XML
47 * element "<html:body>" this will return "body".
49 void GetName(nsAString& aName) const;
52 * Get the name from this node as an atom, this does not include the prefix.
53 * This function never returns a null atom.
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.
58 nsAtom* NameAtom() const { return mInner.mName; }
61 * Get the qualified name from this node as a string, the qualified name
62 * includes the prefix, if one exists.
64 * For the HTML element "<body>" this will return "body" and for the XML
65 * element "<html:body>" this will return "html:body".
67 const nsString& QualifiedName() const { return mQualifiedName; }
70 * Returns the node's nodeName as defined in DOM Core
72 const nsString& NodeName() const { return mNodeName; }
75 * Returns the node's localName as defined in DOM Core
77 const nsString& LocalName() const { return mLocalName; }
80 * Get the prefix from this node as a string.
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".
85 void GetPrefix(nsAString& aPrefix) const;
88 * Get the prefix from this node as an atom.
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.
93 nsAtom* GetPrefixAtom() const { return mInner.mPrefix; }
96 * Get the namespace URI for a node, if the node has a namespace URI.
98 void GetNamespaceURI(nsAString& aNameSpaceURI) const;
101 * Get the namespace ID for a node if the node has a namespace, if not this
102 * returns kNameSpaceID_None.
104 int32_t NamespaceID() const { return mInner.mNamespaceID; }
107 * Get the nodetype for the node. Returns the values specified in Node
108 * for Node.nodeType
110 uint16_t NodeType() const { return mInner.mNodeType; }
113 * Get the extra name, used by PIs and DocTypes, for the node.
115 nsAtom* GetExtraName() const { return mInner.mExtraName; }
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.
121 nsNodeInfoManager* NodeInfoManager() const { return mOwnerManager; }
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.
128 inline bool Equals(NodeInfo* aNodeInfo) const;
130 bool NameAndNamespaceEquals(NodeInfo* aNodeInfo) const;
132 bool Equals(const nsAtom* aNameAtom) const {
133 return mInner.mName == aNameAtom;
136 bool Equals(const nsAtom* aNameAtom, const nsAtom* aPrefixAtom) const {
137 return (mInner.mName == aNameAtom) && (mInner.mPrefix == aPrefixAtom);
140 bool Equals(const nsAtom* aNameAtom, int32_t aNamespaceID) const {
141 return ((mInner.mName == aNameAtom) &&
142 (mInner.mNamespaceID == aNamespaceID));
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));
151 bool NamespaceEquals(int32_t aNamespaceID) const {
152 return mInner.mNamespaceID == aNamespaceID;
155 inline bool Equals(const nsAString& aName) const;
157 inline bool Equals(const nsAString& aName, const nsAString& aPrefix) const;
159 inline bool Equals(const nsAString& aName, int32_t aNamespaceID) const;
161 inline bool Equals(const nsAString& aName, const nsAString& aPrefix,
162 int32_t aNamespaceID) const;
164 bool NamespaceEquals(const nsAString& aNamespaceURI) const;
166 inline bool QualifiedNameEquals(const nsAtom* aNameAtom) const;
168 bool QualifiedNameEquals(const nsAString& aQualifiedName) const {
169 return mQualifiedName == aQualifiedName;
173 * Retrieve a pointer to the document that owns this node info.
175 Document* GetDocument() const { return mDocument; }
177 private:
178 NodeInfo() = delete;
179 NodeInfo(const NodeInfo& aOther) = delete;
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);
187 ~NodeInfo();
189 public:
190 bool CanSkip();
193 * This method gets called by the cycle collector when it's time to delete
194 * this object.
196 void DeleteCycleCollectable();
198 protected:
200 * NodeInfoInner is used for two things:
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
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.
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() {}
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;
248 if (mName) {
249 if (aOther.mName) {
250 return mName == aOther.mName;
252 return mName->Equals(*(aOther.mNameString));
255 if (aOther.mName) {
256 return aOther.mName->Equals(*(mNameString));
259 return mNameString->Equals(*(aOther.mNameString));
262 uint32_t Hash() const {
263 if (!mHash) {
264 mHash.emplace(mName ? mName->hash()
265 : mozilla::HashString(*mNameString));
267 return mHash.value();
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;
279 // nsNodeInfoManager needs to pass mInner to the hash table.
280 friend class ::nsNodeInfoManager;
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
286 NodeInfoInner mInner;
288 RefPtr<nsNodeInfoManager> mOwnerManager;
291 * Members for various functions of mName+mPrefix that we can be
292 * asked to compute.
295 // Qualified name
296 nsString mQualifiedName;
298 // nodeName for the node.
299 nsString mNodeName;
301 // localName for the node. This is either equal to mInner.mName, or a
302 // void string, depending on mInner.mNodeType.
303 nsString mLocalName;
306 } // namespace mozilla::dom
308 #endif /* mozilla_dom_NodeInfo_h___ */