michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Base class for all DOM nodes. michael@0: */ michael@0: michael@0: #include "nsINode.h" michael@0: michael@0: #include "AccessCheck.h" michael@0: #include "jsapi.h" michael@0: #include "mozAutoDocUpdate.h" michael@0: #include "mozilla/AsyncEventDispatcher.h" michael@0: #include "mozilla/CORSMode.h" michael@0: #include "mozilla/EventDispatcher.h" michael@0: #include "mozilla/EventListenerManager.h" michael@0: #include "mozilla/InternalMutationEvent.h" michael@0: #include "mozilla/Likely.h" michael@0: #include "mozilla/MemoryReporting.h" michael@0: #include "mozilla/Telemetry.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "mozilla/dom/Event.h" michael@0: #include "mozilla/dom/ShadowRoot.h" michael@0: #include "nsAttrValueOrString.h" michael@0: #include "nsBindingManager.h" michael@0: #include "nsCCUncollectableMarker.h" michael@0: #include "nsContentCreatorFunctions.h" michael@0: #include "nsContentList.h" michael@0: #include "nsContentUtils.h" michael@0: #include "nsCycleCollectionParticipant.h" michael@0: #include "nsDocument.h" michael@0: #include "mozilla/dom/Attr.h" michael@0: #include "nsDOMAttributeMap.h" michael@0: #include "nsDOMCID.h" michael@0: #include "nsDOMCSSAttrDeclaration.h" michael@0: #include "nsError.h" michael@0: #include "nsDOMMutationObserver.h" michael@0: #include "nsDOMString.h" michael@0: #include "nsDOMTokenList.h" michael@0: #include "nsFocusManager.h" michael@0: #include "nsFrameManager.h" michael@0: #include "nsFrameSelection.h" michael@0: #include "nsGenericHTMLElement.h" michael@0: #include "nsGkAtoms.h" michael@0: #include "nsIAnonymousContentCreator.h" michael@0: #include "nsIAtom.h" michael@0: #include "nsIBaseWindow.h" michael@0: #include "nsICategoryManager.h" michael@0: #include "nsIContentIterator.h" michael@0: #include "nsIControllers.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIDOMDocument.h" michael@0: #include "nsIDOMDocumentType.h" michael@0: #include "nsIDOMEvent.h" michael@0: #include "nsIDOMEventListener.h" michael@0: #include "nsIDOMMutationEvent.h" michael@0: #include "nsIDOMNodeList.h" michael@0: #include "nsIDOMUserDataHandler.h" michael@0: #include "nsIEditor.h" michael@0: #include "nsIEditorIMESupport.h" michael@0: #include "nsILinkHandler.h" michael@0: #include "nsINodeInfo.h" michael@0: #include "nsIPresShell.h" michael@0: #include "nsIScriptError.h" michael@0: #include "nsIScriptGlobalObject.h" michael@0: #include "nsIScriptSecurityManager.h" michael@0: #include "nsIScrollableFrame.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIURL.h" michael@0: #include "nsView.h" michael@0: #include "nsViewManager.h" michael@0: #include "nsIWebNavigation.h" michael@0: #include "nsIWidget.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "nsNameSpaceManager.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsNodeInfoManager.h" michael@0: #include "nsNodeUtils.h" michael@0: #include "nsPIBoxObject.h" michael@0: #include "nsPIDOMWindow.h" michael@0: #include "nsPresContext.h" michael@0: #include "nsRuleProcessorData.h" michael@0: #include "nsString.h" michael@0: #include "nsStyleConsts.h" michael@0: #include "nsSVGFeatures.h" michael@0: #include "nsSVGUtils.h" michael@0: #include "nsTextNode.h" michael@0: #include "nsUnicharUtils.h" michael@0: #include "nsXBLBinding.h" michael@0: #include "nsXBLPrototypeBinding.h" michael@0: #include "prprf.h" michael@0: #include "xpcpublic.h" michael@0: #include "nsCSSRuleProcessor.h" michael@0: #include "nsCSSParser.h" michael@0: #include "HTMLLegendElement.h" michael@0: #include "nsWrapperCacheInlines.h" michael@0: #include "WrapperFactory.h" michael@0: #include "DocumentType.h" michael@0: #include michael@0: #include "nsGlobalWindow.h" michael@0: #include "nsDOMMutationObserver.h" michael@0: #include "GeometryUtils.h" michael@0: michael@0: using namespace mozilla; michael@0: using namespace mozilla::dom; michael@0: michael@0: nsINode::nsSlots::~nsSlots() michael@0: { michael@0: if (mChildNodes) { michael@0: mChildNodes->DropReference(); michael@0: NS_RELEASE(mChildNodes); michael@0: } michael@0: michael@0: if (mWeakReference) { michael@0: mWeakReference->NoticeNodeDestruction(); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsINode::nsSlots::Traverse(nsCycleCollectionTraversalCallback &cb) michael@0: { michael@0: NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mSlots->mChildNodes"); michael@0: cb.NoteXPCOMChild(mChildNodes); michael@0: } michael@0: michael@0: void michael@0: nsINode::nsSlots::Unlink() michael@0: { michael@0: if (mChildNodes) { michael@0: mChildNodes->DropReference(); michael@0: NS_RELEASE(mChildNodes); michael@0: } michael@0: } michael@0: michael@0: //---------------------------------------------------------------------- michael@0: michael@0: nsINode::~nsINode() michael@0: { michael@0: MOZ_ASSERT(!HasSlots(), "nsNodeUtils::LastRelease was not called?"); michael@0: MOZ_ASSERT(mSubtreeRoot == this, "Didn't restore state properly?"); michael@0: } michael@0: michael@0: void* michael@0: nsINode::GetProperty(uint16_t aCategory, nsIAtom *aPropertyName, michael@0: nsresult *aStatus) const michael@0: { michael@0: return OwnerDoc()->PropertyTable(aCategory)->GetProperty(this, aPropertyName, michael@0: aStatus); michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::SetProperty(uint16_t aCategory, nsIAtom *aPropertyName, void *aValue, michael@0: NSPropertyDtorFunc aDtor, bool aTransfer, michael@0: void **aOldValue) michael@0: { michael@0: nsresult rv = OwnerDoc()->PropertyTable(aCategory)->SetProperty(this, michael@0: aPropertyName, michael@0: aValue, aDtor, michael@0: nullptr, michael@0: aTransfer, michael@0: aOldValue); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: SetFlags(NODE_HAS_PROPERTIES); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: void michael@0: nsINode::DeleteProperty(uint16_t aCategory, nsIAtom *aPropertyName) michael@0: { michael@0: OwnerDoc()->PropertyTable(aCategory)->DeleteProperty(this, aPropertyName); michael@0: } michael@0: michael@0: void* michael@0: nsINode::UnsetProperty(uint16_t aCategory, nsIAtom *aPropertyName, michael@0: nsresult *aStatus) michael@0: { michael@0: return OwnerDoc()->PropertyTable(aCategory)->UnsetProperty(this, michael@0: aPropertyName, michael@0: aStatus); michael@0: } michael@0: michael@0: nsINode::nsSlots* michael@0: nsINode::CreateSlots() michael@0: { michael@0: return new nsSlots(); michael@0: } michael@0: michael@0: bool michael@0: nsINode::IsEditableInternal() const michael@0: { michael@0: if (HasFlag(NODE_IS_EDITABLE)) { michael@0: // The node is in an editable contentEditable subtree. michael@0: return true; michael@0: } michael@0: michael@0: nsIDocument *doc = GetCurrentDoc(); michael@0: michael@0: // Check if the node is in a document and the document is in designMode. michael@0: return doc && doc->HasFlag(NODE_IS_EDITABLE); michael@0: } michael@0: michael@0: static nsIContent* GetEditorRootContent(nsIEditor* aEditor) michael@0: { michael@0: nsCOMPtr rootElement; michael@0: aEditor->GetRootElement(getter_AddRefs(rootElement)); michael@0: nsCOMPtr rootContent(do_QueryInterface(rootElement)); michael@0: return rootContent; michael@0: } michael@0: michael@0: nsIContent* michael@0: nsINode::GetTextEditorRootContent(nsIEditor** aEditor) michael@0: { michael@0: if (aEditor) michael@0: *aEditor = nullptr; michael@0: for (nsINode* node = this; node; node = node->GetParentNode()) { michael@0: if (!node->IsElement() || michael@0: !node->AsElement()->IsHTML()) michael@0: continue; michael@0: michael@0: nsCOMPtr editor = michael@0: static_cast(node)->GetEditorInternal(); michael@0: if (!editor) michael@0: continue; michael@0: michael@0: nsIContent* rootContent = GetEditorRootContent(editor); michael@0: if (aEditor) michael@0: editor.swap(*aEditor); michael@0: return rootContent; michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: static nsIContent* GetRootForContentSubtree(nsIContent* aContent) michael@0: { michael@0: NS_ENSURE_TRUE(aContent, nullptr); michael@0: michael@0: // Special case for ShadowRoot because the ShadowRoot itself is michael@0: // the root. This is necessary to prevent selection from crossing michael@0: // the ShadowRoot boundary. michael@0: ShadowRoot* containingShadow = aContent->GetContainingShadow(); michael@0: if (containingShadow) { michael@0: return containingShadow; michael@0: } michael@0: michael@0: nsIContent* stop = aContent->GetBindingParent(); michael@0: while (aContent) { michael@0: nsIContent* parent = aContent->GetParent(); michael@0: if (parent == stop) { michael@0: break; michael@0: } michael@0: aContent = parent; michael@0: } michael@0: return aContent; michael@0: } michael@0: michael@0: nsIContent* michael@0: nsINode::GetSelectionRootContent(nsIPresShell* aPresShell) michael@0: { michael@0: NS_ENSURE_TRUE(aPresShell, nullptr); michael@0: michael@0: if (IsNodeOfType(eDOCUMENT)) michael@0: return static_cast(this)->GetRootElement(); michael@0: if (!IsNodeOfType(eCONTENT)) michael@0: return nullptr; michael@0: michael@0: if (GetCurrentDoc() != aPresShell->GetDocument()) { michael@0: return nullptr; michael@0: } michael@0: michael@0: if (static_cast(this)->HasIndependentSelection()) { michael@0: // This node should be a descendant of input/textarea editor. michael@0: nsIContent* content = GetTextEditorRootContent(); michael@0: if (content) michael@0: return content; michael@0: } michael@0: michael@0: nsPresContext* presContext = aPresShell->GetPresContext(); michael@0: if (presContext) { michael@0: nsIEditor* editor = nsContentUtils::GetHTMLEditor(presContext); michael@0: if (editor) { michael@0: // This node is in HTML editor. michael@0: nsIDocument* doc = GetCurrentDoc(); michael@0: if (!doc || doc->HasFlag(NODE_IS_EDITABLE) || michael@0: !HasFlag(NODE_IS_EDITABLE)) { michael@0: nsIContent* editorRoot = GetEditorRootContent(editor); michael@0: NS_ENSURE_TRUE(editorRoot, nullptr); michael@0: return nsContentUtils::IsInSameAnonymousTree(this, editorRoot) ? michael@0: editorRoot : michael@0: GetRootForContentSubtree(static_cast(this)); michael@0: } michael@0: // If the document isn't editable but this is editable, this is in michael@0: // contenteditable. Use the editing host element for selection root. michael@0: return static_cast(this)->GetEditingHost(); michael@0: } michael@0: } michael@0: michael@0: nsRefPtr fs = aPresShell->FrameSelection(); michael@0: nsIContent* content = fs->GetLimiter(); michael@0: if (!content) { michael@0: content = fs->GetAncestorLimiter(); michael@0: if (!content) { michael@0: nsIDocument* doc = aPresShell->GetDocument(); michael@0: NS_ENSURE_TRUE(doc, nullptr); michael@0: content = doc->GetRootElement(); michael@0: if (!content) michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: // This node might be in another subtree, if so, we should find this subtree's michael@0: // root. Otherwise, we can return the content simply. michael@0: NS_ENSURE_TRUE(content, nullptr); michael@0: if (!nsContentUtils::IsInSameAnonymousTree(this, content)) { michael@0: content = GetRootForContentSubtree(static_cast(this)); michael@0: // Fixup for ShadowRoot because the ShadowRoot itself does not have a frame. michael@0: // Use the host as the root. michael@0: ShadowRoot* shadowRoot = ShadowRoot::FromNode(content); michael@0: if (shadowRoot) { michael@0: content = shadowRoot->GetHost(); michael@0: } michael@0: } michael@0: michael@0: return content; michael@0: } michael@0: michael@0: nsINodeList* michael@0: nsINode::ChildNodes() michael@0: { michael@0: nsSlots* slots = Slots(); michael@0: if (!slots->mChildNodes) { michael@0: slots->mChildNodes = new nsChildContentList(this); michael@0: if (slots->mChildNodes) { michael@0: NS_ADDREF(slots->mChildNodes); michael@0: } michael@0: } michael@0: michael@0: return slots->mChildNodes; michael@0: } michael@0: michael@0: void michael@0: nsINode::GetTextContentInternal(nsAString& aTextContent) michael@0: { michael@0: SetDOMStringToNull(aTextContent); michael@0: } michael@0: michael@0: #ifdef DEBUG michael@0: void michael@0: nsINode::CheckNotNativeAnonymous() const michael@0: { michael@0: if (!IsNodeOfType(eCONTENT)) michael@0: return; michael@0: nsIContent* content = static_cast(this)->GetBindingParent(); michael@0: while (content) { michael@0: if (content->IsRootOfNativeAnonymousSubtree()) { michael@0: NS_ERROR("Element not marked to be in native anonymous subtree!"); michael@0: break; michael@0: } michael@0: content = content->GetBindingParent(); michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: bool michael@0: nsINode::IsInAnonymousSubtree() const michael@0: { michael@0: if (!IsContent()) { michael@0: return false; michael@0: } michael@0: michael@0: return AsContent()->IsInAnonymousSubtree(); michael@0: } michael@0: michael@0: bool michael@0: nsINode::IsAnonymousContentInSVGUseSubtree() const michael@0: { michael@0: MOZ_ASSERT(IsInAnonymousSubtree()); michael@0: nsIContent* parent = AsContent()->GetBindingParent(); michael@0: // Watch out for parentless native-anonymous subtrees. michael@0: return parent && parent->IsSVG(nsGkAtoms::use); michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::GetParentNode(nsIDOMNode** aParentNode) michael@0: { michael@0: *aParentNode = nullptr; michael@0: michael@0: nsINode *parent = GetParentNode(); michael@0: michael@0: return parent ? CallQueryInterface(parent, aParentNode) : NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::GetParentElement(nsIDOMElement** aParentElement) michael@0: { michael@0: *aParentElement = nullptr; michael@0: nsINode* parent = GetParentElement(); michael@0: return parent ? CallQueryInterface(parent, aParentElement) : NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::GetChildNodes(nsIDOMNodeList** aChildNodes) michael@0: { michael@0: NS_ADDREF(*aChildNodes = ChildNodes()); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::GetFirstChild(nsIDOMNode** aNode) michael@0: { michael@0: nsIContent* child = GetFirstChild(); michael@0: if (child) { michael@0: return CallQueryInterface(child, aNode); michael@0: } michael@0: michael@0: *aNode = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::GetLastChild(nsIDOMNode** aNode) michael@0: { michael@0: nsIContent* child = GetLastChild(); michael@0: if (child) { michael@0: return CallQueryInterface(child, aNode); michael@0: } michael@0: michael@0: *aNode = nullptr; michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::GetPreviousSibling(nsIDOMNode** aPrevSibling) michael@0: { michael@0: *aPrevSibling = nullptr; michael@0: michael@0: nsIContent *sibling = GetPreviousSibling(); michael@0: michael@0: return sibling ? CallQueryInterface(sibling, aPrevSibling) : NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::GetNextSibling(nsIDOMNode** aNextSibling) michael@0: { michael@0: *aNextSibling = nullptr; michael@0: michael@0: nsIContent *sibling = GetNextSibling(); michael@0: michael@0: return sibling ? CallQueryInterface(sibling, aNextSibling) : NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::GetOwnerDocument(nsIDOMDocument** aOwnerDocument) michael@0: { michael@0: *aOwnerDocument = nullptr; michael@0: michael@0: nsIDocument *ownerDoc = GetOwnerDocument(); michael@0: michael@0: return ownerDoc ? CallQueryInterface(ownerDoc, aOwnerDocument) : NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsINode::GetNodeValueInternal(nsAString& aNodeValue) michael@0: { michael@0: SetDOMStringToNull(aNodeValue); michael@0: } michael@0: michael@0: nsINode* michael@0: nsINode::RemoveChild(nsINode& aOldChild, ErrorResult& aError) michael@0: { michael@0: if (IsNodeOfType(eDATA_NODE)) { michael@0: // aOldChild can't be one of our children. michael@0: aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: if (aOldChild.GetParentNode() == this) { michael@0: nsContentUtils::MaybeFireNodeRemoved(&aOldChild, this, OwnerDoc()); michael@0: } michael@0: michael@0: int32_t index = IndexOf(&aOldChild); michael@0: if (index == -1) { michael@0: // aOldChild isn't one of our children. michael@0: aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: RemoveChildAt(index, true); michael@0: return &aOldChild; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::RemoveChild(nsIDOMNode* aOldChild, nsIDOMNode** aReturn) michael@0: { michael@0: nsCOMPtr oldChild = do_QueryInterface(aOldChild); michael@0: if (!oldChild) { michael@0: return NS_ERROR_NULL_POINTER; michael@0: } michael@0: michael@0: ErrorResult rv; michael@0: RemoveChild(*oldChild, rv); michael@0: if (!rv.Failed()) { michael@0: NS_ADDREF(*aReturn = aOldChild); michael@0: } michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsINode::Normalize() michael@0: { michael@0: // First collect list of nodes to be removed michael@0: nsAutoTArray, 50> nodes; michael@0: michael@0: bool canMerge = false; michael@0: for (nsIContent* node = this->GetFirstChild(); michael@0: node; michael@0: node = node->GetNextNode(this)) { michael@0: if (node->NodeType() != nsIDOMNode::TEXT_NODE) { michael@0: canMerge = false; michael@0: continue; michael@0: } michael@0: michael@0: if (canMerge || node->TextLength() == 0) { michael@0: // No need to touch canMerge. That way we can merge across empty michael@0: // textnodes if and only if the node before is a textnode michael@0: nodes.AppendElement(node); michael@0: } michael@0: else { michael@0: canMerge = true; michael@0: } michael@0: michael@0: // If there's no following sibling, then we need to ensure that we don't michael@0: // collect following siblings of our (grand)parent as to-be-removed michael@0: canMerge = canMerge && !!node->GetNextSibling(); michael@0: } michael@0: michael@0: if (nodes.IsEmpty()) { michael@0: return; michael@0: } michael@0: michael@0: // We're relying on mozAutoSubtreeModified to keep the doc alive here. michael@0: nsIDocument* doc = OwnerDoc(); michael@0: michael@0: // Batch possible DOMSubtreeModified events. michael@0: mozAutoSubtreeModified subtree(doc, nullptr); michael@0: michael@0: // Fire all DOMNodeRemoved events. Optimize the common case of there being michael@0: // no listeners michael@0: bool hasRemoveListeners = nsContentUtils:: michael@0: HasMutationListeners(doc, NS_EVENT_BITS_MUTATION_NODEREMOVED); michael@0: if (hasRemoveListeners) { michael@0: for (uint32_t i = 0; i < nodes.Length(); ++i) { michael@0: nsINode* parentNode = nodes[i]->GetParentNode(); michael@0: if (parentNode) { // Node may have already been removed. michael@0: nsContentUtils::MaybeFireNodeRemoved(nodes[i], parentNode, michael@0: doc); michael@0: } michael@0: } michael@0: } michael@0: michael@0: mozAutoDocUpdate batch(doc, UPDATE_CONTENT_MODEL, true); michael@0: michael@0: // Merge and remove all nodes michael@0: nsAutoString tmpStr; michael@0: for (uint32_t i = 0; i < nodes.Length(); ++i) { michael@0: nsIContent* node = nodes[i]; michael@0: // Merge with previous node unless empty michael@0: const nsTextFragment* text = node->GetText(); michael@0: if (text->GetLength()) { michael@0: nsIContent* target = node->GetPreviousSibling(); michael@0: NS_ASSERTION((target && target->NodeType() == nsIDOMNode::TEXT_NODE) || michael@0: hasRemoveListeners, michael@0: "Should always have a previous text sibling unless " michael@0: "mutation events messed us up"); michael@0: if (!hasRemoveListeners || michael@0: (target && target->NodeType() == nsIDOMNode::TEXT_NODE)) { michael@0: nsTextNode* t = static_cast(target); michael@0: if (text->Is2b()) { michael@0: t->AppendTextForNormalize(text->Get2b(), text->GetLength(), true, node); michael@0: } michael@0: else { michael@0: tmpStr.Truncate(); michael@0: text->AppendTo(tmpStr); michael@0: t->AppendTextForNormalize(tmpStr.get(), tmpStr.Length(), true, node); michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Remove node michael@0: nsCOMPtr parent = node->GetParentNode(); michael@0: NS_ASSERTION(parent || hasRemoveListeners, michael@0: "Should always have a parent unless " michael@0: "mutation events messed us up"); michael@0: if (parent) { michael@0: parent->RemoveChildAt(parent->IndexOf(node), true); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsINode::GetBaseURI(nsAString &aURI) const michael@0: { michael@0: nsCOMPtr baseURI = GetBaseURI(); michael@0: michael@0: nsAutoCString spec; michael@0: if (baseURI) { michael@0: baseURI->GetSpec(spec); michael@0: } michael@0: michael@0: CopyUTF8toUTF16(spec, aURI); michael@0: } michael@0: michael@0: void michael@0: nsINode::GetBaseURIFromJS(nsAString& aURI) const michael@0: { michael@0: nsCOMPtr baseURI = GetBaseURI(nsContentUtils::IsCallerChrome()); michael@0: nsAutoCString spec; michael@0: if (baseURI) { michael@0: baseURI->GetSpec(spec); michael@0: } michael@0: CopyUTF8toUTF16(spec, aURI); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsINode::GetBaseURIObject() const michael@0: { michael@0: return GetBaseURI(true); michael@0: } michael@0: michael@0: void michael@0: nsINode::LookupPrefix(const nsAString& aNamespaceURI, nsAString& aPrefix) michael@0: { michael@0: Element *element = GetNameSpaceElement(); michael@0: if (element) { michael@0: // XXX Waiting for DOM spec to list error codes. michael@0: michael@0: // Trace up the content parent chain looking for the namespace michael@0: // declaration that defines the aNamespaceURI namespace. Once found, michael@0: // return the prefix (i.e. the attribute localName). michael@0: for (nsIContent* content = element; content; michael@0: content = content->GetParent()) { michael@0: uint32_t attrCount = content->GetAttrCount(); michael@0: michael@0: for (uint32_t i = 0; i < attrCount; ++i) { michael@0: const nsAttrName* name = content->GetAttrNameAt(i); michael@0: michael@0: if (name->NamespaceEquals(kNameSpaceID_XMLNS) && michael@0: content->AttrValueIs(kNameSpaceID_XMLNS, name->LocalName(), michael@0: aNamespaceURI, eCaseMatters)) { michael@0: // If the localName is "xmlns", the prefix we output should be michael@0: // null. michael@0: nsIAtom *localName = name->LocalName(); michael@0: michael@0: if (localName != nsGkAtoms::xmlns) { michael@0: localName->ToString(aPrefix); michael@0: } michael@0: else { michael@0: SetDOMStringToNull(aPrefix); michael@0: } michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: SetDOMStringToNull(aPrefix); michael@0: } michael@0: michael@0: static nsresult michael@0: SetUserDataProperty(uint16_t aCategory, nsINode *aNode, nsIAtom *aKey, michael@0: nsISupports* aValue, void** aOldValue) michael@0: { michael@0: nsresult rv = aNode->SetProperty(aCategory, aKey, aValue, michael@0: nsPropertyTable::SupportsDtorFunc, true, michael@0: aOldValue); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: // Property table owns it now. michael@0: NS_ADDREF(aValue); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::SetUserData(const nsAString &aKey, nsIVariant *aData, michael@0: nsIDOMUserDataHandler *aHandler, nsIVariant **aResult) michael@0: { michael@0: OwnerDoc()->WarnOnceAbout(nsIDocument::eGetSetUserData); michael@0: *aResult = nullptr; michael@0: michael@0: nsCOMPtr key = do_GetAtom(aKey); michael@0: if (!key) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: nsresult rv; michael@0: void *data; michael@0: if (aData) { michael@0: rv = SetUserDataProperty(DOM_USER_DATA, this, key, aData, &data); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: else { michael@0: data = UnsetProperty(DOM_USER_DATA, key); michael@0: } michael@0: michael@0: // Take over ownership of the old data from the property table. michael@0: nsCOMPtr oldData = dont_AddRef(static_cast(data)); michael@0: michael@0: if (aData && aHandler) { michael@0: nsCOMPtr oldHandler; michael@0: rv = SetUserDataProperty(DOM_USER_DATA_HANDLER, this, key, aHandler, michael@0: getter_AddRefs(oldHandler)); michael@0: if (NS_FAILED(rv)) { michael@0: // We failed to set the handler, remove the data. michael@0: DeleteProperty(DOM_USER_DATA, key); michael@0: michael@0: return rv; michael@0: } michael@0: } michael@0: else { michael@0: DeleteProperty(DOM_USER_DATA_HANDLER, key); michael@0: } michael@0: michael@0: oldData.swap(*aResult); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsINode::SetUserData(JSContext* aCx, const nsAString& aKey, michael@0: JS::Handle aData, michael@0: nsIDOMUserDataHandler* aHandler, michael@0: JS::MutableHandle aRetval, michael@0: ErrorResult& aError) michael@0: { michael@0: nsCOMPtr data; michael@0: aError = nsContentUtils::XPConnect()->JSValToVariant(aCx, aData, getter_AddRefs(data)); michael@0: if (aError.Failed()) { michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr oldData; michael@0: aError = SetUserData(aKey, data, aHandler, getter_AddRefs(oldData)); michael@0: if (aError.Failed()) { michael@0: return; michael@0: } michael@0: michael@0: if (!oldData) { michael@0: aRetval.setNull(); michael@0: return; michael@0: } michael@0: michael@0: JSAutoCompartment ac(aCx, GetWrapper()); michael@0: aError = nsContentUtils::XPConnect()->VariantToJS(aCx, GetWrapper(), oldData, michael@0: aRetval); michael@0: } michael@0: michael@0: nsIVariant* michael@0: nsINode::GetUserData(const nsAString& aKey) michael@0: { michael@0: OwnerDoc()->WarnOnceAbout(nsIDocument::eGetSetUserData); michael@0: nsCOMPtr key = do_GetAtom(aKey); michael@0: if (!key) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return static_cast(GetProperty(DOM_USER_DATA, key)); michael@0: } michael@0: michael@0: void michael@0: nsINode::GetUserData(JSContext* aCx, const nsAString& aKey, michael@0: JS::MutableHandle aRetval, ErrorResult& aError) michael@0: { michael@0: nsIVariant* data = GetUserData(aKey); michael@0: if (!data) { michael@0: aRetval.setNull(); michael@0: return; michael@0: } michael@0: michael@0: JSAutoCompartment ac(aCx, GetWrapper()); michael@0: aError = nsContentUtils::XPConnect()->VariantToJS(aCx, GetWrapper(), data, michael@0: aRetval); michael@0: } michael@0: michael@0: uint16_t michael@0: nsINode::CompareDocumentPosition(nsINode& aOtherNode) const michael@0: { michael@0: if (this == &aOtherNode) { michael@0: return 0; michael@0: } michael@0: if (GetPreviousSibling() == &aOtherNode) { michael@0: MOZ_ASSERT(GetParentNode() == aOtherNode.GetParentNode()); michael@0: return static_cast(nsIDOMNode::DOCUMENT_POSITION_PRECEDING); michael@0: } michael@0: if (GetNextSibling() == &aOtherNode) { michael@0: MOZ_ASSERT(GetParentNode() == aOtherNode.GetParentNode()); michael@0: return static_cast(nsIDOMNode::DOCUMENT_POSITION_FOLLOWING); michael@0: } michael@0: michael@0: nsAutoTArray parents1, parents2; michael@0: michael@0: const nsINode *node1 = &aOtherNode, *node2 = this; michael@0: michael@0: // Check if either node is an attribute michael@0: const Attr* attr1 = nullptr; michael@0: if (node1->IsNodeOfType(nsINode::eATTRIBUTE)) { michael@0: attr1 = static_cast(node1); michael@0: const Element* elem = attr1->GetElement(); michael@0: // If there is an owner element add the attribute michael@0: // to the chain and walk up to the element michael@0: if (elem) { michael@0: node1 = elem; michael@0: parents1.AppendElement(attr1); michael@0: } michael@0: } michael@0: if (node2->IsNodeOfType(nsINode::eATTRIBUTE)) { michael@0: const Attr* attr2 = static_cast(node2); michael@0: const Element* elem = attr2->GetElement(); michael@0: if (elem == node1 && attr1) { michael@0: // Both nodes are attributes on the same element. michael@0: // Compare position between the attributes. michael@0: michael@0: uint32_t i; michael@0: const nsAttrName* attrName; michael@0: for (i = 0; (attrName = elem->GetAttrNameAt(i)); ++i) { michael@0: if (attrName->Equals(attr1->NodeInfo())) { michael@0: NS_ASSERTION(!attrName->Equals(attr2->NodeInfo()), michael@0: "Different attrs at same position"); michael@0: return nsIDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | michael@0: nsIDOMNode::DOCUMENT_POSITION_PRECEDING; michael@0: } michael@0: if (attrName->Equals(attr2->NodeInfo())) { michael@0: return nsIDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC | michael@0: nsIDOMNode::DOCUMENT_POSITION_FOLLOWING; michael@0: } michael@0: } michael@0: NS_NOTREACHED("neither attribute in the element"); michael@0: return nsIDOMNode::DOCUMENT_POSITION_DISCONNECTED; michael@0: } michael@0: michael@0: if (elem) { michael@0: node2 = elem; michael@0: parents2.AppendElement(attr2); michael@0: } michael@0: } michael@0: michael@0: // We now know that both nodes are either nsIContents or nsIDocuments. michael@0: // If either node started out as an attribute, that attribute will have michael@0: // the same relative position as its ownerElement, except if the michael@0: // ownerElement ends up being the container for the other node michael@0: michael@0: // Build the chain of parents michael@0: do { michael@0: parents1.AppendElement(node1); michael@0: node1 = node1->GetParentNode(); michael@0: } while (node1); michael@0: do { michael@0: parents2.AppendElement(node2); michael@0: node2 = node2->GetParentNode(); michael@0: } while (node2); michael@0: michael@0: // Check if the nodes are disconnected. michael@0: uint32_t pos1 = parents1.Length(); michael@0: uint32_t pos2 = parents2.Length(); michael@0: const nsINode* top1 = parents1.ElementAt(--pos1); michael@0: const nsINode* top2 = parents2.ElementAt(--pos2); michael@0: if (top1 != top2) { michael@0: return top1 < top2 ? michael@0: (nsIDOMNode::DOCUMENT_POSITION_PRECEDING | michael@0: nsIDOMNode::DOCUMENT_POSITION_DISCONNECTED | michael@0: nsIDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC) : michael@0: (nsIDOMNode::DOCUMENT_POSITION_FOLLOWING | michael@0: nsIDOMNode::DOCUMENT_POSITION_DISCONNECTED | michael@0: nsIDOMNode::DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC); michael@0: } michael@0: michael@0: // Find where the parent chain differs and check indices in the parent. michael@0: const nsINode* parent = top1; michael@0: uint32_t len; michael@0: for (len = std::min(pos1, pos2); len > 0; --len) { michael@0: const nsINode* child1 = parents1.ElementAt(--pos1); michael@0: const nsINode* child2 = parents2.ElementAt(--pos2); michael@0: if (child1 != child2) { michael@0: // child1 or child2 can be an attribute here. This will work fine since michael@0: // IndexOf will return -1 for the attribute making the attribute be michael@0: // considered before any child. michael@0: return parent->IndexOf(child1) < parent->IndexOf(child2) ? michael@0: static_cast(nsIDOMNode::DOCUMENT_POSITION_PRECEDING) : michael@0: static_cast(nsIDOMNode::DOCUMENT_POSITION_FOLLOWING); michael@0: } michael@0: parent = child1; michael@0: } michael@0: michael@0: // We hit the end of one of the parent chains without finding a difference michael@0: // between the chains. That must mean that one node is an ancestor of the michael@0: // other. The one with the shortest chain must be the ancestor. michael@0: return pos1 < pos2 ? michael@0: (nsIDOMNode::DOCUMENT_POSITION_PRECEDING | michael@0: nsIDOMNode::DOCUMENT_POSITION_CONTAINS) : michael@0: (nsIDOMNode::DOCUMENT_POSITION_FOLLOWING | michael@0: nsIDOMNode::DOCUMENT_POSITION_CONTAINED_BY); michael@0: } michael@0: michael@0: bool michael@0: nsINode::IsEqualNode(nsINode* aOther) michael@0: { michael@0: if (!aOther) { michael@0: return false; michael@0: } michael@0: michael@0: nsAutoString string1, string2; michael@0: michael@0: nsINode* node1 = this; michael@0: nsINode* node2 = aOther; michael@0: do { michael@0: uint16_t nodeType = node1->NodeType(); michael@0: if (nodeType != node2->NodeType()) { michael@0: return false; michael@0: } michael@0: michael@0: nsINodeInfo* nodeInfo1 = node1->mNodeInfo; michael@0: nsINodeInfo* nodeInfo2 = node2->mNodeInfo; michael@0: if (!nodeInfo1->Equals(nodeInfo2) || michael@0: nodeInfo1->GetExtraName() != nodeInfo2->GetExtraName()) { michael@0: return false; michael@0: } michael@0: michael@0: switch(nodeType) { michael@0: case nsIDOMNode::ELEMENT_NODE: michael@0: { michael@0: // Both are elements (we checked that their nodeinfos are equal). Do the michael@0: // check on attributes. michael@0: Element* element1 = node1->AsElement(); michael@0: Element* element2 = node2->AsElement(); michael@0: uint32_t attrCount = element1->GetAttrCount(); michael@0: if (attrCount != element2->GetAttrCount()) { michael@0: return false; michael@0: } michael@0: michael@0: // Iterate over attributes. michael@0: for (uint32_t i = 0; i < attrCount; ++i) { michael@0: const nsAttrName* attrName = element1->GetAttrNameAt(i); michael@0: #ifdef DEBUG michael@0: bool hasAttr = michael@0: #endif michael@0: element1->GetAttr(attrName->NamespaceID(), attrName->LocalName(), michael@0: string1); michael@0: NS_ASSERTION(hasAttr, "Why don't we have an attr?"); michael@0: michael@0: if (!element2->AttrValueIs(attrName->NamespaceID(), michael@0: attrName->LocalName(), michael@0: string1, michael@0: eCaseMatters)) { michael@0: return false; michael@0: } michael@0: } michael@0: break; michael@0: } michael@0: case nsIDOMNode::TEXT_NODE: michael@0: case nsIDOMNode::COMMENT_NODE: michael@0: case nsIDOMNode::CDATA_SECTION_NODE: michael@0: case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: michael@0: { michael@0: string1.Truncate(); michael@0: static_cast(node1)->AppendTextTo(string1); michael@0: string2.Truncate(); michael@0: static_cast(node2)->AppendTextTo(string2); michael@0: michael@0: if (!string1.Equals(string2)) { michael@0: return false; michael@0: } michael@0: michael@0: break; michael@0: } michael@0: case nsIDOMNode::DOCUMENT_NODE: michael@0: case nsIDOMNode::DOCUMENT_FRAGMENT_NODE: michael@0: break; michael@0: case nsIDOMNode::ATTRIBUTE_NODE: michael@0: { michael@0: NS_ASSERTION(node1 == this && node2 == aOther, michael@0: "Did we come upon an attribute node while walking a " michael@0: "subtree?"); michael@0: node1->GetNodeValue(string1); michael@0: node2->GetNodeValue(string2); michael@0: michael@0: // Returning here as to not bother walking subtree. And there is no michael@0: // risk that we're half way through walking some other subtree since michael@0: // attribute nodes doesn't appear in subtrees. michael@0: return string1.Equals(string2); michael@0: } michael@0: case nsIDOMNode::DOCUMENT_TYPE_NODE: michael@0: { michael@0: nsCOMPtr docType1 = do_QueryInterface(node1); michael@0: nsCOMPtr docType2 = do_QueryInterface(node2); michael@0: michael@0: NS_ASSERTION(docType1 && docType2, "Why don't we have a document type node?"); michael@0: michael@0: // Public ID michael@0: docType1->GetPublicId(string1); michael@0: docType2->GetPublicId(string2); michael@0: if (!string1.Equals(string2)) { michael@0: return false; michael@0: } michael@0: michael@0: // System ID michael@0: docType1->GetSystemId(string1); michael@0: docType2->GetSystemId(string2); michael@0: if (!string1.Equals(string2)) { michael@0: return false; michael@0: } michael@0: michael@0: // Internal subset michael@0: docType1->GetInternalSubset(string1); michael@0: docType2->GetInternalSubset(string2); michael@0: if (!string1.Equals(string2)) { michael@0: return false; michael@0: } michael@0: michael@0: break; michael@0: } michael@0: default: michael@0: NS_ABORT_IF_FALSE(false, "Unknown node type"); michael@0: } michael@0: michael@0: nsINode* nextNode = node1->GetFirstChild(); michael@0: if (nextNode) { michael@0: node1 = nextNode; michael@0: node2 = node2->GetFirstChild(); michael@0: } michael@0: else { michael@0: if (node2->GetFirstChild()) { michael@0: // node2 has a firstChild, but node1 doesn't michael@0: return false; michael@0: } michael@0: michael@0: // Find next sibling, possibly walking parent chain. michael@0: while (1) { michael@0: if (node1 == this) { michael@0: NS_ASSERTION(node2 == aOther, "Should have reached the start node " michael@0: "for both trees at the same time"); michael@0: return true; michael@0: } michael@0: michael@0: nextNode = node1->GetNextSibling(); michael@0: if (nextNode) { michael@0: node1 = nextNode; michael@0: node2 = node2->GetNextSibling(); michael@0: break; michael@0: } michael@0: michael@0: if (node2->GetNextSibling()) { michael@0: // node2 has a nextSibling, but node1 doesn't michael@0: return false; michael@0: } michael@0: michael@0: node1 = node1->GetParentNode(); michael@0: node2 = node2->GetParentNode(); michael@0: NS_ASSERTION(node1 && node2, "no parent while walking subtree"); michael@0: } michael@0: } michael@0: } while(node2); michael@0: michael@0: return false; michael@0: } michael@0: michael@0: void michael@0: nsINode::LookupNamespaceURI(const nsAString& aNamespacePrefix, michael@0: nsAString& aNamespaceURI) michael@0: { michael@0: Element *element = GetNameSpaceElement(); michael@0: if (!element || michael@0: NS_FAILED(element->LookupNamespaceURIInternal(aNamespacePrefix, michael@0: aNamespaceURI))) { michael@0: SetDOMStringToNull(aNamespaceURI); michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_DOMTARGET_DEFAULTS(nsINode) michael@0: michael@0: NS_IMETHODIMP michael@0: nsINode::AddEventListener(const nsAString& aType, michael@0: nsIDOMEventListener *aListener, michael@0: bool aUseCapture, michael@0: bool aWantsUntrusted, michael@0: uint8_t aOptionalArgc) michael@0: { michael@0: NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1, michael@0: "Won't check if this is chrome, you want to set " michael@0: "aWantsUntrusted to false or make the aWantsUntrusted " michael@0: "explicit by making aOptionalArgc non-zero."); michael@0: michael@0: if (!aWantsUntrusted && michael@0: (aOptionalArgc < 2 && michael@0: !nsContentUtils::IsChromeDoc(OwnerDoc()))) { michael@0: aWantsUntrusted = true; michael@0: } michael@0: michael@0: EventListenerManager* listener_manager = GetOrCreateListenerManager(); michael@0: NS_ENSURE_STATE(listener_manager); michael@0: listener_manager->AddEventListener(aType, aListener, aUseCapture, michael@0: aWantsUntrusted); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsINode::AddEventListener(const nsAString& aType, michael@0: EventListener* aListener, michael@0: bool aUseCapture, michael@0: const Nullable& aWantsUntrusted, michael@0: ErrorResult& aRv) michael@0: { michael@0: bool wantsUntrusted; michael@0: if (aWantsUntrusted.IsNull()) { michael@0: wantsUntrusted = !nsContentUtils::IsChromeDoc(OwnerDoc()); michael@0: } else { michael@0: wantsUntrusted = aWantsUntrusted.Value(); michael@0: } michael@0: michael@0: EventListenerManager* listener_manager = GetOrCreateListenerManager(); michael@0: if (!listener_manager) { michael@0: aRv.Throw(NS_ERROR_UNEXPECTED); michael@0: return; michael@0: } michael@0: listener_manager->AddEventListener(aType, aListener, aUseCapture, michael@0: wantsUntrusted); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsINode::AddSystemEventListener(const nsAString& aType, michael@0: nsIDOMEventListener *aListener, michael@0: bool aUseCapture, michael@0: bool aWantsUntrusted, michael@0: uint8_t aOptionalArgc) michael@0: { michael@0: NS_ASSERTION(!aWantsUntrusted || aOptionalArgc > 1, michael@0: "Won't check if this is chrome, you want to set " michael@0: "aWantsUntrusted to false or make the aWantsUntrusted " michael@0: "explicit by making aOptionalArgc non-zero."); michael@0: michael@0: if (!aWantsUntrusted && michael@0: (aOptionalArgc < 2 && michael@0: !nsContentUtils::IsChromeDoc(OwnerDoc()))) { michael@0: aWantsUntrusted = true; michael@0: } michael@0: michael@0: return NS_AddSystemEventListener(this, aType, aListener, aUseCapture, michael@0: aWantsUntrusted); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsINode::RemoveEventListener(const nsAString& aType, michael@0: nsIDOMEventListener* aListener, michael@0: bool aUseCapture) michael@0: { michael@0: EventListenerManager* elm = GetExistingListenerManager(); michael@0: if (elm) { michael@0: elm->RemoveEventListener(aType, aListener, aUseCapture); michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMPL_REMOVE_SYSTEM_EVENT_LISTENER(nsINode) michael@0: michael@0: nsresult michael@0: nsINode::PreHandleEvent(EventChainPreVisitor& aVisitor) michael@0: { michael@0: // This is only here so that we can use the NS_DECL_NSIDOMTARGET macro michael@0: NS_ABORT(); michael@0: return NS_ERROR_NOT_IMPLEMENTED; michael@0: } michael@0: michael@0: void michael@0: nsINode::GetBoxQuads(const BoxQuadOptions& aOptions, michael@0: nsTArray >& aResult, michael@0: mozilla::ErrorResult& aRv) michael@0: { michael@0: mozilla::GetBoxQuads(this, aOptions, aResult, aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsINode::ConvertQuadFromNode(DOMQuad& aQuad, michael@0: const GeometryNode& aFrom, michael@0: const ConvertCoordinateOptions& aOptions, michael@0: ErrorResult& aRv) michael@0: { michael@0: return mozilla::ConvertQuadFromNode(this, aQuad, aFrom, aOptions, aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsINode::ConvertRectFromNode(DOMRectReadOnly& aRect, michael@0: const GeometryNode& aFrom, michael@0: const ConvertCoordinateOptions& aOptions, michael@0: ErrorResult& aRv) michael@0: { michael@0: return mozilla::ConvertRectFromNode(this, aRect, aFrom, aOptions, aRv); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsINode::ConvertPointFromNode(const DOMPointInit& aPoint, michael@0: const GeometryNode& aFrom, michael@0: const ConvertCoordinateOptions& aOptions, michael@0: ErrorResult& aRv) michael@0: { michael@0: return mozilla::ConvertPointFromNode(this, aPoint, aFrom, aOptions, aRv); michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::DispatchEvent(nsIDOMEvent *aEvent, bool* aRetVal) michael@0: { michael@0: // XXX sXBL/XBL2 issue -- do we really want the owner here? What michael@0: // if that's the XBL document? Would we want its presshell? Or what? michael@0: nsCOMPtr document = OwnerDoc(); michael@0: michael@0: // Do nothing if the element does not belong to a document michael@0: if (!document) { michael@0: *aRetVal = true; michael@0: return NS_OK; michael@0: } michael@0: michael@0: // Obtain a presentation shell michael@0: nsIPresShell *shell = document->GetShell(); michael@0: nsRefPtr context; michael@0: if (shell) { michael@0: context = shell->GetPresContext(); michael@0: } michael@0: michael@0: nsEventStatus status = nsEventStatus_eIgnore; michael@0: nsresult rv = michael@0: EventDispatcher::DispatchDOMEvent(this, nullptr, aEvent, context, &status); michael@0: *aRetVal = (status != nsEventStatus_eConsumeNoDefault); michael@0: return rv; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::PostHandleEvent(EventChainPostVisitor& /*aVisitor*/) michael@0: { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::DispatchDOMEvent(WidgetEvent* aEvent, michael@0: nsIDOMEvent* aDOMEvent, michael@0: nsPresContext* aPresContext, michael@0: nsEventStatus* aEventStatus) michael@0: { michael@0: return EventDispatcher::DispatchDOMEvent(this, aEvent, aDOMEvent, michael@0: aPresContext, aEventStatus); michael@0: } michael@0: michael@0: EventListenerManager* michael@0: nsINode::GetOrCreateListenerManager() michael@0: { michael@0: return nsContentUtils::GetListenerManagerForNode(this); michael@0: } michael@0: michael@0: EventListenerManager* michael@0: nsINode::GetExistingListenerManager() const michael@0: { michael@0: return nsContentUtils::GetExistingListenerManagerForNode(this); michael@0: } michael@0: michael@0: nsIScriptContext* michael@0: nsINode::GetContextForEventHandlers(nsresult* aRv) michael@0: { michael@0: return nsContentUtils::GetContextForEventHandlers(this, aRv); michael@0: } michael@0: michael@0: nsIDOMWindow* michael@0: nsINode::GetOwnerGlobal() michael@0: { michael@0: bool dummy; michael@0: return nsPIDOMWindow::GetOuterFromCurrentInner( michael@0: static_cast(OwnerDoc()->GetScriptHandlingObject(dummy))); michael@0: } michael@0: michael@0: bool michael@0: nsINode::UnoptimizableCCNode() const michael@0: { michael@0: const uintptr_t problematicFlags = (NODE_IS_ANONYMOUS_ROOT | michael@0: NODE_IS_IN_NATIVE_ANONYMOUS_SUBTREE | michael@0: NODE_IS_NATIVE_ANONYMOUS_ROOT | michael@0: NODE_MAY_BE_IN_BINDING_MNGR | michael@0: NODE_IS_IN_SHADOW_TREE); michael@0: return HasFlag(problematicFlags) || michael@0: NodeType() == nsIDOMNode::ATTRIBUTE_NODE || michael@0: // For strange cases like xbl:content/xbl:children michael@0: (IsElement() && michael@0: AsElement()->IsInNamespace(kNameSpaceID_XBL)); michael@0: } michael@0: michael@0: /* static */ michael@0: bool michael@0: nsINode::Traverse(nsINode *tmp, nsCycleCollectionTraversalCallback &cb) michael@0: { michael@0: if (MOZ_LIKELY(!cb.WantAllTraces())) { michael@0: nsIDocument *currentDoc = tmp->GetCurrentDoc(); michael@0: if (currentDoc && michael@0: nsCCUncollectableMarker::InGeneration(currentDoc->GetMarkedCCGeneration())) { michael@0: return false; michael@0: } michael@0: michael@0: if (nsCCUncollectableMarker::sGeneration) { michael@0: // If we're black no need to traverse. michael@0: if (tmp->IsBlack() || tmp->InCCBlackTree()) { michael@0: return false; michael@0: } michael@0: michael@0: if (!tmp->UnoptimizableCCNode()) { michael@0: // If we're in a black document, return early. michael@0: if ((currentDoc && currentDoc->IsBlack())) { michael@0: return false; michael@0: } michael@0: // If we're not in anonymous content and we have a black parent, michael@0: // return early. michael@0: nsIContent* parent = tmp->GetParent(); michael@0: if (parent && !parent->UnoptimizableCCNode() && parent->IsBlack()) { michael@0: NS_ABORT_IF_FALSE(parent->IndexOf(tmp) >= 0, "Parent doesn't own us?"); michael@0: return false; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNodeInfo) michael@0: NS_IMPL_CYCLE_COLLECTION_TRAVERSE_RAWPTR(GetParent()) michael@0: michael@0: nsSlots *slots = tmp->GetExistingSlots(); michael@0: if (slots) { michael@0: slots->Traverse(cb); michael@0: } michael@0: michael@0: if (tmp->HasProperties()) { michael@0: nsNodeUtils::TraverseUserData(tmp, cb); michael@0: nsCOMArray* objects = michael@0: static_cast*>(tmp->GetProperty(nsGkAtoms::keepobjectsalive)); michael@0: if (objects) { michael@0: for (int32_t i = 0; i < objects->Count(); ++i) { michael@0: cb.NoteXPCOMChild(objects->ObjectAt(i)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (tmp->NodeType() != nsIDOMNode::DOCUMENT_NODE && michael@0: tmp->HasFlag(NODE_HAS_LISTENERMANAGER)) { michael@0: nsContentUtils::TraverseListenerManager(tmp, cb); michael@0: } michael@0: michael@0: return true; michael@0: } michael@0: michael@0: /* static */ michael@0: void michael@0: nsINode::Unlink(nsINode* tmp) michael@0: { michael@0: tmp->ReleaseWrapper(tmp); michael@0: michael@0: nsSlots *slots = tmp->GetExistingSlots(); michael@0: if (slots) { michael@0: slots->Unlink(); michael@0: } michael@0: michael@0: if (tmp->NodeType() != nsIDOMNode::DOCUMENT_NODE && michael@0: tmp->HasFlag(NODE_HAS_LISTENERMANAGER)) { michael@0: nsContentUtils::RemoveListenerManager(tmp); michael@0: tmp->UnsetFlags(NODE_HAS_LISTENERMANAGER); michael@0: } michael@0: michael@0: if (tmp->HasProperties()) { michael@0: nsNodeUtils::UnlinkUserData(tmp); michael@0: tmp->DeleteProperty(nsGkAtoms::keepobjectsalive); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: ReleaseURI(void*, /* aObject*/ michael@0: nsIAtom*, /* aPropertyName */ michael@0: void* aPropertyValue, michael@0: void* /* aData */) michael@0: { michael@0: nsIURI* uri = static_cast(aPropertyValue); michael@0: NS_RELEASE(uri); michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::SetExplicitBaseURI(nsIURI* aURI) michael@0: { michael@0: nsresult rv = SetProperty(nsGkAtoms::baseURIProperty, aURI, ReleaseURI); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: SetHasExplicitBaseURI(); michael@0: NS_ADDREF(aURI); michael@0: } michael@0: return rv; michael@0: } michael@0: michael@0: static nsresult michael@0: AdoptNodeIntoOwnerDoc(nsINode *aParent, nsINode *aNode) michael@0: { michael@0: NS_ASSERTION(!aNode->GetParentNode(), michael@0: "Should have removed from parent already"); michael@0: michael@0: nsIDocument *doc = aParent->OwnerDoc(); michael@0: michael@0: nsresult rv; michael@0: nsCOMPtr domDoc = do_QueryInterface(doc, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr node = do_QueryInterface(aNode, &rv); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: nsCOMPtr adoptedNode; michael@0: rv = domDoc->AdoptNode(node, getter_AddRefs(adoptedNode)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: NS_ASSERTION(aParent->OwnerDoc() == doc, michael@0: "ownerDoc chainged while adopting"); michael@0: NS_ASSERTION(adoptedNode == node, "Uh, adopt node changed nodes?"); michael@0: NS_ASSERTION(aParent->OwnerDoc() == aNode->OwnerDoc(), michael@0: "ownerDocument changed again after adopting!"); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: static nsresult michael@0: CheckForOutdatedParent(nsINode* aParent, nsINode* aNode) michael@0: { michael@0: if (JSObject* existingObjUnrooted = aNode->GetWrapper()) { michael@0: AutoJSContext cx; michael@0: JS::Rooted existingObj(cx, existingObjUnrooted); michael@0: nsIGlobalObject* global = aParent->OwnerDoc()->GetScopeObject(); michael@0: MOZ_ASSERT(global); michael@0: michael@0: if (js::GetGlobalForObjectCrossCompartment(existingObj) != michael@0: global->GetGlobalJSObject()) { michael@0: JSAutoCompartment ac(cx, existingObj); michael@0: nsresult rv = ReparentWrapper(cx, existingObj); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::doInsertChildAt(nsIContent* aKid, uint32_t aIndex, michael@0: bool aNotify, nsAttrAndChildArray& aChildArray) michael@0: { michael@0: NS_PRECONDITION(!aKid->GetParentNode(), michael@0: "Inserting node that already has parent"); michael@0: nsresult rv; michael@0: michael@0: // The id-handling code, and in the future possibly other code, need to michael@0: // react to unexpected attribute changes. michael@0: nsMutationGuard::DidMutate(); michael@0: michael@0: // Do this before checking the child-count since this could cause mutations michael@0: nsIDocument* doc = GetCurrentDoc(); michael@0: mozAutoDocUpdate updateBatch(doc, UPDATE_CONTENT_MODEL, aNotify); michael@0: michael@0: if (OwnerDoc() != aKid->OwnerDoc()) { michael@0: rv = AdoptNodeIntoOwnerDoc(this, aKid); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } else if (OwnerDoc()->DidDocumentOpen()) { michael@0: rv = CheckForOutdatedParent(this, aKid); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: michael@0: uint32_t childCount = aChildArray.ChildCount(); michael@0: NS_ENSURE_TRUE(aIndex <= childCount, NS_ERROR_ILLEGAL_VALUE); michael@0: bool isAppend = (aIndex == childCount); michael@0: michael@0: rv = aChildArray.InsertChildAt(aKid, aIndex); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: if (aIndex == 0) { michael@0: mFirstChild = aKid; michael@0: } michael@0: michael@0: nsIContent* parent = michael@0: IsNodeOfType(eDOCUMENT) ? nullptr : static_cast(this); michael@0: michael@0: rv = aKid->BindToTree(doc, parent, michael@0: parent ? parent->GetBindingParent() : nullptr, michael@0: true); michael@0: if (NS_FAILED(rv)) { michael@0: if (GetFirstChild() == aKid) { michael@0: mFirstChild = aKid->GetNextSibling(); michael@0: } michael@0: aChildArray.RemoveChildAt(aIndex); michael@0: aKid->UnbindFromTree(); michael@0: return rv; michael@0: } michael@0: michael@0: NS_ASSERTION(aKid->GetParentNode() == this, michael@0: "Did we run script inappropriately?"); michael@0: michael@0: if (aNotify) { michael@0: // Note that we always want to call ContentInserted when things are added michael@0: // as kids to documents michael@0: if (parent && isAppend) { michael@0: nsNodeUtils::ContentAppended(parent, aKid, aIndex); michael@0: } else { michael@0: nsNodeUtils::ContentInserted(this, aKid, aIndex); michael@0: } michael@0: michael@0: if (nsContentUtils::HasMutationListeners(aKid, michael@0: NS_EVENT_BITS_MUTATION_NODEINSERTED, this)) { michael@0: InternalMutationEvent mutation(true, NS_MUTATION_NODEINSERTED); michael@0: mutation.mRelatedNode = do_QueryInterface(this); michael@0: michael@0: mozAutoSubtreeModified subtree(OwnerDoc(), this); michael@0: (new AsyncEventDispatcher(aKid, mutation))->RunDOMEventWhenSafe(); michael@0: } michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: Element* michael@0: nsINode::GetPreviousElementSibling() const michael@0: { michael@0: nsIContent* previousSibling = GetPreviousSibling(); michael@0: while (previousSibling) { michael@0: if (previousSibling->IsElement()) { michael@0: return previousSibling->AsElement(); michael@0: } michael@0: previousSibling = previousSibling->GetPreviousSibling(); michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: Element* michael@0: nsINode::GetNextElementSibling() const michael@0: { michael@0: nsIContent* nextSibling = GetNextSibling(); michael@0: while (nextSibling) { michael@0: if (nextSibling->IsElement()) { michael@0: return nextSibling->AsElement(); michael@0: } michael@0: nextSibling = nextSibling->GetNextSibling(); michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: void michael@0: nsINode::Remove() michael@0: { michael@0: nsCOMPtr parent = GetParentNode(); michael@0: if (!parent) { michael@0: return; michael@0: } michael@0: int32_t index = parent->IndexOf(this); michael@0: if (index < 0) { michael@0: NS_WARNING("Ignoring call to nsINode::Remove on anonymous child."); michael@0: return; michael@0: } michael@0: parent->RemoveChildAt(uint32_t(index), true); michael@0: } michael@0: michael@0: Element* michael@0: nsINode::GetFirstElementChild() const michael@0: { michael@0: for (nsIContent* child = GetFirstChild(); michael@0: child; michael@0: child = child->GetNextSibling()) { michael@0: if (child->IsElement()) { michael@0: return child->AsElement(); michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: Element* michael@0: nsINode::GetLastElementChild() const michael@0: { michael@0: for (nsIContent* child = GetLastChild(); michael@0: child; michael@0: child = child->GetPreviousSibling()) { michael@0: if (child->IsElement()) { michael@0: return child->AsElement(); michael@0: } michael@0: } michael@0: michael@0: return nullptr; michael@0: } michael@0: michael@0: void michael@0: nsINode::doRemoveChildAt(uint32_t aIndex, bool aNotify, michael@0: nsIContent* aKid, nsAttrAndChildArray& aChildArray) michael@0: { michael@0: NS_PRECONDITION(aKid && aKid->GetParentNode() == this && michael@0: aKid == GetChildAt(aIndex) && michael@0: IndexOf(aKid) == (int32_t)aIndex, "Bogus aKid"); michael@0: michael@0: nsMutationGuard::DidMutate(); michael@0: michael@0: nsIDocument* doc = GetCurrentDoc(); michael@0: michael@0: mozAutoDocUpdate updateBatch(doc, UPDATE_CONTENT_MODEL, aNotify); michael@0: michael@0: nsIContent* previousSibling = aKid->GetPreviousSibling(); michael@0: michael@0: if (GetFirstChild() == aKid) { michael@0: mFirstChild = aKid->GetNextSibling(); michael@0: } michael@0: michael@0: aChildArray.RemoveChildAt(aIndex); michael@0: michael@0: if (aNotify) { michael@0: nsNodeUtils::ContentRemoved(this, aKid, aIndex, previousSibling); michael@0: } michael@0: michael@0: aKid->UnbindFromTree(); michael@0: } michael@0: michael@0: // When replacing, aRefChild is the content being replaced; when michael@0: // inserting it's the content before which we're inserting. In the michael@0: // latter case it may be null. michael@0: static michael@0: bool IsAllowedAsChild(nsIContent* aNewChild, nsINode* aParent, michael@0: bool aIsReplace, nsINode* aRefChild) michael@0: { michael@0: MOZ_ASSERT(aNewChild, "Must have new child"); michael@0: MOZ_ASSERT_IF(aIsReplace, aRefChild); michael@0: MOZ_ASSERT(aParent); michael@0: MOZ_ASSERT(aParent->IsNodeOfType(nsINode::eDOCUMENT) || michael@0: aParent->IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT) || michael@0: aParent->IsElement(), michael@0: "Nodes that are not documents, document fragments or elements " michael@0: "can't be parents!"); michael@0: michael@0: // A common case is that aNewChild has no kids, in which case michael@0: // aParent can't be a descendant of aNewChild unless they're michael@0: // actually equal to each other. Fast-path that case, since aParent michael@0: // could be pretty deep in the DOM tree. michael@0: if (aNewChild == aParent || michael@0: ((aNewChild->GetFirstChild() || michael@0: // HTML template elements and ShadowRoot hosts need michael@0: // to be checked to ensure that they are not inserted into michael@0: // the hosted content. michael@0: aNewChild->Tag() == nsGkAtoms::_template || michael@0: aNewChild->GetShadowRoot()) && michael@0: nsContentUtils::ContentIsHostIncludingDescendantOf(aParent, michael@0: aNewChild))) { michael@0: return false; michael@0: } michael@0: michael@0: // The allowed child nodes differ for documents and elements michael@0: switch (aNewChild->NodeType()) { michael@0: case nsIDOMNode::COMMENT_NODE : michael@0: case nsIDOMNode::PROCESSING_INSTRUCTION_NODE : michael@0: // OK in both cases michael@0: return true; michael@0: case nsIDOMNode::TEXT_NODE : michael@0: case nsIDOMNode::CDATA_SECTION_NODE : michael@0: case nsIDOMNode::ENTITY_REFERENCE_NODE : michael@0: // Allowed under Elements and DocumentFragments michael@0: return aParent->NodeType() != nsIDOMNode::DOCUMENT_NODE; michael@0: case nsIDOMNode::ELEMENT_NODE : michael@0: { michael@0: if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) { michael@0: // Always ok to have elements under other elements or document fragments michael@0: return true; michael@0: } michael@0: michael@0: nsIDocument* parentDocument = static_cast(aParent); michael@0: Element* rootElement = parentDocument->GetRootElement(); michael@0: if (rootElement) { michael@0: // Already have a documentElement, so this is only OK if we're michael@0: // replacing it. michael@0: return aIsReplace && rootElement == aRefChild; michael@0: } michael@0: michael@0: // We don't have a documentElement yet. Our one remaining constraint is michael@0: // that the documentElement must come after the doctype. michael@0: if (!aRefChild) { michael@0: // Appending is just fine. michael@0: return true; michael@0: } michael@0: michael@0: nsIContent* docTypeContent = parentDocument->GetDoctype(); michael@0: if (!docTypeContent) { michael@0: // It's all good. michael@0: return true; michael@0: } michael@0: michael@0: int32_t doctypeIndex = aParent->IndexOf(docTypeContent); michael@0: int32_t insertIndex = aParent->IndexOf(aRefChild); michael@0: michael@0: // Now we're OK in the following two cases only: michael@0: // 1) We're replacing something that's not before the doctype michael@0: // 2) We're inserting before something that comes after the doctype michael@0: return aIsReplace ? (insertIndex >= doctypeIndex) : michael@0: insertIndex > doctypeIndex; michael@0: } michael@0: case nsIDOMNode::DOCUMENT_TYPE_NODE : michael@0: { michael@0: if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) { michael@0: // doctypes only allowed under documents michael@0: return false; michael@0: } michael@0: michael@0: nsIDocument* parentDocument = static_cast(aParent); michael@0: nsIContent* docTypeContent = parentDocument->GetDoctype(); michael@0: if (docTypeContent) { michael@0: // Already have a doctype, so this is only OK if we're replacing it michael@0: return aIsReplace && docTypeContent == aRefChild; michael@0: } michael@0: michael@0: // We don't have a doctype yet. Our one remaining constraint is michael@0: // that the doctype must come before the documentElement. michael@0: Element* rootElement = parentDocument->GetRootElement(); michael@0: if (!rootElement) { michael@0: // It's all good michael@0: return true; michael@0: } michael@0: michael@0: if (!aRefChild) { michael@0: // Trying to append a doctype, but have a documentElement michael@0: return false; michael@0: } michael@0: michael@0: int32_t rootIndex = aParent->IndexOf(rootElement); michael@0: int32_t insertIndex = aParent->IndexOf(aRefChild); michael@0: michael@0: // Now we're OK if and only if insertIndex <= rootIndex. Indeed, either michael@0: // we end up replacing aRefChild or we end up before it. Either one is michael@0: // ok as long as aRefChild is not after rootElement. michael@0: return insertIndex <= rootIndex; michael@0: } michael@0: case nsIDOMNode::DOCUMENT_FRAGMENT_NODE : michael@0: { michael@0: // Note that for now we only allow nodes inside document fragments if michael@0: // they're allowed inside elements. If we ever change this to allow michael@0: // doctype nodes in document fragments, we'll need to update this code. michael@0: // Also, there's a version of this code in ReplaceOrInsertBefore. If you michael@0: // change this code, change that too. michael@0: if (!aParent->IsNodeOfType(nsINode::eDOCUMENT)) { michael@0: // All good here michael@0: return true; michael@0: } michael@0: michael@0: bool sawElement = false; michael@0: for (nsIContent* child = aNewChild->GetFirstChild(); michael@0: child; michael@0: child = child->GetNextSibling()) { michael@0: if (child->IsElement()) { michael@0: if (sawElement) { michael@0: // Can't put two elements into a document michael@0: return false; michael@0: } michael@0: sawElement = true; michael@0: } michael@0: // If we can put this content at the the right place, we might be ok; michael@0: // if not, we bail out. michael@0: if (!IsAllowedAsChild(child, aParent, aIsReplace, aRefChild)) { michael@0: return false; michael@0: } michael@0: } michael@0: michael@0: // Everything in the fragment checked out ok, so we can stick it in here michael@0: return true; michael@0: } michael@0: default: michael@0: /* michael@0: * aNewChild is of invalid type. michael@0: */ michael@0: break; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: nsINode* michael@0: nsINode::ReplaceOrInsertBefore(bool aReplace, nsINode* aNewChild, michael@0: nsINode* aRefChild, ErrorResult& aError) michael@0: { michael@0: // XXXbz I wish I could assert that nsContentUtils::IsSafeToRunScript() so we michael@0: // could rely on scriptblockers going out of scope to actually run XBL michael@0: // teardown, but various crud adds nodes under scriptblockers (e.g. native michael@0: // anonymous content). The only good news is those insertions can't trigger michael@0: // the bad XBL cases. michael@0: MOZ_ASSERT_IF(aReplace, aRefChild); michael@0: michael@0: if ((!IsNodeOfType(eDOCUMENT) && michael@0: !IsNodeOfType(eDOCUMENT_FRAGMENT) && michael@0: !IsElement()) || michael@0: !aNewChild->IsNodeOfType(eCONTENT)) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: uint16_t nodeType = aNewChild->NodeType(); michael@0: michael@0: // Before we do anything else, fire all DOMNodeRemoved mutation events michael@0: // We do this up front as to avoid having to deal with script running michael@0: // at random places further down. michael@0: // Scope firing mutation events so that we don't carry any state that michael@0: // might be stale michael@0: { michael@0: // This check happens again further down (though then using IndexOf). michael@0: // We're only checking this here to avoid firing mutation events when michael@0: // none should be fired. michael@0: // It's ok that we do the check twice in the case when firing mutation michael@0: // events as we need to recheck after running script anyway. michael@0: if (aRefChild && aRefChild->GetParentNode() != this) { michael@0: aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // If we're replacing, fire for node-to-be-replaced. michael@0: // If aRefChild == aNewChild then we'll fire for it in check below michael@0: if (aReplace && aRefChild != aNewChild) { michael@0: nsContentUtils::MaybeFireNodeRemoved(aRefChild, this, OwnerDoc()); michael@0: } michael@0: michael@0: // If the new node already has a parent, fire for removing from old michael@0: // parent michael@0: nsINode* oldParent = aNewChild->GetParentNode(); michael@0: if (oldParent) { michael@0: nsContentUtils::MaybeFireNodeRemoved(aNewChild, oldParent, michael@0: aNewChild->OwnerDoc()); michael@0: } michael@0: michael@0: // If we're inserting a fragment, fire for all the children of the michael@0: // fragment michael@0: if (nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { michael@0: static_cast(aNewChild)->FireNodeRemovedForChildren(); michael@0: } michael@0: // Verify that our aRefChild is still sensible michael@0: if (aRefChild && aRefChild->GetParentNode() != this) { michael@0: aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: nsIDocument* doc = OwnerDoc(); michael@0: nsIContent* newContent = static_cast(aNewChild); michael@0: if (newContent->IsRootOfAnonymousSubtree()) { michael@0: // This is anonymous content. Don't allow its insertion michael@0: // anywhere, since it might have UnbindFromTree calls coming michael@0: // its way. michael@0: aError.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Make sure that the inserted node is allowed as a child of its new parent. michael@0: if (!IsAllowedAsChild(newContent, this, aReplace, aRefChild)) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Record the node to insert before, if any michael@0: nsINode* nodeToInsertBefore; michael@0: if (aReplace) { michael@0: nodeToInsertBefore = aRefChild->GetNextSibling(); michael@0: } else { michael@0: nodeToInsertBefore = aRefChild; michael@0: } michael@0: if (nodeToInsertBefore == aNewChild) { michael@0: // We're going to remove aNewChild from its parent, so use its next sibling michael@0: // as the node to insert before. michael@0: nodeToInsertBefore = nodeToInsertBefore->GetNextSibling(); michael@0: } michael@0: michael@0: Maybe, 50> > fragChildren; michael@0: michael@0: // Remove the new child from the old parent if one exists michael@0: nsCOMPtr oldParent = newContent->GetParentNode(); michael@0: if (oldParent) { michael@0: int32_t removeIndex = oldParent->IndexOf(newContent); michael@0: if (removeIndex < 0) { michael@0: // newContent is anonymous. We can't deal with this, so just bail michael@0: NS_ERROR("How come our flags didn't catch this?"); michael@0: aError.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Hold a strong ref to nodeToInsertBefore across the removal of newContent michael@0: nsCOMPtr kungFuDeathGrip = nodeToInsertBefore; michael@0: michael@0: // Removing a child can run script, via XBL destructors. michael@0: nsMutationGuard guard; michael@0: michael@0: // Scope for the mutation batch and scriptblocker, so they go away michael@0: // while kungFuDeathGrip is still alive. michael@0: { michael@0: mozAutoDocUpdate batch(newContent->GetCurrentDoc(), michael@0: UPDATE_CONTENT_MODEL, true); michael@0: nsAutoMutationBatch mb(oldParent, true, true); michael@0: oldParent->RemoveChildAt(removeIndex, true); michael@0: if (nsAutoMutationBatch::GetCurrentBatch() == &mb) { michael@0: mb.RemovalDone(); michael@0: mb.SetPrevSibling(oldParent->GetChildAt(removeIndex - 1)); michael@0: mb.SetNextSibling(oldParent->GetChildAt(removeIndex)); michael@0: } michael@0: } michael@0: michael@0: // We expect one mutation (the removal) to have happened. michael@0: if (guard.Mutated(1)) { michael@0: // XBL destructors, yuck. michael@0: michael@0: // Verify that nodeToInsertBefore, if non-null, is still our child. If michael@0: // it's not, there's no way we can do this insert sanely; just bail out. michael@0: if (nodeToInsertBefore && nodeToInsertBefore->GetParent() != this) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Verify that newContent has no parent. michael@0: if (newContent->GetParentNode()) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // And verify that newContent is still allowed as our child. michael@0: if (aNewChild == aRefChild) { michael@0: // We've already removed aRefChild. So even if we were doing a replace, michael@0: // now we're doing a simple insert before nodeToInsertBefore. michael@0: if (!IsAllowedAsChild(newContent, this, false, nodeToInsertBefore)) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: } else { michael@0: if ((aRefChild && aRefChild->GetParent() != this) || michael@0: !IsAllowedAsChild(newContent, this, aReplace, aRefChild)) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: // And recompute nodeToInsertBefore, just in case. michael@0: if (aReplace) { michael@0: nodeToInsertBefore = aRefChild->GetNextSibling(); michael@0: } else { michael@0: nodeToInsertBefore = aRefChild; michael@0: } michael@0: } michael@0: } michael@0: } else if (nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { michael@0: // Make sure to remove all the fragment's kids. We need to do this before michael@0: // we start inserting anything, so we will run out XBL destructors and michael@0: // binding teardown (GOD, I HATE THESE THINGS) before we insert anything michael@0: // into the DOM. michael@0: uint32_t count = newContent->GetChildCount(); michael@0: michael@0: fragChildren.construct(); michael@0: michael@0: // Copy the children into a separate array to avoid having to deal with michael@0: // mutations to the fragment later on here. michael@0: fragChildren.ref().SetCapacity(count); michael@0: for (nsIContent* child = newContent->GetFirstChild(); michael@0: child; michael@0: child = child->GetNextSibling()) { michael@0: NS_ASSERTION(child->GetCurrentDoc() == nullptr, michael@0: "How did we get a child with a current doc?"); michael@0: fragChildren.ref().AppendElement(child); michael@0: } michael@0: michael@0: // Hold a strong ref to nodeToInsertBefore across the removals michael@0: nsCOMPtr kungFuDeathGrip = nodeToInsertBefore; michael@0: michael@0: nsMutationGuard guard; michael@0: michael@0: // Scope for the mutation batch and scriptblocker, so they go away michael@0: // while kungFuDeathGrip is still alive. michael@0: { michael@0: mozAutoDocUpdate batch(newContent->GetCurrentDoc(), michael@0: UPDATE_CONTENT_MODEL, true); michael@0: nsAutoMutationBatch mb(newContent, false, true); michael@0: michael@0: for (uint32_t i = count; i > 0;) { michael@0: newContent->RemoveChildAt(--i, true); michael@0: } michael@0: } michael@0: michael@0: // We expect |count| removals michael@0: if (guard.Mutated(count)) { michael@0: // XBL destructors, yuck. michael@0: michael@0: // Verify that nodeToInsertBefore, if non-null, is still our child. If michael@0: // it's not, there's no way we can do this insert sanely; just bail out. michael@0: if (nodeToInsertBefore && nodeToInsertBefore->GetParent() != this) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Verify that all the things in fragChildren have no parent. michael@0: for (uint32_t i = 0; i < count; ++i) { michael@0: if (fragChildren.ref().ElementAt(i)->GetParentNode()) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: // Note that unlike the single-element case above, none of our kids can michael@0: // be aRefChild, so we can always pass through aReplace in the michael@0: // IsAllowedAsChild checks below and don't have to worry about whether michael@0: // recomputing nodeToInsertBefore is OK. michael@0: michael@0: // Verify that our aRefChild is still sensible michael@0: if (aRefChild && aRefChild->GetParent() != this) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Recompute nodeToInsertBefore, just in case. michael@0: if (aReplace) { michael@0: nodeToInsertBefore = aRefChild->GetNextSibling(); michael@0: } else { michael@0: nodeToInsertBefore = aRefChild; michael@0: } michael@0: michael@0: // And verify that newContent is still allowed as our child. Sadly, we michael@0: // need to reimplement the relevant part of IsAllowedAsChild() because michael@0: // now our nodes are in an array and all. If you change this code, michael@0: // change the code there. michael@0: if (IsNodeOfType(nsINode::eDOCUMENT)) { michael@0: bool sawElement = false; michael@0: for (uint32_t i = 0; i < count; ++i) { michael@0: nsIContent* child = fragChildren.ref().ElementAt(i); michael@0: if (child->IsElement()) { michael@0: if (sawElement) { michael@0: // No good michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: sawElement = true; michael@0: } michael@0: if (!IsAllowedAsChild(child, this, aReplace, aRefChild)) { michael@0: aError.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR); michael@0: return nullptr; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: mozAutoDocUpdate batch(GetCurrentDoc(), UPDATE_CONTENT_MODEL, true); michael@0: nsAutoMutationBatch mb; michael@0: michael@0: // Figure out which index we want to insert at. Note that we use michael@0: // nodeToInsertBefore to determine this, because it's possible that michael@0: // aRefChild == aNewChild, in which case we just removed it from the michael@0: // parent list. michael@0: int32_t insPos; michael@0: if (nodeToInsertBefore) { michael@0: insPos = IndexOf(nodeToInsertBefore); michael@0: if (insPos < 0) { michael@0: // XXXbz How the heck would _that_ happen, exactly? michael@0: aError.Throw(NS_ERROR_DOM_NOT_FOUND_ERR); michael@0: return nullptr; michael@0: } michael@0: } michael@0: else { michael@0: insPos = GetChildCount(); michael@0: } michael@0: michael@0: // If we're replacing and we haven't removed aRefChild yet, do so now michael@0: if (aReplace && aRefChild != aNewChild) { michael@0: mb.Init(this, true, true); michael@0: michael@0: // Since aRefChild is never null in the aReplace case, we know that at michael@0: // this point nodeToInsertBefore is the next sibling of aRefChild. michael@0: NS_ASSERTION(aRefChild->GetNextSibling() == nodeToInsertBefore, michael@0: "Unexpected nodeToInsertBefore"); michael@0: michael@0: // An since nodeToInsertBefore is at index insPos, we want to remove michael@0: // at the previous index. michael@0: NS_ASSERTION(insPos >= 1, "insPos too small"); michael@0: RemoveChildAt(insPos-1, true); michael@0: --insPos; michael@0: } michael@0: michael@0: // Move new child over to our document if needed. Do this after removing michael@0: // it from its parent so that AdoptNode doesn't fire DOMNodeRemoved michael@0: // DocumentType nodes are the only nodes that can have a null michael@0: // ownerDocument according to the DOM spec, and we need to allow michael@0: // inserting them w/o calling AdoptNode(). michael@0: if (doc != newContent->OwnerDoc()) { michael@0: aError = AdoptNodeIntoOwnerDoc(this, aNewChild); michael@0: if (aError.Failed()) { michael@0: return nullptr; michael@0: } michael@0: } else if (doc->DidDocumentOpen()) { michael@0: aError = CheckForOutdatedParent(this, aNewChild); michael@0: if (aError.Failed()) { michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: /* michael@0: * Check if we're inserting a document fragment. If we are, we need michael@0: * to actually add its children individually (i.e. we don't add the michael@0: * actual document fragment). michael@0: */ michael@0: nsINode* result = aReplace ? aRefChild : aNewChild; michael@0: if (nodeType == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) { michael@0: if (!aReplace) { michael@0: mb.Init(this, true, true); michael@0: } michael@0: nsAutoMutationBatch* mutationBatch = nsAutoMutationBatch::GetCurrentBatch(); michael@0: if (mutationBatch) { michael@0: mutationBatch->RemovalDone(); michael@0: mutationBatch->SetPrevSibling(GetChildAt(insPos - 1)); michael@0: mutationBatch->SetNextSibling(GetChildAt(insPos)); michael@0: } michael@0: michael@0: uint32_t count = fragChildren.ref().Length(); michael@0: if (!count) { michael@0: return result; michael@0: } michael@0: michael@0: bool appending = michael@0: !IsNodeOfType(eDOCUMENT) && uint32_t(insPos) == GetChildCount(); michael@0: int32_t firstInsPos = insPos; michael@0: nsIContent* firstInsertedContent = fragChildren.ref().ElementAt(0); michael@0: michael@0: // Iterate through the fragment's children, and insert them in the new michael@0: // parent michael@0: for (uint32_t i = 0; i < count; ++i, ++insPos) { michael@0: // XXXbz how come no reparenting here? That seems odd... michael@0: // Insert the child. michael@0: aError = InsertChildAt(fragChildren.ref().ElementAt(i), insPos, michael@0: !appending); michael@0: if (aError.Failed()) { michael@0: // Make sure to notify on any children that we did succeed to insert michael@0: if (appending && i != 0) { michael@0: nsNodeUtils::ContentAppended(static_cast(this), michael@0: firstInsertedContent, michael@0: firstInsPos); michael@0: } michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: if (mutationBatch && !appending) { michael@0: mutationBatch->NodesAdded(); michael@0: } michael@0: michael@0: // Notify and fire mutation events when appending michael@0: if (appending) { michael@0: nsNodeUtils::ContentAppended(static_cast(this), michael@0: firstInsertedContent, firstInsPos); michael@0: if (mutationBatch) { michael@0: mutationBatch->NodesAdded(); michael@0: } michael@0: // Optimize for the case when there are no listeners michael@0: if (nsContentUtils:: michael@0: HasMutationListeners(doc, NS_EVENT_BITS_MUTATION_NODEINSERTED)) { michael@0: Element::FireNodeInserted(doc, this, fragChildren.ref()); michael@0: } michael@0: } michael@0: } michael@0: else { michael@0: // Not inserting a fragment but rather a single node. michael@0: michael@0: // FIXME https://bugzilla.mozilla.org/show_bug.cgi?id=544654 michael@0: // We need to reparent here for nodes for which the parent of their michael@0: // wrapper is not the wrapper for their ownerDocument (XUL elements, michael@0: // form controls, ...). Also applies in the fragment code above. michael@0: michael@0: if (nsAutoMutationBatch::GetCurrentBatch() == &mb) { michael@0: mb.RemovalDone(); michael@0: mb.SetPrevSibling(GetChildAt(insPos - 1)); michael@0: mb.SetNextSibling(GetChildAt(insPos)); michael@0: } michael@0: aError = InsertChildAt(newContent, insPos, true); michael@0: if (aError.Failed()) { michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: return result; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::ReplaceOrInsertBefore(bool aReplace, nsIDOMNode *aNewChild, michael@0: nsIDOMNode *aRefChild, nsIDOMNode **aReturn) michael@0: { michael@0: nsCOMPtr newChild = do_QueryInterface(aNewChild); michael@0: if (!newChild) { michael@0: return NS_ERROR_NULL_POINTER; michael@0: } michael@0: michael@0: if (aReplace && !aRefChild) { michael@0: return NS_ERROR_NULL_POINTER; michael@0: } michael@0: michael@0: nsCOMPtr refChild = do_QueryInterface(aRefChild); michael@0: if (aRefChild && !refChild) { michael@0: return NS_NOINTERFACE; michael@0: } michael@0: michael@0: ErrorResult rv; michael@0: nsINode* result = ReplaceOrInsertBefore(aReplace, newChild, refChild, rv); michael@0: if (result) { michael@0: NS_ADDREF(*aReturn = result->AsDOMNode()); michael@0: } michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::CompareDocumentPosition(nsIDOMNode* aOther, uint16_t* aReturn) michael@0: { michael@0: nsCOMPtr other = do_QueryInterface(aOther); michael@0: NS_ENSURE_ARG(other); michael@0: *aReturn = CompareDocumentPosition(*other); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::IsEqualNode(nsIDOMNode* aOther, bool* aReturn) michael@0: { michael@0: nsCOMPtr other = do_QueryInterface(aOther); michael@0: *aReturn = IsEqualNode(other); michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsINode::BindObject(nsISupports* aObject) michael@0: { michael@0: nsCOMArray* objects = michael@0: static_cast*>(GetProperty(nsGkAtoms::keepobjectsalive)); michael@0: if (!objects) { michael@0: objects = new nsCOMArray(); michael@0: SetProperty(nsGkAtoms::keepobjectsalive, objects, michael@0: nsINode::DeleteProperty< nsCOMArray >, true); michael@0: } michael@0: objects->AppendObject(aObject); michael@0: } michael@0: michael@0: void michael@0: nsINode::UnbindObject(nsISupports* aObject) michael@0: { michael@0: nsCOMArray* objects = michael@0: static_cast*>(GetProperty(nsGkAtoms::keepobjectsalive)); michael@0: if (objects) { michael@0: objects->RemoveObject(aObject); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsINode::GetBoundMutationObservers(nsTArray >& aResult) michael@0: { michael@0: nsCOMArray* objects = michael@0: static_cast*>(GetProperty(nsGkAtoms::keepobjectsalive)); michael@0: if (objects) { michael@0: for (int32_t i = 0; i < objects->Count(); ++i) { michael@0: nsCOMPtr mo = do_QueryInterface(objects->ObjectAt(i)); michael@0: if (mo) { michael@0: MOZ_ASSERT(!aResult.Contains(mo)); michael@0: aResult.AppendElement(mo); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: size_t michael@0: nsINode::SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const michael@0: { michael@0: size_t n = 0; michael@0: EventListenerManager* elm = GetExistingListenerManager(); michael@0: if (elm) { michael@0: n += elm->SizeOfIncludingThis(aMallocSizeOf); michael@0: } michael@0: michael@0: // Measurement of the following members may be added later if DMD finds it is michael@0: // worthwhile: michael@0: // - mNodeInfo michael@0: // - mSlots michael@0: // michael@0: // The following members are not measured: michael@0: // - mParent, mNextSibling, mPreviousSibling, mFirstChild: because they're michael@0: // non-owning michael@0: return n; michael@0: } michael@0: michael@0: #define EVENT(name_, id_, type_, struct_) \ michael@0: EventHandlerNonNull* nsINode::GetOn##name_() { \ michael@0: EventListenerManager *elm = GetExistingListenerManager(); \ michael@0: return elm ? elm->GetEventHandler(nsGkAtoms::on##name_, EmptyString()) \ michael@0: : nullptr; \ michael@0: } \ michael@0: void nsINode::SetOn##name_(EventHandlerNonNull* handler) \ michael@0: { \ michael@0: EventListenerManager *elm = GetOrCreateListenerManager(); \ michael@0: if (elm) { \ michael@0: elm->SetEventHandler(nsGkAtoms::on##name_, EmptyString(), handler); \ michael@0: } \ michael@0: } michael@0: #define TOUCH_EVENT EVENT michael@0: #define DOCUMENT_ONLY_EVENT EVENT michael@0: #include "mozilla/EventNameList.h" michael@0: #undef DOCUMENT_ONLY_EVENT michael@0: #undef TOUCH_EVENT michael@0: #undef EVENT michael@0: michael@0: bool michael@0: nsINode::Contains(const nsINode* aOther) const michael@0: { michael@0: if (aOther == this) { michael@0: return true; michael@0: } michael@0: if (!aOther || michael@0: OwnerDoc() != aOther->OwnerDoc() || michael@0: IsInDoc() != aOther->IsInDoc() || michael@0: !(aOther->IsElement() || michael@0: aOther->IsNodeOfType(nsINode::eCONTENT)) || michael@0: !GetFirstChild()) { michael@0: return false; michael@0: } michael@0: michael@0: const nsIContent* other = static_cast(aOther); michael@0: if (this == OwnerDoc()) { michael@0: // document.contains(aOther) returns true if aOther is in the document, michael@0: // but is not in any anonymous subtree. michael@0: // IsInDoc() check is done already before this. michael@0: return !other->IsInAnonymousSubtree(); michael@0: } michael@0: michael@0: if (!IsElement() && !IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT)) { michael@0: return false; michael@0: } michael@0: michael@0: const nsIContent* thisContent = static_cast(this); michael@0: if (thisContent->GetBindingParent() != other->GetBindingParent()) { michael@0: return false; michael@0: } michael@0: michael@0: return nsContentUtils::ContentIsDescendantOf(other, this); michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::Contains(nsIDOMNode* aOther, bool* aReturn) michael@0: { michael@0: nsCOMPtr node = do_QueryInterface(aOther); michael@0: *aReturn = Contains(node); michael@0: return NS_OK; michael@0: } michael@0: michael@0: uint32_t michael@0: nsINode::Length() const michael@0: { michael@0: switch (NodeType()) { michael@0: case nsIDOMNode::DOCUMENT_TYPE_NODE: michael@0: return 0; michael@0: michael@0: case nsIDOMNode::TEXT_NODE: michael@0: case nsIDOMNode::CDATA_SECTION_NODE: michael@0: case nsIDOMNode::PROCESSING_INSTRUCTION_NODE: michael@0: case nsIDOMNode::COMMENT_NODE: michael@0: MOZ_ASSERT(IsNodeOfType(eCONTENT)); michael@0: return static_cast(this)->TextLength(); michael@0: michael@0: default: michael@0: return GetChildCount(); michael@0: } michael@0: } michael@0: michael@0: nsCSSSelectorList* michael@0: nsINode::ParseSelectorList(const nsAString& aSelectorString, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsIDocument* doc = OwnerDoc(); michael@0: nsIDocument::SelectorCache& cache = doc->GetSelectorCache(); michael@0: nsCSSSelectorList* selectorList = nullptr; michael@0: bool haveCachedList = cache.GetList(aSelectorString, &selectorList); michael@0: if (haveCachedList) { michael@0: if (!selectorList) { michael@0: // Invalid selector. michael@0: aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR); michael@0: } michael@0: return selectorList; michael@0: } michael@0: michael@0: nsCSSParser parser(doc->CSSLoader()); michael@0: michael@0: aRv = parser.ParseSelectorString(aSelectorString, michael@0: doc->GetDocumentURI(), michael@0: 0, // XXXbz get the line number! michael@0: &selectorList); michael@0: if (aRv.Failed()) { michael@0: // We hit this for syntax errors, which are quite common, so don't michael@0: // use NS_ENSURE_SUCCESS. (For example, jQuery has an extended set michael@0: // of selectors, but it sees if we can parse them first.) michael@0: MOZ_ASSERT(aRv.ErrorCode() == NS_ERROR_DOM_SYNTAX_ERR, michael@0: "Unexpected error, so cached version won't return it"); michael@0: cache.CacheList(aSelectorString, nullptr); michael@0: return nullptr; michael@0: } michael@0: michael@0: // Filter out pseudo-element selectors from selectorList michael@0: nsCSSSelectorList** slot = &selectorList; michael@0: do { michael@0: nsCSSSelectorList* cur = *slot; michael@0: if (cur->mSelectors->IsPseudoElement()) { michael@0: *slot = cur->mNext; michael@0: cur->mNext = nullptr; michael@0: delete cur; michael@0: } else { michael@0: slot = &cur->mNext; michael@0: } michael@0: } while (*slot); michael@0: michael@0: if (selectorList) { michael@0: NS_ASSERTION(selectorList->mSelectors, michael@0: "How can we not have any selectors?"); michael@0: cache.CacheList(aSelectorString, selectorList); michael@0: } else { michael@0: // This is the "only pseudo-element selectors" case, which is michael@0: // not common, so just don't worry about caching it. That way a michael@0: // null cached value can always indicate an invalid selector. michael@0: } michael@0: michael@0: return selectorList; michael@0: } michael@0: michael@0: static void michael@0: AddScopeElements(TreeMatchContext& aMatchContext, michael@0: nsINode* aMatchContextNode) michael@0: { michael@0: if (aMatchContextNode->IsElement()) { michael@0: aMatchContext.SetHasSpecifiedScope(); michael@0: aMatchContext.AddScopeElement(aMatchContextNode->AsElement()); michael@0: } michael@0: } michael@0: michael@0: namespace { michael@0: struct SelectorMatchInfo { michael@0: nsCSSSelectorList* const mSelectorList; michael@0: TreeMatchContext& mMatchContext; michael@0: }; michael@0: } michael@0: michael@0: // Given an id, find elements with that id under aRoot that match aMatchInfo if michael@0: // any is provided. If no SelectorMatchInfo is provided, just find the ones michael@0: // with the given id. aRoot must be in the document. michael@0: template michael@0: inline static void michael@0: FindMatchingElementsWithId(const nsAString& aId, nsINode* aRoot, michael@0: SelectorMatchInfo* aMatchInfo, michael@0: T& aList) michael@0: { michael@0: MOZ_ASSERT(aRoot->IsInDoc(), michael@0: "Don't call me if the root is not in the document"); michael@0: MOZ_ASSERT(aRoot->IsElement() || aRoot->IsNodeOfType(nsINode::eDOCUMENT), michael@0: "The optimization below to check ContentIsDescendantOf only for " michael@0: "elements depends on aRoot being either an element or a " michael@0: "document if it's in the document. Note that document fragments " michael@0: "can't be IsInDoc(), so should never show up here."); michael@0: michael@0: const nsSmallVoidArray* elements = aRoot->OwnerDoc()->GetAllElementsForId(aId); michael@0: michael@0: if (!elements) { michael@0: // Nothing to do; we're done michael@0: return; michael@0: } michael@0: michael@0: // XXXbz: Should we fall back to the tree walk if aRoot is not the michael@0: // document and |elements| is long, for some value of "long"? michael@0: for (int32_t i = 0; i < elements->Count(); ++i) { michael@0: Element *element = static_cast(elements->ElementAt(i)); michael@0: if (!aRoot->IsElement() || michael@0: (element != aRoot && michael@0: nsContentUtils::ContentIsDescendantOf(element, aRoot))) { michael@0: // We have an element with the right id and it's a strict descendant michael@0: // of aRoot. Make sure it really matches the selector. michael@0: if (!aMatchInfo || michael@0: nsCSSRuleProcessor::SelectorListMatches(element, michael@0: aMatchInfo->mMatchContext, michael@0: aMatchInfo->mSelectorList)) { michael@0: aList.AppendElement(element); michael@0: if (onlyFirstMatch) { michael@0: return; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Actually find elements matching aSelectorList (which must not be michael@0: // null) and which are descendants of aRoot and put them in aList. If michael@0: // onlyFirstMatch, then stop once the first one is found. michael@0: template michael@0: MOZ_ALWAYS_INLINE static void michael@0: FindMatchingElements(nsINode* aRoot, nsCSSSelectorList* aSelectorList, T &aList, michael@0: ErrorResult& aRv) michael@0: { michael@0: nsIDocument* doc = aRoot->OwnerDoc(); michael@0: michael@0: TreeMatchContext matchingContext(false, nsRuleWalker::eRelevantLinkUnvisited, michael@0: doc, TreeMatchContext::eNeverMatchVisited); michael@0: doc->FlushPendingLinkUpdates(); michael@0: AddScopeElements(matchingContext, aRoot); michael@0: michael@0: // Fast-path selectors involving IDs. We can only do this if aRoot michael@0: // is in the document and the document is not in quirks mode, since michael@0: // ID selectors are case-insensitive in quirks mode. Also, only do michael@0: // this if aSelectorList only has one selector, because otherwise michael@0: // ordering the elements correctly is a pain. michael@0: NS_ASSERTION(aRoot->IsElement() || aRoot->IsNodeOfType(nsINode::eDOCUMENT) || michael@0: !aRoot->IsInDoc(), michael@0: "The optimization below to check ContentIsDescendantOf only for " michael@0: "elements depends on aRoot being either an element or a " michael@0: "document if it's in the document."); michael@0: if (aRoot->IsInDoc() && michael@0: doc->GetCompatibilityMode() != eCompatibility_NavQuirks && michael@0: !aSelectorList->mNext && michael@0: aSelectorList->mSelectors->mIDList) { michael@0: nsIAtom* id = aSelectorList->mSelectors->mIDList->mAtom; michael@0: SelectorMatchInfo info = { aSelectorList, matchingContext }; michael@0: FindMatchingElementsWithId(nsDependentAtomString(id), michael@0: aRoot, &info, aList); michael@0: return; michael@0: } michael@0: michael@0: Collector results; michael@0: for (nsIContent* cur = aRoot->GetFirstChild(); michael@0: cur; michael@0: cur = cur->GetNextNode(aRoot)) { michael@0: if (cur->IsElement() && michael@0: nsCSSRuleProcessor::SelectorListMatches(cur->AsElement(), michael@0: matchingContext, michael@0: aSelectorList)) { michael@0: if (onlyFirstMatch) { michael@0: aList.AppendElement(cur->AsElement()); michael@0: return; michael@0: } michael@0: results.AppendElement(cur->AsElement()); michael@0: } michael@0: } michael@0: michael@0: const uint32_t len = results.Length(); michael@0: if (len) { michael@0: aList.SetCapacity(len); michael@0: for (uint32_t i = 0; i < len; ++i) { michael@0: aList.AppendElement(results.ElementAt(i)); michael@0: } michael@0: } michael@0: } michael@0: michael@0: struct ElementHolder { michael@0: ElementHolder() : mElement(nullptr) {} michael@0: void AppendElement(Element* aElement) { michael@0: NS_ABORT_IF_FALSE(!mElement, "Should only get one element"); michael@0: mElement = aElement; michael@0: } michael@0: void SetCapacity(uint32_t aCapacity) { MOZ_CRASH("Don't call me!"); } michael@0: uint32_t Length() { return 0; } michael@0: Element* ElementAt(uint32_t aIndex) { return nullptr; } michael@0: michael@0: Element* mElement; michael@0: }; michael@0: michael@0: Element* michael@0: nsINode::QuerySelector(const nsAString& aSelector, ErrorResult& aResult) michael@0: { michael@0: nsCSSSelectorList* selectorList = ParseSelectorList(aSelector, aResult); michael@0: if (!selectorList) { michael@0: // Either we failed (and aResult already has the exception), or this michael@0: // is a pseudo-element-only selector that matches nothing. michael@0: return nullptr; michael@0: } michael@0: ElementHolder holder; michael@0: FindMatchingElements(this, selectorList, holder, aResult); michael@0: return holder.mElement; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsINode::QuerySelectorAll(const nsAString& aSelector, ErrorResult& aResult) michael@0: { michael@0: nsRefPtr contentList = new nsSimpleContentList(this); michael@0: michael@0: nsCSSSelectorList* selectorList = ParseSelectorList(aSelector, aResult); michael@0: if (selectorList) { michael@0: FindMatchingElements>(this, michael@0: selectorList, michael@0: *contentList, michael@0: aResult); michael@0: } else { michael@0: // Either we failed (and aResult already has the exception), or this michael@0: // is a pseudo-element-only selector that matches nothing. michael@0: } michael@0: michael@0: return contentList.forget(); michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::QuerySelector(const nsAString& aSelector, nsIDOMElement **aReturn) michael@0: { michael@0: ErrorResult rv; michael@0: Element* result = nsINode::QuerySelector(aSelector, rv); michael@0: if (rv.Failed()) { michael@0: return rv.ErrorCode(); michael@0: } michael@0: nsCOMPtr elt = do_QueryInterface(result); michael@0: elt.forget(aReturn); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsINode::QuerySelectorAll(const nsAString& aSelector, nsIDOMNodeList **aReturn) michael@0: { michael@0: ErrorResult rv; michael@0: *aReturn = nsINode::QuerySelectorAll(aSelector, rv).take(); michael@0: return rv.ErrorCode(); michael@0: } michael@0: michael@0: Element* michael@0: nsINode::GetElementById(const nsAString& aId) michael@0: { michael@0: MOZ_ASSERT(IsElement() || IsNodeOfType(eDOCUMENT_FRAGMENT), michael@0: "Bogus this object for GetElementById call"); michael@0: if (IsInDoc()) { michael@0: ElementHolder holder; michael@0: FindMatchingElementsWithId(aId, this, nullptr, holder); michael@0: return holder.mElement; michael@0: } michael@0: michael@0: for (nsIContent* kid = GetFirstChild(); kid; kid = kid->GetNextNode(this)) { michael@0: if (!kid->IsElement()) { michael@0: continue; michael@0: } michael@0: nsIAtom* id = kid->AsElement()->GetID(); michael@0: if (id && id->Equals(aId)) { michael@0: return kid->AsElement(); michael@0: } michael@0: } michael@0: return nullptr; michael@0: } michael@0: michael@0: JSObject* michael@0: nsINode::WrapObject(JSContext *aCx) michael@0: { michael@0: MOZ_ASSERT(IsDOMBinding()); michael@0: michael@0: // Make sure one of these is true michael@0: // (1) our owner document has a script handling object, michael@0: // (2) Our owner document has had a script handling object, or has been marked michael@0: // to have had one, michael@0: // (3) we are running a privileged script. michael@0: // Event handling is possible only if (1). If (2) event handling is michael@0: // prevented. michael@0: // If the document has never had a script handling object, untrusted michael@0: // scripts (3) shouldn't touch it! michael@0: bool hasHadScriptHandlingObject = false; michael@0: if (!OwnerDoc()->GetScriptHandlingObject(hasHadScriptHandlingObject) && michael@0: !hasHadScriptHandlingObject && michael@0: !nsContentUtils::IsCallerChrome()) { michael@0: Throw(aCx, NS_ERROR_UNEXPECTED); michael@0: return nullptr; michael@0: } michael@0: michael@0: JS::Rooted obj(aCx, WrapNode(aCx)); michael@0: MOZ_ASSERT_IF(ChromeOnlyAccess(), michael@0: xpc::IsInXBLScope(obj) || !xpc::UseXBLScope(js::GetObjectCompartment(obj))); michael@0: return obj; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsINode::CloneNode(bool aDeep, ErrorResult& aError) michael@0: { michael@0: bool callUserDataHandlers = NodeType() != nsIDOMNode::DOCUMENT_NODE || michael@0: !static_cast(this)->CreatingStaticClone(); michael@0: michael@0: nsCOMPtr result; michael@0: aError = nsNodeUtils::CloneNodeImpl(this, aDeep, callUserDataHandlers, michael@0: getter_AddRefs(result)); michael@0: return result.forget(); michael@0: } michael@0: michael@0: nsDOMAttributeMap* michael@0: nsINode::GetAttributes() michael@0: { michael@0: if (!IsElement()) { michael@0: return nullptr; michael@0: } michael@0: return AsElement()->Attributes(); michael@0: } michael@0: michael@0: bool michael@0: EventTarget::DispatchEvent(Event& aEvent, michael@0: ErrorResult& aRv) michael@0: { michael@0: bool result = false; michael@0: aRv = DispatchEvent(&aEvent, &result); michael@0: return result; michael@0: }