michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Class that represents a prefix/namespace/localName triple; a single michael@0: * nodeinfo is shared by all elements in a document that have that michael@0: * prefix, namespace, and localName. michael@0: */ michael@0: michael@0: #include "mozilla/ArrayUtils.h" michael@0: #include "mozilla/Likely.h" michael@0: michael@0: #include "nscore.h" michael@0: #include "nsNodeInfo.h" michael@0: #include "nsNodeInfoManager.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsString.h" michael@0: #include "nsIAtom.h" michael@0: #include "nsDOMString.h" michael@0: #include "nsCRT.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsReadableUtils.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "prprf.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsCCUncollectableMarker.h" michael@0: michael@0: using namespace mozilla; michael@0: michael@0: nsNodeInfo::~nsNodeInfo() michael@0: { michael@0: mOwnerManager->RemoveNodeInfo(this); michael@0: michael@0: NS_RELEASE(mInner.mName); michael@0: NS_IF_RELEASE(mInner.mPrefix); michael@0: NS_IF_RELEASE(mInner.mExtraName); michael@0: } michael@0: michael@0: michael@0: nsNodeInfo::nsNodeInfo(nsIAtom *aName, nsIAtom *aPrefix, int32_t aNamespaceID, michael@0: uint16_t aNodeType, nsIAtom* aExtraName, michael@0: nsNodeInfoManager *aOwnerManager) michael@0: { michael@0: CheckValidNodeInfo(aNodeType, aName, aNamespaceID, aExtraName); michael@0: NS_ABORT_IF_FALSE(aOwnerManager, "Invalid aOwnerManager"); michael@0: michael@0: // Initialize mInner michael@0: NS_ADDREF(mInner.mName = aName); michael@0: NS_IF_ADDREF(mInner.mPrefix = aPrefix); michael@0: mInner.mNamespaceID = aNamespaceID; michael@0: mInner.mNodeType = aNodeType; michael@0: mOwnerManager = aOwnerManager; michael@0: NS_IF_ADDREF(mInner.mExtraName = aExtraName); michael@0: michael@0: mDocument = aOwnerManager->GetDocument(); michael@0: michael@0: // Now compute our cached members. michael@0: michael@0: // Qualified name. If we have no prefix, use ToString on michael@0: // mInner.mName so that we get to share its buffer. michael@0: if (aPrefix) { michael@0: mQualifiedName = nsDependentAtomString(mInner.mPrefix) + michael@0: NS_LITERAL_STRING(":") + michael@0: nsDependentAtomString(mInner.mName); michael@0: } else { michael@0: mInner.mName->ToString(mQualifiedName); michael@0: } michael@0: michael@0: MOZ_ASSERT_IF(aNodeType != nsIDOMNode::ELEMENT_NODE && michael@0: aNodeType != nsIDOMNode::ATTRIBUTE_NODE && michael@0: aNodeType != UINT16_MAX, michael@0: aNamespaceID == kNameSpaceID_None && !aPrefix); michael@0: michael@0: switch (aNodeType) { michael@0: case nsIDOMNode::ELEMENT_NODE: michael@0: case nsIDOMNode::ATTRIBUTE_NODE: michael@0: // Correct the case for HTML michael@0: if (aNodeType == nsIDOMNode::ELEMENT_NODE && michael@0: aNamespaceID == kNameSpaceID_XHTML && GetDocument() && michael@0: GetDocument()->IsHTML()) { michael@0: nsContentUtils::ASCIIToUpper(mQualifiedName, mNodeName); michael@0: } else { michael@0: mNodeName = mQualifiedName; michael@0: } michael@0: mInner.mName->ToString(mLocalName); michael@0: break; michael@0: case nsIDOMNode::TEXT_NODE: michael@0: case nsIDOMNode::CDATA_SECTION_NODE: michael@0: case nsIDOMNode::COMMENT_NODE: michael@0: case nsIDOMNode::DOCUMENT_NODE: michael@0: case nsIDOMNode::DOCUMENT_FRAGMENT_NODE: michael@0: mInner.mName->ToString(mNodeName); michael@0: SetDOMStringToNull(mLocalName); michael@0: break; michael@0: case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: michael@0: case nsIDOMNode::DOCUMENT_TYPE_NODE: michael@0: mInner.mExtraName->ToString(mNodeName); michael@0: SetDOMStringToNull(mLocalName); michael@0: break; michael@0: default: michael@0: NS_ABORT_IF_FALSE(aNodeType == UINT16_MAX, michael@0: "Unknown node type"); michael@0: } michael@0: } michael@0: michael@0: michael@0: // nsISupports michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CLASS(nsNodeInfo) michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_UNLINK_0(nsNodeInfo) michael@0: michael@0: static const char* kNSURIs[] = { michael@0: " ([none])", michael@0: " (xmlns)", michael@0: " (xml)", michael@0: " (xhtml)", michael@0: " (XLink)", michael@0: " (XSLT)", michael@0: " (XBL)", michael@0: " (MathML)", michael@0: " (RDF)", michael@0: " (XUL)" michael@0: }; michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsNodeInfo) michael@0: if (MOZ_UNLIKELY(cb.WantDebugInfo())) { michael@0: char name[72]; michael@0: uint32_t nsid = tmp->NamespaceID(); michael@0: nsAtomCString localName(tmp->NameAtom()); michael@0: if (nsid < ArrayLength(kNSURIs)) { michael@0: PR_snprintf(name, sizeof(name), "nsNodeInfo%s %s", kNSURIs[nsid], michael@0: localName.get()); michael@0: } michael@0: else { michael@0: PR_snprintf(name, sizeof(name), "nsNodeInfo %s", localName.get()); michael@0: } michael@0: michael@0: cb.DescribeRefCountedNode(tmp->mRefCnt.get(), name); michael@0: } michael@0: else { michael@0: NS_IMPL_CYCLE_COLLECTION_DESCRIBE(nsNodeInfo, tmp->mRefCnt.get()) michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwnerManager) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsNodeInfo) michael@0: return nsCCUncollectableMarker::sGeneration && tmp->CanSkip(); michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsNodeInfo) michael@0: return nsCCUncollectableMarker::sGeneration && tmp->CanSkip(); michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsNodeInfo) michael@0: return nsCCUncollectableMarker::sGeneration && tmp->CanSkip(); michael@0: NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END michael@0: michael@0: michael@0: NS_IMPL_CYCLE_COLLECTING_ADDREF(nsNodeInfo) michael@0: NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_DESTROY(nsNodeInfo, LastRelease()) michael@0: NS_INTERFACE_TABLE_HEAD(nsNodeInfo) michael@0: NS_INTERFACE_TABLE(nsNodeInfo, nsINodeInfo) michael@0: NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(nsNodeInfo) michael@0: NS_INTERFACE_MAP_END michael@0: michael@0: // nsINodeInfo michael@0: michael@0: void michael@0: nsNodeInfo::GetNamespaceURI(nsAString& aNameSpaceURI) const michael@0: { michael@0: if (mInner.mNamespaceID > 0) { michael@0: nsresult rv = michael@0: nsContentUtils::NameSpaceManager()->GetNameSpaceURI(mInner.mNamespaceID, michael@0: aNameSpaceURI); michael@0: // How can we possibly end up with a bogus namespace ID here? michael@0: if (NS_FAILED(rv)) { michael@0: MOZ_CRASH(); michael@0: } michael@0: } else { michael@0: SetDOMStringToNull(aNameSpaceURI); michael@0: } michael@0: } michael@0: michael@0: michael@0: bool michael@0: nsNodeInfo::NamespaceEquals(const nsAString& aNamespaceURI) const michael@0: { michael@0: int32_t nsid = michael@0: nsContentUtils::NameSpaceManager()->GetNameSpaceID(aNamespaceURI); michael@0: michael@0: return nsINodeInfo::NamespaceEquals(nsid); michael@0: } michael@0: michael@0: void michael@0: nsNodeInfo::LastRelease() michael@0: { michael@0: nsRefPtr kungFuDeathGrip = mOwnerManager; michael@0: delete this; michael@0: } michael@0: michael@0: bool michael@0: nsNodeInfo::CanSkip() michael@0: { michael@0: return mDocument && michael@0: nsCCUncollectableMarker::InGeneration(mDocument->GetMarkedCCGeneration()); michael@0: }