2013-02-28 18:53:49 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07: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.
|
|
|
|
*/
|
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
#include "mozilla/Util.h"
|
2012-10-26 06:32:10 -07:00
|
|
|
#include "mozilla/Likely.h"
|
2011-10-10 22:50:08 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nscore.h"
|
|
|
|
#include "nsNodeInfo.h"
|
|
|
|
#include "nsNodeInfoManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsIAtom.h"
|
|
|
|
#include "nsDOMString.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2010-09-14 21:46:40 -07:00
|
|
|
#include "prprf.h"
|
2011-07-20 12:18:54 -07:00
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2013-01-25 14:11:13 -08:00
|
|
|
#include "nsCCUncollectableMarker.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-10 22:50:08 -07:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNodeInfo::~nsNodeInfo()
|
|
|
|
{
|
2011-05-05 09:26:32 -07:00
|
|
|
mOwnerManager->RemoveNodeInfo(this);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-05-05 09:26:32 -07:00
|
|
|
NS_RELEASE(mInner.mName);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_IF_RELEASE(mInner.mPrefix);
|
2011-06-14 00:56:49 -07:00
|
|
|
NS_IF_RELEASE(mInner.mExtraName);
|
2011-07-26 04:11:14 -07:00
|
|
|
NS_RELEASE(mOwnerManager);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsNodeInfo::nsNodeInfo(nsIAtom *aName, nsIAtom *aPrefix, int32_t aNamespaceID,
|
|
|
|
uint16_t aNodeType, nsIAtom* aExtraName,
|
2011-05-05 09:26:32 -07:00
|
|
|
nsNodeInfoManager *aOwnerManager)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-08-16 08:07:55 -07:00
|
|
|
CheckValidNodeInfo(aNodeType, aName, aNamespaceID, aExtraName);
|
2011-06-14 00:56:49 -07:00
|
|
|
NS_ABORT_IF_FALSE(aOwnerManager, "Invalid aOwnerManager");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-06-14 00:56:49 -07:00
|
|
|
// Initialize mInner
|
|
|
|
NS_ADDREF(mInner.mName = aName);
|
|
|
|
NS_IF_ADDREF(mInner.mPrefix = aPrefix);
|
2007-03-22 10:30:00 -07:00
|
|
|
mInner.mNamespaceID = aNamespaceID;
|
2011-06-14 00:56:49 -07:00
|
|
|
mInner.mNodeType = aNodeType;
|
|
|
|
NS_ADDREF(mOwnerManager = aOwnerManager);
|
|
|
|
NS_IF_ADDREF(mInner.mExtraName = aExtraName);
|
2011-05-05 09:26:32 -07:00
|
|
|
|
2011-06-14 17:58:57 -07:00
|
|
|
mDocument = aOwnerManager->GetDocument();
|
|
|
|
|
2011-05-05 09:26:32 -07:00
|
|
|
// Now compute our cached members.
|
|
|
|
|
|
|
|
// Qualified name. If we have no prefix, use ToString on
|
|
|
|
// mInner.mName so that we get to share its buffer.
|
|
|
|
if (aPrefix) {
|
2011-06-14 00:56:49 -07:00
|
|
|
mQualifiedName = nsDependentAtomString(mInner.mPrefix) +
|
|
|
|
NS_LITERAL_STRING(":") +
|
|
|
|
nsDependentAtomString(mInner.mName);
|
2011-05-05 09:26:32 -07:00
|
|
|
} else {
|
|
|
|
mInner.mName->ToString(mQualifiedName);
|
|
|
|
}
|
|
|
|
|
2012-10-09 05:31:24 -07:00
|
|
|
MOZ_ASSERT_IF(aNodeType != nsIDOMNode::ELEMENT_NODE &&
|
|
|
|
aNodeType != nsIDOMNode::ATTRIBUTE_NODE &&
|
|
|
|
aNodeType != UINT16_MAX,
|
|
|
|
aNamespaceID == kNameSpaceID_None && !aPrefix);
|
|
|
|
|
2011-06-14 00:56:49 -07:00
|
|
|
switch (aNodeType) {
|
|
|
|
case nsIDOMNode::ELEMENT_NODE:
|
|
|
|
case nsIDOMNode::ATTRIBUTE_NODE:
|
2011-06-14 00:56:49 -07:00
|
|
|
// Correct the case for HTML
|
|
|
|
if (aNodeType == nsIDOMNode::ELEMENT_NODE &&
|
|
|
|
aNamespaceID == kNameSpaceID_XHTML && GetDocument() &&
|
|
|
|
GetDocument()->IsHTML()) {
|
|
|
|
nsContentUtils::ASCIIToUpper(mQualifiedName, mNodeName);
|
|
|
|
} else {
|
|
|
|
mNodeName = mQualifiedName;
|
|
|
|
}
|
2011-06-14 00:56:49 -07:00
|
|
|
mInner.mName->ToString(mLocalName);
|
|
|
|
break;
|
|
|
|
case nsIDOMNode::TEXT_NODE:
|
|
|
|
case nsIDOMNode::CDATA_SECTION_NODE:
|
|
|
|
case nsIDOMNode::COMMENT_NODE:
|
|
|
|
case nsIDOMNode::DOCUMENT_NODE:
|
|
|
|
case nsIDOMNode::DOCUMENT_FRAGMENT_NODE:
|
|
|
|
mInner.mName->ToString(mNodeName);
|
|
|
|
SetDOMStringToNull(mLocalName);
|
|
|
|
break;
|
|
|
|
case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:
|
|
|
|
case nsIDOMNode::DOCUMENT_TYPE_NODE:
|
|
|
|
mInner.mExtraName->ToString(mNodeName);
|
|
|
|
SetDOMStringToNull(mLocalName);
|
|
|
|
break;
|
|
|
|
default:
|
2012-09-27 23:57:33 -07:00
|
|
|
NS_ABORT_IF_FALSE(aNodeType == UINT16_MAX,
|
2011-06-14 00:56:49 -07:00
|
|
|
"Unknown node type");
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// nsISupports
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsNodeInfo)
|
|
|
|
|
2008-03-28 07:09:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_0(nsNodeInfo)
|
2009-02-27 06:48:26 -08:00
|
|
|
|
|
|
|
static const char* kNSURIs[] = {
|
|
|
|
" ([none])",
|
|
|
|
" (xmlns)",
|
|
|
|
" (xml)",
|
|
|
|
" (xhtml)",
|
|
|
|
" (XLink)",
|
|
|
|
" (XSLT)",
|
|
|
|
" (XBL)",
|
|
|
|
" (MathML)",
|
|
|
|
" (RDF)",
|
|
|
|
" (XUL)"
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsNodeInfo)
|
2012-10-26 06:32:10 -07:00
|
|
|
if (MOZ_UNLIKELY(cb.WantDebugInfo())) {
|
2009-02-27 06:48:26 -08:00
|
|
|
char name[72];
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t nsid = tmp->NamespaceID();
|
2009-02-27 06:48:26 -08:00
|
|
|
nsAtomCString localName(tmp->NameAtom());
|
2011-10-10 22:50:08 -07:00
|
|
|
if (nsid < ArrayLength(kNSURIs)) {
|
2009-02-27 06:48:26 -08:00
|
|
|
PR_snprintf(name, sizeof(name), "nsNodeInfo%s %s", kNSURIs[nsid],
|
|
|
|
localName.get());
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PR_snprintf(name, sizeof(name), "nsNodeInfo %s", localName.get());
|
|
|
|
}
|
|
|
|
|
2012-08-27 10:41:04 -07:00
|
|
|
cb.DescribeRefCountedNode(tmp->mRefCnt.get(), name);
|
2009-02-27 06:48:26 -08:00
|
|
|
}
|
|
|
|
else {
|
2011-06-23 14:10:52 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_DESCRIBE(nsNodeInfo, tmp->mRefCnt.get())
|
2009-02-27 06:48:26 -08:00
|
|
|
}
|
|
|
|
|
2012-11-14 23:32:39 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(mOwnerManager)
|
2008-03-28 07:09:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-01-25 14:11:13 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsNodeInfo)
|
|
|
|
return nsCCUncollectableMarker::sGeneration && tmp->CanSkip();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsNodeInfo)
|
|
|
|
return nsCCUncollectableMarker::sGeneration && tmp->CanSkip();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsNodeInfo)
|
|
|
|
return nsCCUncollectableMarker::sGeneration && tmp->CanSkip();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
|
|
|
|
|
|
|
|
|
2008-03-28 07:09:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsNodeInfo)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_DESTROY(nsNodeInfo, LastRelease())
|
|
|
|
NS_INTERFACE_TABLE_HEAD(nsNodeInfo)
|
|
|
|
NS_INTERFACE_TABLE1(nsNodeInfo, nsINodeInfo)
|
|
|
|
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(nsNodeInfo)
|
|
|
|
NS_INTERFACE_MAP_END
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// nsINodeInfo
|
|
|
|
|
2013-01-29 14:53:52 -08:00
|
|
|
void
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNodeInfo::GetNamespaceURI(nsAString& aNameSpaceURI) const
|
|
|
|
{
|
|
|
|
if (mInner.mNamespaceID > 0) {
|
2013-01-29 14:53:52 -08:00
|
|
|
nsresult rv =
|
|
|
|
nsContentUtils::NameSpaceManager()->GetNameSpaceURI(mInner.mNamespaceID,
|
|
|
|
aNameSpaceURI);
|
|
|
|
// How can we possibly end up with a bogus namespace ID here?
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
MOZ_CRASH();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
} else {
|
|
|
|
SetDOMStringToNull(aNameSpaceURI);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNodeInfo::NamespaceEquals(const nsAString& aNamespaceURI) const
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t nsid =
|
2007-03-22 10:30:00 -07:00
|
|
|
nsContentUtils::NameSpaceManager()->GetNameSpaceID(aNamespaceURI);
|
|
|
|
|
|
|
|
return nsINodeInfo::NamespaceEquals(nsid);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeInfo::LastRelease()
|
|
|
|
{
|
|
|
|
nsRefPtr<nsNodeInfoManager> kungFuDeathGrip = mOwnerManager;
|
2013-02-28 18:53:49 -08:00
|
|
|
delete this;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-01-25 14:11:13 -08:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsNodeInfo::CanSkip()
|
|
|
|
{
|
|
|
|
return mDocument &&
|
|
|
|
nsCCUncollectableMarker::InGeneration(mDocument->GetMarkedCCGeneration());
|
|
|
|
}
|