1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
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
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. 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 NPL, 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 NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
40
#include "nsNodeInfo.h"
41
#include "nsNodeInfoManager.h"
45
#include "nsDOMString.h"
47
#include "nsContentUtils.h"
48
#include "nsReadableUtils.h"
54
if (sCachedNodeInfo) {
55
// We have cached unused instances of this class, return a cached
56
// instance instead of always creating a new one.
57
nsNodeInfo *nodeInfo = sCachedNodeInfo;
58
sCachedNodeInfo = nsnull;
63
return new nsNodeInfo();
66
nsNodeInfo::nsNodeInfo()
71
nsNodeInfo::~nsNodeInfo()
80
NS_STATIC_CAST(nsNodeInfoManager*, mOwnerManager)->RemoveNodeInfo(this);
81
NS_RELEASE(mOwnerManager);
84
NS_IF_RELEASE(mInner.mName);
85
NS_IF_RELEASE(mInner.mPrefix);
90
nsNodeInfo::Init(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID,
91
nsNodeInfoManager *aOwnerManager)
93
NS_ENSURE_TRUE(!mInner.mName && !mInner.mPrefix && !mOwnerManager,
94
NS_ERROR_ALREADY_INITIALIZED);
95
NS_ENSURE_ARG_POINTER(aName);
96
NS_ENSURE_ARG_POINTER(aOwnerManager);
99
NS_ADDREF(mInner.mName);
101
mInner.mPrefix = aPrefix;
102
NS_IF_ADDREF(mInner.mPrefix);
104
mInner.mNamespaceID = aNamespaceID;
106
mOwnerManager = aOwnerManager;
107
NS_ADDREF(mOwnerManager);
115
NS_IMPL_ADDREF(nsNodeInfo)
116
NS_IMPL_RELEASE_WITH_DESTROY(nsNodeInfo, LastRelease())
117
NS_IMPL_QUERY_INTERFACE1(nsNodeInfo, nsINodeInfo)
122
nsNodeInfo::GetQualifiedName(nsAString& aQualifiedName) const
124
if (mInner.mPrefix) {
125
mInner.mPrefix->ToString(aQualifiedName);
127
aQualifiedName.Append(PRUnichar(':'));
129
aQualifiedName.Truncate();
133
mInner.mName->ToString(name);
135
aQualifiedName.Append(name);
140
nsNodeInfo::GetLocalName(nsAString& aLocalName) const
142
#ifdef STRICT_DOM_LEVEL2_LOCALNAME
143
if (mInner.mNamespaceID > 0) {
144
mInner.mName->ToString(aLocalName);
146
SetDOMStringToNull(aLocalName);
149
mInner.mName->ToString(aLocalName);
155
nsNodeInfo::GetNamespaceURI(nsAString& aNameSpaceURI) const
159
if (mInner.mNamespaceID > 0) {
160
rv = nsContentUtils::GetNSManagerWeakRef()->GetNameSpaceURI(mInner.mNamespaceID,
163
SetDOMStringToNull(aNameSpaceURI);
171
nsNodeInfo::Equals(const nsAString& aName) const
173
return mInner.mName->Equals(aName);
178
nsNodeInfo::Equals(const nsAString& aName, const nsAString& aPrefix) const
180
if (!mInner.mName->Equals(aName)) {
184
if (!mInner.mPrefix) {
185
return aPrefix.IsEmpty();
188
return mInner.mPrefix->Equals(aPrefix);
193
nsNodeInfo::Equals(const nsAString& aName, PRInt32 aNamespaceID) const
195
return mInner.mNamespaceID == aNamespaceID &&
196
mInner.mName->Equals(aName);
201
nsNodeInfo::Equals(const nsAString& aName, const nsAString& aPrefix,
202
PRInt32 aNamespaceID) const
204
if (!mInner.mNamespaceID == aNamespaceID ||
205
!mInner.mName->Equals(aName))
208
return mInner.mPrefix ? mInner.mPrefix->Equals(aPrefix) :
214
nsNodeInfo::NamespaceEquals(const nsAString& aNamespaceURI) const
217
nsContentUtils::GetNSManagerWeakRef()->GetNameSpaceID(aNamespaceURI, &nsid);
219
return nsINodeInfo::NamespaceEquals(nsid);
223
nsNodeInfo::QualifiedNameEquals(const nsACString& aQualifiedName) const
227
return mInner.mName->EqualsUTF8(aQualifiedName);
229
nsACString::const_iterator start;
230
aQualifiedName.BeginReading(start);
232
nsACString::const_iterator colon(start);
235
mInner.mPrefix->GetUTF8String(&prefix);
237
PRUint32 len = strlen(prefix);
239
if (len >= aQualifiedName.Length()) {
245
// If the character at the prefix length index is not a colon,
246
// aQualifiedName is not equal to this string.
251
// Compare the prefix to the string from the start to the colon
252
if (!mInner.mPrefix->EqualsUTF8(Substring(start, colon)))
255
++colon; // Skip the ':'
257
nsACString::const_iterator end;
258
aQualifiedName.EndReading(end);
260
// Compare the local name to the string between the colon and the
261
// end of aQualifiedName
262
return mInner.mName->EqualsUTF8(Substring(colon, end));
266
nsNodeInfo *nsNodeInfo::sCachedNodeInfo = nsnull;
270
nsNodeInfo::ClearCache()
273
delete sCachedNodeInfo;
274
sCachedNodeInfo = nsnull;
278
nsNodeInfo::LastRelease()
280
if (sCachedNodeInfo) {
286
// There's space in the cache for one instance. Put
287
// this instance in the cache instead of deleting it.
288
sCachedNodeInfo = this;
290
// Clear object so that we have no references to anything external
293
// The refcount balancing and destructor re-entrancy protection
294
// code in Release() sets mRefCnt to 1 so we have to set it to 0
295
// here to prevent leaks