1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/base/public/nsINodeInfo.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,354 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* vim: set ts=8 sts=2 et sw=2 tw=80: */ 1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +/* 1.11 + * nsINodeInfo is an interface to node info, such as name, prefix, namespace 1.12 + * ID and possibly other data that is shared between nodes (elements 1.13 + * and attributes) that have the same name, prefix and namespace ID within 1.14 + * the same document. 1.15 + * 1.16 + * nsNodeInfoManager's are internal objects that manage a list of 1.17 + * nsINodeInfo's, every document object should hold a strong reference to 1.18 + * a nsNodeInfoManager and every nsINodeInfo also holds a strong reference 1.19 + * to their owning manager. When a nsINodeInfo is no longer used it will 1.20 + * automatically remove itself from its owner manager, and when all 1.21 + * nsINodeInfo's have been removed from a nsNodeInfoManager and all external 1.22 + * references are released the nsNodeInfoManager deletes itself. 1.23 + * 1.24 + * -- jst@netscape.com 1.25 + */ 1.26 + 1.27 +#ifndef nsINodeInfo_h___ 1.28 +#define nsINodeInfo_h___ 1.29 + 1.30 +#include "nsCOMPtr.h" // for member 1.31 +#include "nsIAtom.h" // for member (in nsCOMPtr) 1.32 +#include "nsISupports.h" // for base class 1.33 +#include "nsNameSpaceManager.h" // for kNameSpaceID_* 1.34 + 1.35 +#ifdef MOZILLA_INTERNAL_API 1.36 +#include "nsDOMString.h" 1.37 +#endif 1.38 + 1.39 +class nsIDocument; 1.40 +class nsIURI; 1.41 +class nsIPrincipal; 1.42 +class nsNodeInfoManager; 1.43 + 1.44 +// IID for the nsINodeInfo interface 1.45 +#define NS_INODEINFO_IID \ 1.46 +{ 0xc5188ea1, 0x0a9c, 0x43e6, \ 1.47 + { 0x95, 0x90, 0xcc, 0x43, 0x6b, 0xe9, 0xcf, 0xa0 } } 1.48 + 1.49 +class nsINodeInfo : public nsISupports 1.50 +{ 1.51 +public: 1.52 + NS_DECLARE_STATIC_IID_ACCESSOR(NS_INODEINFO_IID) 1.53 + 1.54 + nsINodeInfo() 1.55 + : mInner(nullptr, nullptr, kNameSpaceID_None, 0, nullptr) 1.56 + { 1.57 + } 1.58 + 1.59 + /* 1.60 + * Get the name from this node as a string, this does not include the prefix. 1.61 + * 1.62 + * For the HTML element "<body>" this will return "body" and for the XML 1.63 + * element "<html:body>" this will return "body". 1.64 + */ 1.65 + void GetName(nsAString& aName) const 1.66 + { 1.67 + mInner.mName->ToString(aName); 1.68 + } 1.69 + 1.70 + /* 1.71 + * Get the name from this node as an atom, this does not include the prefix. 1.72 + * This function never returns a null atom. 1.73 + * 1.74 + * For the HTML element "<body>" this will return the "body" atom and for 1.75 + * the XML element "<html:body>" this will return the "body" atom. 1.76 + */ 1.77 + nsIAtom* NameAtom() const 1.78 + { 1.79 + return mInner.mName; 1.80 + } 1.81 + 1.82 + /* 1.83 + * Get the qualified name from this node as a string, the qualified name 1.84 + * includes the prefix, if one exists. 1.85 + * 1.86 + * For the HTML element "<body>" this will return "body" and for the XML 1.87 + * element "<html:body>" this will return "html:body". 1.88 + */ 1.89 + const nsString& QualifiedName() const { 1.90 + return mQualifiedName; 1.91 + } 1.92 + 1.93 + /* 1.94 + * Returns the node's nodeName as defined in DOM Core 1.95 + */ 1.96 + const nsString& NodeName() const { 1.97 + return mNodeName; 1.98 + } 1.99 + 1.100 + /* 1.101 + * Returns the node's localName as defined in DOM Core 1.102 + */ 1.103 + const nsString& LocalName() const { 1.104 + return mLocalName; 1.105 + } 1.106 + 1.107 +#ifdef MOZILLA_INTERNAL_API 1.108 + /* 1.109 + * Get the prefix from this node as a string. 1.110 + * 1.111 + * For the HTML element "<body>" this will return a null string and for 1.112 + * the XML element "<html:body>" this will return the string "html". 1.113 + */ 1.114 + void GetPrefix(nsAString& aPrefix) const 1.115 + { 1.116 + if (mInner.mPrefix) { 1.117 + mInner.mPrefix->ToString(aPrefix); 1.118 + } else { 1.119 + SetDOMStringToNull(aPrefix); 1.120 + } 1.121 + } 1.122 +#endif 1.123 + 1.124 + /* 1.125 + * Get the prefix from this node as an atom. 1.126 + * 1.127 + * For the HTML element "<body>" this will return a null atom and for 1.128 + * the XML element "<html:body>" this will return the "html" atom. 1.129 + */ 1.130 + nsIAtom* GetPrefixAtom() const 1.131 + { 1.132 + return mInner.mPrefix; 1.133 + } 1.134 + 1.135 + /* 1.136 + * Get the namespace URI for a node, if the node has a namespace URI. 1.137 + */ 1.138 + virtual void GetNamespaceURI(nsAString& aNameSpaceURI) const = 0; 1.139 + 1.140 + /* 1.141 + * Get the namespace ID for a node if the node has a namespace, if not this 1.142 + * returns kNameSpaceID_None. 1.143 + */ 1.144 + int32_t NamespaceID() const 1.145 + { 1.146 + return mInner.mNamespaceID; 1.147 + } 1.148 + 1.149 + /* 1.150 + * Get the nodetype for the node. Returns the values specified in nsIDOMNode 1.151 + * for nsIDOMNode.nodeType 1.152 + */ 1.153 + uint16_t NodeType() const 1.154 + { 1.155 + return mInner.mNodeType; 1.156 + } 1.157 + 1.158 + /* 1.159 + * Get the extra name, used by PIs and DocTypes, for the node. 1.160 + */ 1.161 + nsIAtom* GetExtraName() const 1.162 + { 1.163 + return mInner.mExtraName; 1.164 + } 1.165 + 1.166 + /* 1.167 + * Get and set the ID attribute atom for this node. 1.168 + * See http://www.w3.org/TR/1998/REC-xml-19980210#sec-attribute-types 1.169 + * for the definition of an ID attribute. 1.170 + * 1.171 + */ 1.172 + nsIAtom* GetIDAttributeAtom() const 1.173 + { 1.174 + return mIDAttributeAtom; 1.175 + } 1.176 + 1.177 + void SetIDAttributeAtom(nsIAtom* aID) 1.178 + { 1.179 + mIDAttributeAtom = aID; 1.180 + } 1.181 + 1.182 + /** 1.183 + * Get the owning node info manager. Only to be used inside Gecko, you can't 1.184 + * really do anything with the pointer outside Gecko anyway. 1.185 + */ 1.186 + nsNodeInfoManager *NodeInfoManager() const 1.187 + { 1.188 + return mOwnerManager; 1.189 + } 1.190 + 1.191 + /* 1.192 + * Utility functions that can be used to check if a nodeinfo holds a specific 1.193 + * name, name and prefix, name and prefix and namespace ID, or just 1.194 + * namespace ID. 1.195 + */ 1.196 + bool Equals(nsINodeInfo *aNodeInfo) const 1.197 + { 1.198 + return aNodeInfo == this || aNodeInfo->Equals(mInner.mName, mInner.mPrefix, 1.199 + mInner.mNamespaceID); 1.200 + } 1.201 + 1.202 + bool NameAndNamespaceEquals(nsINodeInfo *aNodeInfo) const 1.203 + { 1.204 + return aNodeInfo == this || aNodeInfo->Equals(mInner.mName, 1.205 + mInner.mNamespaceID); 1.206 + } 1.207 + 1.208 + bool Equals(nsIAtom *aNameAtom) const 1.209 + { 1.210 + return mInner.mName == aNameAtom; 1.211 + } 1.212 + 1.213 + bool Equals(nsIAtom *aNameAtom, nsIAtom *aPrefixAtom) const 1.214 + { 1.215 + return (mInner.mName == aNameAtom) && (mInner.mPrefix == aPrefixAtom); 1.216 + } 1.217 + 1.218 + bool Equals(nsIAtom *aNameAtom, int32_t aNamespaceID) const 1.219 + { 1.220 + return ((mInner.mName == aNameAtom) && 1.221 + (mInner.mNamespaceID == aNamespaceID)); 1.222 + } 1.223 + 1.224 + bool Equals(nsIAtom *aNameAtom, nsIAtom *aPrefixAtom, 1.225 + int32_t aNamespaceID) const 1.226 + { 1.227 + return ((mInner.mName == aNameAtom) && 1.228 + (mInner.mPrefix == aPrefixAtom) && 1.229 + (mInner.mNamespaceID == aNamespaceID)); 1.230 + } 1.231 + 1.232 + bool NamespaceEquals(int32_t aNamespaceID) const 1.233 + { 1.234 + return mInner.mNamespaceID == aNamespaceID; 1.235 + } 1.236 + 1.237 + bool Equals(const nsAString& aName) const 1.238 + { 1.239 + return mInner.mName->Equals(aName); 1.240 + } 1.241 + 1.242 + bool Equals(const nsAString& aName, const nsAString& aPrefix) const 1.243 + { 1.244 + return mInner.mName->Equals(aName) && 1.245 + (mInner.mPrefix ? mInner.mPrefix->Equals(aPrefix) : aPrefix.IsEmpty()); 1.246 + } 1.247 + 1.248 + bool Equals(const nsAString& aName, int32_t aNamespaceID) const 1.249 + { 1.250 + return mInner.mNamespaceID == aNamespaceID && 1.251 + mInner.mName->Equals(aName); 1.252 + } 1.253 + 1.254 + bool Equals(const nsAString& aName, const nsAString& aPrefix, 1.255 + int32_t aNamespaceID) const 1.256 + { 1.257 + return mInner.mName->Equals(aName) && mInner.mNamespaceID == aNamespaceID && 1.258 + (mInner.mPrefix ? mInner.mPrefix->Equals(aPrefix) : aPrefix.IsEmpty()); 1.259 + } 1.260 + 1.261 + virtual bool NamespaceEquals(const nsAString& aNamespaceURI) const = 0; 1.262 + 1.263 + bool QualifiedNameEquals(nsIAtom* aNameAtom) const 1.264 + { 1.265 + NS_PRECONDITION(aNameAtom, "Must have name atom"); 1.266 + if (!GetPrefixAtom()) 1.267 + return Equals(aNameAtom); 1.268 + 1.269 + return aNameAtom->Equals(mQualifiedName); 1.270 + } 1.271 + 1.272 + bool QualifiedNameEquals(const nsAString& aQualifiedName) const 1.273 + { 1.274 + return mQualifiedName == aQualifiedName; 1.275 + } 1.276 + 1.277 + /* 1.278 + * Retrieve a pointer to the document that owns this node info. 1.279 + */ 1.280 + nsIDocument* GetDocument() const 1.281 + { 1.282 + return mDocument; 1.283 + } 1.284 + 1.285 +protected: 1.286 + /* 1.287 + * nsNodeInfoInner is used for two things: 1.288 + * 1.289 + * 1. as a member in nsNodeInfo for holding the name, prefix and 1.290 + * namespace ID 1.291 + * 2. as the hash key in the hash table in nsNodeInfoManager 1.292 + * 1.293 + * nsNodeInfoInner does not do any kind of reference counting, 1.294 + * that's up to the user of this class. Since nsNodeInfoInner is 1.295 + * typically used as a member of nsNodeInfo, the hash table doesn't 1.296 + * need to delete the keys. When the value (nsNodeInfo) is deleted 1.297 + * the key is automatically deleted. 1.298 + */ 1.299 + 1.300 + class nsNodeInfoInner 1.301 + { 1.302 + public: 1.303 + nsNodeInfoInner() 1.304 + : mName(nullptr), mPrefix(nullptr), mNamespaceID(kNameSpaceID_Unknown), 1.305 + mNodeType(0), mNameString(nullptr), mExtraName(nullptr) 1.306 + { 1.307 + } 1.308 + nsNodeInfoInner(nsIAtom *aName, nsIAtom *aPrefix, int32_t aNamespaceID, 1.309 + uint16_t aNodeType, nsIAtom* aExtraName) 1.310 + : mName(aName), mPrefix(aPrefix), mNamespaceID(aNamespaceID), 1.311 + mNodeType(aNodeType), mNameString(nullptr), mExtraName(aExtraName) 1.312 + { 1.313 + } 1.314 + nsNodeInfoInner(const nsAString& aTmpName, nsIAtom *aPrefix, 1.315 + int32_t aNamespaceID, uint16_t aNodeType) 1.316 + : mName(nullptr), mPrefix(aPrefix), mNamespaceID(aNamespaceID), 1.317 + mNodeType(aNodeType), mNameString(&aTmpName), mExtraName(nullptr) 1.318 + { 1.319 + } 1.320 + 1.321 + nsIAtom* mName; 1.322 + nsIAtom* mPrefix; 1.323 + int32_t mNamespaceID; 1.324 + uint16_t mNodeType; // As defined by nsIDOMNode.nodeType 1.325 + const nsAString* mNameString; 1.326 + nsIAtom* mExtraName; // Only used by PIs and DocTypes 1.327 + }; 1.328 + 1.329 + // nsNodeInfoManager needs to pass mInner to the hash table. 1.330 + friend class nsNodeInfoManager; 1.331 + 1.332 + nsIDocument* mDocument; // Weak. Cache of mOwnerManager->mDocument 1.333 + 1.334 + nsNodeInfoInner mInner; 1.335 + 1.336 + nsCOMPtr<nsIAtom> mIDAttributeAtom; 1.337 + nsRefPtr<nsNodeInfoManager> mOwnerManager; 1.338 + 1.339 + /* 1.340 + * Members for various functions of mName+mPrefix that we can be 1.341 + * asked to compute. 1.342 + */ 1.343 + 1.344 + // Qualified name 1.345 + nsString mQualifiedName; 1.346 + 1.347 + // nodeName for the node. 1.348 + nsString mNodeName; 1.349 + 1.350 + // localName for the node. This is either equal to mInner.mName, or a 1.351 + // void string, depending on mInner.mNodeType. 1.352 + nsString mLocalName; 1.353 +}; 1.354 + 1.355 +NS_DEFINE_STATIC_IID_ACCESSOR(nsINodeInfo, NS_INODEINFO_IID) 1.356 + 1.357 +#endif /* nsINodeInfo_h___ */