2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-17 21:52:36 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2000-05-08 14:29:44 +00:00
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2000-05-08 14:29:44 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2000-05-08 14:29:44 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-17 21:52:36 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-25 01:32:19 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2000-05-08 14:29:44 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-17 21:52:36 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-25 01:32:19 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-17 21:52:36 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-25 01:32:19 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-17 21:52:36 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-25 01:32:19 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2000-05-08 14:29:44 +00:00
|
|
|
|
2006-03-31 08:00:42 +00:00
|
|
|
/*
|
|
|
|
* Class that represents a prefix/namespace/localName triple; a single
|
|
|
|
* nodeinfo is shared by all elements in a document that have that
|
|
|
|
* prefix, namespace, and localName.
|
|
|
|
*/
|
|
|
|
|
2001-08-14 07:59:59 +00:00
|
|
|
#include "nscore.h"
|
2000-05-08 14:29:44 +00:00
|
|
|
#include "nsNodeInfo.h"
|
|
|
|
#include "nsNodeInfoManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIAtom.h"
|
2004-04-01 19:44:17 +00:00
|
|
|
#include "nsDOMString.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2002-11-29 23:44:07 +00:00
|
|
|
#include "nsContentUtils.h"
|
2003-01-05 09:16:14 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2000-05-08 14:29:44 +00:00
|
|
|
|
2003-10-30 13:47:29 +00:00
|
|
|
// static
|
|
|
|
nsNodeInfo*
|
|
|
|
nsNodeInfo::Create()
|
|
|
|
{
|
|
|
|
if (sCachedNodeInfo) {
|
|
|
|
// We have cached unused instances of this class, return a cached
|
|
|
|
// instance instead of always creating a new one.
|
|
|
|
nsNodeInfo *nodeInfo = sCachedNodeInfo;
|
|
|
|
sCachedNodeInfo = nsnull;
|
|
|
|
return nodeInfo;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new one
|
2005-01-31 16:18:31 +00:00
|
|
|
return new nsNodeInfo();
|
2003-10-30 13:47:29 +00:00
|
|
|
}
|
|
|
|
|
2000-05-08 14:29:44 +00:00
|
|
|
nsNodeInfo::nsNodeInfo()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-01-31 16:18:31 +00:00
|
|
|
|
|
|
|
nsNodeInfo::~nsNodeInfo()
|
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeInfo::Clear()
|
|
|
|
{
|
|
|
|
if (mOwnerManager) {
|
|
|
|
mOwnerManager->RemoveNodeInfo(this);
|
|
|
|
NS_RELEASE(mOwnerManager);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IF_RELEASE(mInner.mName);
|
|
|
|
NS_IF_RELEASE(mInner.mPrefix);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-05-08 14:29:44 +00:00
|
|
|
nsresult
|
|
|
|
nsNodeInfo::Init(nsIAtom *aName, nsIAtom *aPrefix, PRInt32 aNamespaceID,
|
|
|
|
nsNodeInfoManager *aOwnerManager)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!mInner.mName && !mInner.mPrefix && !mOwnerManager,
|
|
|
|
NS_ERROR_ALREADY_INITIALIZED);
|
|
|
|
NS_ENSURE_ARG_POINTER(aName);
|
|
|
|
NS_ENSURE_ARG_POINTER(aOwnerManager);
|
|
|
|
|
|
|
|
mInner.mName = aName;
|
|
|
|
NS_ADDREF(mInner.mName);
|
|
|
|
|
|
|
|
mInner.mPrefix = aPrefix;
|
|
|
|
NS_IF_ADDREF(mInner.mPrefix);
|
|
|
|
|
|
|
|
mInner.mNamespaceID = aNamespaceID;
|
|
|
|
|
|
|
|
mOwnerManager = aOwnerManager;
|
|
|
|
NS_ADDREF(mOwnerManager);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
|
2003-10-30 13:47:29 +00:00
|
|
|
NS_IMPL_ADDREF(nsNodeInfo)
|
|
|
|
NS_IMPL_RELEASE_WITH_DESTROY(nsNodeInfo, LastRelease())
|
|
|
|
NS_IMPL_QUERY_INTERFACE1(nsNodeInfo, nsINodeInfo)
|
2000-05-08 14:29:44 +00:00
|
|
|
|
|
|
|
// nsINodeInfo
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
void
|
2002-01-03 04:51:02 +00:00
|
|
|
nsNodeInfo::GetQualifiedName(nsAString& aQualifiedName) const
|
2000-05-08 14:29:44 +00:00
|
|
|
{
|
2000-08-31 00:36:31 +00:00
|
|
|
if (mInner.mPrefix) {
|
2000-05-08 14:29:44 +00:00
|
|
|
mInner.mPrefix->ToString(aQualifiedName);
|
|
|
|
|
2000-05-16 14:43:32 +00:00
|
|
|
aQualifiedName.Append(PRUnichar(':'));
|
2001-10-08 22:37:23 +00:00
|
|
|
} else {
|
|
|
|
aQualifiedName.Truncate();
|
2000-05-08 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
2003-03-25 18:55:59 +00:00
|
|
|
nsAutoString name;
|
|
|
|
mInner.mName->ToString(name);
|
2000-05-08 14:29:44 +00:00
|
|
|
|
2000-05-25 23:48:45 +00:00
|
|
|
aQualifiedName.Append(name);
|
2000-05-08 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
void
|
2002-01-03 04:51:02 +00:00
|
|
|
nsNodeInfo::GetLocalName(nsAString& aLocalName) const
|
2000-05-08 14:29:44 +00:00
|
|
|
{
|
2000-05-16 02:17:14 +00:00
|
|
|
#ifdef STRICT_DOM_LEVEL2_LOCALNAME
|
2000-05-08 14:29:44 +00:00
|
|
|
if (mInner.mNamespaceID > 0) {
|
2003-11-15 05:27:37 +00:00
|
|
|
mInner.mName->ToString(aLocalName);
|
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aLocalName);
|
2000-05-08 14:29:44 +00:00
|
|
|
}
|
2000-05-16 02:17:14 +00:00
|
|
|
#else
|
2003-11-15 05:27:37 +00:00
|
|
|
mInner.mName->ToString(aLocalName);
|
2000-05-16 02:17:14 +00:00
|
|
|
#endif
|
2000-05-08 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
nsresult
|
2002-01-03 04:51:02 +00:00
|
|
|
nsNodeInfo::GetNamespaceURI(nsAString& aNameSpaceURI) const
|
2000-05-08 14:29:44 +00:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
|
|
|
|
if (mInner.mNamespaceID > 0) {
|
2005-09-11 10:08:43 +00:00
|
|
|
rv = nsContentUtils::NameSpaceManager()->GetNameSpaceURI(mInner.mNamespaceID,
|
|
|
|
aNameSpaceURI);
|
2000-05-08 14:29:44 +00:00
|
|
|
} else {
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 16:46:42 +00:00
|
|
|
SetDOMStringToNull(aNameSpaceURI);
|
2000-05-08 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
PRBool
|
2002-01-03 04:51:02 +00:00
|
|
|
nsNodeInfo::Equals(const nsAString& aName) const
|
2000-05-14 18:34:35 +00:00
|
|
|
{
|
2003-03-25 18:55:59 +00:00
|
|
|
return mInner.mName->Equals(aName);
|
2000-05-14 18:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
PRBool
|
2002-01-03 04:51:02 +00:00
|
|
|
nsNodeInfo::Equals(const nsAString& aName, const nsAString& aPrefix) const
|
2000-05-08 14:29:44 +00:00
|
|
|
{
|
2003-03-25 18:55:59 +00:00
|
|
|
if (!mInner.mName->Equals(aName)) {
|
2002-01-03 04:51:02 +00:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2000-05-14 18:34:35 +00:00
|
|
|
|
2002-01-03 04:51:02 +00:00
|
|
|
if (!mInner.mPrefix) {
|
|
|
|
return aPrefix.IsEmpty();
|
|
|
|
}
|
2000-05-14 18:34:35 +00:00
|
|
|
|
2003-03-25 18:55:59 +00:00
|
|
|
return mInner.mPrefix->Equals(aPrefix);
|
2000-05-08 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
PRBool
|
2002-01-03 04:51:02 +00:00
|
|
|
nsNodeInfo::Equals(const nsAString& aName, PRInt32 aNamespaceID) const
|
2000-05-14 18:34:35 +00:00
|
|
|
{
|
2003-03-25 18:55:59 +00:00
|
|
|
return mInner.mNamespaceID == aNamespaceID &&
|
|
|
|
mInner.mName->Equals(aName);
|
2000-05-14 18:34:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
PRBool
|
2002-01-03 04:51:02 +00:00
|
|
|
nsNodeInfo::Equals(const nsAString& aName, const nsAString& aPrefix,
|
|
|
|
PRInt32 aNamespaceID) const
|
2000-05-08 14:29:44 +00:00
|
|
|
{
|
2003-03-25 18:55:59 +00:00
|
|
|
if (!mInner.mNamespaceID == aNamespaceID ||
|
|
|
|
!mInner.mName->Equals(aName))
|
|
|
|
return PR_FALSE;
|
2000-05-14 18:34:35 +00:00
|
|
|
|
2003-03-25 18:55:59 +00:00
|
|
|
return mInner.mPrefix ? mInner.mPrefix->Equals(aPrefix) :
|
|
|
|
aPrefix.IsEmpty();
|
2000-05-08 14:29:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
PRBool
|
2002-01-03 04:51:02 +00:00
|
|
|
nsNodeInfo::NamespaceEquals(const nsAString& aNamespaceURI) const
|
2000-05-14 18:34:35 +00:00
|
|
|
{
|
2005-09-11 11:24:16 +00:00
|
|
|
PRInt32 nsid =
|
|
|
|
nsContentUtils::NameSpaceManager()->GetNameSpaceID(aNamespaceURI);
|
2000-05-14 18:34:35 +00:00
|
|
|
|
2002-01-03 04:51:02 +00:00
|
|
|
return nsINodeInfo::NamespaceEquals(nsid);
|
2000-05-14 18:34:35 +00:00
|
|
|
}
|
|
|
|
|
2003-11-15 05:27:37 +00:00
|
|
|
PRBool
|
2006-04-25 21:21:49 +00:00
|
|
|
nsNodeInfo::QualifiedNameEqualsInternal(const nsACString& aQualifiedName) const
|
2000-08-31 00:36:31 +00:00
|
|
|
{
|
2006-04-25 21:21:49 +00:00
|
|
|
NS_PRECONDITION(mInner.mPrefix, "Must have prefix");
|
2003-03-25 18:55:59 +00:00
|
|
|
|
|
|
|
nsACString::const_iterator start;
|
2002-01-03 04:51:02 +00:00
|
|
|
aQualifiedName.BeginReading(start);
|
2000-08-31 00:36:31 +00:00
|
|
|
|
2003-03-25 18:55:59 +00:00
|
|
|
nsACString::const_iterator colon(start);
|
2000-08-31 00:36:31 +00:00
|
|
|
|
2003-03-25 18:55:59 +00:00
|
|
|
const char* prefix;
|
|
|
|
mInner.mPrefix->GetUTF8String(&prefix);
|
2000-08-31 00:36:31 +00:00
|
|
|
|
2003-03-25 18:55:59 +00:00
|
|
|
PRUint32 len = strlen(prefix);
|
2002-01-03 04:51:02 +00:00
|
|
|
|
|
|
|
if (len >= aQualifiedName.Length()) {
|
|
|
|
return PR_FALSE;
|
2000-08-31 00:36:31 +00:00
|
|
|
}
|
|
|
|
|
2002-01-03 04:51:02 +00:00
|
|
|
colon.advance(len);
|
|
|
|
|
|
|
|
// If the character at the prefix length index is not a colon,
|
|
|
|
// aQualifiedName is not equal to this string.
|
|
|
|
if (*colon != ':') {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
2000-08-31 00:36:31 +00:00
|
|
|
|
2002-01-03 04:51:02 +00:00
|
|
|
// Compare the prefix to the string from the start to the colon
|
2003-03-25 18:55:59 +00:00
|
|
|
if (!mInner.mPrefix->EqualsUTF8(Substring(start, colon)))
|
2000-08-31 00:36:31 +00:00
|
|
|
return PR_FALSE;
|
2002-01-03 04:51:02 +00:00
|
|
|
|
|
|
|
++colon; // Skip the ':'
|
2000-08-31 00:36:31 +00:00
|
|
|
|
2003-03-25 18:55:59 +00:00
|
|
|
nsACString::const_iterator end;
|
2002-01-03 04:51:02 +00:00
|
|
|
aQualifiedName.EndReading(end);
|
|
|
|
|
|
|
|
// Compare the local name to the string between the colon and the
|
|
|
|
// end of aQualifiedName
|
2003-03-25 18:55:59 +00:00
|
|
|
return mInner.mName->EqualsUTF8(Substring(colon, end));
|
2000-08-31 00:36:31 +00:00
|
|
|
}
|
|
|
|
|
2003-10-30 13:47:29 +00:00
|
|
|
// static
|
|
|
|
nsNodeInfo *nsNodeInfo::sCachedNodeInfo = nsnull;
|
|
|
|
|
|
|
|
// static
|
|
|
|
void
|
|
|
|
nsNodeInfo::ClearCache()
|
|
|
|
{
|
|
|
|
// Clear our cache.
|
2005-01-31 16:18:31 +00:00
|
|
|
delete sCachedNodeInfo;
|
|
|
|
sCachedNodeInfo = nsnull;
|
2003-10-30 13:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeInfo::LastRelease()
|
|
|
|
{
|
2006-04-03 15:29:29 +00:00
|
|
|
// Clear object so that we have no references to anything external.
|
|
|
|
// Need to do this first so we don't have sCachedNodeInfo pointing
|
|
|
|
// to a nodeinfo that's not cleared yet.
|
|
|
|
Clear();
|
|
|
|
|
2003-10-30 13:47:29 +00:00
|
|
|
if (sCachedNodeInfo) {
|
|
|
|
// No room in cache
|
2005-01-31 16:18:31 +00:00
|
|
|
delete this;
|
2003-10-30 13:47:29 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// There's space in the cache for one instance. Put
|
|
|
|
// this instance in the cache instead of deleting it.
|
|
|
|
sCachedNodeInfo = this;
|
|
|
|
|
|
|
|
// The refcount balancing and destructor re-entrancy protection
|
|
|
|
// code in Release() sets mRefCnt to 1 so we have to set it to 0
|
|
|
|
// here to prevent leaks
|
|
|
|
mRefCnt = 0;
|
|
|
|
}
|