Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 | * Base class for all DOM nodes. |
michael@0 | 9 | */ |
michael@0 | 10 | |
michael@0 | 11 | #include "nsINode.h" |
michael@0 | 12 | |
michael@0 | 13 | #include "AccessCheck.h" |
michael@0 | 14 | #include "jsapi.h" |
michael@0 | 15 | #include "mozAutoDocUpdate.h" |
michael@0 | 16 | #include "mozilla/AsyncEventDispatcher.h" |
michael@0 | 17 | #include "mozilla/CORSMode.h" |
michael@0 | 18 | #include "mozilla/EventDispatcher.h" |
michael@0 | 19 | #include "mozilla/EventListenerManager.h" |
michael@0 | 20 | #include "mozilla/InternalMutationEvent.h" |
michael@0 | 21 | #include "mozilla/Likely.h" |
michael@0 | 22 | #include "mozilla/MemoryReporting.h" |
michael@0 | 23 | #include "mozilla/Telemetry.h" |
michael@0 | 24 | #include "mozilla/dom/Element.h" |
michael@0 | 25 | #include "mozilla/dom/Event.h" |
michael@0 | 26 | #include "mozilla/dom/ShadowRoot.h" |
michael@0 | 27 | #include "nsAttrValueOrString.h" |
michael@0 | 28 | #include "nsBindingManager.h" |
michael@0 | 29 | #include "nsCCUncollectableMarker.h" |
michael@0 | 30 | #include "nsContentCreatorFunctions.h" |
michael@0 | 31 | #include "nsContentList.h" |
michael@0 | 32 | #include "nsContentUtils.h" |
michael@0 | 33 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 34 | #include "nsDocument.h" |
michael@0 | 35 | #include "mozilla/dom/Attr.h" |
michael@0 | 36 | #include "nsDOMAttributeMap.h" |
michael@0 | 37 | #include "nsDOMCID.h" |
michael@0 | 38 | #include "nsDOMCSSAttrDeclaration.h" |
michael@0 | 39 | #include "nsError.h" |
michael@0 | 40 | #include "nsDOMMutationObserver.h" |
michael@0 | 41 | #include "nsDOMString.h" |
michael@0 | 42 | #include "nsDOMTokenList.h" |
michael@0 | 43 | #include "nsFocusManager.h" |
michael@0 | 44 | #include "nsFrameManager.h" |
michael@0 | 45 | #include "nsFrameSelection.h" |
michael@0 | 46 | #include "nsGenericHTMLElement.h" |
michael@0 | 47 | #include "nsGkAtoms.h" |
michael@0 | 48 | #include "nsIAnonymousContentCreator.h" |
michael@0 | 49 | #include "nsIAtom.h" |
michael@0 | 50 | #include "nsIBaseWindow.h" |
michael@0 | 51 | #include "nsICategoryManager.h" |
michael@0 | 52 | #include "nsIContentIterator.h" |
michael@0 | 53 | #include "nsIControllers.h" |
michael@0 | 54 | #include "nsIDocument.h" |
michael@0 | 55 | #include "nsIDOMDocument.h" |
michael@0 | 56 | #include "nsIDOMDocumentType.h" |
michael@0 | 57 | #include "nsIDOMEvent.h" |
michael@0 | 58 | #include "nsIDOMEventListener.h" |
michael@0 | 59 | #include "nsIDOMMutationEvent.h" |
michael@0 | 60 | #include "nsIDOMNodeList.h" |
michael@0 | 61 | #include "nsIDOMUserDataHandler.h" |
michael@0 | 62 | #include "nsIEditor.h" |
michael@0 | 63 | #include "nsIEditorIMESupport.h" |
michael@0 | 64 | #include "nsILinkHandler.h" |
michael@0 | 65 | #include "nsINodeInfo.h" |
michael@0 | 66 | #include "nsIPresShell.h" |
michael@0 | 67 | #include "nsIScriptError.h" |
michael@0 | 68 | #include "nsIScriptGlobalObject.h" |
michael@0 | 69 | #include "nsIScriptSecurityManager.h" |
michael@0 | 70 | #include "nsIScrollableFrame.h" |
michael@0 | 71 | #include "nsIServiceManager.h" |
michael@0 | 72 | #include "nsIURL.h" |
michael@0 | 73 | #include "nsView.h" |
michael@0 | 74 | #include "nsViewManager.h" |
michael@0 | 75 | #include "nsIWebNavigation.h" |
michael@0 | 76 | #include "nsIWidget.h" |
michael@0 | 77 | #include "nsLayoutUtils.h" |
michael@0 | 78 | #include "nsNameSpaceManager.h" |
michael@0 | 79 | #include "nsNetUtil.h" |
michael@0 | 80 | #include "nsNodeInfoManager.h" |
michael@0 | 81 | #include "nsNodeUtils.h" |
michael@0 | 82 | #include "nsPIBoxObject.h" |
michael@0 | 83 | #include "nsPIDOMWindow.h" |
michael@0 | 84 | #include "nsPresContext.h" |
michael@0 | 85 | #include "nsRuleProcessorData.h" |
michael@0 | 86 | #include "nsString.h" |
michael@0 | 87 | #include "nsStyleConsts.h" |
michael@0 | 88 | #include "nsSVGFeatures.h" |
michael@0 | 89 | #include "nsSVGUtils.h" |
michael@0 | 90 | #include "nsTextNode.h" |
michael@0 | 91 | #include "nsUnicharUtils.h" |
michael@0 | 92 | #include "nsXBLBinding.h" |
michael@0 | 93 | #include "nsXBLPrototypeBinding.h" |
michael@0 | 94 | #include "prprf.h" |
michael@0 | 95 | #include "xpcpublic.h" |
michael@0 | 96 | #include "nsCSSRuleProcessor.h" |
michael@0 | 97 | #include "nsCSSParser.h" |
michael@0 | 98 | #include "HTMLLegendElement.h" |
michael@0 | 99 | #include "nsWrapperCacheInlines.h" |
michael@0 | 100 | #include "WrapperFactory.h" |
michael@0 | 101 | #include "DocumentType.h" |
michael@0 | 102 | #include <algorithm> |
michael@0 | 103 | #include "nsGlobalWindow.h" |
michael@0 | 104 | #include "nsDOMMutationObserver.h" |
michael@0 | 105 | #include "GeometryUtils.h" |
michael@0 | 106 | |
michael@0 | 107 | using namespace mozilla; |
michael@0 | 108 | using namespace mozilla::dom; |
michael@0 | 109 | |
michael@0 | 110 | nsINode::nsSlots::~nsSlots() |
michael@0 | 111 | { |
michael@0 | 112 | if (mChildNodes) { |
michael@0 | 113 | mChildNodes->DropReference(); |
michael@0 | 114 | NS_RELEASE(mChildNodes); |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | if (mWeakReference) { |
michael@0 | 118 | mWeakReference->NoticeNodeDestruction(); |
michael@0 | 119 | } |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | void |
michael@0 | 123 | nsINode::nsSlots::Traverse(nsCycleCollectionTraversalCallback &cb) |
michael@0 | 124 | { |
michael@0 | 125 | NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mChildNodes"); |
michael@0 | 126 | cb.NoteXPCOMChild(mChildNodes); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | void |
michael@0 | 130 | nsINode::nsSlots::Unlink() |
michael@0 | 131 | { |
michael@0 | 132 | if (mChildNodes) { |
michael@0 | 133 | mChildNodes->DropReference(); |
michael@0 | 134 | NS_RELEASE(mChildNodes); |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | //---------------------------------------------------------------------- |
michael@0 | 139 | |
michael@0 | 140 | nsINode::~nsINode() |
michael@0 | 141 | { |
michael@0 | 142 | MOZ_ASSERT(!HasSlots(), "nsNodeUtils::LastRelease was not called?"); |
michael@0 | 143 | MOZ_ASSERT(mSubtreeRoot == this, "Didn't restore state properly?"); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | void* |
michael@0 | 147 | nsINode::GetProperty(uint16_t aCategory, nsIAtom *aPropertyName, |
michael@0 | 148 | nsresult *aStatus) const |
michael@0 | 149 | { |
michael@0 | 150 | return OwnerDoc()->PropertyTable(aCategory)->GetProperty(this, aPropertyName, |
michael@0 | 151 | aStatus); |
michael@0 | 152 | } |
michael@0 | 153 | |
michael@0 | 154 | nsresult |
michael@0 | 155 | nsINode::SetProperty(uint16_t aCategory, nsIAtom *aPropertyName, void *aValue, |
michael@0 | 156 | NSPropertyDtorFunc aDtor, bool aTransfer, |
michael@0 | 157 | void **aOldValue) |
michael@0 | 158 | { |
michael@0 | 159 | nsresult rv = OwnerDoc()->PropertyTable(aCategory)->SetProperty(this, |
michael@0 | 160 | aPropertyName, |
michael@0 | 161 | aValue, aDtor, |
michael@0 | 162 | nullptr, |
michael@0 | 163 | aTransfer, |
michael@0 | 164 | aOldValue); |
michael@0 | 165 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 166 | SetFlags(NODE_HAS_PROPERTIES); |
michael@0 | 167 | } |
michael@0 | 168 | |
michael@0 | 169 | return rv; |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | void |
michael@0 | 173 | nsINode::DeleteProperty(uint16_t aCategory, nsIAtom *aPropertyName) |
michael@0 | 174 | { |
michael@0 | 175 | OwnerDoc()->PropertyTable(aCategory)->DeleteProperty(this, aPropertyName); |
michael@0 | 176 | } |
michael@0 | 177 | |
michael@0 | 178 | void* |
michael@0 | 179 | nsINode::UnsetProperty(uint16_t aCategory, nsIAtom *aPropertyName, |
michael@0 | 180 | nsresult *aStatus) |
michael@0 | 181 | { |
michael@0 | 182 | return OwnerDoc()->PropertyTable(aCategory)->UnsetProperty(this, |
michael@0 | 183 | aPropertyName, |
michael@0 | 184 | aStatus); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | nsINode::nsSlots* |
michael@0 | 188 | nsINode::CreateSlots() |
michael@0 | 189 | { |
michael@0 | 190 | return new nsSlots(); |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | bool |
michael@0 | 194 | nsINode::IsEditableInternal() const |
michael@0 | 195 | { |
michael@0 | 196 | if (HasFlag(NODE_IS_EDITABLE)) { |
michael@0 | 197 | // The node is in an editable contentEditable subtree. |
michael@0 | 198 | return true; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | nsIDocument *doc = GetCurrentDoc(); |
michael@0 | 202 | |
michael@0 | 203 | // Check if the node is in a document and the document is in designMode. |
michael@0 | 204 | return doc && doc->HasFlag(NODE_IS_EDITABLE); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | static nsIContent* GetEditorRootContent(nsIEditor* aEditor) |
michael@0 | 208 | { |
michael@0 | 209 | nsCOMPtr<nsIDOMElement> rootElement; |
michael@0 | 210 | aEditor->GetRootElement(getter_AddRefs(rootElement)); |
michael@0 | 211 | nsCOMPtr<nsIContent> rootContent(do_QueryInterface(rootElement)); |
michael@0 | 212 | return rootContent; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | nsIContent* |
michael@0 | 216 | nsINode::GetTextEditorRootContent(nsIEditor** aEditor) |
michael@0 | 217 | { |
michael@0 | 218 | if (aEditor) |
michael@0 | 219 | *aEditor = nullptr; |
michael@0 | 220 | for (nsINode* node = this; node; node = node->GetParentNode()) { |
michael@0 | 221 | if (!node->IsElement() || |
michael@0 | 222 | !node->AsElement()->IsHTML()) |
michael@0 | 223 | continue; |
michael@0 | 224 | |
michael@0 | 225 | nsCOMPtr<nsIEditor> editor = |
michael@0 | 226 | static_cast<nsGenericHTMLElement*>(node)->GetEditorInternal(); |
michael@0 | 227 | if (!editor) |
michael@0 | 228 | continue; |
michael@0 | 229 | |
michael@0 | 230 | nsIContent* rootContent = GetEditorRootContent(editor); |
michael@0 | 231 | if (aEditor) |
michael@0 | 232 | editor.swap(*aEditor); |
michael@0 | 233 | return rootContent; |
michael@0 | 234 | } |
michael@0 | 235 | return nullptr; |
michael@0 | 236 | } |
michael@0 | 237 | |
michael@0 | 238 | static nsIContent* GetRootForContentSubtree(nsIContent* aContent) |
michael@0 | 239 | { |
michael@0 | 240 | NS_ENSURE_TRUE(aContent, nullptr); |
michael@0 | 241 | |
michael@0 | 242 | // Special case for ShadowRoot because the ShadowRoot itself is |
michael@0 | 243 | // the root. This is necessary to prevent selection from crossing |
michael@0 | 244 | // the ShadowRoot boundary. |
michael@0 | 245 | ShadowRoot* containingShadow = aContent->GetContainingShadow(); |
michael@0 | 246 | if (containingShadow) { |
michael@0 | 247 | return containingShadow; |
michael@0 | 248 | } |
michael@0 | 249 | |
michael@0 | 250 | nsIContent* stop = aContent->GetBindingParent(); |
michael@0 | 251 | while (aContent) { |
michael@0 | 252 | nsIContent* parent = aContent->GetParent(); |
michael@0 | 253 | if (parent == stop) { |
michael@0 | 254 | break; |
michael@0 | 255 | } |
michael@0 | 256 | aContent = parent; |
michael@0 | 257 | } |
michael@0 | 258 | return aContent; |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | nsIContent* |
michael@0 | 262 | nsINode::GetSelectionRootContent(nsIPresShell* aPresShell) |
michael@0 | 263 | { |
michael@0 | 264 | NS_ENSURE_TRUE(aPresShell, nullptr); |
michael@0 | 265 | |
michael@0 | 266 | if (IsNodeOfType(eDOCUMENT)) |
michael@0 | 267 | return static_cast<nsIDocument*>(this)->GetRootElement(); |
michael@0 | 268 | if (!IsNodeOfType(eCONTENT)) |
michael@0 | 269 | return nullptr; |
michael@0 | 270 | |
michael@0 | 271 | if (GetCurrentDoc() != aPresShell->GetDocument()) { |
michael@0 | 272 | return nullptr; |
michael@0 | 273 | } |
michael@0 | 274 | |
michael@0 | 275 | if (static_cast<nsIContent*>(this)->HasIndependentSelection()) { |
michael@0 | 276 | // This node should be a descendant of input/textarea editor. |
michael@0 | 277 | nsIContent* content = GetTextEditorRootContent(); |
michael@0 | 278 | if (content) |
michael@0 | 279 | return content; |
michael@0 | 280 | } |
michael@0 | 281 | |
michael@0 | 282 | nsPresContext* presContext = aPresShell->GetPresContext(); |
michael@0 | 283 | if (presContext) { |
michael@0 | 284 | nsIEditor* editor = nsContentUtils::GetHTMLEditor(presContext); |
michael@0 | 285 | if (editor) { |
michael@0 | 286 | // This node is in HTML editor. |
michael@0 | 287 | nsIDocument* doc = GetCurrentDoc(); |
michael@0 | 288 | if (!doc || doc->HasFlag(NODE_IS_EDITABLE) || |
michael@0 | 289 | !HasFlag(NODE_IS_EDITABLE)) { |
michael@0 | 290 | nsIContent* editorRoot = GetEditorRootContent(editor); |
michael@0 | 291 | NS_ENSURE_TRUE(editorRoot, nullptr); |
michael@0 | 292 | return nsContentUtils::IsInSameAnonymousTree(this, editorRoot) ? |
michael@0 | 293 | editorRoot : |
michael@0 | 294 | GetRootForContentSubtree(static_cast<nsIContent*>(this)); |
michael@0 | 295 | } |
michael@0 | 296 | // If the document isn't editable but this is editable, this is in |
michael@0 | 297 | // contenteditable. Use the editing host element for selection root. |
michael@0 | 298 | return static_cast<nsIContent*>(this)->GetEditingHost(); |
michael@0 | 299 | } |
michael@0 | 300 | } |
michael@0 | 301 | |
michael@0 | 302 | nsRefPtr<nsFrameSelection> fs = aPresShell->FrameSelection(); |
michael@0 | 303 | nsIContent* content = fs->GetLimiter(); |
michael@0 | 304 | if (!content) { |
michael@0 | 305 | content = fs->GetAncestorLimiter(); |
michael@0 | 306 | if (!content) { |
michael@0 | 307 | nsIDocument* doc = aPresShell->GetDocument(); |
michael@0 | 308 | NS_ENSURE_TRUE(doc, nullptr); |
michael@0 | 309 | content = doc->GetRootElement(); |
michael@0 | 310 | if (!content) |
michael@0 | 311 | return nullptr; |
michael@0 | 312 | } |
michael@0 | 313 | } |
michael@0 | 314 | |
michael@0 | 315 | // This node might be in another subtree, if so, we should find this subtree's |
michael@0 | 316 | // root. Otherwise, we can return the content simply. |
michael@0 | 317 | NS_ENSURE_TRUE(content, nullptr); |
michael@0 | 318 | if (!nsContentUtils::IsInSameAnonymousTree(this, content)) { |
michael@0 | 319 | content = GetRootForContentSubtree(static_cast<nsIContent*>(this)); |
michael@0 | 320 | // Fixup for ShadowRoot because the ShadowRoot itself does not have a frame. |
michael@0 | 321 | // Use the host as the root. |
michael@0 | 322 | ShadowRoot* shadowRoot = ShadowRoot::FromNode(content); |
michael@0 | 323 | if (shadowRoot) { |
michael@0 | 324 | content = shadowRoot->GetHost(); |
michael@0 | 325 | } |
michael@0 | 326 | } |
michael@0 | 327 | |
michael@0 | 328 | return content; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | nsINodeList* |
michael@0 | 332 | nsINode::ChildNodes() |
michael@0 | 333 | { |
michael@0 | 334 | nsSlots* slots = Slots(); |
michael@0 | 335 | if (!slots->mChildNodes) { |
michael@0 | 336 | slots->mChildNodes = new nsChildContentList(this); |
michael@0 | 337 | if (slots->mChildNodes) { |
michael@0 | 338 | NS_ADDREF(slots->mChildNodes); |
michael@0 | 339 | } |
michael@0 | 340 | } |
michael@0 | 341 | |
michael@0 | 342 | return slots->mChildNodes; |
michael@0 | 343 | } |
michael@0 | 344 | |
michael@0 | 345 | void |
michael@0 | 346 | nsINode::GetTextContentInternal(nsAString& aTextContent) |
michael@0 | 347 | { |
michael@0 | 348 | SetDOMStringToNull(aTextContent); |
michael@0 | 349 | } |
michael@0 | 350 | |
michael@0 | 351 | #ifdef DEBUG |
michael@0 | 352 | void |
michael@0 | 353 | nsINode::CheckNotNativeAnonymous() const |
michael@0 | 354 | { |
michael@0 | 355 | if (!IsNodeOfType(eCONTENT)) |
michael@0 | 356 | return; |
michael@0 | 357 | nsIContent* content = static_cast<const nsIContent *>(this)->GetBindingParent(); |
michael@0 | 358 | while (content) { |
michael@0 | 359 | if (content->IsRootOfNativeAnonymousSubtree()) { |
michael@0 | 360 | NS_ERROR("Element not marked to be in native anonymous subtree!"); |
michael@0 | 361 | break; |
michael@0 | 362 | } |
michael@0 | 363 | content = content->GetBindingParent(); |
michael@0 | 364 | } |
michael@0 | 365 | } |
michael@0 | 366 | #endif |
michael@0 | 367 | |
michael@0 | 368 | bool |
michael@0 | 369 | nsINode::IsInAnonymousSubtree() const |
michael@0 | 370 | { |
michael@0 | 371 | if (!IsContent()) { |
michael@0 | 372 | return false; |
michael@0 | 373 | } |
michael@0 | 374 | |
michael@0 | 375 | return AsContent()->IsInAnonymousSubtree(); |
michael@0 | 376 | } |
michael@0 | 377 | |
michael@0 | 378 | bool |
michael@0 | 379 | nsINode::IsAnonymousContentInSVGUseSubtree() const |
michael@0 | 380 | { |
michael@0 | 381 | MOZ_ASSERT(IsInAnonymousSubtree()); |
michael@0 | 382 | nsIContent* parent = AsContent()->GetBindingParent(); |
michael@0 | 383 | // Watch out for parentless native-anonymous subtrees. |
michael@0 | 384 | return parent && parent->IsSVG(nsGkAtoms::use); |
michael@0 | 385 | } |
michael@0 | 386 | |
michael@0 | 387 | nsresult |
michael@0 | 388 | nsINode::GetParentNode(nsIDOMNode** aParentNode) |
michael@0 | 389 | { |
michael@0 | 390 | *aParentNode = nullptr; |
michael@0 | 391 | |
michael@0 | 392 | nsINode *parent = GetParentNode(); |
michael@0 | 393 | |
michael@0 | 394 | return parent ? CallQueryInterface(parent, aParentNode) : NS_OK; |
michael@0 | 395 | } |
michael@0 | 396 | |
michael@0 | 397 | nsresult |
michael@0 | 398 | nsINode::GetParentElement(nsIDOMElement** aParentElement) |
michael@0 | 399 | { |
michael@0 | 400 | *aParentElement = nullptr; |
michael@0 | 401 | nsINode* parent = GetParentElement(); |
michael@0 | 402 | return parent ? CallQueryInterface(parent, aParentElement) : NS_OK; |
michael@0 | 403 | } |
michael@0 | 404 | |
michael@0 | 405 | nsresult |
michael@0 | 406 | nsINode::GetChildNodes(nsIDOMNodeList** aChildNodes) |
michael@0 | 407 | { |
michael@0 | 408 | NS_ADDREF(*aChildNodes = ChildNodes()); |
michael@0 | 409 | |
michael@0 | 410 | return NS_OK; |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | nsresult |
michael@0 | 414 | nsINode::GetFirstChild(nsIDOMNode** aNode) |
michael@0 | 415 | { |
michael@0 | 416 | nsIContent* child = GetFirstChild(); |
michael@0 | 417 | if (child) { |
michael@0 | 418 | return CallQueryInterface(child, aNode); |
michael@0 | 419 | } |
michael@0 | 420 | |
michael@0 | 421 | *aNode = nullptr; |
michael@0 | 422 | |
michael@0 | 423 | return NS_OK; |
michael@0 | 424 | } |
michael@0 | 425 | |
michael@0 | 426 | nsresult |
michael@0 | 427 | nsINode::GetLastChild(nsIDOMNode** aNode) |
michael@0 | 428 | { |
michael@0 | 429 | nsIContent* child = GetLastChild(); |
michael@0 | 430 | if (child) { |
michael@0 | 431 | return CallQueryInterface(child, aNode); |
michael@0 | 432 | } |
michael@0 | 433 | |
michael@0 | 434 | *aNode = nullptr; |
michael@0 | 435 | |
michael@0 | 436 | return NS_OK; |
michael@0 | 437 | } |
michael@0 | 438 | |
michael@0 | 439 | nsresult |
michael@0 | 440 | nsINode::GetPreviousSibling(nsIDOMNode** aPrevSibling) |
michael@0 | 441 | { |
michael@0 | 442 | *aPrevSibling = nullptr; |
michael@0 | 443 | |
michael@0 | 444 | nsIContent *sibling = GetPreviousSibling(); |
michael@0 | 445 | |
michael@0 | 446 | return sibling ? CallQueryInterface(sibling, aPrevSibling) : NS_OK; |
michael@0 | 447 | } |
michael@0 | 448 | |
michael@0 | 449 | nsresult |
michael@0 | 450 | nsINode::GetNextSibling(nsIDOMNode** aNextSibling) |
michael@0 | 451 | { |
michael@0 | 452 | *aNextSibling = nullptr; |
michael@0 | 453 | |
michael@0 | 454 | nsIContent *sibling = GetNextSibling(); |
michael@0 | 455 | |
michael@0 | 456 | return sibling ? CallQueryInterface(sibling, aNextSibling) : NS_OK; |
michael@0 | 457 | } |
michael@0 | 458 | |
michael@0 | 459 | nsresult |
michael@0 | 460 | nsINode::GetOwnerDocument(nsIDOMDocument** aOwnerDocument) |
michael@0 | 461 | { |
michael@0 | 462 | *aOwnerDocument = nullptr; |
michael@0 | 463 | |
michael@0 | 464 | nsIDocument *ownerDoc = GetOwnerDocument(); |
michael@0 | 465 | |
michael@0 | 466 | return ownerDoc ? CallQueryInterface(ownerDoc, aOwnerDocument) : NS_OK; |
michael@0 | 467 | } |
michael@0 | 468 | |
michael@0 | 469 | void |
michael@0 | 470 | nsINode::GetNodeValueInternal(nsAString& aNodeValue) |
michael@0 | 471 | { |
michael@0 | 472 | SetDOMStringToNull(aNodeValue); |
michael@0 | 473 | } |
michael@0 | 474 | |
michael@0 | 475 | nsINode* |
michael@0 | 476 | nsINode::RemoveChild(nsINode& aOldChild, ErrorResult& aError) |
michael@0 | 477 | { |
michael@0 | 478 | if (IsNodeOfType(eDATA_NODE)) { |
michael@0 | 479 | // aOldChild can't be one of our children. |
michael@0 | 480 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
michael@0 | 481 | return nullptr; |
michael@0 | 482 | } |
michael@0 | 483 | |
michael@0 | 484 | if (aOldChild.GetParentNode() == this) { |
michael@0 | 485 | nsContentUtils::MaybeFireNodeRemoved(&aOldChild, this, OwnerDoc()); |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | int32_t index = IndexOf(&aOldChild); |
michael@0 | 489 | if (index == -1) { |
michael@0 | 490 | // aOldChild isn't one of our children. |
michael@0 | 491 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
michael@0 | 492 | return nullptr; |
michael@0 | 493 | } |
michael@0 | 494 | |
michael@0 | 495 | RemoveChildAt(index, true); |
michael@0 | 496 | return &aOldChild; |
michael@0 | 497 | } |
michael@0 | 498 | |
michael@0 | 499 | nsresult |
michael@0 | 500 | nsINode::RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn) |
michael@0 | 501 | { |
michael@0 | 502 | nsCOMPtr<nsINode> oldChild = do_QueryInterface(aOldChild); |
michael@0 | 503 | if (!oldChild) { |
michael@0 | 504 | return NS_ERROR_NULL_POINTER; |
michael@0 | 505 | } |
michael@0 | 506 | |
michael@0 | 507 | ErrorResult rv; |
michael@0 | 508 | RemoveChild(*oldChild, rv); |
michael@0 | 509 | if (!rv.Failed()) { |
michael@0 | 510 | NS_ADDREF(*aReturn = aOldChild); |
michael@0 | 511 | } |
michael@0 | 512 | return rv.ErrorCode(); |
michael@0 | 513 | } |
michael@0 | 514 | |
michael@0 | 515 | void |
michael@0 | 516 | nsINode::Normalize() |
michael@0 | 517 | { |
michael@0 | 518 | // First collect list of nodes to be removed |
michael@0 | 519 | nsAutoTArray<nsCOMPtr<nsIContent>, 50> nodes; |
michael@0 | 520 | |
michael@0 | 521 | bool canMerge = false; |
michael@0 | 522 | for (nsIContent* node = this->GetFirstChild(); |
michael@0 | 523 | node; |
michael@0 | 524 | node = node->GetNextNode(this)) { |
michael@0 | 525 | if (node->NodeType() != nsIDOMNode::TEXT_NODE) { |
michael@0 | 526 | canMerge = false; |
michael@0 | 527 | continue; |
michael@0 | 528 | } |
michael@0 | 529 | |
michael@0 | 530 | if (canMerge || node->TextLength() == 0) { |
michael@0 | 531 | // No need to touch canMerge. That way we can merge across empty |
michael@0 | 532 | // textnodes if and only if the node before is a textnode |
michael@0 | 533 | nodes.AppendElement(node); |
michael@0 | 534 | } |
michael@0 | 535 | else { |
michael@0 | 536 | canMerge = true; |
michael@0 | 537 | } |
michael@0 | 538 | |
michael@0 | 539 | // If there's no following sibling, then we need to ensure that we don't |
michael@0 | 540 | // collect following siblings of our (grand)parent as to-be-removed |
michael@0 | 541 | canMerge = canMerge && !!node->GetNextSibling(); |
michael@0 | 542 | } |
michael@0 | 543 | |
michael@0 | 544 | if (nodes.IsEmpty()) { |
michael@0 | 545 | return; |
michael@0 | 546 | } |
michael@0 | 547 | |
michael@0 | 548 | // We're relying on mozAutoSubtreeModified to keep the doc alive here. |
michael@0 | 549 | nsIDocument* doc = OwnerDoc(); |
michael@0 | 550 | |
michael@0 | 551 | // Batch possible DOMSubtreeModified events. |
michael@0 | 552 | mozAutoSubtreeModified subtree(doc, nullptr); |
michael@0 | 553 | |
michael@0 | 554 | // Fire all DOMNodeRemoved events. Optimize the common case of there being |
michael@0 | 555 | // no listeners |
michael@0 | 556 | bool hasRemoveListeners = nsContentUtils:: |
michael@0 | 557 | HasMutationListeners(doc, NS_EVENT_BITS_MUTATION_NODEREMOVED); |
michael@0 | 558 | if (hasRemoveListeners) { |
michael@0 | 559 | for (uint32_t i = 0; i < nodes.Length(); ++i) { |
michael@0 | 560 | nsINode* parentNode = nodes[i]->GetParentNode(); |
michael@0 | 561 | if (parentNode) { // Node may have already been removed. |
michael@0 | 562 | nsContentUtils::MaybeFireNodeRemoved(nodes[i], parentNode, |
michael@0 | 563 | doc); |
michael@0 | 564 | } |
michael@0 | 565 | } |
michael@0 | 566 | } |
michael@0 | 567 | |
michael@0 | 568 | mozAutoDocUpdate batch(doc, UPDATE_CONTENT_MODEL, true); |
michael@0 | 569 | |
michael@0 | 570 | // Merge and remove all nodes |
michael@0 | 571 | nsAutoString tmpStr; |
michael@0 | 572 | for (uint32_t i = 0; i < nodes.Length(); ++i) { |
michael@0 | 573 | nsIContent* node = nodes[i]; |
michael@0 | 574 | // Merge with previous node unless empty |
michael@0 | 575 | const nsTextFragment* text = node->GetText(); |
michael@0 | 576 | if (text->GetLength()) { |
michael@0 | 577 | nsIContent* target = node->GetPreviousSibling(); |
michael@0 | 578 | NS_ASSERTION((target && target->NodeType() == nsIDOMNode::TEXT_NODE) || |
michael@0 | 579 | hasRemoveListeners, |
michael@0 | 580 | "Should always have a previous text sibling unless " |
michael@0 | 581 | "mutation events messed us up"); |
michael@0 | 582 | if (!hasRemoveListeners || |
michael@0 | 583 | (target && target->NodeType() == nsIDOMNode::TEXT_NODE)) { |
michael@0 | 584 | nsTextNode* t = static_cast<nsTextNode*>(target); |
michael@0 | 585 | if (text->Is2b()) { |
michael@0 | 586 | t->AppendTextForNormalize(text->Get2b(), text->GetLength(), true, node); |
michael@0 | 587 | } |
michael@0 | 588 | else { |
michael@0 | 589 | tmpStr.Truncate(); |
michael@0 | 590 | text->AppendTo(tmpStr); |
michael@0 | 591 | t->AppendTextForNormalize(tmpStr.get(), tmpStr.Length(), true, node); |
michael@0 | 592 | } |
michael@0 | 593 | } |
michael@0 | 594 | } |
michael@0 | 595 | |
michael@0 | 596 | // Remove node |
michael@0 | 597 | nsCOMPtr<nsINode> parent = node->GetParentNode(); |
michael@0 | 598 | NS_ASSERTION(parent || hasRemoveListeners, |
michael@0 | 599 | "Should always have a parent unless " |
michael@0 | 600 | "mutation events messed us up"); |
michael@0 | 601 | if (parent) { |
michael@0 | 602 | parent->RemoveChildAt(parent->IndexOf(node), true); |
michael@0 | 603 | } |
michael@0 | 604 | } |
michael@0 | 605 | } |
michael@0 | 606 | |
michael@0 | 607 | void |
michael@0 | 608 | nsINode::GetBaseURI(nsAString &aURI) const |
michael@0 | 609 | { |
michael@0 | 610 | nsCOMPtr<nsIURI> baseURI = GetBaseURI(); |
michael@0 | 611 | |
michael@0 | 612 | nsAutoCString spec; |
michael@0 | 613 | if (baseURI) { |
michael@0 | 614 | baseURI->GetSpec(spec); |
michael@0 | 615 | } |
michael@0 | 616 | |
michael@0 | 617 | CopyUTF8toUTF16(spec, aURI); |
michael@0 | 618 | } |
michael@0 | 619 | |
michael@0 | 620 | void |
michael@0 | 621 | nsINode::GetBaseURIFromJS(nsAString& aURI) const |
michael@0 | 622 | { |
michael@0 | 623 | nsCOMPtr<nsIURI> baseURI = GetBaseURI(nsContentUtils::IsCallerChrome()); |
michael@0 | 624 | nsAutoCString spec; |
michael@0 | 625 | if (baseURI) { |
michael@0 | 626 | baseURI->GetSpec(spec); |
michael@0 | 627 | } |
michael@0 | 628 | CopyUTF8toUTF16(spec, aURI); |
michael@0 | 629 | } |
michael@0 | 630 | |
michael@0 | 631 | already_AddRefed<nsIURI> |
michael@0 | 632 | nsINode::GetBaseURIObject() const |
michael@0 | 633 | { |
michael@0 | 634 | return GetBaseURI(true); |
michael@0 | 635 | } |
michael@0 | 636 | |
michael@0 | 637 | void |
michael@0 | 638 | nsINode::LookupPrefix(const nsAString& aNamespaceURI, nsAString& aPrefix) |
michael@0 | 639 | { |
michael@0 | 640 | Element *element = GetNameSpaceElement(); |
michael@0 | 641 | if (element) { |
michael@0 | 642 | // XXX Waiting for DOM spec to list error codes. |
michael@0 | 643 | |
michael@0 | 644 | // Trace up the content parent chain looking for the namespace |
michael@0 | 645 | // declaration that defines the aNamespaceURI namespace. Once found, |
michael@0 | 646 | // return the prefix (i.e. the attribute localName). |
michael@0 | 647 | for (nsIContent* content = element; content; |
michael@0 | 648 | content = content->GetParent()) { |
michael@0 | 649 | uint32_t attrCount = content->GetAttrCount(); |
michael@0 | 650 | |
michael@0 | 651 | for (uint32_t i = 0; i < attrCount; ++i) { |
michael@0 | 652 | const nsAttrName* name = content->GetAttrNameAt(i); |
michael@0 | 653 | |
michael@0 | 654 | if (name->NamespaceEquals(kNameSpaceID_XMLNS) && |
michael@0 | 655 | content->AttrValueIs(kNameSpaceID_XMLNS, name->LocalName(), |
michael@0 | 656 | aNamespaceURI, eCaseMatters)) { |
michael@0 | 657 | // If the localName is "xmlns", the prefix we output should be |
michael@0 | 658 | // null. |
michael@0 | 659 | nsIAtom *localName = name->LocalName(); |
michael@0 | 660 | |
michael@0 | 661 | if (localName != nsGkAtoms::xmlns) { |
michael@0 | 662 | localName->ToString(aPrefix); |
michael@0 | 663 | } |
michael@0 | 664 | else { |
michael@0 | 665 | SetDOMStringToNull(aPrefix); |
michael@0 | 666 | } |
michael@0 | 667 | return; |
michael@0 | 668 | } |
michael@0 | 669 | } |
michael@0 | 670 | } |
michael@0 | 671 | } |
michael@0 | 672 | |
michael@0 | 673 | SetDOMStringToNull(aPrefix); |
michael@0 | 674 | } |
michael@0 | 675 | |
michael@0 | 676 | static nsresult |
michael@0 | 677 | SetUserDataProperty(uint16_t aCategory, nsINode *aNode, nsIAtom *aKey, |
michael@0 | 678 | nsISupports* aValue, void** aOldValue) |
michael@0 | 679 | { |
michael@0 | 680 | nsresult rv = aNode->SetProperty(aCategory, aKey, aValue, |
michael@0 | 681 | nsPropertyTable::SupportsDtorFunc, true, |
michael@0 | 682 | aOldValue); |
michael@0 | 683 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 684 | |
michael@0 | 685 | // Property table owns it now. |
michael@0 | 686 | NS_ADDREF(aValue); |
michael@0 | 687 | |
michael@0 | 688 | return NS_OK; |
michael@0 | 689 | } |
michael@0 | 690 | |
michael@0 | 691 | nsresult |
michael@0 | 692 | nsINode::SetUserData(const nsAString &aKey, nsIVariant *aData, |
michael@0 | 693 | nsIDOMUserDataHandler *aHandler, nsIVariant **aResult) |
michael@0 | 694 | { |
michael@0 | 695 | OwnerDoc()->WarnOnceAbout(nsIDocument::eGetSetUserData); |
michael@0 | 696 | *aResult = nullptr; |
michael@0 | 697 | |
michael@0 | 698 | nsCOMPtr<nsIAtom> key = do_GetAtom(aKey); |
michael@0 | 699 | if (!key) { |
michael@0 | 700 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 701 | } |
michael@0 | 702 | |
michael@0 | 703 | nsresult rv; |
michael@0 | 704 | void *data; |
michael@0 | 705 | if (aData) { |
michael@0 | 706 | rv = SetUserDataProperty(DOM_USER_DATA, this, key, aData, &data); |
michael@0 | 707 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 708 | } |
michael@0 | 709 | else { |
michael@0 | 710 | data = UnsetProperty(DOM_USER_DATA, key); |
michael@0 | 711 | } |
michael@0 | 712 | |
michael@0 | 713 | // Take over ownership of the old data from the property table. |
michael@0 | 714 | nsCOMPtr<nsIVariant> oldData = dont_AddRef(static_cast<nsIVariant*>(data)); |
michael@0 | 715 | |
michael@0 | 716 | if (aData && aHandler) { |
michael@0 | 717 | nsCOMPtr<nsIDOMUserDataHandler> oldHandler; |
michael@0 | 718 | rv = SetUserDataProperty(DOM_USER_DATA_HANDLER, this, key, aHandler, |
michael@0 | 719 | getter_AddRefs(oldHandler)); |
michael@0 | 720 | if (NS_FAILED(rv)) { |
michael@0 | 721 | // We failed to set the handler, remove the data. |
michael@0 | 722 | DeleteProperty(DOM_USER_DATA, key); |
michael@0 | 723 | |
michael@0 | 724 | return rv; |
michael@0 | 725 | } |
michael@0 | 726 | } |
michael@0 | 727 | else { |
michael@0 | 728 | DeleteProperty(DOM_USER_DATA_HANDLER, key); |
michael@0 | 729 | } |
michael@0 | 730 | |
michael@0 | 731 | oldData.swap(*aResult); |
michael@0 | 732 | |
michael@0 | 733 | return NS_OK; |
michael@0 | 734 | } |
michael@0 | 735 | |
michael@0 | 736 | void |
michael@0 | 737 | nsINode::SetUserData(JSContext* aCx, const nsAString& aKey, |
michael@0 | 738 | JS::Handle<JS::Value> aData, |
michael@0 | 739 | nsIDOMUserDataHandler* aHandler, |
michael@0 | 740 | JS::MutableHandle<JS::Value> aRetval, |
michael@0 | 741 | ErrorResult& aError) |
michael@0 | 742 | { |
michael@0 | 743 | nsCOMPtr<nsIVariant> data; |
michael@0 | 744 | aError = nsContentUtils::XPConnect()->JSValToVariant(aCx, aData, getter_AddRefs(data)); |
michael@0 | 745 | if (aError.Failed()) { |
michael@0 | 746 | return; |
michael@0 | 747 | } |
michael@0 | 748 | |
michael@0 | 749 | nsCOMPtr<nsIVariant> oldData; |
michael@0 | 750 | aError = SetUserData(aKey, data, aHandler, getter_AddRefs(oldData)); |
michael@0 | 751 | if (aError.Failed()) { |
michael@0 | 752 | return; |
michael@0 | 753 | } |
michael@0 | 754 | |
michael@0 | 755 | if (!oldData) { |
michael@0 | 756 | aRetval.setNull(); |
michael@0 | 757 | return; |
michael@0 | 758 | } |
michael@0 | 759 | |
michael@0 | 760 | JSAutoCompartment ac(aCx, GetWrapper()); |
michael@0 | 761 | aError = nsContentUtils::XPConnect()->VariantToJS(aCx, GetWrapper(), oldData, |
michael@0 | 762 | aRetval); |
michael@0 | 763 | } |
michael@0 | 764 | |
michael@0 | 765 | nsIVariant* |
michael@0 | 766 | nsINode::GetUserData(const nsAString& aKey) |
michael@0 | 767 | { |
michael@0 | 768 | OwnerDoc()->WarnOnceAbout(nsIDocument::eGetSetUserData); |
michael@0 | 769 | nsCOMPtr<nsIAtom> key = do_GetAtom(aKey); |
michael@0 | 770 | if (!key) { |
michael@0 | 771 | return nullptr; |
michael@0 | 772 | } |
michael@0 | 773 | |
michael@0 | 774 | return static_cast<nsIVariant*>(GetProperty(DOM_USER_DATA, key)); |
michael@0 | 775 | } |
michael@0 | 776 | |
michael@0 | 777 | void |
michael@0 | 778 | nsINode::GetUserData(JSContext* aCx, const nsAString& aKey, |
michael@0 | 779 | JS::MutableHandle<JS::Value> aRetval, ErrorResult& aError) |
michael@0 | 780 | { |
michael@0 | 781 | nsIVariant* data = GetUserData(aKey); |
michael@0 | 782 | if (!data) { |
michael@0 | 783 | aRetval.setNull(); |
michael@0 | 784 | return; |
michael@0 | 785 | } |
michael@0 | 786 | |
michael@0 | 787 | JSAutoCompartment ac(aCx, GetWrapper()); |
michael@0 | 788 | aError = nsContentUtils::XPConnect()->VariantToJS(aCx, GetWrapper(), data, |
michael@0 | 789 | aRetval); |
michael@0 | 790 | } |
michael@0 | 791 | |
michael@0 | 792 | uint16_t |
michael@0 | 793 | nsINode::CompareDocumentPosition(nsINode& aOtherNode) const |
michael@0 | 794 | { |
michael@0 | 795 | if (this == &aOtherNode) { |
michael@0 | 796 | return 0; |
michael@0 | 797 | } |
michael@0 | 798 | if (GetPreviousSibling() == &aOtherNode) { |
michael@0 | 799 | MOZ_ASSERT(GetParentNode() == aOtherNode.GetParentNode()); |
michael@0 | 800 | return static_cast<uint16_t>(nsIDOMNode::DOCUMENT_POSITION_PRECEDING); |
michael@0 | 801 | } |
michael@0 | 802 | if (GetNextSibling() == &aOtherNode) { |
michael@0 | 803 | MOZ_ASSERT(GetParentNode() == aOtherNode.GetParentNode()); |
michael@0 | 804 | return static_cast<uint16_t>(nsIDOMNode::DOCUMENT_POSITION_FOLLOWING); |
michael@0 | 805 | } |
michael@0 | 806 | |
michael@0 | 807 | nsAutoTArray<const nsINode*, 32> parents1, parents2; |
michael@0 | 808 | |
michael@0 | 809 | const nsINode *node1 = &aOtherNode, *node2 = this; |
michael@0 | 810 | |
michael@0 | 811 | // Check if either node is an attribute |
michael@0 | 812 | const Attr* attr1 = nullptr; |
michael@0 | 813 | if (node1->IsNodeOfType(nsINode::eATTRIBUTE)) { |
michael@0 | 814 | attr1 = static_cast<const Attr*>(node1); |
michael@0 | 815 | const Element* elem = attr1->GetElement(); |
michael@0 | 816 | // If there is an owner element add the attribute |
michael@0 | 817 | // to the chain and walk up to the element |
michael@0 | 818 | if (elem) { |
michael@0 | 819 | node1 = elem; |
michael@0 | 820 | parents1.AppendElement(attr1); |
michael@0 | 821 | } |
michael@0 | 822 | } |
michael@0 | 823 | if (node2->IsNodeOfType(nsINode::eATTRIBUTE)) { |
michael@0 | 824 | const Attr* attr2 = static_cast<const Attr*>(node2); |
michael@0 | 825 | const Element* elem = attr2->GetElement(); |
michael@0 | 826 | if (elem == node1 && attr1) { |
michael@0 | 827 | // Both nodes are attributes on the same element. |
michael@0 | 828 | // Compare position between the attributes. |
michael@0 | 829 | |
michael@0 | 830 | uint32_t i; |
michael@0 | 831 | const nsAttrName* attrName; |
michael@0 | 832 | for (i = 0; (attrName = elem->GetAttrNameAt(i)); ++i) { |
michael@0 | 833 | if (attrName->Equals(attr1->NodeInfo())) { |
michael@0 | 834 | NS_ASSERTION(!attrName->Equals(attr2->NodeInfo()), |
michael@0 | 835 | "Different attrs at same position"); |
michael@0 | 836 | return nsIDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | |
michael@0 | 837 | nsIDOMNode::DOCUMENT_POSITION_PRECEDING; |
michael@0 | 838 | } |
michael@0 | 839 | if (attrName->Equals(attr2->NodeInfo())) { |
michael@0 | 840 | return nsIDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | |
michael@0 | 841 | nsIDOMNode::DOCUMENT_POSITION_FOLLOWING; |
michael@0 | 842 | } |
michael@0 | 843 | } |
michael@0 | 844 | NS_NOTREACHED("neither attribute in the element"); |
michael@0 | 845 | return nsIDOMNode::DOCUMENT_POSITION_DISCONNECTED; |
michael@0 | 846 | } |
michael@0 | 847 | |
michael@0 | 848 | if (elem) { |
michael@0 | 849 | node2 = elem; |
michael@0 | 850 | parents2.AppendElement(attr2); |
michael@0 | 851 | } |
michael@0 | 852 | } |
michael@0 | 853 | |
michael@0 | 854 | // We now know that both nodes are either nsIContents or nsIDocuments. |
michael@0 | 855 | // If either node started out as an attribute, that attribute will have |
michael@0 | 856 | // the same relative position as its ownerElement, except if the |
michael@0 | 857 | // ownerElement ends up being the container for the other node |
michael@0 | 858 | |
michael@0 | 859 | // Build the chain of parents |
michael@0 | 860 | do { |
michael@0 | 861 | parents1.AppendElement(node1); |
michael@0 | 862 | node1 = node1->GetParentNode(); |
michael@0 | 863 | } while (node1); |
michael@0 | 864 | do { |
michael@0 | 865 | parents2.AppendElement(node2); |
michael@0 | 866 | node2 = node2->GetParentNode(); |
michael@0 | 867 | } while (node2); |
michael@0 | 868 | |
michael@0 | 869 | // Check if the nodes are disconnected. |
michael@0 | 870 | uint32_t pos1 = parents1.Length(); |
michael@0 | 871 | uint32_t pos2 = parents2.Length(); |
michael@0 | 872 | const nsINode* top1 = parents1.ElementAt(--pos1); |
michael@0 | 873 | const nsINode* top2 = parents2.ElementAt(--pos2); |
michael@0 | 874 | if (top1 != top2) { |
michael@0 | 875 | return top1 < top2 ? |
michael@0 | 876 | (nsIDOMNode::DOCUMENT_POSITION_PRECEDING | |
michael@0 | 877 | nsIDOMNode::DOCUMENT_POSITION_DISCONNECTED | |
michael@0 | 878 | nsIDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC) : |
michael@0 | 879 | (nsIDOMNode::DOCUMENT_POSITION_FOLLOWING | |
michael@0 | 880 | nsIDOMNode::DOCUMENT_POSITION_DISCONNECTED | |
michael@0 | 881 | nsIDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); |
michael@0 | 882 | } |
michael@0 | 883 | |
michael@0 | 884 | // Find where the parent chain differs and check indices in the parent. |
michael@0 | 885 | const nsINode* parent = top1; |
michael@0 | 886 | uint32_t len; |
michael@0 | 887 | for (len = std::min(pos1, pos2); len > 0; --len) { |
michael@0 | 888 | const nsINode* child1 = parents1.ElementAt(--pos1); |
michael@0 | 889 | const nsINode* child2 = parents2.ElementAt(--pos2); |
michael@0 | 890 | if (child1 != child2) { |
michael@0 | 891 | // child1 or child2 can be an attribute here. This will work fine since |
michael@0 | 892 | // IndexOf will return -1 for the attribute making the attribute be |
michael@0 | 893 | // considered before any child. |
michael@0 | 894 | return parent->IndexOf(child1) < parent->IndexOf(child2) ? |
michael@0 | 895 | static_cast<uint16_t>(nsIDOMNode::DOCUMENT_POSITION_PRECEDING) : |
michael@0 | 896 | static_cast<uint16_t>(nsIDOMNode::DOCUMENT_POSITION_FOLLOWING); |
michael@0 | 897 | } |
michael@0 | 898 | parent = child1; |
michael@0 | 899 | } |
michael@0 | 900 | |
michael@0 | 901 | // We hit the end of one of the parent chains without finding a difference |
michael@0 | 902 | // between the chains. That must mean that one node is an ancestor of the |
michael@0 | 903 | // other. The one with the shortest chain must be the ancestor. |
michael@0 | 904 | return pos1 < pos2 ? |
michael@0 | 905 | (nsIDOMNode::DOCUMENT_POSITION_PRECEDING | |
michael@0 | 906 | nsIDOMNode::DOCUMENT_POSITION_CONTAINS) : |
michael@0 | 907 | (nsIDOMNode::DOCUMENT_POSITION_FOLLOWING | |
michael@0 | 908 | nsIDOMNode::DOCUMENT_POSITION_CONTAINED_BY); |
michael@0 | 909 | } |
michael@0 | 910 | |
michael@0 | 911 | bool |
michael@0 | 912 | nsINode::IsEqualNode(nsINode* aOther) |
michael@0 | 913 | { |
michael@0 | 914 | if (!aOther) { |
michael@0 | 915 | return false; |
michael@0 | 916 | } |
michael@0 | 917 | |
michael@0 | 918 | nsAutoString string1, string2; |
michael@0 | 919 | |
michael@0 | 920 | nsINode* node1 = this; |
michael@0 | 921 | nsINode* node2 = aOther; |
michael@0 | 922 | do { |
michael@0 | 923 | uint16_t nodeType = node1->NodeType(); |
michael@0 | 924 | if (nodeType != node2->NodeType()) { |
michael@0 | 925 | return false; |
michael@0 | 926 | } |
michael@0 | 927 | |
michael@0 | 928 | nsINodeInfo* nodeInfo1 = node1->mNodeInfo; |
michael@0 | 929 | nsINodeInfo* nodeInfo2 = node2->mNodeInfo; |
michael@0 | 930 | if (!nodeInfo1->Equals(nodeInfo2) || |
michael@0 | 931 | nodeInfo1->GetExtraName() != nodeInfo2->GetExtraName()) { |
michael@0 | 932 | return false; |
michael@0 | 933 | } |
michael@0 | 934 | |
michael@0 | 935 | switch(nodeType) { |
michael@0 | 936 | case nsIDOMNode::ELEMENT_NODE: |
michael@0 | 937 | { |
michael@0 | 938 | // Both are elements (we checked that their nodeinfos are equal). Do the |
michael@0 | 939 | // check on attributes. |
michael@0 | 940 | Element* element1 = node1->AsElement(); |
michael@0 | 941 | Element* element2 = node2->AsElement(); |
michael@0 | 942 | uint32_t attrCount = element1->GetAttrCount(); |
michael@0 | 943 | if (attrCount != element2->GetAttrCount()) { |
michael@0 | 944 | return false; |
michael@0 | 945 | } |
michael@0 | 946 | |
michael@0 | 947 | // Iterate over attributes. |
michael@0 | 948 | for (uint32_t i = 0; i < attrCount; ++i) { |
michael@0 | 949 | const nsAttrName* attrName = element1->GetAttrNameAt(i); |
michael@0 | 950 | #ifdef DEBUG |
michael@0 | 951 | bool hasAttr = |
michael@0 | 952 | #endif |
michael@0 | 953 | element1->GetAttr(attrName->NamespaceID(), attrName->LocalName(), |
michael@0 | 954 | string1); |
michael@0 | 955 | NS_ASSERTION(hasAttr, "Why don't we have an attr?"); |
michael@0 | 956 | |
michael@0 | 957 | if (!element2->AttrValueIs(attrName->NamespaceID(), |
michael@0 | 958 | attrName->LocalName(), |
michael@0 | 959 | string1, |
michael@0 | 960 | eCaseMatters)) { |
michael@0 | 961 | return false; |
michael@0 | 962 | } |
michael@0 | 963 | } |
michael@0 | 964 | break; |
michael@0 | 965 | } |
michael@0 | 966 | case nsIDOMNode::TEXT_NODE: |
michael@0 | 967 | case nsIDOMNode::COMMENT_NODE: |
michael@0 | 968 | case nsIDOMNode::CDATA_SECTION_NODE: |
michael@0 | 969 | case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: |
michael@0 | 970 | { |
michael@0 | 971 | string1.Truncate(); |
michael@0 | 972 | static_cast<nsIContent*>(node1)->AppendTextTo(string1); |
michael@0 | 973 | string2.Truncate(); |
michael@0 | 974 | static_cast<nsIContent*>(node2)->AppendTextTo(string2); |
michael@0 | 975 | |
michael@0 | 976 | if (!string1.Equals(string2)) { |
michael@0 | 977 | return false; |
michael@0 | 978 | } |
michael@0 | 979 | |
michael@0 | 980 | break; |
michael@0 | 981 | } |
michael@0 | 982 | case nsIDOMNode::DOCUMENT_NODE: |
michael@0 | 983 | case nsIDOMNode::DOCUMENT_FRAGMENT_NODE: |
michael@0 | 984 | break; |
michael@0 | 985 | case nsIDOMNode::ATTRIBUTE_NODE: |
michael@0 | 986 | { |
michael@0 | 987 | NS_ASSERTION(node1 == this && node2 == aOther, |
michael@0 | 988 | "Did we come upon an attribute node while walking a " |
michael@0 | 989 | "subtree?"); |
michael@0 | 990 | node1->GetNodeValue(string1); |
michael@0 | 991 | node2->GetNodeValue(string2); |
michael@0 | 992 | |
michael@0 | 993 | // Returning here as to not bother walking subtree. And there is no |
michael@0 | 994 | // risk that we're half way through walking some other subtree since |
michael@0 | 995 | // attribute nodes doesn't appear in subtrees. |
michael@0 | 996 | return string1.Equals(string2); |
michael@0 | 997 | } |
michael@0 | 998 | case nsIDOMNode::DOCUMENT_TYPE_NODE: |
michael@0 | 999 | { |
michael@0 | 1000 | nsCOMPtr<nsIDOMDocumentType> docType1 = do_QueryInterface(node1); |
michael@0 | 1001 | nsCOMPtr<nsIDOMDocumentType> docType2 = do_QueryInterface(node2); |
michael@0 | 1002 | |
michael@0 | 1003 | NS_ASSERTION(docType1 && docType2, "Why don't we have a document type node?"); |
michael@0 | 1004 | |
michael@0 | 1005 | // Public ID |
michael@0 | 1006 | docType1->GetPublicId(string1); |
michael@0 | 1007 | docType2->GetPublicId(string2); |
michael@0 | 1008 | if (!string1.Equals(string2)) { |
michael@0 | 1009 | return false; |
michael@0 | 1010 | } |
michael@0 | 1011 | |
michael@0 | 1012 | // System ID |
michael@0 | 1013 | docType1->GetSystemId(string1); |
michael@0 | 1014 | docType2->GetSystemId(string2); |
michael@0 | 1015 | if (!string1.Equals(string2)) { |
michael@0 | 1016 | return false; |
michael@0 | 1017 | } |
michael@0 | 1018 | |
michael@0 | 1019 | // Internal subset |
michael@0 | 1020 | docType1->GetInternalSubset(string1); |
michael@0 | 1021 | docType2->GetInternalSubset(string2); |
michael@0 | 1022 | if (!string1.Equals(string2)) { |
michael@0 | 1023 | return false; |
michael@0 | 1024 | } |
michael@0 | 1025 | |
michael@0 | 1026 | break; |
michael@0 | 1027 | } |
michael@0 | 1028 | default: |
michael@0 | 1029 | NS_ABORT_IF_FALSE(false, "Unknown node type"); |
michael@0 | 1030 | } |
michael@0 | 1031 | |
michael@0 | 1032 | nsINode* nextNode = node1->GetFirstChild(); |
michael@0 | 1033 | if (nextNode) { |
michael@0 | 1034 | node1 = nextNode; |
michael@0 | 1035 | node2 = node2->GetFirstChild(); |
michael@0 | 1036 | } |
michael@0 | 1037 | else { |
michael@0 | 1038 | if (node2->GetFirstChild()) { |
michael@0 | 1039 | // node2 has a firstChild, but node1 doesn't |
michael@0 | 1040 | return false; |
michael@0 | 1041 | } |
michael@0 | 1042 | |
michael@0 | 1043 | // Find next sibling, possibly walking parent chain. |
michael@0 | 1044 | while (1) { |
michael@0 | 1045 | if (node1 == this) { |
michael@0 | 1046 | NS_ASSERTION(node2 == aOther, "Should have reached the start node " |
michael@0 | 1047 | "for both trees at the same time"); |
michael@0 | 1048 | return true; |
michael@0 | 1049 | } |
michael@0 | 1050 | |
michael@0 | 1051 | nextNode = node1->GetNextSibling(); |
michael@0 | 1052 | if (nextNode) { |
michael@0 | 1053 | node1 = nextNode; |
michael@0 | 1054 | node2 = node2->GetNextSibling(); |
michael@0 | 1055 | break; |
michael@0 | 1056 | } |
michael@0 | 1057 | |
michael@0 | 1058 | if (node2->GetNextSibling()) { |
michael@0 | 1059 | // node2 has a nextSibling, but node1 doesn't |
michael@0 | 1060 | return false; |
michael@0 | 1061 | } |
michael@0 | 1062 | |
michael@0 | 1063 | node1 = node1->GetParentNode(); |
michael@0 | 1064 | node2 = node2->GetParentNode(); |
michael@0 | 1065 | NS_ASSERTION(node1 && node2, "no parent while walking subtree"); |
michael@0 | 1066 | } |
michael@0 | 1067 | } |
michael@0 | 1068 | } while(node2); |
michael@0 | 1069 | |
michael@0 | 1070 | return false; |
michael@0 | 1071 | } |
michael@0 | 1072 | |
michael@0 | 1073 | void |
michael@0 | 1074 | nsINode::LookupNamespaceURI(const nsAString& aNamespacePrefix, |
michael@0 | 1075 | nsAString& aNamespaceURI) |
michael@0 | 1076 | { |
michael@0 | 1077 | Element *element = GetNameSpaceElement(); |
michael@0 | 1078 | if (!element || |
michael@0 | 1079 | NS_FAILED(element->LookupNamespaceURIInternal(aNamespacePrefix, |
michael@0 | 1080 | aNamespaceURI))) { |
michael@0 | 1081 | SetDOMStringToNull(aNamespaceURI); |
michael@0 | 1082 | } |
michael@0 | 1083 | } |
michael@0 | 1084 | |
michael@0 | 1085 | NS_IMPL_DOMTARGET_DEFAULTS(nsINode) |
michael@0 | 1086 | |
michael@0 | 1087 | NS_IMETHODIMP |
michael@0 | 1088 | nsINode::AddEventListener(const nsAString& aType, |
michael@0 | 1089 | nsIDOMEventListener *aListener, |
michael@0 | 1090 | bool aUseCapture, |
michael@0 | 1091 | bool aWantsUntrusted, |
michael@0 | 1092 | uint8_t aOptionalArgc) |
michael@0 | 1093 | { |
michael@0 | 1094 | NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1, |
michael@0 | 1095 | "Won't check if this is chrome, you want to set " |
michael@0 | 1096 | "aWantsUntrusted to false or make the aWantsUntrusted " |
michael@0 | 1097 | "explicit by making aOptionalArgc non-zero."); |
michael@0 | 1098 | |
michael@0 | 1099 | if (!aWantsUntrusted && |
michael@0 | 1100 | (aOptionalArgc < 2 && |
michael@0 | 1101 | !nsContentUtils::IsChromeDoc(OwnerDoc()))) { |
michael@0 | 1102 | aWantsUntrusted = true; |
michael@0 | 1103 | } |
michael@0 | 1104 | |
michael@0 | 1105 | EventListenerManager* listener_manager = GetOrCreateListenerManager(); |
michael@0 | 1106 | NS_ENSURE_STATE(listener_manager); |
michael@0 | 1107 | listener_manager->AddEventListener(aType, aListener, aUseCapture, |
michael@0 | 1108 | aWantsUntrusted); |
michael@0 | 1109 | return NS_OK; |
michael@0 | 1110 | } |
michael@0 | 1111 | |
michael@0 | 1112 | void |
michael@0 | 1113 | nsINode::AddEventListener(const nsAString& aType, |
michael@0 | 1114 | EventListener* aListener, |
michael@0 | 1115 | bool aUseCapture, |
michael@0 | 1116 | const Nullable<bool>& aWantsUntrusted, |
michael@0 | 1117 | ErrorResult& aRv) |
michael@0 | 1118 | { |
michael@0 | 1119 | bool wantsUntrusted; |
michael@0 | 1120 | if (aWantsUntrusted.IsNull()) { |
michael@0 | 1121 | wantsUntrusted = !nsContentUtils::IsChromeDoc(OwnerDoc()); |
michael@0 | 1122 | } else { |
michael@0 | 1123 | wantsUntrusted = aWantsUntrusted.Value(); |
michael@0 | 1124 | } |
michael@0 | 1125 | |
michael@0 | 1126 | EventListenerManager* listener_manager = GetOrCreateListenerManager(); |
michael@0 | 1127 | if (!listener_manager) { |
michael@0 | 1128 | aRv.Throw(NS_ERROR_UNEXPECTED); |
michael@0 | 1129 | return; |
michael@0 | 1130 | } |
michael@0 | 1131 | listener_manager->AddEventListener(aType, aListener, aUseCapture, |
michael@0 | 1132 | wantsUntrusted); |
michael@0 | 1133 | } |
michael@0 | 1134 | |
michael@0 | 1135 | NS_IMETHODIMP |
michael@0 | 1136 | nsINode::AddSystemEventListener(const nsAString& aType, |
michael@0 | 1137 | nsIDOMEventListener *aListener, |
michael@0 | 1138 | bool aUseCapture, |
michael@0 | 1139 | bool aWantsUntrusted, |
michael@0 | 1140 | uint8_t aOptionalArgc) |
michael@0 | 1141 | { |
michael@0 | 1142 | NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1, |
michael@0 | 1143 | "Won't check if this is chrome, you want to set " |
michael@0 | 1144 | "aWantsUntrusted to false or make the aWantsUntrusted " |
michael@0 | 1145 | "explicit by making aOptionalArgc non-zero."); |
michael@0 | 1146 | |
michael@0 | 1147 | if (!aWantsUntrusted && |
michael@0 | 1148 | (aOptionalArgc < 2 && |
michael@0 | 1149 | !nsContentUtils::IsChromeDoc(OwnerDoc()))) { |
michael@0 | 1150 | aWantsUntrusted = true; |
michael@0 | 1151 | } |
michael@0 | 1152 | |
michael@0 | 1153 | return NS_AddSystemEventListener(this, aType, aListener, aUseCapture, |
michael@0 | 1154 | aWantsUntrusted); |
michael@0 | 1155 | } |
michael@0 | 1156 | |
michael@0 | 1157 | NS_IMETHODIMP |
michael@0 | 1158 | nsINode::RemoveEventListener(const nsAString& aType, |
michael@0 | 1159 | nsIDOMEventListener* aListener, |
michael@0 | 1160 | bool aUseCapture) |
michael@0 | 1161 | { |
michael@0 | 1162 | EventListenerManager* elm = GetExistingListenerManager(); |
michael@0 | 1163 | if (elm) { |
michael@0 | 1164 | elm->RemoveEventListener(aType, aListener, aUseCapture); |
michael@0 | 1165 | } |
michael@0 | 1166 | return NS_OK; |
michael@0 | 1167 | } |
michael@0 | 1168 | |
michael@0 | 1169 | NS_IMPL_REMOVE_SYSTEM_EVENT_LISTENER(nsINode) |
michael@0 | 1170 | |
michael@0 | 1171 | nsresult |
michael@0 | 1172 | nsINode::PreHandleEvent(EventChainPreVisitor& aVisitor) |
michael@0 | 1173 | { |
michael@0 | 1174 | // This is only here so that we can use the NS_DECL_NSIDOMTARGET macro |
michael@0 | 1175 | NS_ABORT(); |
michael@0 | 1176 | return NS_ERROR_NOT_IMPLEMENTED; |
michael@0 | 1177 | } |
michael@0 | 1178 | |
michael@0 | 1179 | void |
michael@0 | 1180 | nsINode::GetBoxQuads(const BoxQuadOptions& aOptions, |
michael@0 | 1181 | nsTArray<nsRefPtr<DOMQuad> >& aResult, |
michael@0 | 1182 | mozilla::ErrorResult& aRv) |
michael@0 | 1183 | { |
michael@0 | 1184 | mozilla::GetBoxQuads(this, aOptions, aResult, aRv); |
michael@0 | 1185 | } |
michael@0 | 1186 | |
michael@0 | 1187 | already_AddRefed<DOMQuad> |
michael@0 | 1188 | nsINode::ConvertQuadFromNode(DOMQuad& aQuad, |
michael@0 | 1189 | const GeometryNode& aFrom, |
michael@0 | 1190 | const ConvertCoordinateOptions& aOptions, |
michael@0 | 1191 | ErrorResult& aRv) |
michael@0 | 1192 | { |
michael@0 | 1193 | return mozilla::ConvertQuadFromNode(this, aQuad, aFrom, aOptions, aRv); |
michael@0 | 1194 | } |
michael@0 | 1195 | |
michael@0 | 1196 | already_AddRefed<DOMQuad> |
michael@0 | 1197 | nsINode::ConvertRectFromNode(DOMRectReadOnly& aRect, |
michael@0 | 1198 | const GeometryNode& aFrom, |
michael@0 | 1199 | const ConvertCoordinateOptions& aOptions, |
michael@0 | 1200 | ErrorResult& aRv) |
michael@0 | 1201 | { |
michael@0 | 1202 | return mozilla::ConvertRectFromNode(this, aRect, aFrom, aOptions, aRv); |
michael@0 | 1203 | } |
michael@0 | 1204 | |
michael@0 | 1205 | already_AddRefed<DOMPoint> |
michael@0 | 1206 | nsINode::ConvertPointFromNode(const DOMPointInit& aPoint, |
michael@0 | 1207 | const GeometryNode& aFrom, |
michael@0 | 1208 | const ConvertCoordinateOptions& aOptions, |
michael@0 | 1209 | ErrorResult& aRv) |
michael@0 | 1210 | { |
michael@0 | 1211 | return mozilla::ConvertPointFromNode(this, aPoint, aFrom, aOptions, aRv); |
michael@0 | 1212 | } |
michael@0 | 1213 | |
michael@0 | 1214 | nsresult |
michael@0 | 1215 | nsINode::DispatchEvent(nsIDOMEvent *aEvent, bool* aRetVal) |
michael@0 | 1216 | { |
michael@0 | 1217 | // XXX sXBL/XBL2 issue -- do we really want the owner here? What |
michael@0 | 1218 | // if that's the XBL document? Would we want its presshell? Or what? |
michael@0 | 1219 | nsCOMPtr<nsIDocument> document = OwnerDoc(); |
michael@0 | 1220 | |
michael@0 | 1221 | // Do nothing if the element does not belong to a document |
michael@0 | 1222 | if (!document) { |
michael@0 | 1223 | *aRetVal = true; |
michael@0 | 1224 | return NS_OK; |
michael@0 | 1225 | } |
michael@0 | 1226 | |
michael@0 | 1227 | // Obtain a presentation shell |
michael@0 | 1228 | nsIPresShell *shell = document->GetShell(); |
michael@0 | 1229 | nsRefPtr<nsPresContext> context; |
michael@0 | 1230 | if (shell) { |
michael@0 | 1231 | context = shell->GetPresContext(); |
michael@0 | 1232 | } |
michael@0 | 1233 | |
michael@0 | 1234 | nsEventStatus status = nsEventStatus_eIgnore; |
michael@0 | 1235 | nsresult rv = |
michael@0 | 1236 | EventDispatcher::DispatchDOMEvent(this, nullptr, aEvent, context, &status); |
michael@0 | 1237 | *aRetVal = (status != nsEventStatus_eConsumeNoDefault); |
michael@0 | 1238 | return rv; |
michael@0 | 1239 | } |
michael@0 | 1240 | |
michael@0 | 1241 | nsresult |
michael@0 | 1242 | nsINode::PostHandleEvent(EventChainPostVisitor& /*aVisitor*/) |
michael@0 | 1243 | { |
michael@0 | 1244 | return NS_OK; |
michael@0 | 1245 | } |
michael@0 | 1246 | |
michael@0 | 1247 | nsresult |
michael@0 | 1248 | nsINode::DispatchDOMEvent(WidgetEvent* aEvent, |
michael@0 | 1249 | nsIDOMEvent* aDOMEvent, |
michael@0 | 1250 | nsPresContext* aPresContext, |
michael@0 | 1251 | nsEventStatus* aEventStatus) |
michael@0 | 1252 | { |
michael@0 | 1253 | return EventDispatcher::DispatchDOMEvent(this, aEvent, aDOMEvent, |
michael@0 | 1254 | aPresContext, aEventStatus); |
michael@0 | 1255 | } |
michael@0 | 1256 | |
michael@0 | 1257 | EventListenerManager* |
michael@0 | 1258 | nsINode::GetOrCreateListenerManager() |
michael@0 | 1259 | { |
michael@0 | 1260 | return nsContentUtils::GetListenerManagerForNode(this); |
michael@0 | 1261 | } |
michael@0 | 1262 | |
michael@0 | 1263 | EventListenerManager* |
michael@0 | 1264 | nsINode::GetExistingListenerManager() const |
michael@0 | 1265 | { |
michael@0 | 1266 | return nsContentUtils::GetExistingListenerManagerForNode(this); |
michael@0 | 1267 | } |
michael@0 | 1268 | |
michael@0 | 1269 | nsIScriptContext* |
michael@0 | 1270 | nsINode::GetContextForEventHandlers(nsresult* aRv) |
michael@0 | 1271 | { |
michael@0 | 1272 | return nsContentUtils::GetContextForEventHandlers(this, aRv); |
michael@0 | 1273 | } |
michael@0 | 1274 | |
michael@0 | 1275 | nsIDOMWindow* |
michael@0 | 1276 | nsINode::GetOwnerGlobal() |
michael@0 | 1277 | { |
michael@0 | 1278 | bool dummy; |
michael@0 | 1279 | return nsPIDOMWindow::GetOuterFromCurrentInner( |
michael@0 | 1280 | static_cast<nsGlobalWindow*>(OwnerDoc()->GetScriptHandlingObject(dummy))); |
michael@0 | 1281 | } |
michael@0 | 1282 | |
michael@0 | 1283 | bool |
michael@0 | 1284 | nsINode::UnoptimizableCCNode() const |
michael@0 | 1285 | { |
michael@0 | 1286 | const uintptr_t problematicFlags = (NODE_IS_ANONYMOUS_ROOT | |
michael@0 | 1287 | NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | |
michael@0 | 1288 | NODE_IS_NATIVE_ANONYMOUS_ROOT | |
michael@0 | 1289 | NODE_MAY_BE_IN_BINDING_MNGR | |
michael@0 | 1290 | NODE_IS_IN_SHADOW_TREE); |
michael@0 | 1291 | return HasFlag(problematicFlags) || |
michael@0 | 1292 | NodeType() == nsIDOMNode::ATTRIBUTE_NODE || |
michael@0 | 1293 | // For strange cases like xbl:content/xbl:children |
michael@0 | 1294 | (IsElement() && |
michael@0 | 1295 | AsElement()->IsInNamespace(kNameSpaceID_XBL)); |
michael@0 | 1296 | } |
michael@0 | 1297 | |
michael@0 | 1298 | /* static */ |
michael@0 | 1299 | bool |
michael@0 | 1300 | nsINode::Traverse(nsINode *tmp, nsCycleCollectionTraversalCallback &cb) |
michael@0 | 1301 | { |
michael@0 | 1302 | if (MOZ_LIKELY(!cb.WantAllTraces())) { |
michael@0 | 1303 | nsIDocument *currentDoc = tmp->GetCurrentDoc(); |
michael@0 | 1304 | if (currentDoc && |
michael@0 | 1305 | nsCCUncollectableMarker::InGeneration(currentDoc->GetMarkedCCGeneration())) { |
michael@0 | 1306 | return false; |
michael@0 | 1307 | } |
michael@0 | 1308 | |
michael@0 | 1309 | if (nsCCUncollectableMarker::sGeneration) { |
michael@0 | 1310 | // If we're black no need to traverse. |
michael@0 | 1311 | if (tmp->IsBlack() || tmp->InCCBlackTree()) { |
michael@0 | 1312 | return false; |
michael@0 | 1313 | } |
michael@0 | 1314 | |
michael@0 | 1315 | if (!tmp->UnoptimizableCCNode()) { |
michael@0 | 1316 | // If we're in a black document, return early. |
michael@0 | 1317 | if ((currentDoc && currentDoc->IsBlack())) { |
michael@0 | 1318 | return false; |
michael@0 | 1319 | } |
michael@0 | 1320 | // If we're not in anonymous content and we have a black parent, |
michael@0 | 1321 | // return early. |
michael@0 | 1322 | nsIContent* parent = tmp->GetParent(); |
michael@0 | 1323 | if (parent && !parent->UnoptimizableCCNode() && parent->IsBlack()) { |
michael@0 | 1324 | NS_ABORT_IF_FALSE(parent->IndexOf(tmp) >= 0, "Parent doesn't own us?"); |
michael@0 | 1325 | return false; |
michael@0 | 1326 | } |
michael@0 | 1327 | } |
michael@0 | 1328 | } |
michael@0 | 1329 | } |
michael@0 | 1330 | |
michael@0 | 1331 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNodeInfo) |
michael@0 | 1332 | NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(GetParent()) |
michael@0 | 1333 | |
michael@0 | 1334 | nsSlots *slots = tmp->GetExistingSlots(); |
michael@0 | 1335 | if (slots) { |
michael@0 | 1336 | slots->Traverse(cb); |
michael@0 | 1337 | } |
michael@0 | 1338 | |
michael@0 | 1339 | if (tmp->HasProperties()) { |
michael@0 | 1340 | nsNodeUtils::TraverseUserData(tmp, cb); |
michael@0 | 1341 | nsCOMArray<nsISupports>* objects = |
michael@0 | 1342 | static_cast<nsCOMArray<nsISupports>*>(tmp->GetProperty(nsGkAtoms::keepobjectsalive)); |
michael@0 | 1343 | if (objects) { |
michael@0 | 1344 | for (int32_t i = 0; i < objects->Count(); ++i) { |
michael@0 | 1345 | cb.NoteXPCOMChild(objects->ObjectAt(i)); |
michael@0 | 1346 | } |
michael@0 | 1347 | } |
michael@0 | 1348 | } |
michael@0 | 1349 | |
michael@0 | 1350 | if (tmp->NodeType() != nsIDOMNode::DOCUMENT_NODE && |
michael@0 | 1351 | tmp->HasFlag(NODE_HAS_LISTENERMANAGER)) { |
michael@0 | 1352 | nsContentUtils::TraverseListenerManager(tmp, cb); |
michael@0 | 1353 | } |
michael@0 | 1354 | |
michael@0 | 1355 | return true; |
michael@0 | 1356 | } |
michael@0 | 1357 | |
michael@0 | 1358 | /* static */ |
michael@0 | 1359 | void |
michael@0 | 1360 | nsINode::Unlink(nsINode* tmp) |
michael@0 | 1361 | { |
michael@0 | 1362 | tmp->ReleaseWrapper(tmp); |
michael@0 | 1363 | |
michael@0 | 1364 | nsSlots *slots = tmp->GetExistingSlots(); |
michael@0 | 1365 | if (slots) { |
michael@0 | 1366 | slots->Unlink(); |
michael@0 | 1367 | } |
michael@0 | 1368 | |
michael@0 | 1369 | if (tmp->NodeType() != nsIDOMNode::DOCUMENT_NODE && |
michael@0 | 1370 | tmp->HasFlag(NODE_HAS_LISTENERMANAGER)) { |
michael@0 | 1371 | nsContentUtils::RemoveListenerManager(tmp); |
michael@0 | 1372 | tmp->UnsetFlags(NODE_HAS_LISTENERMANAGER); |
michael@0 | 1373 | } |
michael@0 | 1374 | |
michael@0 | 1375 | if (tmp->HasProperties()) { |
michael@0 | 1376 | nsNodeUtils::UnlinkUserData(tmp); |
michael@0 | 1377 | tmp->DeleteProperty(nsGkAtoms::keepobjectsalive); |
michael@0 | 1378 | } |
michael@0 | 1379 | } |
michael@0 | 1380 | |
michael@0 | 1381 | static void |
michael@0 | 1382 | ReleaseURI(void*, /* aObject*/ |
michael@0 | 1383 | nsIAtom*, /* aPropertyName */ |
michael@0 | 1384 | void* aPropertyValue, |
michael@0 | 1385 | void* /* aData */) |
michael@0 | 1386 | { |
michael@0 | 1387 | nsIURI* uri = static_cast<nsIURI*>(aPropertyValue); |
michael@0 | 1388 | NS_RELEASE(uri); |
michael@0 | 1389 | } |
michael@0 | 1390 | |
michael@0 | 1391 | nsresult |
michael@0 | 1392 | nsINode::SetExplicitBaseURI(nsIURI* aURI) |
michael@0 | 1393 | { |
michael@0 | 1394 | nsresult rv = SetProperty(nsGkAtoms::baseURIProperty, aURI, ReleaseURI); |
michael@0 | 1395 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 1396 | SetHasExplicitBaseURI(); |
michael@0 | 1397 | NS_ADDREF(aURI); |
michael@0 | 1398 | } |
michael@0 | 1399 | return rv; |
michael@0 | 1400 | } |
michael@0 | 1401 | |
michael@0 | 1402 | static nsresult |
michael@0 | 1403 | AdoptNodeIntoOwnerDoc(nsINode *aParent, nsINode *aNode) |
michael@0 | 1404 | { |
michael@0 | 1405 | NS_ASSERTION(!aNode->GetParentNode(), |
michael@0 | 1406 | "Should have removed from parent already"); |
michael@0 | 1407 | |
michael@0 | 1408 | nsIDocument *doc = aParent->OwnerDoc(); |
michael@0 | 1409 | |
michael@0 | 1410 | nsresult rv; |
michael@0 | 1411 | nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(doc, &rv); |
michael@0 | 1412 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1413 | |
michael@0 | 1414 | nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aNode, &rv); |
michael@0 | 1415 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1416 | |
michael@0 | 1417 | nsCOMPtr<nsIDOMNode> adoptedNode; |
michael@0 | 1418 | rv = domDoc->AdoptNode(node, getter_AddRefs(adoptedNode)); |
michael@0 | 1419 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1420 | |
michael@0 | 1421 | NS_ASSERTION(aParent->OwnerDoc() == doc, |
michael@0 | 1422 | "ownerDoc chainged while adopting"); |
michael@0 | 1423 | NS_ASSERTION(adoptedNode == node, "Uh, adopt node changed nodes?"); |
michael@0 | 1424 | NS_ASSERTION(aParent->OwnerDoc() == aNode->OwnerDoc(), |
michael@0 | 1425 | "ownerDocument changed again after adopting!"); |
michael@0 | 1426 | |
michael@0 | 1427 | return NS_OK; |
michael@0 | 1428 | } |
michael@0 | 1429 | |
michael@0 | 1430 | static nsresult |
michael@0 | 1431 | CheckForOutdatedParent(nsINode* aParent, nsINode* aNode) |
michael@0 | 1432 | { |
michael@0 | 1433 | if (JSObject* existingObjUnrooted = aNode->GetWrapper()) { |
michael@0 | 1434 | AutoJSContext cx; |
michael@0 | 1435 | JS::Rooted<JSObject*> existingObj(cx, existingObjUnrooted); |
michael@0 | 1436 | nsIGlobalObject* global = aParent->OwnerDoc()->GetScopeObject(); |
michael@0 | 1437 | MOZ_ASSERT(global); |
michael@0 | 1438 | |
michael@0 | 1439 | if (js::GetGlobalForObjectCrossCompartment(existingObj) != |
michael@0 | 1440 | global->GetGlobalJSObject()) { |
michael@0 | 1441 | JSAutoCompartment ac(cx, existingObj); |
michael@0 | 1442 | nsresult rv = ReparentWrapper(cx, existingObj); |
michael@0 | 1443 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1444 | } |
michael@0 | 1445 | } |
michael@0 | 1446 | |
michael@0 | 1447 | return NS_OK; |
michael@0 | 1448 | } |
michael@0 | 1449 | |
michael@0 | 1450 | nsresult |
michael@0 | 1451 | nsINode::doInsertChildAt(nsIContent* aKid, uint32_t aIndex, |
michael@0 | 1452 | bool aNotify, nsAttrAndChildArray& aChildArray) |
michael@0 | 1453 | { |
michael@0 | 1454 | NS_PRECONDITION(!aKid->GetParentNode(), |
michael@0 | 1455 | "Inserting node that already has parent"); |
michael@0 | 1456 | nsresult rv; |
michael@0 | 1457 | |
michael@0 | 1458 | // The id-handling code, and in the future possibly other code, need to |
michael@0 | 1459 | // react to unexpected attribute changes. |
michael@0 | 1460 | nsMutationGuard::DidMutate(); |
michael@0 | 1461 | |
michael@0 | 1462 | // Do this before checking the child-count since this could cause mutations |
michael@0 | 1463 | nsIDocument* doc = GetCurrentDoc(); |
michael@0 | 1464 | mozAutoDocUpdate updateBatch(doc, UPDATE_CONTENT_MODEL, aNotify); |
michael@0 | 1465 | |
michael@0 | 1466 | if (OwnerDoc() != aKid->OwnerDoc()) { |
michael@0 | 1467 | rv = AdoptNodeIntoOwnerDoc(this, aKid); |
michael@0 | 1468 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1469 | } else if (OwnerDoc()->DidDocumentOpen()) { |
michael@0 | 1470 | rv = CheckForOutdatedParent(this, aKid); |
michael@0 | 1471 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1472 | } |
michael@0 | 1473 | |
michael@0 | 1474 | uint32_t childCount = aChildArray.ChildCount(); |
michael@0 | 1475 | NS_ENSURE_TRUE(aIndex <= childCount, NS_ERROR_ILLEGAL_VALUE); |
michael@0 | 1476 | bool isAppend = (aIndex == childCount); |
michael@0 | 1477 | |
michael@0 | 1478 | rv = aChildArray.InsertChildAt(aKid, aIndex); |
michael@0 | 1479 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 1480 | if (aIndex == 0) { |
michael@0 | 1481 | mFirstChild = aKid; |
michael@0 | 1482 | } |
michael@0 | 1483 | |
michael@0 | 1484 | nsIContent* parent = |
michael@0 | 1485 | IsNodeOfType(eDOCUMENT) ? nullptr : static_cast<nsIContent*>(this); |
michael@0 | 1486 | |
michael@0 | 1487 | rv = aKid->BindToTree(doc, parent, |
michael@0 | 1488 | parent ? parent->GetBindingParent() : nullptr, |
michael@0 | 1489 | true); |
michael@0 | 1490 | if (NS_FAILED(rv)) { |
michael@0 | 1491 | if (GetFirstChild() == aKid) { |
michael@0 | 1492 | mFirstChild = aKid->GetNextSibling(); |
michael@0 | 1493 | } |
michael@0 | 1494 | aChildArray.RemoveChildAt(aIndex); |
michael@0 | 1495 | aKid->UnbindFromTree(); |
michael@0 | 1496 | return rv; |
michael@0 | 1497 | } |
michael@0 | 1498 | |
michael@0 | 1499 | NS_ASSERTION(aKid->GetParentNode() == this, |
michael@0 | 1500 | "Did we run script inappropriately?"); |
michael@0 | 1501 | |
michael@0 | 1502 | if (aNotify) { |
michael@0 | 1503 | // Note that we always want to call ContentInserted when things are added |
michael@0 | 1504 | // as kids to documents |
michael@0 | 1505 | if (parent && isAppend) { |
michael@0 | 1506 | nsNodeUtils::ContentAppended(parent, aKid, aIndex); |
michael@0 | 1507 | } else { |
michael@0 | 1508 | nsNodeUtils::ContentInserted(this, aKid, aIndex); |
michael@0 | 1509 | } |
michael@0 | 1510 | |
michael@0 | 1511 | if (nsContentUtils::HasMutationListeners(aKid, |
michael@0 | 1512 | NS_EVENT_BITS_MUTATION_NODEINSERTED, this)) { |
michael@0 | 1513 | InternalMutationEvent mutation(true, NS_MUTATION_NODEINSERTED); |
michael@0 | 1514 | mutation.mRelatedNode = do_QueryInterface(this); |
michael@0 | 1515 | |
michael@0 | 1516 | mozAutoSubtreeModified subtree(OwnerDoc(), this); |
michael@0 | 1517 | (new AsyncEventDispatcher(aKid, mutation))->RunDOMEventWhenSafe(); |
michael@0 | 1518 | } |
michael@0 | 1519 | } |
michael@0 | 1520 | |
michael@0 | 1521 | return NS_OK; |
michael@0 | 1522 | } |
michael@0 | 1523 | |
michael@0 | 1524 | Element* |
michael@0 | 1525 | nsINode::GetPreviousElementSibling() const |
michael@0 | 1526 | { |
michael@0 | 1527 | nsIContent* previousSibling = GetPreviousSibling(); |
michael@0 | 1528 | while (previousSibling) { |
michael@0 | 1529 | if (previousSibling->IsElement()) { |
michael@0 | 1530 | return previousSibling->AsElement(); |
michael@0 | 1531 | } |
michael@0 | 1532 | previousSibling = previousSibling->GetPreviousSibling(); |
michael@0 | 1533 | } |
michael@0 | 1534 | |
michael@0 | 1535 | return nullptr; |
michael@0 | 1536 | } |
michael@0 | 1537 | |
michael@0 | 1538 | Element* |
michael@0 | 1539 | nsINode::GetNextElementSibling() const |
michael@0 | 1540 | { |
michael@0 | 1541 | nsIContent* nextSibling = GetNextSibling(); |
michael@0 | 1542 | while (nextSibling) { |
michael@0 | 1543 | if (nextSibling->IsElement()) { |
michael@0 | 1544 | return nextSibling->AsElement(); |
michael@0 | 1545 | } |
michael@0 | 1546 | nextSibling = nextSibling->GetNextSibling(); |
michael@0 | 1547 | } |
michael@0 | 1548 | |
michael@0 | 1549 | return nullptr; |
michael@0 | 1550 | } |
michael@0 | 1551 | |
michael@0 | 1552 | void |
michael@0 | 1553 | nsINode::Remove() |
michael@0 | 1554 | { |
michael@0 | 1555 | nsCOMPtr<nsINode> parent = GetParentNode(); |
michael@0 | 1556 | if (!parent) { |
michael@0 | 1557 | return; |
michael@0 | 1558 | } |
michael@0 | 1559 | int32_t index = parent->IndexOf(this); |
michael@0 | 1560 | if (index < 0) { |
michael@0 | 1561 | NS_WARNING("Ignoring call to nsINode::Remove on anonymous child."); |
michael@0 | 1562 | return; |
michael@0 | 1563 | } |
michael@0 | 1564 | parent->RemoveChildAt(uint32_t(index), true); |
michael@0 | 1565 | } |
michael@0 | 1566 | |
michael@0 | 1567 | Element* |
michael@0 | 1568 | nsINode::GetFirstElementChild() const |
michael@0 | 1569 | { |
michael@0 | 1570 | for (nsIContent* child = GetFirstChild(); |
michael@0 | 1571 | child; |
michael@0 | 1572 | child = child->GetNextSibling()) { |
michael@0 | 1573 | if (child->IsElement()) { |
michael@0 | 1574 | return child->AsElement(); |
michael@0 | 1575 | } |
michael@0 | 1576 | } |
michael@0 | 1577 | |
michael@0 | 1578 | return nullptr; |
michael@0 | 1579 | } |
michael@0 | 1580 | |
michael@0 | 1581 | Element* |
michael@0 | 1582 | nsINode::GetLastElementChild() const |
michael@0 | 1583 | { |
michael@0 | 1584 | for (nsIContent* child = GetLastChild(); |
michael@0 | 1585 | child; |
michael@0 | 1586 | child = child->GetPreviousSibling()) { |
michael@0 | 1587 | if (child->IsElement()) { |
michael@0 | 1588 | return child->AsElement(); |
michael@0 | 1589 | } |
michael@0 | 1590 | } |
michael@0 | 1591 | |
michael@0 | 1592 | return nullptr; |
michael@0 | 1593 | } |
michael@0 | 1594 | |
michael@0 | 1595 | void |
michael@0 | 1596 | nsINode::doRemoveChildAt(uint32_t aIndex, bool aNotify, |
michael@0 | 1597 | nsIContent* aKid, nsAttrAndChildArray& aChildArray) |
michael@0 | 1598 | { |
michael@0 | 1599 | NS_PRECONDITION(aKid && aKid->GetParentNode() == this && |
michael@0 | 1600 | aKid == GetChildAt(aIndex) && |
michael@0 | 1601 | IndexOf(aKid) == (int32_t)aIndex, "Bogus aKid"); |
michael@0 | 1602 | |
michael@0 | 1603 | nsMutationGuard::DidMutate(); |
michael@0 | 1604 | |
michael@0 | 1605 | nsIDocument* doc = GetCurrentDoc(); |
michael@0 | 1606 | |
michael@0 | 1607 | mozAutoDocUpdate updateBatch(doc, UPDATE_CONTENT_MODEL, aNotify); |
michael@0 | 1608 | |
michael@0 | 1609 | nsIContent* previousSibling = aKid->GetPreviousSibling(); |
michael@0 | 1610 | |
michael@0 | 1611 | if (GetFirstChild() == aKid) { |
michael@0 | 1612 | mFirstChild = aKid->GetNextSibling(); |
michael@0 | 1613 | } |
michael@0 | 1614 | |
michael@0 | 1615 | aChildArray.RemoveChildAt(aIndex); |
michael@0 | 1616 | |
michael@0 | 1617 | if (aNotify) { |
michael@0 | 1618 | nsNodeUtils::ContentRemoved(this, aKid, aIndex, previousSibling); |
michael@0 | 1619 | } |
michael@0 | 1620 | |
michael@0 | 1621 | aKid->UnbindFromTree(); |
michael@0 | 1622 | } |
michael@0 | 1623 | |
michael@0 | 1624 | // When replacing, aRefChild is the content being replaced; when |
michael@0 | 1625 | // inserting it's the content before which we're inserting. In the |
michael@0 | 1626 | // latter case it may be null. |
michael@0 | 1627 | static |
michael@0 | 1628 | bool IsAllowedAsChild(nsIContent* aNewChild, nsINode* aParent, |
michael@0 | 1629 | bool aIsReplace, nsINode* aRefChild) |
michael@0 | 1630 | { |
michael@0 | 1631 | MOZ_ASSERT(aNewChild, "Must have new child"); |
michael@0 | 1632 | MOZ_ASSERT_IF(aIsReplace, aRefChild); |
michael@0 | 1633 | MOZ_ASSERT(aParent); |
michael@0 | 1634 | MOZ_ASSERT(aParent->IsNodeOfType(nsINode::eDOCUMENT) || |
michael@0 | 1635 | aParent->IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT) || |
michael@0 | 1636 | aParent->IsElement(), |
michael@0 | 1637 | "Nodes that are not documents, document fragments or elements " |
michael@0 | 1638 | "can't be parents!"); |
michael@0 | 1639 | |
michael@0 | 1640 | // A common case is that aNewChild has no kids, in which case |
michael@0 | 1641 | // aParent can't be a descendant of aNewChild unless they're |
michael@0 | 1642 | // actually equal to each other. Fast-path that case, since aParent |
michael@0 | 1643 | // could be pretty deep in the DOM tree. |
michael@0 | 1644 | if (aNewChild == aParent || |
michael@0 | 1645 | ((aNewChild->GetFirstChild() || |
michael@0 | 1646 | // HTML template elements and ShadowRoot hosts need |
michael@0 | 1647 | // to be checked to ensure that they are not inserted into |
michael@0 | 1648 | // the hosted content. |
michael@0 | 1649 | aNewChild->Tag() == nsGkAtoms::_template || |
michael@0 | 1650 | aNewChild->GetShadowRoot()) && |
michael@0 | 1651 | nsContentUtils::ContentIsHostIncludingDescendantOf(aParent, |
michael@0 | 1652 | aNewChild))) { |
michael@0 | 1653 | return false; |
michael@0 | 1654 | } |
michael@0 | 1655 | |
michael@0 | 1656 | // The allowed child nodes differ for documents and elements |
michael@0 | 1657 | switch (aNewChild->NodeType()) { |
michael@0 | 1658 | case nsIDOMNode::COMMENT_NODE : |
michael@0 | 1659 | case nsIDOMNode::PROCESSING_INSTRUCTION_NODE : |
michael@0 | 1660 | // OK in both cases |
michael@0 | 1661 | return true; |
michael@0 | 1662 | case nsIDOMNode::TEXT_NODE : |
michael@0 | 1663 | case nsIDOMNode::CDATA_SECTION_NODE : |
michael@0 | 1664 | case nsIDOMNode::ENTITY_REFERENCE_NODE : |
michael@0 | 1665 | // Allowed under Elements and DocumentFragments |
michael@0 | 1666 | return aParent->NodeType() != nsIDOMNode::DOCUMENT_NODE; |
michael@0 | 1667 | case nsIDOMNode::ELEMENT_NODE : |
michael@0 | 1668 | { |
michael@0 | 1669 | if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) { |
michael@0 | 1670 | // Always ok to have elements under other elements or document fragments |
michael@0 | 1671 | return true; |
michael@0 | 1672 | } |
michael@0 | 1673 | |
michael@0 | 1674 | nsIDocument* parentDocument = static_cast<nsIDocument*>(aParent); |
michael@0 | 1675 | Element* rootElement = parentDocument->GetRootElement(); |
michael@0 | 1676 | if (rootElement) { |
michael@0 | 1677 | // Already have a documentElement, so this is only OK if we're |
michael@0 | 1678 | // replacing it. |
michael@0 | 1679 | return aIsReplace && rootElement == aRefChild; |
michael@0 | 1680 | } |
michael@0 | 1681 | |
michael@0 | 1682 | // We don't have a documentElement yet. Our one remaining constraint is |
michael@0 | 1683 | // that the documentElement must come after the doctype. |
michael@0 | 1684 | if (!aRefChild) { |
michael@0 | 1685 | // Appending is just fine. |
michael@0 | 1686 | return true; |
michael@0 | 1687 | } |
michael@0 | 1688 | |
michael@0 | 1689 | nsIContent* docTypeContent = parentDocument->GetDoctype(); |
michael@0 | 1690 | if (!docTypeContent) { |
michael@0 | 1691 | // It's all good. |
michael@0 | 1692 | return true; |
michael@0 | 1693 | } |
michael@0 | 1694 | |
michael@0 | 1695 | int32_t doctypeIndex = aParent->IndexOf(docTypeContent); |
michael@0 | 1696 | int32_t insertIndex = aParent->IndexOf(aRefChild); |
michael@0 | 1697 | |
michael@0 | 1698 | // Now we're OK in the following two cases only: |
michael@0 | 1699 | // 1) We're replacing something that's not before the doctype |
michael@0 | 1700 | // 2) We're inserting before something that comes after the doctype |
michael@0 | 1701 | return aIsReplace ? (insertIndex >= doctypeIndex) : |
michael@0 | 1702 | insertIndex > doctypeIndex; |
michael@0 | 1703 | } |
michael@0 | 1704 | case nsIDOMNode::DOCUMENT_TYPE_NODE : |
michael@0 | 1705 | { |
michael@0 | 1706 | if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) { |
michael@0 | 1707 | // doctypes only allowed under documents |
michael@0 | 1708 | return false; |
michael@0 | 1709 | } |
michael@0 | 1710 | |
michael@0 | 1711 | nsIDocument* parentDocument = static_cast<nsIDocument*>(aParent); |
michael@0 | 1712 | nsIContent* docTypeContent = parentDocument->GetDoctype(); |
michael@0 | 1713 | if (docTypeContent) { |
michael@0 | 1714 | // Already have a doctype, so this is only OK if we're replacing it |
michael@0 | 1715 | return aIsReplace && docTypeContent == aRefChild; |
michael@0 | 1716 | } |
michael@0 | 1717 | |
michael@0 | 1718 | // We don't have a doctype yet. Our one remaining constraint is |
michael@0 | 1719 | // that the doctype must come before the documentElement. |
michael@0 | 1720 | Element* rootElement = parentDocument->GetRootElement(); |
michael@0 | 1721 | if (!rootElement) { |
michael@0 | 1722 | // It's all good |
michael@0 | 1723 | return true; |
michael@0 | 1724 | } |
michael@0 | 1725 | |
michael@0 | 1726 | if (!aRefChild) { |
michael@0 | 1727 | // Trying to append a doctype, but have a documentElement |
michael@0 | 1728 | return false; |
michael@0 | 1729 | } |
michael@0 | 1730 | |
michael@0 | 1731 | int32_t rootIndex = aParent->IndexOf(rootElement); |
michael@0 | 1732 | int32_t insertIndex = aParent->IndexOf(aRefChild); |
michael@0 | 1733 | |
michael@0 | 1734 | // Now we're OK if and only if insertIndex <= rootIndex. Indeed, either |
michael@0 | 1735 | // we end up replacing aRefChild or we end up before it. Either one is |
michael@0 | 1736 | // ok as long as aRefChild is not after rootElement. |
michael@0 | 1737 | return insertIndex <= rootIndex; |
michael@0 | 1738 | } |
michael@0 | 1739 | case nsIDOMNode::DOCUMENT_FRAGMENT_NODE : |
michael@0 | 1740 | { |
michael@0 | 1741 | // Note that for now we only allow nodes inside document fragments if |
michael@0 | 1742 | // they're allowed inside elements. If we ever change this to allow |
michael@0 | 1743 | // doctype nodes in document fragments, we'll need to update this code. |
michael@0 | 1744 | // Also, there's a version of this code in ReplaceOrInsertBefore. If you |
michael@0 | 1745 | // change this code, change that too. |
michael@0 | 1746 | if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) { |
michael@0 | 1747 | // All good here |
michael@0 | 1748 | return true; |
michael@0 | 1749 | } |
michael@0 | 1750 | |
michael@0 | 1751 | bool sawElement = false; |
michael@0 | 1752 | for (nsIContent* child = aNewChild->GetFirstChild(); |
michael@0 | 1753 | child; |
michael@0 | 1754 | child = child->GetNextSibling()) { |
michael@0 | 1755 | if (child->IsElement()) { |
michael@0 | 1756 | if (sawElement) { |
michael@0 | 1757 | // Can't put two elements into a document |
michael@0 | 1758 | return false; |
michael@0 | 1759 | } |
michael@0 | 1760 | sawElement = true; |
michael@0 | 1761 | } |
michael@0 | 1762 | // If we can put this content at the the right place, we might be ok; |
michael@0 | 1763 | // if not, we bail out. |
michael@0 | 1764 | if (!IsAllowedAsChild(child, aParent, aIsReplace, aRefChild)) { |
michael@0 | 1765 | return false; |
michael@0 | 1766 | } |
michael@0 | 1767 | } |
michael@0 | 1768 | |
michael@0 | 1769 | // Everything in the fragment checked out ok, so we can stick it in here |
michael@0 | 1770 | return true; |
michael@0 | 1771 | } |
michael@0 | 1772 | default: |
michael@0 | 1773 | /* |
michael@0 | 1774 | * aNewChild is of invalid type. |
michael@0 | 1775 | */ |
michael@0 | 1776 | break; |
michael@0 | 1777 | } |
michael@0 | 1778 | |
michael@0 | 1779 | return false; |
michael@0 | 1780 | } |
michael@0 | 1781 | |
michael@0 | 1782 | nsINode* |
michael@0 | 1783 | nsINode::ReplaceOrInsertBefore(bool aReplace, nsINode* aNewChild, |
michael@0 | 1784 | nsINode* aRefChild, ErrorResult& aError) |
michael@0 | 1785 | { |
michael@0 | 1786 | // XXXbz I wish I could assert that nsContentUtils::IsSafeToRunScript() so we |
michael@0 | 1787 | // could rely on scriptblockers going out of scope to actually run XBL |
michael@0 | 1788 | // teardown, but various crud adds nodes under scriptblockers (e.g. native |
michael@0 | 1789 | // anonymous content). The only good news is those insertions can't trigger |
michael@0 | 1790 | // the bad XBL cases. |
michael@0 | 1791 | MOZ_ASSERT_IF(aReplace, aRefChild); |
michael@0 | 1792 | |
michael@0 | 1793 | if ((!IsNodeOfType(eDOCUMENT) && |
michael@0 | 1794 | !IsNodeOfType(eDOCUMENT_FRAGMENT) && |
michael@0 | 1795 | !IsElement()) || |
michael@0 | 1796 | !aNewChild->IsNodeOfType(eCONTENT)) { |
michael@0 | 1797 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 1798 | return nullptr; |
michael@0 | 1799 | } |
michael@0 | 1800 | |
michael@0 | 1801 | uint16_t nodeType = aNewChild->NodeType(); |
michael@0 | 1802 | |
michael@0 | 1803 | // Before we do anything else, fire all DOMNodeRemoved mutation events |
michael@0 | 1804 | // We do this up front as to avoid having to deal with script running |
michael@0 | 1805 | // at random places further down. |
michael@0 | 1806 | // Scope firing mutation events so that we don't carry any state that |
michael@0 | 1807 | // might be stale |
michael@0 | 1808 | { |
michael@0 | 1809 | // This check happens again further down (though then using IndexOf). |
michael@0 | 1810 | // We're only checking this here to avoid firing mutation events when |
michael@0 | 1811 | // none should be fired. |
michael@0 | 1812 | // It's ok that we do the check twice in the case when firing mutation |
michael@0 | 1813 | // events as we need to recheck after running script anyway. |
michael@0 | 1814 | if (aRefChild && aRefChild->GetParentNode() != this) { |
michael@0 | 1815 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
michael@0 | 1816 | return nullptr; |
michael@0 | 1817 | } |
michael@0 | 1818 | |
michael@0 | 1819 | // If we're replacing, fire for node-to-be-replaced. |
michael@0 | 1820 | // If aRefChild == aNewChild then we'll fire for it in check below |
michael@0 | 1821 | if (aReplace && aRefChild != aNewChild) { |
michael@0 | 1822 | nsContentUtils::MaybeFireNodeRemoved(aRefChild, this, OwnerDoc()); |
michael@0 | 1823 | } |
michael@0 | 1824 | |
michael@0 | 1825 | // If the new node already has a parent, fire for removing from old |
michael@0 | 1826 | // parent |
michael@0 | 1827 | nsINode* oldParent = aNewChild->GetParentNode(); |
michael@0 | 1828 | if (oldParent) { |
michael@0 | 1829 | nsContentUtils::MaybeFireNodeRemoved(aNewChild, oldParent, |
michael@0 | 1830 | aNewChild->OwnerDoc()); |
michael@0 | 1831 | } |
michael@0 | 1832 | |
michael@0 | 1833 | // If we're inserting a fragment, fire for all the children of the |
michael@0 | 1834 | // fragment |
michael@0 | 1835 | if (nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { |
michael@0 | 1836 | static_cast<FragmentOrElement*>(aNewChild)->FireNodeRemovedForChildren(); |
michael@0 | 1837 | } |
michael@0 | 1838 | // Verify that our aRefChild is still sensible |
michael@0 | 1839 | if (aRefChild && aRefChild->GetParentNode() != this) { |
michael@0 | 1840 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
michael@0 | 1841 | return nullptr; |
michael@0 | 1842 | } |
michael@0 | 1843 | } |
michael@0 | 1844 | |
michael@0 | 1845 | nsIDocument* doc = OwnerDoc(); |
michael@0 | 1846 | nsIContent* newContent = static_cast<nsIContent*>(aNewChild); |
michael@0 | 1847 | if (newContent->IsRootOfAnonymousSubtree()) { |
michael@0 | 1848 | // This is anonymous content. Don't allow its insertion |
michael@0 | 1849 | // anywhere, since it might have UnbindFromTree calls coming |
michael@0 | 1850 | // its way. |
michael@0 | 1851 | aError.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 1852 | return nullptr; |
michael@0 | 1853 | } |
michael@0 | 1854 | |
michael@0 | 1855 | // Make sure that the inserted node is allowed as a child of its new parent. |
michael@0 | 1856 | if (!IsAllowedAsChild(newContent, this, aReplace, aRefChild)) { |
michael@0 | 1857 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 1858 | return nullptr; |
michael@0 | 1859 | } |
michael@0 | 1860 | |
michael@0 | 1861 | // Record the node to insert before, if any |
michael@0 | 1862 | nsINode* nodeToInsertBefore; |
michael@0 | 1863 | if (aReplace) { |
michael@0 | 1864 | nodeToInsertBefore = aRefChild->GetNextSibling(); |
michael@0 | 1865 | } else { |
michael@0 | 1866 | nodeToInsertBefore = aRefChild; |
michael@0 | 1867 | } |
michael@0 | 1868 | if (nodeToInsertBefore == aNewChild) { |
michael@0 | 1869 | // We're going to remove aNewChild from its parent, so use its next sibling |
michael@0 | 1870 | // as the node to insert before. |
michael@0 | 1871 | nodeToInsertBefore = nodeToInsertBefore->GetNextSibling(); |
michael@0 | 1872 | } |
michael@0 | 1873 | |
michael@0 | 1874 | Maybe<nsAutoTArray<nsCOMPtr<nsIContent>, 50> > fragChildren; |
michael@0 | 1875 | |
michael@0 | 1876 | // Remove the new child from the old parent if one exists |
michael@0 | 1877 | nsCOMPtr<nsINode> oldParent = newContent->GetParentNode(); |
michael@0 | 1878 | if (oldParent) { |
michael@0 | 1879 | int32_t removeIndex = oldParent->IndexOf(newContent); |
michael@0 | 1880 | if (removeIndex < 0) { |
michael@0 | 1881 | // newContent is anonymous. We can't deal with this, so just bail |
michael@0 | 1882 | NS_ERROR("How come our flags didn't catch this?"); |
michael@0 | 1883 | aError.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); |
michael@0 | 1884 | return nullptr; |
michael@0 | 1885 | } |
michael@0 | 1886 | |
michael@0 | 1887 | // Hold a strong ref to nodeToInsertBefore across the removal of newContent |
michael@0 | 1888 | nsCOMPtr<nsINode> kungFuDeathGrip = nodeToInsertBefore; |
michael@0 | 1889 | |
michael@0 | 1890 | // Removing a child can run script, via XBL destructors. |
michael@0 | 1891 | nsMutationGuard guard; |
michael@0 | 1892 | |
michael@0 | 1893 | // Scope for the mutation batch and scriptblocker, so they go away |
michael@0 | 1894 | // while kungFuDeathGrip is still alive. |
michael@0 | 1895 | { |
michael@0 | 1896 | mozAutoDocUpdate batch(newContent->GetCurrentDoc(), |
michael@0 | 1897 | UPDATE_CONTENT_MODEL, true); |
michael@0 | 1898 | nsAutoMutationBatch mb(oldParent, true, true); |
michael@0 | 1899 | oldParent->RemoveChildAt(removeIndex, true); |
michael@0 | 1900 | if (nsAutoMutationBatch::GetCurrentBatch() == &mb) { |
michael@0 | 1901 | mb.RemovalDone(); |
michael@0 | 1902 | mb.SetPrevSibling(oldParent->GetChildAt(removeIndex - 1)); |
michael@0 | 1903 | mb.SetNextSibling(oldParent->GetChildAt(removeIndex)); |
michael@0 | 1904 | } |
michael@0 | 1905 | } |
michael@0 | 1906 | |
michael@0 | 1907 | // We expect one mutation (the removal) to have happened. |
michael@0 | 1908 | if (guard.Mutated(1)) { |
michael@0 | 1909 | // XBL destructors, yuck. |
michael@0 | 1910 | |
michael@0 | 1911 | // Verify that nodeToInsertBefore, if non-null, is still our child. If |
michael@0 | 1912 | // it's not, there's no way we can do this insert sanely; just bail out. |
michael@0 | 1913 | if (nodeToInsertBefore && nodeToInsertBefore->GetParent() != this) { |
michael@0 | 1914 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 1915 | return nullptr; |
michael@0 | 1916 | } |
michael@0 | 1917 | |
michael@0 | 1918 | // Verify that newContent has no parent. |
michael@0 | 1919 | if (newContent->GetParentNode()) { |
michael@0 | 1920 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 1921 | return nullptr; |
michael@0 | 1922 | } |
michael@0 | 1923 | |
michael@0 | 1924 | // And verify that newContent is still allowed as our child. |
michael@0 | 1925 | if (aNewChild == aRefChild) { |
michael@0 | 1926 | // We've already removed aRefChild. So even if we were doing a replace, |
michael@0 | 1927 | // now we're doing a simple insert before nodeToInsertBefore. |
michael@0 | 1928 | if (!IsAllowedAsChild(newContent, this, false, nodeToInsertBefore)) { |
michael@0 | 1929 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 1930 | return nullptr; |
michael@0 | 1931 | } |
michael@0 | 1932 | } else { |
michael@0 | 1933 | if ((aRefChild && aRefChild->GetParent() != this) || |
michael@0 | 1934 | !IsAllowedAsChild(newContent, this, aReplace, aRefChild)) { |
michael@0 | 1935 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 1936 | return nullptr; |
michael@0 | 1937 | } |
michael@0 | 1938 | // And recompute nodeToInsertBefore, just in case. |
michael@0 | 1939 | if (aReplace) { |
michael@0 | 1940 | nodeToInsertBefore = aRefChild->GetNextSibling(); |
michael@0 | 1941 | } else { |
michael@0 | 1942 | nodeToInsertBefore = aRefChild; |
michael@0 | 1943 | } |
michael@0 | 1944 | } |
michael@0 | 1945 | } |
michael@0 | 1946 | } else if (nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { |
michael@0 | 1947 | // Make sure to remove all the fragment's kids. We need to do this before |
michael@0 | 1948 | // we start inserting anything, so we will run out XBL destructors and |
michael@0 | 1949 | // binding teardown (GOD, I HATE THESE THINGS) before we insert anything |
michael@0 | 1950 | // into the DOM. |
michael@0 | 1951 | uint32_t count = newContent->GetChildCount(); |
michael@0 | 1952 | |
michael@0 | 1953 | fragChildren.construct(); |
michael@0 | 1954 | |
michael@0 | 1955 | // Copy the children into a separate array to avoid having to deal with |
michael@0 | 1956 | // mutations to the fragment later on here. |
michael@0 | 1957 | fragChildren.ref().SetCapacity(count); |
michael@0 | 1958 | for (nsIContent* child = newContent->GetFirstChild(); |
michael@0 | 1959 | child; |
michael@0 | 1960 | child = child->GetNextSibling()) { |
michael@0 | 1961 | NS_ASSERTION(child->GetCurrentDoc() == nullptr, |
michael@0 | 1962 | "How did we get a child with a current doc?"); |
michael@0 | 1963 | fragChildren.ref().AppendElement(child); |
michael@0 | 1964 | } |
michael@0 | 1965 | |
michael@0 | 1966 | // Hold a strong ref to nodeToInsertBefore across the removals |
michael@0 | 1967 | nsCOMPtr<nsINode> kungFuDeathGrip = nodeToInsertBefore; |
michael@0 | 1968 | |
michael@0 | 1969 | nsMutationGuard guard; |
michael@0 | 1970 | |
michael@0 | 1971 | // Scope for the mutation batch and scriptblocker, so they go away |
michael@0 | 1972 | // while kungFuDeathGrip is still alive. |
michael@0 | 1973 | { |
michael@0 | 1974 | mozAutoDocUpdate batch(newContent->GetCurrentDoc(), |
michael@0 | 1975 | UPDATE_CONTENT_MODEL, true); |
michael@0 | 1976 | nsAutoMutationBatch mb(newContent, false, true); |
michael@0 | 1977 | |
michael@0 | 1978 | for (uint32_t i = count; i > 0;) { |
michael@0 | 1979 | newContent->RemoveChildAt(--i, true); |
michael@0 | 1980 | } |
michael@0 | 1981 | } |
michael@0 | 1982 | |
michael@0 | 1983 | // We expect |count| removals |
michael@0 | 1984 | if (guard.Mutated(count)) { |
michael@0 | 1985 | // XBL destructors, yuck. |
michael@0 | 1986 | |
michael@0 | 1987 | // Verify that nodeToInsertBefore, if non-null, is still our child. If |
michael@0 | 1988 | // it's not, there's no way we can do this insert sanely; just bail out. |
michael@0 | 1989 | if (nodeToInsertBefore && nodeToInsertBefore->GetParent() != this) { |
michael@0 | 1990 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 1991 | return nullptr; |
michael@0 | 1992 | } |
michael@0 | 1993 | |
michael@0 | 1994 | // Verify that all the things in fragChildren have no parent. |
michael@0 | 1995 | for (uint32_t i = 0; i < count; ++i) { |
michael@0 | 1996 | if (fragChildren.ref().ElementAt(i)->GetParentNode()) { |
michael@0 | 1997 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 1998 | return nullptr; |
michael@0 | 1999 | } |
michael@0 | 2000 | } |
michael@0 | 2001 | |
michael@0 | 2002 | // Note that unlike the single-element case above, none of our kids can |
michael@0 | 2003 | // be aRefChild, so we can always pass through aReplace in the |
michael@0 | 2004 | // IsAllowedAsChild checks below and don't have to worry about whether |
michael@0 | 2005 | // recomputing nodeToInsertBefore is OK. |
michael@0 | 2006 | |
michael@0 | 2007 | // Verify that our aRefChild is still sensible |
michael@0 | 2008 | if (aRefChild && aRefChild->GetParent() != this) { |
michael@0 | 2009 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 2010 | return nullptr; |
michael@0 | 2011 | } |
michael@0 | 2012 | |
michael@0 | 2013 | // Recompute nodeToInsertBefore, just in case. |
michael@0 | 2014 | if (aReplace) { |
michael@0 | 2015 | nodeToInsertBefore = aRefChild->GetNextSibling(); |
michael@0 | 2016 | } else { |
michael@0 | 2017 | nodeToInsertBefore = aRefChild; |
michael@0 | 2018 | } |
michael@0 | 2019 | |
michael@0 | 2020 | // And verify that newContent is still allowed as our child. Sadly, we |
michael@0 | 2021 | // need to reimplement the relevant part of IsAllowedAsChild() because |
michael@0 | 2022 | // now our nodes are in an array and all. If you change this code, |
michael@0 | 2023 | // change the code there. |
michael@0 | 2024 | if (IsNodeOfType(nsINode::eDOCUMENT)) { |
michael@0 | 2025 | bool sawElement = false; |
michael@0 | 2026 | for (uint32_t i = 0; i < count; ++i) { |
michael@0 | 2027 | nsIContent* child = fragChildren.ref().ElementAt(i); |
michael@0 | 2028 | if (child->IsElement()) { |
michael@0 | 2029 | if (sawElement) { |
michael@0 | 2030 | // No good |
michael@0 | 2031 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 2032 | return nullptr; |
michael@0 | 2033 | } |
michael@0 | 2034 | sawElement = true; |
michael@0 | 2035 | } |
michael@0 | 2036 | if (!IsAllowedAsChild(child, this, aReplace, aRefChild)) { |
michael@0 | 2037 | aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); |
michael@0 | 2038 | return nullptr; |
michael@0 | 2039 | } |
michael@0 | 2040 | } |
michael@0 | 2041 | } |
michael@0 | 2042 | } |
michael@0 | 2043 | } |
michael@0 | 2044 | |
michael@0 | 2045 | mozAutoDocUpdate batch(GetCurrentDoc(), UPDATE_CONTENT_MODEL, true); |
michael@0 | 2046 | nsAutoMutationBatch mb; |
michael@0 | 2047 | |
michael@0 | 2048 | // Figure out which index we want to insert at. Note that we use |
michael@0 | 2049 | // nodeToInsertBefore to determine this, because it's possible that |
michael@0 | 2050 | // aRefChild == aNewChild, in which case we just removed it from the |
michael@0 | 2051 | // parent list. |
michael@0 | 2052 | int32_t insPos; |
michael@0 | 2053 | if (nodeToInsertBefore) { |
michael@0 | 2054 | insPos = IndexOf(nodeToInsertBefore); |
michael@0 | 2055 | if (insPos < 0) { |
michael@0 | 2056 | // XXXbz How the heck would _that_ happen, exactly? |
michael@0 | 2057 | aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); |
michael@0 | 2058 | return nullptr; |
michael@0 | 2059 | } |
michael@0 | 2060 | } |
michael@0 | 2061 | else { |
michael@0 | 2062 | insPos = GetChildCount(); |
michael@0 | 2063 | } |
michael@0 | 2064 | |
michael@0 | 2065 | // If we're replacing and we haven't removed aRefChild yet, do so now |
michael@0 | 2066 | if (aReplace && aRefChild != aNewChild) { |
michael@0 | 2067 | mb.Init(this, true, true); |
michael@0 | 2068 | |
michael@0 | 2069 | // Since aRefChild is never null in the aReplace case, we know that at |
michael@0 | 2070 | // this point nodeToInsertBefore is the next sibling of aRefChild. |
michael@0 | 2071 | NS_ASSERTION(aRefChild->GetNextSibling() == nodeToInsertBefore, |
michael@0 | 2072 | "Unexpected nodeToInsertBefore"); |
michael@0 | 2073 | |
michael@0 | 2074 | // An since nodeToInsertBefore is at index insPos, we want to remove |
michael@0 | 2075 | // at the previous index. |
michael@0 | 2076 | NS_ASSERTION(insPos >= 1, "insPos too small"); |
michael@0 | 2077 | RemoveChildAt(insPos-1, true); |
michael@0 | 2078 | --insPos; |
michael@0 | 2079 | } |
michael@0 | 2080 | |
michael@0 | 2081 | // Move new child over to our document if needed. Do this after removing |
michael@0 | 2082 | // it from its parent so that AdoptNode doesn't fire DOMNodeRemoved |
michael@0 | 2083 | // DocumentType nodes are the only nodes that can have a null |
michael@0 | 2084 | // ownerDocument according to the DOM spec, and we need to allow |
michael@0 | 2085 | // inserting them w/o calling AdoptNode(). |
michael@0 | 2086 | if (doc != newContent->OwnerDoc()) { |
michael@0 | 2087 | aError = AdoptNodeIntoOwnerDoc(this, aNewChild); |
michael@0 | 2088 | if (aError.Failed()) { |
michael@0 | 2089 | return nullptr; |
michael@0 | 2090 | } |
michael@0 | 2091 | } else if (doc->DidDocumentOpen()) { |
michael@0 | 2092 | aError = CheckForOutdatedParent(this, aNewChild); |
michael@0 | 2093 | if (aError.Failed()) { |
michael@0 | 2094 | return nullptr; |
michael@0 | 2095 | } |
michael@0 | 2096 | } |
michael@0 | 2097 | |
michael@0 | 2098 | /* |
michael@0 | 2099 | * Check if we're inserting a document fragment. If we are, we need |
michael@0 | 2100 | * to actually add its children individually (i.e. we don't add the |
michael@0 | 2101 | * actual document fragment). |
michael@0 | 2102 | */ |
michael@0 | 2103 | nsINode* result = aReplace ? aRefChild : aNewChild; |
michael@0 | 2104 | if (nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { |
michael@0 | 2105 | if (!aReplace) { |
michael@0 | 2106 | mb.Init(this, true, true); |
michael@0 | 2107 | } |
michael@0 | 2108 | nsAutoMutationBatch* mutationBatch = nsAutoMutationBatch::GetCurrentBatch(); |
michael@0 | 2109 | if (mutationBatch) { |
michael@0 | 2110 | mutationBatch->RemovalDone(); |
michael@0 | 2111 | mutationBatch->SetPrevSibling(GetChildAt(insPos - 1)); |
michael@0 | 2112 | mutationBatch->SetNextSibling(GetChildAt(insPos)); |
michael@0 | 2113 | } |
michael@0 | 2114 | |
michael@0 | 2115 | uint32_t count = fragChildren.ref().Length(); |
michael@0 | 2116 | if (!count) { |
michael@0 | 2117 | return result; |
michael@0 | 2118 | } |
michael@0 | 2119 | |
michael@0 | 2120 | bool appending = |
michael@0 | 2121 | !IsNodeOfType(eDOCUMENT) && uint32_t(insPos) == GetChildCount(); |
michael@0 | 2122 | int32_t firstInsPos = insPos; |
michael@0 | 2123 | nsIContent* firstInsertedContent = fragChildren.ref().ElementAt(0); |
michael@0 | 2124 | |
michael@0 | 2125 | // Iterate through the fragment's children, and insert them in the new |
michael@0 | 2126 | // parent |
michael@0 | 2127 | for (uint32_t i = 0; i < count; ++i, ++insPos) { |
michael@0 | 2128 | // XXXbz how come no reparenting here? That seems odd... |
michael@0 | 2129 | // Insert the child. |
michael@0 | 2130 | aError = InsertChildAt(fragChildren.ref().ElementAt(i), insPos, |
michael@0 | 2131 | !appending); |
michael@0 | 2132 | if (aError.Failed()) { |
michael@0 | 2133 | // Make sure to notify on any children that we did succeed to insert |
michael@0 | 2134 | if (appending && i != 0) { |
michael@0 | 2135 | nsNodeUtils::ContentAppended(static_cast<nsIContent*>(this), |
michael@0 | 2136 | firstInsertedContent, |
michael@0 | 2137 | firstInsPos); |
michael@0 | 2138 | } |
michael@0 | 2139 | return nullptr; |
michael@0 | 2140 | } |
michael@0 | 2141 | } |
michael@0 | 2142 | |
michael@0 | 2143 | if (mutationBatch && !appending) { |
michael@0 | 2144 | mutationBatch->NodesAdded(); |
michael@0 | 2145 | } |
michael@0 | 2146 | |
michael@0 | 2147 | // Notify and fire mutation events when appending |
michael@0 | 2148 | if (appending) { |
michael@0 | 2149 | nsNodeUtils::ContentAppended(static_cast<nsIContent*>(this), |
michael@0 | 2150 | firstInsertedContent, firstInsPos); |
michael@0 | 2151 | if (mutationBatch) { |
michael@0 | 2152 | mutationBatch->NodesAdded(); |
michael@0 | 2153 | } |
michael@0 | 2154 | // Optimize for the case when there are no listeners |
michael@0 | 2155 | if (nsContentUtils:: |
michael@0 | 2156 | HasMutationListeners(doc, NS_EVENT_BITS_MUTATION_NODEINSERTED)) { |
michael@0 | 2157 | Element::FireNodeInserted(doc, this, fragChildren.ref()); |
michael@0 | 2158 | } |
michael@0 | 2159 | } |
michael@0 | 2160 | } |
michael@0 | 2161 | else { |
michael@0 | 2162 | // Not inserting a fragment but rather a single node. |
michael@0 | 2163 | |
michael@0 | 2164 | // FIXME https://bugzilla.mozilla.org/show_bug.cgi?id=544654 |
michael@0 | 2165 | // We need to reparent here for nodes for which the parent of their |
michael@0 | 2166 | // wrapper is not the wrapper for their ownerDocument (XUL elements, |
michael@0 | 2167 | // form controls, ...). Also applies in the fragment code above. |
michael@0 | 2168 | |
michael@0 | 2169 | if (nsAutoMutationBatch::GetCurrentBatch() == &mb) { |
michael@0 | 2170 | mb.RemovalDone(); |
michael@0 | 2171 | mb.SetPrevSibling(GetChildAt(insPos - 1)); |
michael@0 | 2172 | mb.SetNextSibling(GetChildAt(insPos)); |
michael@0 | 2173 | } |
michael@0 | 2174 | aError = InsertChildAt(newContent, insPos, true); |
michael@0 | 2175 | if (aError.Failed()) { |
michael@0 | 2176 | return nullptr; |
michael@0 | 2177 | } |
michael@0 | 2178 | } |
michael@0 | 2179 | |
michael@0 | 2180 | return result; |
michael@0 | 2181 | } |
michael@0 | 2182 | |
michael@0 | 2183 | nsresult |
michael@0 | 2184 | nsINode::ReplaceOrInsertBefore(bool aReplace, nsIDOMNode *aNewChild, |
michael@0 | 2185 | nsIDOMNode *aRefChild, nsIDOMNode **aReturn) |
michael@0 | 2186 | { |
michael@0 | 2187 | nsCOMPtr<nsINode> newChild = do_QueryInterface(aNewChild); |
michael@0 | 2188 | if (!newChild) { |
michael@0 | 2189 | return NS_ERROR_NULL_POINTER; |
michael@0 | 2190 | } |
michael@0 | 2191 | |
michael@0 | 2192 | if (aReplace && !aRefChild) { |
michael@0 | 2193 | return NS_ERROR_NULL_POINTER; |
michael@0 | 2194 | } |
michael@0 | 2195 | |
michael@0 | 2196 | nsCOMPtr<nsINode> refChild = do_QueryInterface(aRefChild); |
michael@0 | 2197 | if (aRefChild && !refChild) { |
michael@0 | 2198 | return NS_NOINTERFACE; |
michael@0 | 2199 | } |
michael@0 | 2200 | |
michael@0 | 2201 | ErrorResult rv; |
michael@0 | 2202 | nsINode* result = ReplaceOrInsertBefore(aReplace, newChild, refChild, rv); |
michael@0 | 2203 | if (result) { |
michael@0 | 2204 | NS_ADDREF(*aReturn = result->AsDOMNode()); |
michael@0 | 2205 | } |
michael@0 | 2206 | return rv.ErrorCode(); |
michael@0 | 2207 | } |
michael@0 | 2208 | |
michael@0 | 2209 | nsresult |
michael@0 | 2210 | nsINode::CompareDocumentPosition(nsIDOMNode* aOther, uint16_t* aReturn) |
michael@0 | 2211 | { |
michael@0 | 2212 | nsCOMPtr<nsINode> other = do_QueryInterface(aOther); |
michael@0 | 2213 | NS_ENSURE_ARG(other); |
michael@0 | 2214 | *aReturn = CompareDocumentPosition(*other); |
michael@0 | 2215 | return NS_OK; |
michael@0 | 2216 | } |
michael@0 | 2217 | |
michael@0 | 2218 | nsresult |
michael@0 | 2219 | nsINode::IsEqualNode(nsIDOMNode* aOther, bool* aReturn) |
michael@0 | 2220 | { |
michael@0 | 2221 | nsCOMPtr<nsINode> other = do_QueryInterface(aOther); |
michael@0 | 2222 | *aReturn = IsEqualNode(other); |
michael@0 | 2223 | return NS_OK; |
michael@0 | 2224 | } |
michael@0 | 2225 | |
michael@0 | 2226 | void |
michael@0 | 2227 | nsINode::BindObject(nsISupports* aObject) |
michael@0 | 2228 | { |
michael@0 | 2229 | nsCOMArray<nsISupports>* objects = |
michael@0 | 2230 | static_cast<nsCOMArray<nsISupports>*>(GetProperty(nsGkAtoms::keepobjectsalive)); |
michael@0 | 2231 | if (!objects) { |
michael@0 | 2232 | objects = new nsCOMArray<nsISupports>(); |
michael@0 | 2233 | SetProperty(nsGkAtoms::keepobjectsalive, objects, |
michael@0 | 2234 | nsINode::DeleteProperty< nsCOMArray<nsISupports> >, true); |
michael@0 | 2235 | } |
michael@0 | 2236 | objects->AppendObject(aObject); |
michael@0 | 2237 | } |
michael@0 | 2238 | |
michael@0 | 2239 | void |
michael@0 | 2240 | nsINode::UnbindObject(nsISupports* aObject) |
michael@0 | 2241 | { |
michael@0 | 2242 | nsCOMArray<nsISupports>* objects = |
michael@0 | 2243 | static_cast<nsCOMArray<nsISupports>*>(GetProperty(nsGkAtoms::keepobjectsalive)); |
michael@0 | 2244 | if (objects) { |
michael@0 | 2245 | objects->RemoveObject(aObject); |
michael@0 | 2246 | } |
michael@0 | 2247 | } |
michael@0 | 2248 | |
michael@0 | 2249 | void |
michael@0 | 2250 | nsINode::GetBoundMutationObservers(nsTArray<nsRefPtr<nsDOMMutationObserver> >& aResult) |
michael@0 | 2251 | { |
michael@0 | 2252 | nsCOMArray<nsISupports>* objects = |
michael@0 | 2253 | static_cast<nsCOMArray<nsISupports>*>(GetProperty(nsGkAtoms::keepobjectsalive)); |
michael@0 | 2254 | if (objects) { |
michael@0 | 2255 | for (int32_t i = 0; i < objects->Count(); ++i) { |
michael@0 | 2256 | nsCOMPtr<nsDOMMutationObserver> mo = do_QueryInterface(objects->ObjectAt(i)); |
michael@0 | 2257 | if (mo) { |
michael@0 | 2258 | MOZ_ASSERT(!aResult.Contains(mo)); |
michael@0 | 2259 | aResult.AppendElement(mo); |
michael@0 | 2260 | } |
michael@0 | 2261 | } |
michael@0 | 2262 | } |
michael@0 | 2263 | } |
michael@0 | 2264 | |
michael@0 | 2265 | size_t |
michael@0 | 2266 | nsINode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const |
michael@0 | 2267 | { |
michael@0 | 2268 | size_t n = 0; |
michael@0 | 2269 | EventListenerManager* elm = GetExistingListenerManager(); |
michael@0 | 2270 | if (elm) { |
michael@0 | 2271 | n += elm->SizeOfIncludingThis(aMallocSizeOf); |
michael@0 | 2272 | } |
michael@0 | 2273 | |
michael@0 | 2274 | // Measurement of the following members may be added later if DMD finds it is |
michael@0 | 2275 | // worthwhile: |
michael@0 | 2276 | // - mNodeInfo |
michael@0 | 2277 | // - mSlots |
michael@0 | 2278 | // |
michael@0 | 2279 | // The following members are not measured: |
michael@0 | 2280 | // - mParent, mNextSibling, mPreviousSibling, mFirstChild: because they're |
michael@0 | 2281 | // non-owning |
michael@0 | 2282 | return n; |
michael@0 | 2283 | } |
michael@0 | 2284 | |
michael@0 | 2285 | #define EVENT(name_, id_, type_, struct_) \ |
michael@0 | 2286 | EventHandlerNonNull* nsINode::GetOn##name_() { \ |
michael@0 | 2287 | EventListenerManager *elm = GetExistingListenerManager(); \ |
michael@0 | 2288 | return elm ? elm->GetEventHandler(nsGkAtoms::on##name_, EmptyString()) \ |
michael@0 | 2289 | : nullptr; \ |
michael@0 | 2290 | } \ |
michael@0 | 2291 | void nsINode::SetOn##name_(EventHandlerNonNull* handler) \ |
michael@0 | 2292 | { \ |
michael@0 | 2293 | EventListenerManager *elm = GetOrCreateListenerManager(); \ |
michael@0 | 2294 | if (elm) { \ |
michael@0 | 2295 | elm->SetEventHandler(nsGkAtoms::on##name_, EmptyString(), handler); \ |
michael@0 | 2296 | } \ |
michael@0 | 2297 | } |
michael@0 | 2298 | #define TOUCH_EVENT EVENT |
michael@0 | 2299 | #define DOCUMENT_ONLY_EVENT EVENT |
michael@0 | 2300 | #include "mozilla/EventNameList.h" |
michael@0 | 2301 | #undef DOCUMENT_ONLY_EVENT |
michael@0 | 2302 | #undef TOUCH_EVENT |
michael@0 | 2303 | #undef EVENT |
michael@0 | 2304 | |
michael@0 | 2305 | bool |
michael@0 | 2306 | nsINode::Contains(const nsINode* aOther) const |
michael@0 | 2307 | { |
michael@0 | 2308 | if (aOther == this) { |
michael@0 | 2309 | return true; |
michael@0 | 2310 | } |
michael@0 | 2311 | if (!aOther || |
michael@0 | 2312 | OwnerDoc() != aOther->OwnerDoc() || |
michael@0 | 2313 | IsInDoc() != aOther->IsInDoc() || |
michael@0 | 2314 | !(aOther->IsElement() || |
michael@0 | 2315 | aOther->IsNodeOfType(nsINode::eCONTENT)) || |
michael@0 | 2316 | !GetFirstChild()) { |
michael@0 | 2317 | return false; |
michael@0 | 2318 | } |
michael@0 | 2319 | |
michael@0 | 2320 | const nsIContent* other = static_cast<const nsIContent*>(aOther); |
michael@0 | 2321 | if (this == OwnerDoc()) { |
michael@0 | 2322 | // document.contains(aOther) returns true if aOther is in the document, |
michael@0 | 2323 | // but is not in any anonymous subtree. |
michael@0 | 2324 | // IsInDoc() check is done already before this. |
michael@0 | 2325 | return !other->IsInAnonymousSubtree(); |
michael@0 | 2326 | } |
michael@0 | 2327 | |
michael@0 | 2328 | if (!IsElement() && !IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT)) { |
michael@0 | 2329 | return false; |
michael@0 | 2330 | } |
michael@0 | 2331 | |
michael@0 | 2332 | const nsIContent* thisContent = static_cast<const nsIContent*>(this); |
michael@0 | 2333 | if (thisContent->GetBindingParent() != other->GetBindingParent()) { |
michael@0 | 2334 | return false; |
michael@0 | 2335 | } |
michael@0 | 2336 | |
michael@0 | 2337 | return nsContentUtils::ContentIsDescendantOf(other, this); |
michael@0 | 2338 | } |
michael@0 | 2339 | |
michael@0 | 2340 | nsresult |
michael@0 | 2341 | nsINode::Contains(nsIDOMNode* aOther, bool* aReturn) |
michael@0 | 2342 | { |
michael@0 | 2343 | nsCOMPtr<nsINode> node = do_QueryInterface(aOther); |
michael@0 | 2344 | *aReturn = Contains(node); |
michael@0 | 2345 | return NS_OK; |
michael@0 | 2346 | } |
michael@0 | 2347 | |
michael@0 | 2348 | uint32_t |
michael@0 | 2349 | nsINode::Length() const |
michael@0 | 2350 | { |
michael@0 | 2351 | switch (NodeType()) { |
michael@0 | 2352 | case nsIDOMNode::DOCUMENT_TYPE_NODE: |
michael@0 | 2353 | return 0; |
michael@0 | 2354 | |
michael@0 | 2355 | case nsIDOMNode::TEXT_NODE: |
michael@0 | 2356 | case nsIDOMNode::CDATA_SECTION_NODE: |
michael@0 | 2357 | case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: |
michael@0 | 2358 | case nsIDOMNode::COMMENT_NODE: |
michael@0 | 2359 | MOZ_ASSERT(IsNodeOfType(eCONTENT)); |
michael@0 | 2360 | return static_cast<const nsIContent*>(this)->TextLength(); |
michael@0 | 2361 | |
michael@0 | 2362 | default: |
michael@0 | 2363 | return GetChildCount(); |
michael@0 | 2364 | } |
michael@0 | 2365 | } |
michael@0 | 2366 | |
michael@0 | 2367 | nsCSSSelectorList* |
michael@0 | 2368 | nsINode::ParseSelectorList(const nsAString& aSelectorString, |
michael@0 | 2369 | ErrorResult& aRv) |
michael@0 | 2370 | { |
michael@0 | 2371 | nsIDocument* doc = OwnerDoc(); |
michael@0 | 2372 | nsIDocument::SelectorCache& cache = doc->GetSelectorCache(); |
michael@0 | 2373 | nsCSSSelectorList* selectorList = nullptr; |
michael@0 | 2374 | bool haveCachedList = cache.GetList(aSelectorString, &selectorList); |
michael@0 | 2375 | if (haveCachedList) { |
michael@0 | 2376 | if (!selectorList) { |
michael@0 | 2377 | // Invalid selector. |
michael@0 | 2378 | aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); |
michael@0 | 2379 | } |
michael@0 | 2380 | return selectorList; |
michael@0 | 2381 | } |
michael@0 | 2382 | |
michael@0 | 2383 | nsCSSParser parser(doc->CSSLoader()); |
michael@0 | 2384 | |
michael@0 | 2385 | aRv = parser.ParseSelectorString(aSelectorString, |
michael@0 | 2386 | doc->GetDocumentURI(), |
michael@0 | 2387 | 0, // XXXbz get the line number! |
michael@0 | 2388 | &selectorList); |
michael@0 | 2389 | if (aRv.Failed()) { |
michael@0 | 2390 | // We hit this for syntax errors, which are quite common, so don't |
michael@0 | 2391 | // use NS_ENSURE_SUCCESS. (For example, jQuery has an extended set |
michael@0 | 2392 | // of selectors, but it sees if we can parse them first.) |
michael@0 | 2393 | MOZ_ASSERT(aRv.ErrorCode() == NS_ERROR_DOM_SYNTAX_ERR, |
michael@0 | 2394 | "Unexpected error, so cached version won't return it"); |
michael@0 | 2395 | cache.CacheList(aSelectorString, nullptr); |
michael@0 | 2396 | return nullptr; |
michael@0 | 2397 | } |
michael@0 | 2398 | |
michael@0 | 2399 | // Filter out pseudo-element selectors from selectorList |
michael@0 | 2400 | nsCSSSelectorList** slot = &selectorList; |
michael@0 | 2401 | do { |
michael@0 | 2402 | nsCSSSelectorList* cur = *slot; |
michael@0 | 2403 | if (cur->mSelectors->IsPseudoElement()) { |
michael@0 | 2404 | *slot = cur->mNext; |
michael@0 | 2405 | cur->mNext = nullptr; |
michael@0 | 2406 | delete cur; |
michael@0 | 2407 | } else { |
michael@0 | 2408 | slot = &cur->mNext; |
michael@0 | 2409 | } |
michael@0 | 2410 | } while (*slot); |
michael@0 | 2411 | |
michael@0 | 2412 | if (selectorList) { |
michael@0 | 2413 | NS_ASSERTION(selectorList->mSelectors, |
michael@0 | 2414 | "How can we not have any selectors?"); |
michael@0 | 2415 | cache.CacheList(aSelectorString, selectorList); |
michael@0 | 2416 | } else { |
michael@0 | 2417 | // This is the "only pseudo-element selectors" case, which is |
michael@0 | 2418 | // not common, so just don't worry about caching it. That way a |
michael@0 | 2419 | // null cached value can always indicate an invalid selector. |
michael@0 | 2420 | } |
michael@0 | 2421 | |
michael@0 | 2422 | return selectorList; |
michael@0 | 2423 | } |
michael@0 | 2424 | |
michael@0 | 2425 | static void |
michael@0 | 2426 | AddScopeElements(TreeMatchContext& aMatchContext, |
michael@0 | 2427 | nsINode* aMatchContextNode) |
michael@0 | 2428 | { |
michael@0 | 2429 | if (aMatchContextNode->IsElement()) { |
michael@0 | 2430 | aMatchContext.SetHasSpecifiedScope(); |
michael@0 | 2431 | aMatchContext.AddScopeElement(aMatchContextNode->AsElement()); |
michael@0 | 2432 | } |
michael@0 | 2433 | } |
michael@0 | 2434 | |
michael@0 | 2435 | namespace { |
michael@0 | 2436 | struct SelectorMatchInfo { |
michael@0 | 2437 | nsCSSSelectorList* const mSelectorList; |
michael@0 | 2438 | TreeMatchContext& mMatchContext; |
michael@0 | 2439 | }; |
michael@0 | 2440 | } |
michael@0 | 2441 | |
michael@0 | 2442 | // Given an id, find elements with that id under aRoot that match aMatchInfo if |
michael@0 | 2443 | // any is provided. If no SelectorMatchInfo is provided, just find the ones |
michael@0 | 2444 | // with the given id. aRoot must be in the document. |
michael@0 | 2445 | template<bool onlyFirstMatch, class T> |
michael@0 | 2446 | inline static void |
michael@0 | 2447 | FindMatchingElementsWithId(const nsAString& aId, nsINode* aRoot, |
michael@0 | 2448 | SelectorMatchInfo* aMatchInfo, |
michael@0 | 2449 | T& aList) |
michael@0 | 2450 | { |
michael@0 | 2451 | MOZ_ASSERT(aRoot->IsInDoc(), |
michael@0 | 2452 | "Don't call me if the root is not in the document"); |
michael@0 | 2453 | MOZ_ASSERT(aRoot->IsElement() || aRoot->IsNodeOfType(nsINode::eDOCUMENT), |
michael@0 | 2454 | "The optimization below to check ContentIsDescendantOf only for " |
michael@0 | 2455 | "elements depends on aRoot being either an element or a " |
michael@0 | 2456 | "document if it's in the document. Note that document fragments " |
michael@0 | 2457 | "can't be IsInDoc(), so should never show up here."); |
michael@0 | 2458 | |
michael@0 | 2459 | const nsSmallVoidArray* elements = aRoot->OwnerDoc()->GetAllElementsForId(aId); |
michael@0 | 2460 | |
michael@0 | 2461 | if (!elements) { |
michael@0 | 2462 | // Nothing to do; we're done |
michael@0 | 2463 | return; |
michael@0 | 2464 | } |
michael@0 | 2465 | |
michael@0 | 2466 | // XXXbz: Should we fall back to the tree walk if aRoot is not the |
michael@0 | 2467 | // document and |elements| is long, for some value of "long"? |
michael@0 | 2468 | for (int32_t i = 0; i < elements->Count(); ++i) { |
michael@0 | 2469 | Element *element = static_cast<Element*>(elements->ElementAt(i)); |
michael@0 | 2470 | if (!aRoot->IsElement() || |
michael@0 | 2471 | (element != aRoot && |
michael@0 | 2472 | nsContentUtils::ContentIsDescendantOf(element, aRoot))) { |
michael@0 | 2473 | // We have an element with the right id and it's a strict descendant |
michael@0 | 2474 | // of aRoot. Make sure it really matches the selector. |
michael@0 | 2475 | if (!aMatchInfo || |
michael@0 | 2476 | nsCSSRuleProcessor::SelectorListMatches(element, |
michael@0 | 2477 | aMatchInfo->mMatchContext, |
michael@0 | 2478 | aMatchInfo->mSelectorList)) { |
michael@0 | 2479 | aList.AppendElement(element); |
michael@0 | 2480 | if (onlyFirstMatch) { |
michael@0 | 2481 | return; |
michael@0 | 2482 | } |
michael@0 | 2483 | } |
michael@0 | 2484 | } |
michael@0 | 2485 | } |
michael@0 | 2486 | } |
michael@0 | 2487 | |
michael@0 | 2488 | // Actually find elements matching aSelectorList (which must not be |
michael@0 | 2489 | // null) and which are descendants of aRoot and put them in aList. If |
michael@0 | 2490 | // onlyFirstMatch, then stop once the first one is found. |
michael@0 | 2491 | template<bool onlyFirstMatch, class Collector, class T> |
michael@0 | 2492 | MOZ_ALWAYS_INLINE static void |
michael@0 | 2493 | FindMatchingElements(nsINode* aRoot, nsCSSSelectorList* aSelectorList, T &aList, |
michael@0 | 2494 | ErrorResult& aRv) |
michael@0 | 2495 | { |
michael@0 | 2496 | nsIDocument* doc = aRoot->OwnerDoc(); |
michael@0 | 2497 | |
michael@0 | 2498 | TreeMatchContext matchingContext(false, nsRuleWalker::eRelevantLinkUnvisited, |
michael@0 | 2499 | doc, TreeMatchContext::eNeverMatchVisited); |
michael@0 | 2500 | doc->FlushPendingLinkUpdates(); |
michael@0 | 2501 | AddScopeElements(matchingContext, aRoot); |
michael@0 | 2502 | |
michael@0 | 2503 | // Fast-path selectors involving IDs. We can only do this if aRoot |
michael@0 | 2504 | // is in the document and the document is not in quirks mode, since |
michael@0 | 2505 | // ID selectors are case-insensitive in quirks mode. Also, only do |
michael@0 | 2506 | // this if aSelectorList only has one selector, because otherwise |
michael@0 | 2507 | // ordering the elements correctly is a pain. |
michael@0 | 2508 | NS_ASSERTION(aRoot->IsElement() || aRoot->IsNodeOfType(nsINode::eDOCUMENT) || |
michael@0 | 2509 | !aRoot->IsInDoc(), |
michael@0 | 2510 | "The optimization below to check ContentIsDescendantOf only for " |
michael@0 | 2511 | "elements depends on aRoot being either an element or a " |
michael@0 | 2512 | "document if it's in the document."); |
michael@0 | 2513 | if (aRoot->IsInDoc() && |
michael@0 | 2514 | doc->GetCompatibilityMode() != eCompatibility_NavQuirks && |
michael@0 | 2515 | !aSelectorList->mNext && |
michael@0 | 2516 | aSelectorList->mSelectors->mIDList) { |
michael@0 | 2517 | nsIAtom* id = aSelectorList->mSelectors->mIDList->mAtom; |
michael@0 | 2518 | SelectorMatchInfo info = { aSelectorList, matchingContext }; |
michael@0 | 2519 | FindMatchingElementsWithId<onlyFirstMatch, T>(nsDependentAtomString(id), |
michael@0 | 2520 | aRoot, &info, aList); |
michael@0 | 2521 | return; |
michael@0 | 2522 | } |
michael@0 | 2523 | |
michael@0 | 2524 | Collector results; |
michael@0 | 2525 | for (nsIContent* cur = aRoot->GetFirstChild(); |
michael@0 | 2526 | cur; |
michael@0 | 2527 | cur = cur->GetNextNode(aRoot)) { |
michael@0 | 2528 | if (cur->IsElement() && |
michael@0 | 2529 | nsCSSRuleProcessor::SelectorListMatches(cur->AsElement(), |
michael@0 | 2530 | matchingContext, |
michael@0 | 2531 | aSelectorList)) { |
michael@0 | 2532 | if (onlyFirstMatch) { |
michael@0 | 2533 | aList.AppendElement(cur->AsElement()); |
michael@0 | 2534 | return; |
michael@0 | 2535 | } |
michael@0 | 2536 | results.AppendElement(cur->AsElement()); |
michael@0 | 2537 | } |
michael@0 | 2538 | } |
michael@0 | 2539 | |
michael@0 | 2540 | const uint32_t len = results.Length(); |
michael@0 | 2541 | if (len) { |
michael@0 | 2542 | aList.SetCapacity(len); |
michael@0 | 2543 | for (uint32_t i = 0; i < len; ++i) { |
michael@0 | 2544 | aList.AppendElement(results.ElementAt(i)); |
michael@0 | 2545 | } |
michael@0 | 2546 | } |
michael@0 | 2547 | } |
michael@0 | 2548 | |
michael@0 | 2549 | struct ElementHolder { |
michael@0 | 2550 | ElementHolder() : mElement(nullptr) {} |
michael@0 | 2551 | void AppendElement(Element* aElement) { |
michael@0 | 2552 | NS_ABORT_IF_FALSE(!mElement, "Should only get one element"); |
michael@0 | 2553 | mElement = aElement; |
michael@0 | 2554 | } |
michael@0 | 2555 | void SetCapacity(uint32_t aCapacity) { MOZ_CRASH("Don't call me!"); } |
michael@0 | 2556 | uint32_t Length() { return 0; } |
michael@0 | 2557 | Element* ElementAt(uint32_t aIndex) { return nullptr; } |
michael@0 | 2558 | |
michael@0 | 2559 | Element* mElement; |
michael@0 | 2560 | }; |
michael@0 | 2561 | |
michael@0 | 2562 | Element* |
michael@0 | 2563 | nsINode::QuerySelector(const nsAString& aSelector, ErrorResult& aResult) |
michael@0 | 2564 | { |
michael@0 | 2565 | nsCSSSelectorList* selectorList = ParseSelectorList(aSelector, aResult); |
michael@0 | 2566 | if (!selectorList) { |
michael@0 | 2567 | // Either we failed (and aResult already has the exception), or this |
michael@0 | 2568 | // is a pseudo-element-only selector that matches nothing. |
michael@0 | 2569 | return nullptr; |
michael@0 | 2570 | } |
michael@0 | 2571 | ElementHolder holder; |
michael@0 | 2572 | FindMatchingElements<true, ElementHolder>(this, selectorList, holder, aResult); |
michael@0 | 2573 | return holder.mElement; |
michael@0 | 2574 | } |
michael@0 | 2575 | |
michael@0 | 2576 | already_AddRefed<nsINodeList> |
michael@0 | 2577 | nsINode::QuerySelectorAll(const nsAString& aSelector, ErrorResult& aResult) |
michael@0 | 2578 | { |
michael@0 | 2579 | nsRefPtr<nsSimpleContentList> contentList = new nsSimpleContentList(this); |
michael@0 | 2580 | |
michael@0 | 2581 | nsCSSSelectorList* selectorList = ParseSelectorList(aSelector, aResult); |
michael@0 | 2582 | if (selectorList) { |
michael@0 | 2583 | FindMatchingElements<false, nsAutoTArray<Element*, 128>>(this, |
michael@0 | 2584 | selectorList, |
michael@0 | 2585 | *contentList, |
michael@0 | 2586 | aResult); |
michael@0 | 2587 | } else { |
michael@0 | 2588 | // Either we failed (and aResult already has the exception), or this |
michael@0 | 2589 | // is a pseudo-element-only selector that matches nothing. |
michael@0 | 2590 | } |
michael@0 | 2591 | |
michael@0 | 2592 | return contentList.forget(); |
michael@0 | 2593 | } |
michael@0 | 2594 | |
michael@0 | 2595 | nsresult |
michael@0 | 2596 | nsINode::QuerySelector(const nsAString& aSelector, nsIDOMElement **aReturn) |
michael@0 | 2597 | { |
michael@0 | 2598 | ErrorResult rv; |
michael@0 | 2599 | Element* result = nsINode::QuerySelector(aSelector, rv); |
michael@0 | 2600 | if (rv.Failed()) { |
michael@0 | 2601 | return rv.ErrorCode(); |
michael@0 | 2602 | } |
michael@0 | 2603 | nsCOMPtr<nsIDOMElement> elt = do_QueryInterface(result); |
michael@0 | 2604 | elt.forget(aReturn); |
michael@0 | 2605 | return NS_OK; |
michael@0 | 2606 | } |
michael@0 | 2607 | |
michael@0 | 2608 | nsresult |
michael@0 | 2609 | nsINode::QuerySelectorAll(const nsAString& aSelector, nsIDOMNodeList **aReturn) |
michael@0 | 2610 | { |
michael@0 | 2611 | ErrorResult rv; |
michael@0 | 2612 | *aReturn = nsINode::QuerySelectorAll(aSelector, rv).take(); |
michael@0 | 2613 | return rv.ErrorCode(); |
michael@0 | 2614 | } |
michael@0 | 2615 | |
michael@0 | 2616 | Element* |
michael@0 | 2617 | nsINode::GetElementById(const nsAString& aId) |
michael@0 | 2618 | { |
michael@0 | 2619 | MOZ_ASSERT(IsElement() || IsNodeOfType(eDOCUMENT_FRAGMENT), |
michael@0 | 2620 | "Bogus this object for GetElementById call"); |
michael@0 | 2621 | if (IsInDoc()) { |
michael@0 | 2622 | ElementHolder holder; |
michael@0 | 2623 | FindMatchingElementsWithId<true>(aId, this, nullptr, holder); |
michael@0 | 2624 | return holder.mElement; |
michael@0 | 2625 | } |
michael@0 | 2626 | |
michael@0 | 2627 | for (nsIContent* kid = GetFirstChild(); kid; kid = kid->GetNextNode(this)) { |
michael@0 | 2628 | if (!kid->IsElement()) { |
michael@0 | 2629 | continue; |
michael@0 | 2630 | } |
michael@0 | 2631 | nsIAtom* id = kid->AsElement()->GetID(); |
michael@0 | 2632 | if (id && id->Equals(aId)) { |
michael@0 | 2633 | return kid->AsElement(); |
michael@0 | 2634 | } |
michael@0 | 2635 | } |
michael@0 | 2636 | return nullptr; |
michael@0 | 2637 | } |
michael@0 | 2638 | |
michael@0 | 2639 | JSObject* |
michael@0 | 2640 | nsINode::WrapObject(JSContext *aCx) |
michael@0 | 2641 | { |
michael@0 | 2642 | MOZ_ASSERT(IsDOMBinding()); |
michael@0 | 2643 | |
michael@0 | 2644 | // Make sure one of these is true |
michael@0 | 2645 | // (1) our owner document has a script handling object, |
michael@0 | 2646 | // (2) Our owner document has had a script handling object, or has been marked |
michael@0 | 2647 | // to have had one, |
michael@0 | 2648 | // (3) we are running a privileged script. |
michael@0 | 2649 | // Event handling is possible only if (1). If (2) event handling is |
michael@0 | 2650 | // prevented. |
michael@0 | 2651 | // If the document has never had a script handling object, untrusted |
michael@0 | 2652 | // scripts (3) shouldn't touch it! |
michael@0 | 2653 | bool hasHadScriptHandlingObject = false; |
michael@0 | 2654 | if (!OwnerDoc()->GetScriptHandlingObject(hasHadScriptHandlingObject) && |
michael@0 | 2655 | !hasHadScriptHandlingObject && |
michael@0 | 2656 | !nsContentUtils::IsCallerChrome()) { |
michael@0 | 2657 | Throw(aCx, NS_ERROR_UNEXPECTED); |
michael@0 | 2658 | return nullptr; |
michael@0 | 2659 | } |
michael@0 | 2660 | |
michael@0 | 2661 | JS::Rooted<JSObject*> obj(aCx, WrapNode(aCx)); |
michael@0 | 2662 | MOZ_ASSERT_IF(ChromeOnlyAccess(), |
michael@0 | 2663 | xpc::IsInXBLScope(obj) || !xpc::UseXBLScope(js::GetObjectCompartment(obj))); |
michael@0 | 2664 | return obj; |
michael@0 | 2665 | } |
michael@0 | 2666 | |
michael@0 | 2667 | already_AddRefed<nsINode> |
michael@0 | 2668 | nsINode::CloneNode(bool aDeep, ErrorResult& aError) |
michael@0 | 2669 | { |
michael@0 | 2670 | bool callUserDataHandlers = NodeType() != nsIDOMNode::DOCUMENT_NODE || |
michael@0 | 2671 | !static_cast<nsIDocument*>(this)->CreatingStaticClone(); |
michael@0 | 2672 | |
michael@0 | 2673 | nsCOMPtr<nsINode> result; |
michael@0 | 2674 | aError = nsNodeUtils::CloneNodeImpl(this, aDeep, callUserDataHandlers, |
michael@0 | 2675 | getter_AddRefs(result)); |
michael@0 | 2676 | return result.forget(); |
michael@0 | 2677 | } |
michael@0 | 2678 | |
michael@0 | 2679 | nsDOMAttributeMap* |
michael@0 | 2680 | nsINode::GetAttributes() |
michael@0 | 2681 | { |
michael@0 | 2682 | if (!IsElement()) { |
michael@0 | 2683 | return nullptr; |
michael@0 | 2684 | } |
michael@0 | 2685 | return AsElement()->Attributes(); |
michael@0 | 2686 | } |
michael@0 | 2687 | |
michael@0 | 2688 | bool |
michael@0 | 2689 | EventTarget::DispatchEvent(Event& aEvent, |
michael@0 | 2690 | ErrorResult& aRv) |
michael@0 | 2691 | { |
michael@0 | 2692 | bool result = false; |
michael@0 | 2693 | aRv = DispatchEvent(&aEvent, &result); |
michael@0 | 2694 | return result; |
michael@0 | 2695 | } |