content/base/public/nsINodeInfo.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: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8 * nsINodeInfo is an interface to node info, such as name, prefix, namespace
michael@0 9 * ID and possibly other data that is shared between nodes (elements
michael@0 10 * and attributes) that have the same name, prefix and namespace ID within
michael@0 11 * the same document.
michael@0 12 *
michael@0 13 * nsNodeInfoManager's are internal objects that manage a list of
michael@0 14 * nsINodeInfo's, every document object should hold a strong reference to
michael@0 15 * a nsNodeInfoManager and every nsINodeInfo also holds a strong reference
michael@0 16 * to their owning manager. When a nsINodeInfo is no longer used it will
michael@0 17 * automatically remove itself from its owner manager, and when all
michael@0 18 * nsINodeInfo's have been removed from a nsNodeInfoManager and all external
michael@0 19 * references are released the nsNodeInfoManager deletes itself.
michael@0 20 *
michael@0 21 * -- jst@netscape.com
michael@0 22 */
michael@0 23
michael@0 24 #ifndef nsINodeInfo_h___
michael@0 25 #define nsINodeInfo_h___
michael@0 26
michael@0 27 #include "nsCOMPtr.h" // for member
michael@0 28 #include "nsIAtom.h" // for member (in nsCOMPtr)
michael@0 29 #include "nsISupports.h" // for base class
michael@0 30 #include "nsNameSpaceManager.h" // for kNameSpaceID_*
michael@0 31
michael@0 32 #ifdef MOZILLA_INTERNAL_API
michael@0 33 #include "nsDOMString.h"
michael@0 34 #endif
michael@0 35
michael@0 36 class nsIDocument;
michael@0 37 class nsIURI;
michael@0 38 class nsIPrincipal;
michael@0 39 class nsNodeInfoManager;
michael@0 40
michael@0 41 // IID for the nsINodeInfo interface
michael@0 42 #define NS_INODEINFO_IID \
michael@0 43 { 0xc5188ea1, 0x0a9c, 0x43e6, \
michael@0 44 { 0x95, 0x90, 0xcc, 0x43, 0x6b, 0xe9, 0xcf, 0xa0 } }
michael@0 45
michael@0 46 class nsINodeInfo : public nsISupports
michael@0 47 {
michael@0 48 public:
michael@0 49 NS_DECLARE_STATIC_IID_ACCESSOR(NS_INODEINFO_IID)
michael@0 50
michael@0 51 nsINodeInfo()
michael@0 52 : mInner(nullptr, nullptr, kNameSpaceID_None, 0, nullptr)
michael@0 53 {
michael@0 54 }
michael@0 55
michael@0 56 /*
michael@0 57 * Get the name from this node as a string, this does not include the prefix.
michael@0 58 *
michael@0 59 * For the HTML element "<body>" this will return "body" and for the XML
michael@0 60 * element "<html:body>" this will return "body".
michael@0 61 */
michael@0 62 void GetName(nsAString& aName) const
michael@0 63 {
michael@0 64 mInner.mName->ToString(aName);
michael@0 65 }
michael@0 66
michael@0 67 /*
michael@0 68 * Get the name from this node as an atom, this does not include the prefix.
michael@0 69 * This function never returns a null atom.
michael@0 70 *
michael@0 71 * For the HTML element "<body>" this will return the "body" atom and for
michael@0 72 * the XML element "<html:body>" this will return the "body" atom.
michael@0 73 */
michael@0 74 nsIAtom* NameAtom() const
michael@0 75 {
michael@0 76 return mInner.mName;
michael@0 77 }
michael@0 78
michael@0 79 /*
michael@0 80 * Get the qualified name from this node as a string, the qualified name
michael@0 81 * includes the prefix, if one exists.
michael@0 82 *
michael@0 83 * For the HTML element "<body>" this will return "body" and for the XML
michael@0 84 * element "<html:body>" this will return "html:body".
michael@0 85 */
michael@0 86 const nsString& QualifiedName() const {
michael@0 87 return mQualifiedName;
michael@0 88 }
michael@0 89
michael@0 90 /*
michael@0 91 * Returns the node's nodeName as defined in DOM Core
michael@0 92 */
michael@0 93 const nsString& NodeName() const {
michael@0 94 return mNodeName;
michael@0 95 }
michael@0 96
michael@0 97 /*
michael@0 98 * Returns the node's localName as defined in DOM Core
michael@0 99 */
michael@0 100 const nsString& LocalName() const {
michael@0 101 return mLocalName;
michael@0 102 }
michael@0 103
michael@0 104 #ifdef MOZILLA_INTERNAL_API
michael@0 105 /*
michael@0 106 * Get the prefix from this node as a string.
michael@0 107 *
michael@0 108 * For the HTML element "<body>" this will return a null string and for
michael@0 109 * the XML element "<html:body>" this will return the string "html".
michael@0 110 */
michael@0 111 void GetPrefix(nsAString& aPrefix) const
michael@0 112 {
michael@0 113 if (mInner.mPrefix) {
michael@0 114 mInner.mPrefix->ToString(aPrefix);
michael@0 115 } else {
michael@0 116 SetDOMStringToNull(aPrefix);
michael@0 117 }
michael@0 118 }
michael@0 119 #endif
michael@0 120
michael@0 121 /*
michael@0 122 * Get the prefix from this node as an atom.
michael@0 123 *
michael@0 124 * For the HTML element "<body>" this will return a null atom and for
michael@0 125 * the XML element "<html:body>" this will return the "html" atom.
michael@0 126 */
michael@0 127 nsIAtom* GetPrefixAtom() const
michael@0 128 {
michael@0 129 return mInner.mPrefix;
michael@0 130 }
michael@0 131
michael@0 132 /*
michael@0 133 * Get the namespace URI for a node, if the node has a namespace URI.
michael@0 134 */
michael@0 135 virtual void GetNamespaceURI(nsAString& aNameSpaceURI) const = 0;
michael@0 136
michael@0 137 /*
michael@0 138 * Get the namespace ID for a node if the node has a namespace, if not this
michael@0 139 * returns kNameSpaceID_None.
michael@0 140 */
michael@0 141 int32_t NamespaceID() const
michael@0 142 {
michael@0 143 return mInner.mNamespaceID;
michael@0 144 }
michael@0 145
michael@0 146 /*
michael@0 147 * Get the nodetype for the node. Returns the values specified in nsIDOMNode
michael@0 148 * for nsIDOMNode.nodeType
michael@0 149 */
michael@0 150 uint16_t NodeType() const
michael@0 151 {
michael@0 152 return mInner.mNodeType;
michael@0 153 }
michael@0 154
michael@0 155 /*
michael@0 156 * Get the extra name, used by PIs and DocTypes, for the node.
michael@0 157 */
michael@0 158 nsIAtom* GetExtraName() const
michael@0 159 {
michael@0 160 return mInner.mExtraName;
michael@0 161 }
michael@0 162
michael@0 163 /*
michael@0 164 * Get and set the ID attribute atom for this node.
michael@0 165 * See http://www.w3.org/TR/1998/REC-xml-19980210#sec-attribute-types
michael@0 166 * for the definition of an ID attribute.
michael@0 167 *
michael@0 168 */
michael@0 169 nsIAtom* GetIDAttributeAtom() const
michael@0 170 {
michael@0 171 return mIDAttributeAtom;
michael@0 172 }
michael@0 173
michael@0 174 void SetIDAttributeAtom(nsIAtom* aID)
michael@0 175 {
michael@0 176 mIDAttributeAtom = aID;
michael@0 177 }
michael@0 178
michael@0 179 /**
michael@0 180 * Get the owning node info manager. Only to be used inside Gecko, you can't
michael@0 181 * really do anything with the pointer outside Gecko anyway.
michael@0 182 */
michael@0 183 nsNodeInfoManager *NodeInfoManager() const
michael@0 184 {
michael@0 185 return mOwnerManager;
michael@0 186 }
michael@0 187
michael@0 188 /*
michael@0 189 * Utility functions that can be used to check if a nodeinfo holds a specific
michael@0 190 * name, name and prefix, name and prefix and namespace ID, or just
michael@0 191 * namespace ID.
michael@0 192 */
michael@0 193 bool Equals(nsINodeInfo *aNodeInfo) const
michael@0 194 {
michael@0 195 return aNodeInfo == this || aNodeInfo->Equals(mInner.mName, mInner.mPrefix,
michael@0 196 mInner.mNamespaceID);
michael@0 197 }
michael@0 198
michael@0 199 bool NameAndNamespaceEquals(nsINodeInfo *aNodeInfo) const
michael@0 200 {
michael@0 201 return aNodeInfo == this || aNodeInfo->Equals(mInner.mName,
michael@0 202 mInner.mNamespaceID);
michael@0 203 }
michael@0 204
michael@0 205 bool Equals(nsIAtom *aNameAtom) const
michael@0 206 {
michael@0 207 return mInner.mName == aNameAtom;
michael@0 208 }
michael@0 209
michael@0 210 bool Equals(nsIAtom *aNameAtom, nsIAtom *aPrefixAtom) const
michael@0 211 {
michael@0 212 return (mInner.mName == aNameAtom) && (mInner.mPrefix == aPrefixAtom);
michael@0 213 }
michael@0 214
michael@0 215 bool Equals(nsIAtom *aNameAtom, int32_t aNamespaceID) const
michael@0 216 {
michael@0 217 return ((mInner.mName == aNameAtom) &&
michael@0 218 (mInner.mNamespaceID == aNamespaceID));
michael@0 219 }
michael@0 220
michael@0 221 bool Equals(nsIAtom *aNameAtom, nsIAtom *aPrefixAtom,
michael@0 222 int32_t aNamespaceID) const
michael@0 223 {
michael@0 224 return ((mInner.mName == aNameAtom) &&
michael@0 225 (mInner.mPrefix == aPrefixAtom) &&
michael@0 226 (mInner.mNamespaceID == aNamespaceID));
michael@0 227 }
michael@0 228
michael@0 229 bool NamespaceEquals(int32_t aNamespaceID) const
michael@0 230 {
michael@0 231 return mInner.mNamespaceID == aNamespaceID;
michael@0 232 }
michael@0 233
michael@0 234 bool Equals(const nsAString& aName) const
michael@0 235 {
michael@0 236 return mInner.mName->Equals(aName);
michael@0 237 }
michael@0 238
michael@0 239 bool Equals(const nsAString& aName, const nsAString& aPrefix) const
michael@0 240 {
michael@0 241 return mInner.mName->Equals(aName) &&
michael@0 242 (mInner.mPrefix ? mInner.mPrefix->Equals(aPrefix) : aPrefix.IsEmpty());
michael@0 243 }
michael@0 244
michael@0 245 bool Equals(const nsAString& aName, int32_t aNamespaceID) const
michael@0 246 {
michael@0 247 return mInner.mNamespaceID == aNamespaceID &&
michael@0 248 mInner.mName->Equals(aName);
michael@0 249 }
michael@0 250
michael@0 251 bool Equals(const nsAString& aName, const nsAString& aPrefix,
michael@0 252 int32_t aNamespaceID) const
michael@0 253 {
michael@0 254 return mInner.mName->Equals(aName) && mInner.mNamespaceID == aNamespaceID &&
michael@0 255 (mInner.mPrefix ? mInner.mPrefix->Equals(aPrefix) : aPrefix.IsEmpty());
michael@0 256 }
michael@0 257
michael@0 258 virtual bool NamespaceEquals(const nsAString& aNamespaceURI) const = 0;
michael@0 259
michael@0 260 bool QualifiedNameEquals(nsIAtom* aNameAtom) const
michael@0 261 {
michael@0 262 NS_PRECONDITION(aNameAtom, "Must have name atom");
michael@0 263 if (!GetPrefixAtom())
michael@0 264 return Equals(aNameAtom);
michael@0 265
michael@0 266 return aNameAtom->Equals(mQualifiedName);
michael@0 267 }
michael@0 268
michael@0 269 bool QualifiedNameEquals(const nsAString& aQualifiedName) const
michael@0 270 {
michael@0 271 return mQualifiedName == aQualifiedName;
michael@0 272 }
michael@0 273
michael@0 274 /*
michael@0 275 * Retrieve a pointer to the document that owns this node info.
michael@0 276 */
michael@0 277 nsIDocument* GetDocument() const
michael@0 278 {
michael@0 279 return mDocument;
michael@0 280 }
michael@0 281
michael@0 282 protected:
michael@0 283 /*
michael@0 284 * nsNodeInfoInner is used for two things:
michael@0 285 *
michael@0 286 * 1. as a member in nsNodeInfo for holding the name, prefix and
michael@0 287 * namespace ID
michael@0 288 * 2. as the hash key in the hash table in nsNodeInfoManager
michael@0 289 *
michael@0 290 * nsNodeInfoInner does not do any kind of reference counting,
michael@0 291 * that's up to the user of this class. Since nsNodeInfoInner is
michael@0 292 * typically used as a member of nsNodeInfo, the hash table doesn't
michael@0 293 * need to delete the keys. When the value (nsNodeInfo) is deleted
michael@0 294 * the key is automatically deleted.
michael@0 295 */
michael@0 296
michael@0 297 class nsNodeInfoInner
michael@0 298 {
michael@0 299 public:
michael@0 300 nsNodeInfoInner()
michael@0 301 : mName(nullptr), mPrefix(nullptr), mNamespaceID(kNameSpaceID_Unknown),
michael@0 302 mNodeType(0), mNameString(nullptr), mExtraName(nullptr)
michael@0 303 {
michael@0 304 }
michael@0 305 nsNodeInfoInner(nsIAtom *aName, nsIAtom *aPrefix, int32_t aNamespaceID,
michael@0 306 uint16_t aNodeType, nsIAtom* aExtraName)
michael@0 307 : mName(aName), mPrefix(aPrefix), mNamespaceID(aNamespaceID),
michael@0 308 mNodeType(aNodeType), mNameString(nullptr), mExtraName(aExtraName)
michael@0 309 {
michael@0 310 }
michael@0 311 nsNodeInfoInner(const nsAString& aTmpName, nsIAtom *aPrefix,
michael@0 312 int32_t aNamespaceID, uint16_t aNodeType)
michael@0 313 : mName(nullptr), mPrefix(aPrefix), mNamespaceID(aNamespaceID),
michael@0 314 mNodeType(aNodeType), mNameString(&aTmpName), mExtraName(nullptr)
michael@0 315 {
michael@0 316 }
michael@0 317
michael@0 318 nsIAtom* mName;
michael@0 319 nsIAtom* mPrefix;
michael@0 320 int32_t mNamespaceID;
michael@0 321 uint16_t mNodeType; // As defined by nsIDOMNode.nodeType
michael@0 322 const nsAString* mNameString;
michael@0 323 nsIAtom* mExtraName; // Only used by PIs and DocTypes
michael@0 324 };
michael@0 325
michael@0 326 // nsNodeInfoManager needs to pass mInner to the hash table.
michael@0 327 friend class nsNodeInfoManager;
michael@0 328
michael@0 329 nsIDocument* mDocument; // Weak. Cache of mOwnerManager->mDocument
michael@0 330
michael@0 331 nsNodeInfoInner mInner;
michael@0 332
michael@0 333 nsCOMPtr<nsIAtom> mIDAttributeAtom;
michael@0 334 nsRefPtr<nsNodeInfoManager> mOwnerManager;
michael@0 335
michael@0 336 /*
michael@0 337 * Members for various functions of mName+mPrefix that we can be
michael@0 338 * asked to compute.
michael@0 339 */
michael@0 340
michael@0 341 // Qualified name
michael@0 342 nsString mQualifiedName;
michael@0 343
michael@0 344 // nodeName for the node.
michael@0 345 nsString mNodeName;
michael@0 346
michael@0 347 // localName for the node. This is either equal to mInner.mName, or a
michael@0 348 // void string, depending on mInner.mNodeType.
michael@0 349 nsString mLocalName;
michael@0 350 };
michael@0 351
michael@0 352 NS_DEFINE_STATIC_IID_ACCESSOR(nsINodeInfo, NS_INODEINFO_IID)
michael@0 353
michael@0 354 #endif /* nsINodeInfo_h___ */

mercurial