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/. */
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 "nsAutoPtr.h"
25 #include "nsCycleCollectionParticipant.h"
26 #include "mozilla/dom/NameSpaceConstants.h"
27 #include "nsStringGlue.h"
31 class nsNodeInfoManager
;
36 class NodeInfo MOZ_FINAL
39 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(NodeInfo
)
40 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_NATIVE_CLASS_WITH_CUSTOM_DELETE(NodeInfo
)
43 * Get the name from this node as a string, this does not include the prefix.
45 * For the HTML element "<body>" this will return "body" and for the XML
46 * element "<html:body>" this will return "body".
48 void GetName(nsAString
& aName
) const;
51 * Get the name from this node as an atom, this does not include the prefix.
52 * This function never returns a null atom.
54 * For the HTML element "<body>" this will return the "body" atom and for
55 * the XML element "<html:body>" this will return the "body" atom.
57 nsIAtom
* NameAtom() const
63 * Get the qualified name from this node as a string, the qualified name
64 * includes the prefix, if one exists.
66 * For the HTML element "<body>" this will return "body" and for the XML
67 * element "<html:body>" this will return "html:body".
69 const nsString
& QualifiedName() const {
70 return mQualifiedName
;
74 * Returns the node's nodeName as defined in DOM Core
76 const nsString
& NodeName() const {
81 * Returns the node's localName as defined in DOM Core
83 const nsString
& LocalName() const {
88 * Get the prefix from this node as a string.
90 * For the HTML element "<body>" this will return a null string and for
91 * the XML element "<html:body>" this will return the string "html".
93 void GetPrefix(nsAString
& aPrefix
) const;
96 * Get the prefix from this node as an atom.
98 * For the HTML element "<body>" this will return a null atom and for
99 * the XML element "<html:body>" this will return the "html" atom.
101 nsIAtom
* GetPrefixAtom() const
103 return mInner
.mPrefix
;
107 * Get the namespace URI for a node, if the node has a namespace URI.
109 void GetNamespaceURI(nsAString
& aNameSpaceURI
) const;
112 * Get the namespace ID for a node if the node has a namespace, if not this
113 * returns kNameSpaceID_None.
115 int32_t NamespaceID() const
117 return mInner
.mNamespaceID
;
121 * Get the nodetype for the node. Returns the values specified in nsIDOMNode
122 * for nsIDOMNode.nodeType
124 uint16_t NodeType() const
126 return mInner
.mNodeType
;
130 * Get the extra name, used by PIs and DocTypes, for the node.
132 nsIAtom
* GetExtraName() const
134 return mInner
.mExtraName
;
138 * Get the owning node info manager. Only to be used inside Gecko, you can't
139 * really do anything with the pointer outside Gecko anyway.
141 nsNodeInfoManager
* NodeInfoManager() const
143 return mOwnerManager
;
147 * Utility functions that can be used to check if a nodeinfo holds a specific
148 * name, name and prefix, name and prefix and namespace ID, or just
151 inline bool Equals(NodeInfo
* aNodeInfo
) const;
153 bool NameAndNamespaceEquals(NodeInfo
* aNodeInfo
) const;
155 bool Equals(nsIAtom
* aNameAtom
) const
157 return mInner
.mName
== aNameAtom
;
160 bool Equals(nsIAtom
* aNameAtom
, nsIAtom
* aPrefixAtom
) const
162 return (mInner
.mName
== aNameAtom
) && (mInner
.mPrefix
== aPrefixAtom
);
165 bool Equals(nsIAtom
* aNameAtom
, int32_t aNamespaceID
) const
167 return ((mInner
.mName
== aNameAtom
) &&
168 (mInner
.mNamespaceID
== aNamespaceID
));
171 bool Equals(nsIAtom
* aNameAtom
, nsIAtom
* aPrefixAtom
, int32_t aNamespaceID
) const
173 return ((mInner
.mName
== aNameAtom
) &&
174 (mInner
.mPrefix
== aPrefixAtom
) &&
175 (mInner
.mNamespaceID
== aNamespaceID
));
178 bool NamespaceEquals(int32_t aNamespaceID
) const
180 return mInner
.mNamespaceID
== aNamespaceID
;
183 inline bool Equals(const nsAString
& aName
) const;
185 inline bool Equals(const nsAString
& aName
, const nsAString
& aPrefix
) const;
187 inline bool Equals(const nsAString
& aName
, int32_t aNamespaceID
) const;
189 inline bool Equals(const nsAString
& aName
, const nsAString
& aPrefix
, int32_t aNamespaceID
) const;
191 bool NamespaceEquals(const nsAString
& aNamespaceURI
) const;
193 inline bool QualifiedNameEquals(nsIAtom
* aNameAtom
) const;
195 bool QualifiedNameEquals(const nsAString
& aQualifiedName
) const
197 return mQualifiedName
== aQualifiedName
;
201 * Retrieve a pointer to the document that owns this node info.
203 nsIDocument
* GetDocument() const
210 NodeInfo(const NodeInfo
& aOther
) = delete;
212 // NodeInfo is only constructed by nsNodeInfoManager which is a friend class.
213 // aName and aOwnerManager may not be null.
214 NodeInfo(nsIAtom
* aName
, nsIAtom
* aPrefix
, int32_t aNamespaceID
,
215 uint16_t aNodeType
, nsIAtom
* aExtraName
,
216 nsNodeInfoManager
* aOwnerManager
);
224 * This method gets called by the cycle collector when it's time to delete
227 void DeleteCycleCollectable();
231 * NodeInfoInner is used for two things:
233 * 1. as a member in nsNodeInfo for holding the name, prefix and
235 * 2. as the hash key in the hash table in nsNodeInfoManager
237 * NodeInfoInner does not do any kind of reference counting,
238 * that's up to the user of this class. Since NodeInfoInner is
239 * typically used as a member of NodeInfo, the hash table doesn't
240 * need to delete the keys. When the value (NodeInfo) is deleted
241 * the key is automatically deleted.
248 : mName(nullptr), mPrefix(nullptr), mNamespaceID(kNameSpaceID_Unknown
),
249 mNodeType(0), mNameString(nullptr), mExtraName(nullptr)
252 NodeInfoInner(nsIAtom
*aName
, nsIAtom
*aPrefix
, int32_t aNamespaceID
,
253 uint16_t aNodeType
, nsIAtom
* aExtraName
)
254 : mName(aName
), mPrefix(aPrefix
), mNamespaceID(aNamespaceID
),
255 mNodeType(aNodeType
), mNameString(nullptr), mExtraName(aExtraName
)
258 NodeInfoInner(const nsAString
& aTmpName
, nsIAtom
*aPrefix
,
259 int32_t aNamespaceID
, uint16_t aNodeType
)
260 : mName(nullptr), mPrefix(aPrefix
), mNamespaceID(aNamespaceID
),
261 mNodeType(aNodeType
), mNameString(&aTmpName
), mExtraName(nullptr)
267 int32_t mNamespaceID
;
268 uint16_t mNodeType
; // As defined by nsIDOMNode.nodeType
269 const nsAString
* mNameString
;
270 nsIAtom
* mExtraName
; // Only used by PIs and DocTypes
273 // nsNodeInfoManager needs to pass mInner to the hash table.
274 friend class ::nsNodeInfoManager
;
276 nsIDocument
* mDocument
; // Weak. Cache of mOwnerManager->mDocument
278 NodeInfoInner mInner
;
280 nsRefPtr
<nsNodeInfoManager
> mOwnerManager
;
283 * Members for various functions of mName+mPrefix that we can be
288 nsString mQualifiedName
;
290 // nodeName for the node.
293 // localName for the node. This is either equal to mInner.mName, or a
294 // void string, depending on mInner.mNodeType.
299 } // namespace mozilla
301 #endif /* mozilla_dom_NodeInfo_h___ */