~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/content/base/src/nsAttrName.h

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
17
 * The Initial Developer of the Original Code is
 
18
 * IBM Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 2003
 
20
 * IBM Corporation. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   IBM Corporation
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#ifndef nsAttrName_h___
 
40
#define nsAttrName_h___
 
41
 
 
42
#include "nsINodeInfo.h"
 
43
#include "nsIAtom.h"
 
44
#include "nsDOMString.h"
 
45
 
 
46
typedef unsigned long PtrBits;
 
47
 
 
48
#define NS_ATTRNAME_NODEINFO_BIT 1
 
49
class nsAttrName
 
50
{
 
51
public:
 
52
  explicit nsAttrName(const nsAttrName& aOther)
 
53
    : mBits(aOther.mBits)
 
54
  {
 
55
    AddRefInternalName();
 
56
  }
 
57
 
 
58
  explicit nsAttrName(nsIAtom* aAtom)
 
59
    : mBits(NS_REINTERPRET_CAST(PtrBits, aAtom))
 
60
  {
 
61
    NS_ASSERTION(aAtom, "null atom-name in nsAttrName");
 
62
    NS_ADDREF(aAtom);
 
63
  }
 
64
 
 
65
  explicit nsAttrName(nsINodeInfo* aNodeInfo)
 
66
  {
 
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());
 
71
    }
 
72
    else {
 
73
      mBits = NS_REINTERPRET_CAST(PtrBits, aNodeInfo) |
 
74
              NS_ATTRNAME_NODEINFO_BIT;
 
75
      NS_ADDREF(aNodeInfo);
 
76
    }
 
77
  }
 
78
 
 
79
  ~nsAttrName()
 
80
  {
 
81
    ReleaseInternalName();
 
82
  }
 
83
 
 
84
  void SetTo(nsINodeInfo* aNodeInfo)
 
85
  {
 
86
    NS_ASSERTION(aNodeInfo, "null nodeinfo-name in nsAttrName");
 
87
 
 
88
    ReleaseInternalName();
 
89
    if (aNodeInfo->NamespaceEquals(kNameSpaceID_None)) {
 
90
      mBits = NS_REINTERPRET_CAST(PtrBits, aNodeInfo->NameAtom());
 
91
      NS_ADDREF(aNodeInfo->NameAtom());
 
92
    }
 
93
    else {
 
94
      mBits = NS_REINTERPRET_CAST(PtrBits, aNodeInfo) |
 
95
              NS_ATTRNAME_NODEINFO_BIT;
 
96
      NS_ADDREF(aNodeInfo);
 
97
    }
 
98
  }
 
99
 
 
100
  void SetTo(nsIAtom* aAtom)
 
101
  {
 
102
    NS_ASSERTION(aAtom, "null atom-name in nsAttrName");
 
103
 
 
104
    ReleaseInternalName();
 
105
    mBits = NS_REINTERPRET_CAST(PtrBits, aAtom);
 
106
    NS_ADDREF(aAtom);
 
107
  }
 
108
 
 
109
  PRBool IsAtom() const
 
110
  {
 
111
    return !(mBits & NS_ATTRNAME_NODEINFO_BIT);
 
112
  }
 
113
 
 
114
  nsINodeInfo* NodeInfo() const
 
115
  {
 
116
    NS_ASSERTION(!IsAtom(), "getting nodeinfo-value of atom-name");
 
117
    return NS_REINTERPRET_CAST(nsINodeInfo*, mBits & ~NS_ATTRNAME_NODEINFO_BIT);
 
118
  }
 
119
 
 
120
  nsIAtom* Atom() const
 
121
  {
 
122
    NS_ASSERTION(IsAtom(), "getting atom-value of nodeinfo-name");
 
123
    return NS_REINTERPRET_CAST(nsIAtom*, mBits);
 
124
  }
 
125
 
 
126
  PRBool Equals(const nsAttrName& aOther) const
 
127
  {
 
128
    return mBits == aOther.mBits;
 
129
  }
 
130
 
 
131
  // Faster comparison in the case we know the namespace is null
 
132
  PRBool Equals(nsIAtom* aAtom) const
 
133
  {
 
134
    return NS_REINTERPRET_CAST(PtrBits, aAtom) == mBits;
 
135
  }
 
136
 
 
137
  PRBool Equals(nsIAtom* aLocalName, PRInt32 aNamespaceID) const
 
138
  {
 
139
    if (aNamespaceID == kNameSpaceID_None) {
 
140
      return Equals(aLocalName);
 
141
    }
 
142
    return !IsAtom() && NodeInfo()->Equals(aLocalName, aNamespaceID);
 
143
  }
 
144
 
 
145
  PRInt32 NamespaceID() const
 
146
  {
 
147
    return IsAtom() ? kNameSpaceID_None : NodeInfo()->NamespaceID();
 
148
  }
 
149
 
 
150
  nsIAtom* LocalName() const
 
151
  {
 
152
    return IsAtom() ? Atom() : NodeInfo()->NameAtom();
 
153
  }
 
154
 
 
155
  nsIAtom* GetPrefix() const
 
156
  {
 
157
    return IsAtom() ? nsnull : NodeInfo()->GetPrefixAtom();
 
158
  }
 
159
 
 
160
  PRBool QualifiedNameEquals(const nsACString& aName) const
 
161
  {
 
162
    return IsAtom() ? Atom()->EqualsUTF8(aName) :
 
163
                      NodeInfo()->QualifiedNameEquals(aName);
 
164
  }
 
165
 
 
166
  void GetQualifiedName(nsAString& aStr) const
 
167
  {
 
168
    if (IsAtom()) {
 
169
      Atom()->ToString(aStr);
 
170
    }
 
171
    else {
 
172
      NodeInfo()->GetQualifiedName(aStr);
 
173
    }
 
174
  }
 
175
 
 
176
  void GetPrefix(nsAString& aStr) const
 
177
  {
 
178
    if (IsAtom()) {
 
179
      SetDOMStringToNull(aStr);
 
180
    }
 
181
    else {
 
182
      NodeInfo()->GetPrefix(aStr);
 
183
    }
 
184
  }
 
185
 
 
186
  PRUint32 HashValue() const
 
187
  {
 
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.
 
191
    return mBits - 0;
 
192
  }
 
193
 
 
194
  PRBool IsSmaller(nsIAtom* aOther) const
 
195
  {
 
196
    return mBits < NS_REINTERPRET_CAST(PtrBits, aOther);
 
197
  }
 
198
 
 
199
private:
 
200
 
 
201
  void AddRefInternalName()
 
202
  {
 
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);
 
207
 
 
208
    NS_ADDREF(name);
 
209
  }
 
210
 
 
211
  void ReleaseInternalName()
 
212
  {
 
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);
 
217
 
 
218
    NS_RELEASE(name);
 
219
  }
 
220
 
 
221
  PtrBits mBits;
 
222
};
 
223
 
 
224
#endif