1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 * Class that represents a prefix/namespace/localName triple; a single
9 * nodeinfo is shared by all elements in a document that have that
10 * prefix, namespace, and localName.
13 #include "mozilla/dom/NodeInfo.h"
14 #include "mozilla/dom/NodeInfoInlines.h"
16 #include "mozilla/ArrayUtils.h"
17 #include "mozilla/Likely.h"
19 #include "nsNodeInfoManager.h"
23 #include "nsDOMString.h"
26 #include "nsContentUtils.h"
27 #include "nsReadableUtils.h"
28 #include "mozilla/Sprintf.h"
29 #include "mozilla/dom/Document.h"
30 #include "nsGkAtoms.h"
31 #include "nsCCUncollectableMarker.h"
32 #include "nsNameSpaceManager.h"
34 using namespace mozilla
;
35 using mozilla::dom::NodeInfo
;
37 NodeInfo::~NodeInfo() {
38 mOwnerManager
->RemoveNodeInfo(this);
40 // We can't use NS_IF_RELEASE because mName is const.
42 mInner
.mName
->Release();
44 NS_IF_RELEASE(mInner
.mPrefix
);
45 NS_IF_RELEASE(mInner
.mExtraName
);
48 NodeInfo::NodeInfo(nsAtom
* aName
, nsAtom
* aPrefix
, int32_t aNamespaceID
,
49 uint16_t aNodeType
, nsAtom
* aExtraName
,
50 nsNodeInfoManager
* aOwnerManager
)
51 : mDocument(aOwnerManager
->GetDocument()),
52 mInner(aName
, aPrefix
, aNamespaceID
, aNodeType
, aExtraName
),
53 mOwnerManager(aOwnerManager
) {
54 CheckValidNodeInfo(aNodeType
, aName
, aNamespaceID
, aExtraName
);
56 NS_IF_ADDREF(mInner
.mName
);
57 NS_IF_ADDREF(mInner
.mPrefix
);
58 NS_IF_ADDREF(mInner
.mExtraName
);
60 // Now compute our cached members.
62 // Qualified name. If we have no prefix, use ToString on
63 // mInner.mName so that we get to share its buffer.
65 mQualifiedName
= nsDependentAtomString(mInner
.mPrefix
) + u
":"_ns
+
66 nsDependentAtomString(mInner
.mName
);
68 mInner
.mName
->ToString(mQualifiedName
);
71 MOZ_ASSERT_IF(aNodeType
!= nsINode::ELEMENT_NODE
&&
72 aNodeType
!= nsINode::ATTRIBUTE_NODE
&&
73 aNodeType
!= UINT16_MAX
,
74 aNamespaceID
== kNameSpaceID_None
&& !aPrefix
);
77 case nsINode::ELEMENT_NODE
:
78 case nsINode::ATTRIBUTE_NODE
:
79 // Correct the case for HTML
80 if (aNodeType
== nsINode::ELEMENT_NODE
&&
81 aNamespaceID
== kNameSpaceID_XHTML
&& GetDocument() &&
82 GetDocument()->IsHTMLDocument()) {
83 nsContentUtils::ASCIIToUpper(mQualifiedName
, mNodeName
);
85 mNodeName
= mQualifiedName
;
87 mInner
.mName
->ToString(mLocalName
);
89 case nsINode::TEXT_NODE
:
90 case nsINode::CDATA_SECTION_NODE
:
91 case nsINode::COMMENT_NODE
:
92 case nsINode::DOCUMENT_NODE
:
93 case nsINode::DOCUMENT_FRAGMENT_NODE
:
94 mInner
.mName
->ToString(mNodeName
);
95 SetDOMStringToNull(mLocalName
);
97 case nsINode::PROCESSING_INSTRUCTION_NODE
:
98 case nsINode::DOCUMENT_TYPE_NODE
:
99 mInner
.mExtraName
->ToString(mNodeName
);
100 SetDOMStringToNull(mLocalName
);
103 MOZ_ASSERT(aNodeType
== UINT16_MAX
, "Unknown node type");
109 NS_IMPL_CYCLE_COLLECTION_CLASS(NodeInfo
)
111 NS_IMPL_CYCLE_COLLECTION_UNLINK_0(NodeInfo
)
113 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(NodeInfo
)
114 if (MOZ_UNLIKELY(cb
.WantDebugInfo())) {
116 uint32_t nsid
= tmp
->NamespaceID();
117 nsAtomCString
localName(tmp
->NameAtom());
118 const char* nsuri
= nsNameSpaceManager::GetNameSpaceDisplayName(nsid
);
119 SprintfLiteral(name
, "NodeInfo %s %s", nsuri
, localName
.get());
121 cb
.DescribeRefCountedNode(tmp
->mRefCnt
.get(), name
);
123 NS_IMPL_CYCLE_COLLECTION_DESCRIBE(NodeInfo
, tmp
->mRefCnt
.get())
126 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwnerManager
)
127 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
129 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(NodeInfo
)
130 return nsCCUncollectableMarker::sGeneration
&& tmp
->CanSkip();
131 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
133 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(NodeInfo
)
134 return nsCCUncollectableMarker::sGeneration
&& tmp
->CanSkip();
135 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
137 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(NodeInfo
)
138 return nsCCUncollectableMarker::sGeneration
&& tmp
->CanSkip();
139 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
141 NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(NodeInfo
, AddRef
)
142 NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(NodeInfo
, Release
)
144 void NodeInfo::GetName(nsAString
& aName
) const {
145 mInner
.mName
->ToString(aName
);
148 void NodeInfo::GetPrefix(nsAString
& aPrefix
) const {
149 if (mInner
.mPrefix
) {
150 mInner
.mPrefix
->ToString(aPrefix
);
152 SetDOMStringToNull(aPrefix
);
156 void NodeInfo::GetNamespaceURI(nsAString
& aNameSpaceURI
) const {
157 if (mInner
.mNamespaceID
> 0) {
158 nsresult rv
= nsNameSpaceManager::GetInstance()->GetNameSpaceURI(
159 mInner
.mNamespaceID
, aNameSpaceURI
);
160 // How can we possibly end up with a bogus namespace ID here?
165 SetDOMStringToNull(aNameSpaceURI
);
169 bool NodeInfo::NamespaceEquals(const nsAString
& aNamespaceURI
) const {
170 int32_t nsid
= nsNameSpaceManager::GetInstance()->GetNameSpaceID(
171 aNamespaceURI
, nsContentUtils::IsChromeDoc(mOwnerManager
->GetDocument()));
173 return mozilla::dom::NodeInfo::NamespaceEquals(nsid
);
176 void NodeInfo::DeleteCycleCollectable() {
177 RefPtr
<nsNodeInfoManager
> kungFuDeathGrip
= mOwnerManager
;
179 << kungFuDeathGrip
; // Just keeping value alive for longer than this
183 bool NodeInfo::CanSkip() {
184 return mDocument
&& nsCCUncollectableMarker::InGeneration(
185 mDocument
->GetMarkedCCGeneration());