1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Mozilla Public License Version
6
* 1.1 (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/MPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
19
* Portions created by the Initial Developer are Copyright (C) 2003
20
* IBM Corporation. All Rights Reserved.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the MPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the MPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#ifndef nsAttrName_h___
40
#define nsAttrName_h___
42
#include "nsINodeInfo.h"
44
#include "nsDOMString.h"
46
typedef unsigned long PtrBits;
48
#define NS_ATTRNAME_NODEINFO_BIT 1
52
explicit nsAttrName(const nsAttrName& aOther)
58
explicit nsAttrName(nsIAtom* aAtom)
59
: mBits(NS_REINTERPRET_CAST(PtrBits, aAtom))
61
NS_ASSERTION(aAtom, "null atom-name in nsAttrName");
65
explicit nsAttrName(nsINodeInfo* aNodeInfo)
67
NS_ASSERTION(aNodeInfo, "null nodeinfo-name in nsAttrName");
68
if (aNodeInfo->NamespaceEquals(kNameSpaceID_None)) {
69
mBits = NS_REINTERPRET_CAST(PtrBits, aNodeInfo->NameAtom());
70
NS_ADDREF(aNodeInfo->NameAtom());
73
mBits = NS_REINTERPRET_CAST(PtrBits, aNodeInfo) |
74
NS_ATTRNAME_NODEINFO_BIT;
81
ReleaseInternalName();
84
void SetTo(nsINodeInfo* aNodeInfo)
86
NS_ASSERTION(aNodeInfo, "null nodeinfo-name in nsAttrName");
88
ReleaseInternalName();
89
if (aNodeInfo->NamespaceEquals(kNameSpaceID_None)) {
90
mBits = NS_REINTERPRET_CAST(PtrBits, aNodeInfo->NameAtom());
91
NS_ADDREF(aNodeInfo->NameAtom());
94
mBits = NS_REINTERPRET_CAST(PtrBits, aNodeInfo) |
95
NS_ATTRNAME_NODEINFO_BIT;
100
void SetTo(nsIAtom* aAtom)
102
NS_ASSERTION(aAtom, "null atom-name in nsAttrName");
104
ReleaseInternalName();
105
mBits = NS_REINTERPRET_CAST(PtrBits, aAtom);
109
PRBool IsAtom() const
111
return !(mBits & NS_ATTRNAME_NODEINFO_BIT);
114
nsINodeInfo* NodeInfo() const
116
NS_ASSERTION(!IsAtom(), "getting nodeinfo-value of atom-name");
117
return NS_REINTERPRET_CAST(nsINodeInfo*, mBits & ~NS_ATTRNAME_NODEINFO_BIT);
120
nsIAtom* Atom() const
122
NS_ASSERTION(IsAtom(), "getting atom-value of nodeinfo-name");
123
return NS_REINTERPRET_CAST(nsIAtom*, mBits);
126
PRBool Equals(const nsAttrName& aOther) const
128
return mBits == aOther.mBits;
131
// Faster comparison in the case we know the namespace is null
132
PRBool Equals(nsIAtom* aAtom) const
134
return NS_REINTERPRET_CAST(PtrBits, aAtom) == mBits;
137
PRBool Equals(nsIAtom* aLocalName, PRInt32 aNamespaceID) const
139
if (aNamespaceID == kNameSpaceID_None) {
140
return Equals(aLocalName);
142
return !IsAtom() && NodeInfo()->Equals(aLocalName, aNamespaceID);
145
PRInt32 NamespaceID() const
147
return IsAtom() ? kNameSpaceID_None : NodeInfo()->NamespaceID();
150
nsIAtom* LocalName() const
152
return IsAtom() ? Atom() : NodeInfo()->NameAtom();
155
nsIAtom* GetPrefix() const
157
return IsAtom() ? nsnull : NodeInfo()->GetPrefixAtom();
160
PRBool QualifiedNameEquals(const nsACString& aName) const
162
return IsAtom() ? Atom()->EqualsUTF8(aName) :
163
NodeInfo()->QualifiedNameEquals(aName);
166
void GetQualifiedName(nsAString& aStr) const
169
Atom()->ToString(aStr);
172
NodeInfo()->GetQualifiedName(aStr);
176
void GetPrefix(nsAString& aStr) const
179
SetDOMStringToNull(aStr);
182
NodeInfo()->GetPrefix(aStr);
186
PRUint32 HashValue() const
188
// mBits and PRUint32 might have different size. This should silence
189
// any warnings or compile-errors. This is what the implementation of
190
// NS_PTR_TO_INT32 does to take care of the same problem.
194
PRBool IsSmaller(nsIAtom* aOther) const
196
return mBits < NS_REINTERPRET_CAST(PtrBits, aOther);
201
void AddRefInternalName()
203
// Since both nsINodeInfo and nsIAtom inherit nsISupports as its first
204
// interface we can safely assume that it's first in the vtable
205
nsISupports* name = NS_REINTERPRET_CAST(nsISupports *,
206
mBits & ~NS_ATTRNAME_NODEINFO_BIT);
211
void ReleaseInternalName()
213
// Since both nsINodeInfo and nsIAtom inherit nsISupports as its first
214
// interface we can safely assume that it's first in the vtable
215
nsISupports* name = NS_REINTERPRET_CAST(nsISupports *,
216
mBits & ~NS_ATTRNAME_NODEINFO_BIT);