content/base/src/nsNodeInfo.cpp

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8 * Class that represents a prefix/namespace/localName triple; a single
michael@0 9 * nodeinfo is shared by all elements in a document that have that
michael@0 10 * prefix, namespace, and localName.
michael@0 11 */
michael@0 12
michael@0 13 #include "mozilla/ArrayUtils.h"
michael@0 14 #include "mozilla/Likely.h"
michael@0 15
michael@0 16 #include "nscore.h"
michael@0 17 #include "nsNodeInfo.h"
michael@0 18 #include "nsNodeInfoManager.h"
michael@0 19 #include "nsCOMPtr.h"
michael@0 20 #include "nsString.h"
michael@0 21 #include "nsIAtom.h"
michael@0 22 #include "nsDOMString.h"
michael@0 23 #include "nsCRT.h"
michael@0 24 #include "nsContentUtils.h"
michael@0 25 #include "nsReadableUtils.h"
michael@0 26 #include "nsAutoPtr.h"
michael@0 27 #include "prprf.h"
michael@0 28 #include "nsIDocument.h"
michael@0 29 #include "nsGkAtoms.h"
michael@0 30 #include "nsCCUncollectableMarker.h"
michael@0 31
michael@0 32 using namespace mozilla;
michael@0 33
michael@0 34 nsNodeInfo::~nsNodeInfo()
michael@0 35 {
michael@0 36 mOwnerManager->RemoveNodeInfo(this);
michael@0 37
michael@0 38 NS_RELEASE(mInner.mName);
michael@0 39 NS_IF_RELEASE(mInner.mPrefix);
michael@0 40 NS_IF_RELEASE(mInner.mExtraName);
michael@0 41 }
michael@0 42
michael@0 43
michael@0 44 nsNodeInfo::nsNodeInfo(nsIAtom *aName, nsIAtom *aPrefix, int32_t aNamespaceID,
michael@0 45 uint16_t aNodeType, nsIAtom* aExtraName,
michael@0 46 nsNodeInfoManager *aOwnerManager)
michael@0 47 {
michael@0 48 CheckValidNodeInfo(aNodeType, aName, aNamespaceID, aExtraName);
michael@0 49 NS_ABORT_IF_FALSE(aOwnerManager, "Invalid aOwnerManager");
michael@0 50
michael@0 51 // Initialize mInner
michael@0 52 NS_ADDREF(mInner.mName = aName);
michael@0 53 NS_IF_ADDREF(mInner.mPrefix = aPrefix);
michael@0 54 mInner.mNamespaceID = aNamespaceID;
michael@0 55 mInner.mNodeType = aNodeType;
michael@0 56 mOwnerManager = aOwnerManager;
michael@0 57 NS_IF_ADDREF(mInner.mExtraName = aExtraName);
michael@0 58
michael@0 59 mDocument = aOwnerManager->GetDocument();
michael@0 60
michael@0 61 // Now compute our cached members.
michael@0 62
michael@0 63 // Qualified name. If we have no prefix, use ToString on
michael@0 64 // mInner.mName so that we get to share its buffer.
michael@0 65 if (aPrefix) {
michael@0 66 mQualifiedName = nsDependentAtomString(mInner.mPrefix) +
michael@0 67 NS_LITERAL_STRING(":") +
michael@0 68 nsDependentAtomString(mInner.mName);
michael@0 69 } else {
michael@0 70 mInner.mName->ToString(mQualifiedName);
michael@0 71 }
michael@0 72
michael@0 73 MOZ_ASSERT_IF(aNodeType != nsIDOMNode::ELEMENT_NODE &&
michael@0 74 aNodeType != nsIDOMNode::ATTRIBUTE_NODE &&
michael@0 75 aNodeType != UINT16_MAX,
michael@0 76 aNamespaceID == kNameSpaceID_None && !aPrefix);
michael@0 77
michael@0 78 switch (aNodeType) {
michael@0 79 case nsIDOMNode::ELEMENT_NODE:
michael@0 80 case nsIDOMNode::ATTRIBUTE_NODE:
michael@0 81 // Correct the case for HTML
michael@0 82 if (aNodeType == nsIDOMNode::ELEMENT_NODE &&
michael@0 83 aNamespaceID == kNameSpaceID_XHTML && GetDocument() &&
michael@0 84 GetDocument()->IsHTML()) {
michael@0 85 nsContentUtils::ASCIIToUpper(mQualifiedName, mNodeName);
michael@0 86 } else {
michael@0 87 mNodeName = mQualifiedName;
michael@0 88 }
michael@0 89 mInner.mName->ToString(mLocalName);
michael@0 90 break;
michael@0 91 case nsIDOMNode::TEXT_NODE:
michael@0 92 case nsIDOMNode::CDATA_SECTION_NODE:
michael@0 93 case nsIDOMNode::COMMENT_NODE:
michael@0 94 case nsIDOMNode::DOCUMENT_NODE:
michael@0 95 case nsIDOMNode::DOCUMENT_FRAGMENT_NODE:
michael@0 96 mInner.mName->ToString(mNodeName);
michael@0 97 SetDOMStringToNull(mLocalName);
michael@0 98 break;
michael@0 99 case nsIDOMNode::PROCESSING_INSTRUCTION_NODE:
michael@0 100 case nsIDOMNode::DOCUMENT_TYPE_NODE:
michael@0 101 mInner.mExtraName->ToString(mNodeName);
michael@0 102 SetDOMStringToNull(mLocalName);
michael@0 103 break;
michael@0 104 default:
michael@0 105 NS_ABORT_IF_FALSE(aNodeType == UINT16_MAX,
michael@0 106 "Unknown node type");
michael@0 107 }
michael@0 108 }
michael@0 109
michael@0 110
michael@0 111 // nsISupports
michael@0 112
michael@0 113 NS_IMPL_CYCLE_COLLECTION_CLASS(nsNodeInfo)
michael@0 114
michael@0 115 NS_IMPL_CYCLE_COLLECTION_UNLINK_0(nsNodeInfo)
michael@0 116
michael@0 117 static const char* kNSURIs[] = {
michael@0 118 " ([none])",
michael@0 119 " (xmlns)",
michael@0 120 " (xml)",
michael@0 121 " (xhtml)",
michael@0 122 " (XLink)",
michael@0 123 " (XSLT)",
michael@0 124 " (XBL)",
michael@0 125 " (MathML)",
michael@0 126 " (RDF)",
michael@0 127 " (XUL)"
michael@0 128 };
michael@0 129
michael@0 130 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INTERNAL(nsNodeInfo)
michael@0 131 if (MOZ_UNLIKELY(cb.WantDebugInfo())) {
michael@0 132 char name[72];
michael@0 133 uint32_t nsid = tmp->NamespaceID();
michael@0 134 nsAtomCString localName(tmp->NameAtom());
michael@0 135 if (nsid < ArrayLength(kNSURIs)) {
michael@0 136 PR_snprintf(name, sizeof(name), "nsNodeInfo%s %s", kNSURIs[nsid],
michael@0 137 localName.get());
michael@0 138 }
michael@0 139 else {
michael@0 140 PR_snprintf(name, sizeof(name), "nsNodeInfo %s", localName.get());
michael@0 141 }
michael@0 142
michael@0 143 cb.DescribeRefCountedNode(tmp->mRefCnt.get(), name);
michael@0 144 }
michael@0 145 else {
michael@0 146 NS_IMPL_CYCLE_COLLECTION_DESCRIBE(nsNodeInfo, tmp->mRefCnt.get())
michael@0 147 }
michael@0 148
michael@0 149 NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwnerManager)
michael@0 150 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
michael@0 151
michael@0 152 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_BEGIN(nsNodeInfo)
michael@0 153 return nsCCUncollectableMarker::sGeneration && tmp->CanSkip();
michael@0 154 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_END
michael@0 155
michael@0 156 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_BEGIN(nsNodeInfo)
michael@0 157 return nsCCUncollectableMarker::sGeneration && tmp->CanSkip();
michael@0 158 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_IN_CC_END
michael@0 159
michael@0 160 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_BEGIN(nsNodeInfo)
michael@0 161 return nsCCUncollectableMarker::sGeneration && tmp->CanSkip();
michael@0 162 NS_IMPL_CYCLE_COLLECTION_CAN_SKIP_THIS_END
michael@0 163
michael@0 164
michael@0 165 NS_IMPL_CYCLE_COLLECTING_ADDREF(nsNodeInfo)
michael@0 166 NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_DESTROY(nsNodeInfo, LastRelease())
michael@0 167 NS_INTERFACE_TABLE_HEAD(nsNodeInfo)
michael@0 168 NS_INTERFACE_TABLE(nsNodeInfo, nsINodeInfo)
michael@0 169 NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(nsNodeInfo)
michael@0 170 NS_INTERFACE_MAP_END
michael@0 171
michael@0 172 // nsINodeInfo
michael@0 173
michael@0 174 void
michael@0 175 nsNodeInfo::GetNamespaceURI(nsAString& aNameSpaceURI) const
michael@0 176 {
michael@0 177 if (mInner.mNamespaceID > 0) {
michael@0 178 nsresult rv =
michael@0 179 nsContentUtils::NameSpaceManager()->GetNameSpaceURI(mInner.mNamespaceID,
michael@0 180 aNameSpaceURI);
michael@0 181 // How can we possibly end up with a bogus namespace ID here?
michael@0 182 if (NS_FAILED(rv)) {
michael@0 183 MOZ_CRASH();
michael@0 184 }
michael@0 185 } else {
michael@0 186 SetDOMStringToNull(aNameSpaceURI);
michael@0 187 }
michael@0 188 }
michael@0 189
michael@0 190
michael@0 191 bool
michael@0 192 nsNodeInfo::NamespaceEquals(const nsAString& aNamespaceURI) const
michael@0 193 {
michael@0 194 int32_t nsid =
michael@0 195 nsContentUtils::NameSpaceManager()->GetNameSpaceID(aNamespaceURI);
michael@0 196
michael@0 197 return nsINodeInfo::NamespaceEquals(nsid);
michael@0 198 }
michael@0 199
michael@0 200 void
michael@0 201 nsNodeInfo::LastRelease()
michael@0 202 {
michael@0 203 nsRefPtr<nsNodeInfoManager> kungFuDeathGrip = mOwnerManager;
michael@0 204 delete this;
michael@0 205 }
michael@0 206
michael@0 207 bool
michael@0 208 nsNodeInfo::CanSkip()
michael@0 209 {
michael@0 210 return mDocument &&
michael@0 211 nsCCUncollectableMarker::InGeneration(mDocument->GetMarkedCCGeneration());
michael@0 212 }

mercurial