2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2010-11-15 17:21:25 -08:00
|
|
|
/* vim: set ts=2 sw=2 et tw=99: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsNodeUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIContent.h"
|
2010-05-05 11:18:05 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIMutationObserver.h"
|
|
|
|
#include "nsIDocument.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "nsIDOMAttr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2009-09-10 06:23:40 -07:00
|
|
|
#include "nsDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef MOZ_XUL
|
|
|
|
#include "nsXULElement.h"
|
|
|
|
#endif
|
2008-09-05 08:45:03 -07:00
|
|
|
#include "nsBindingManager.h"
|
2009-09-30 15:56:50 -07:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2013-06-18 05:53:23 -07:00
|
|
|
#include "mozilla/dom/HTMLImageElement.h"
|
2013-03-19 05:23:54 -07:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2011-05-26 12:58:35 -07:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-01-31 13:55:54 -08:00
|
|
|
#include "nsObjectLoadingContent.h"
|
2012-03-31 09:30:13 -07:00
|
|
|
#include "nsDOMMutationObserver.h"
|
2012-09-26 07:17:46 -07:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-03-26 00:15:23 -07:00
|
|
|
#include "mozilla/dom/HTMLTemplateElement.h"
|
2013-12-02 02:26:11 -08:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
using namespace mozilla;
|
2010-04-30 06:12:05 -07:00
|
|
|
using namespace mozilla::dom;
|
2013-04-08 10:24:21 -07:00
|
|
|
using mozilla::AutoJSContext;
|
2010-04-30 06:12:05 -07:00
|
|
|
|
2007-12-04 10:37:54 -08:00
|
|
|
// This macro expects the ownerDocument of content_ to be in scope as
|
|
|
|
// |nsIDocument* doc|
|
2007-03-22 10:30:00 -07:00
|
|
|
#define IMPL_MUTATION_NOTIFICATION(func_, content_, params_) \
|
|
|
|
PR_BEGIN_MACRO \
|
2012-03-31 09:30:13 -07:00
|
|
|
bool needsEnterLeave = doc->MayHaveDOMMutationObservers(); \
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
nsDOMMutationObserver::EnterMutationHandling(); \
|
|
|
|
} \
|
2007-03-22 10:30:00 -07:00
|
|
|
nsINode* node = content_; \
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_ASSERTION(node->OwnerDoc() == doc, "Bogus document"); \
|
2007-12-04 10:37:54 -08:00
|
|
|
if (doc) { \
|
2012-10-26 07:48:29 -07:00
|
|
|
doc->BindingManager()->func_ params_; \
|
2007-12-04 10:37:54 -08:00
|
|
|
} \
|
2007-03-22 10:30:00 -07:00
|
|
|
do { \
|
|
|
|
nsINode::nsSlots* slots = node->GetExistingSlots(); \
|
|
|
|
if (slots && !slots->mMutationObservers.IsEmpty()) { \
|
|
|
|
/* No need to explicitly notify the first observer first \
|
|
|
|
since that'll happen anyway. */ \
|
|
|
|
NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS( \
|
|
|
|
slots->mMutationObservers, nsIMutationObserver, \
|
|
|
|
func_, params_); \
|
|
|
|
} \
|
2013-12-02 02:26:11 -08:00
|
|
|
ShadowRoot* shadow = ShadowRoot::FromNode(node); \
|
|
|
|
if (shadow) { \
|
2013-12-20 22:43:58 -08:00
|
|
|
node = shadow->GetPoolHost(); \
|
2013-12-02 02:26:11 -08:00
|
|
|
} else { \
|
|
|
|
node = node->GetParentNode(); \
|
|
|
|
} \
|
2007-03-22 10:30:00 -07:00
|
|
|
} while (node); \
|
2012-03-31 09:30:13 -07:00
|
|
|
if (needsEnterLeave) { \
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling(); \
|
|
|
|
} \
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_END_MACRO
|
|
|
|
|
2007-09-05 01:22:17 -07:00
|
|
|
void
|
|
|
|
nsNodeUtils::CharacterDataWillChange(nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2010-07-21 08:33:32 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataWillChange, aContent,
|
2007-09-05 01:22:17 -07:00
|
|
|
(doc, aContent, aInfo));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsNodeUtils::CharacterDataChanged(nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2010-07-21 08:33:32 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataChanged, aContent,
|
2007-03-22 10:30:00 -07:00
|
|
|
(doc, aContent, aInfo));
|
|
|
|
}
|
|
|
|
|
2009-06-29 11:36:25 -07:00
|
|
|
void
|
2010-08-24 00:06:20 -07:00
|
|
|
nsNodeUtils::AttributeWillChange(Element* aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNameSpaceID,
|
2009-06-29 11:36:25 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aModType)
|
2009-06-29 11:36:25 -07:00
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
2010-08-24 00:06:20 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeWillChange, aElement,
|
|
|
|
(doc, aElement, aNameSpaceID, aAttribute,
|
2009-06-29 11:36:25 -07:00
|
|
|
aModType));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2010-08-24 00:06:07 -07:00
|
|
|
nsNodeUtils::AttributeChanged(Element* aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNameSpaceID,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aModType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
2010-08-24 00:05:56 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeChanged, aElement,
|
|
|
|
(doc, aElement, aNameSpaceID, aAttribute,
|
2009-12-10 14:36:04 -08:00
|
|
|
aModType));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-08-15 03:06:01 -07:00
|
|
|
void
|
|
|
|
nsNodeUtils::AttributeSetToCurrentValue(Element* aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNameSpaceID,
|
2012-08-15 03:06:01 -07:00
|
|
|
nsIAtom* aAttribute)
|
|
|
|
{
|
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeSetToCurrentValue, aElement,
|
|
|
|
(doc, aElement, aNameSpaceID, aAttribute));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsNodeUtils::ContentAppended(nsIContent* aContainer,
|
2010-05-10 18:12:34 -07:00
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNewIndexInContainer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-21 08:33:32 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentAppended, aContainer,
|
2010-05-10 18:12:34 -07:00
|
|
|
(doc, aContainer, aFirstNewContent,
|
|
|
|
aNewIndexInContainer));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeUtils::ContentInserted(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aContainer->IsNodeOfType(nsINode::eCONTENT) ||
|
|
|
|
aContainer->IsNodeOfType(nsINode::eDOCUMENT),
|
|
|
|
"container must be an nsIContent or an nsIDocument");
|
|
|
|
nsIContent* container;
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDocument* document;
|
|
|
|
if (aContainer->IsNodeOfType(nsINode::eCONTENT)) {
|
2007-07-08 00:08:04 -07:00
|
|
|
container = static_cast<nsIContent*>(aContainer);
|
2007-12-04 10:37:54 -08:00
|
|
|
document = doc;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-07-30 07:20:58 -07:00
|
|
|
container = nullptr;
|
2007-07-08 00:08:04 -07:00
|
|
|
document = static_cast<nsIDocument*>(aContainer);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 08:33:32 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentInserted, aContainer,
|
2007-03-22 10:30:00 -07:00
|
|
|
(document, container, aChild, aIndexInContainer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeUtils::ContentRemoved(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer,
|
2010-07-21 15:05:17 -07:00
|
|
|
nsIContent* aPreviousSibling)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aContainer->IsNodeOfType(nsINode::eCONTENT) ||
|
|
|
|
aContainer->IsNodeOfType(nsINode::eDOCUMENT),
|
|
|
|
"container must be an nsIContent or an nsIDocument");
|
|
|
|
nsIContent* container;
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDocument* document;
|
|
|
|
if (aContainer->IsNodeOfType(nsINode::eCONTENT)) {
|
2007-07-08 00:08:04 -07:00
|
|
|
container = static_cast<nsIContent*>(aContainer);
|
2007-12-04 10:37:54 -08:00
|
|
|
document = doc;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
2012-07-30 07:20:58 -07:00
|
|
|
container = nullptr;
|
2007-07-08 00:08:04 -07:00
|
|
|
document = static_cast<nsIDocument*>(aContainer);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 08:37:41 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentRemoved, aContainer,
|
2010-07-21 15:05:17 -07:00
|
|
|
(document, container, aChild, aIndexInContainer,
|
|
|
|
aPreviousSibling));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-12 08:36:28 -07:00
|
|
|
nsNodeUtils::LastRelease(nsINode* aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsINode::nsSlots* slots = aNode->GetExistingSlots();
|
|
|
|
if (slots) {
|
|
|
|
if (!slots->mMutationObservers.IsEmpty()) {
|
2010-07-21 08:33:32 -07:00
|
|
|
NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS(slots->mMutationObservers,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIMutationObserver,
|
|
|
|
NodeWillBeDestroyed, (aNode));
|
|
|
|
}
|
|
|
|
|
2008-02-02 15:41:24 -08:00
|
|
|
delete slots;
|
2012-07-30 07:20:58 -07:00
|
|
|
aNode->mSlots = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kill properties first since that may run external code, so we want to
|
|
|
|
// be in as complete state as possible at that time.
|
2007-05-12 08:36:28 -07:00
|
|
|
if (aNode->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// Delete all properties before tearing down the document. Some of the
|
|
|
|
// properties are bound to nsINode objects and the destructor functions of
|
|
|
|
// the properties may want to use the owner document of the nsINode.
|
2010-04-22 19:41:38 -07:00
|
|
|
static_cast<nsIDocument*>(aNode)->DeleteAllProperties();
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
2009-09-30 15:56:50 -07:00
|
|
|
else {
|
|
|
|
if (aNode->HasProperties()) {
|
|
|
|
// Strong reference to the document so that deleting properties can't
|
|
|
|
// delete the document.
|
2011-10-18 03:53:36 -07:00
|
|
|
nsCOMPtr<nsIDocument> document = aNode->OwnerDoc();
|
2011-10-18 04:19:44 -07:00
|
|
|
document->DeleteAllPropertiesFor(aNode);
|
2009-09-30 15:56:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// I wonder whether it's faster to do the HasFlag check first....
|
|
|
|
if (aNode->IsNodeOfType(nsINode::eHTML_FORM_CONTROL) &&
|
|
|
|
aNode->HasFlag(ADDED_TO_FORM)) {
|
|
|
|
// Tell the form (if any) this node is going away. Don't
|
|
|
|
// notify, since we're being destroyed in any case.
|
2011-10-17 07:59:28 -07:00
|
|
|
static_cast<nsGenericHTMLFormElement*>(aNode)->ClearForm(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-06-18 05:53:23 -07:00
|
|
|
|
2013-07-03 06:21:30 -07:00
|
|
|
if (aNode->IsElement() && aNode->AsElement()->IsHTML(nsGkAtoms::img) &&
|
|
|
|
aNode->HasFlag(ADDED_TO_FORM)) {
|
2013-06-18 05:53:23 -07:00
|
|
|
HTMLImageElement* imageElem = static_cast<HTMLImageElement*>(aNode);
|
|
|
|
imageElem->ClearForm(true);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-05-12 08:36:28 -07:00
|
|
|
aNode->UnsetFlags(NODE_HAS_PROPERTIES);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-27 06:03:15 -08:00
|
|
|
if (aNode->NodeType() != nsIDOMNode::DOCUMENT_NODE &&
|
|
|
|
aNode->HasFlag(NODE_HAS_LISTENERMANAGER)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (nsContentUtils::IsInitialized()) {
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager* manager =
|
2013-10-22 16:32:04 -07:00
|
|
|
nsContentUtils::GetExistingListenerManagerForNode(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!manager) {
|
|
|
|
NS_ERROR("Huh, our bit says we have a listener manager list, "
|
|
|
|
"but there's nothing in the hash!?!!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsContentUtils::RemoveListenerManager(aNode);
|
|
|
|
aNode->UnsetFlags(NODE_HAS_LISTENERMANAGER);
|
|
|
|
}
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
if (aNode->IsElement()) {
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* ownerDoc = aNode->OwnerDoc();
|
2010-05-26 15:39:13 -07:00
|
|
|
Element* elem = aNode->AsElement();
|
2011-10-18 04:19:44 -07:00
|
|
|
ownerDoc->ClearBoxObjectFor(elem);
|
2013-12-06 10:17:19 -08:00
|
|
|
|
2010-05-26 15:39:13 -07:00
|
|
|
NS_ASSERTION(aNode->HasFlag(NODE_FORCE_XBL_BINDINGS) ||
|
2013-07-17 09:05:03 -07:00
|
|
|
!elem->GetXBLBinding(),
|
2010-05-26 15:39:13 -07:00
|
|
|
"Non-forced node has binding on destruction");
|
|
|
|
|
|
|
|
// if NODE_FORCE_XBL_BINDINGS is set, the node might still have a binding
|
|
|
|
// attached
|
|
|
|
if (aNode->HasFlag(NODE_FORCE_XBL_BINDINGS) &&
|
2011-10-18 04:19:44 -07:00
|
|
|
ownerDoc->BindingManager()) {
|
2010-06-03 18:09:08 -07:00
|
|
|
ownerDoc->BindingManager()->RemovedFromDocument(elem, ownerDoc);
|
2008-02-14 12:45:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 00:04:01 -07:00
|
|
|
aNode->ReleaseWrapper(aNode);
|
2013-12-06 10:17:19 -08:00
|
|
|
|
|
|
|
FragmentOrElement::RemoveBlackMarkedNode(aNode);
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
NoteUserData(void *aObject, nsIAtom *aKey, void *aXPCOMChild, void *aData)
|
|
|
|
{
|
|
|
|
nsCycleCollectionTraversalCallback* cb =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsCycleCollectionTraversalCallback*>(aData);
|
2014-09-11 03:22:30 -07:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*cb, "[user data]");
|
2007-07-08 00:08:04 -07:00
|
|
|
cb->NoteXPCOMChild(static_cast<nsISupports*>(aXPCOMChild));
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsNodeUtils::TraverseUserData(nsINode* aNode,
|
|
|
|
nsCycleCollectionTraversalCallback &aCb)
|
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* ownerDoc = aNode->OwnerDoc();
|
2010-04-22 19:41:38 -07:00
|
|
|
ownerDoc->PropertyTable(DOM_USER_DATA)->Enumerate(aNode, NoteUserData, &aCb);
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* static */
|
|
|
|
nsresult
|
2014-09-11 03:22:30 -07:00
|
|
|
nsNodeUtils::CloneNodeImpl(nsINode *aNode, bool aDeep, nsINode **aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-10-09 05:31:24 -07:00
|
|
|
nsCOMPtr<nsINode> newNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMArray<nsINode> nodesWithProperties;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsresult rv = Clone(aNode, aDeep, nullptr, nodesWithProperties,
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_AddRefs(newNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
newNode.swap(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNodeInfoManager *aNewNodeInfoManager,
|
2013-05-07 19:34:56 -07:00
|
|
|
JS::Handle<JSObject*> aReparentScope,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMArray<nsINode> &aNodesWithProperties,
|
2009-09-28 13:33:29 -07:00
|
|
|
nsINode *aParent, nsINode **aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-08 10:24:21 -07:00
|
|
|
NS_PRECONDITION((!aClone && aNewNodeInfoManager) || !aReparentScope,
|
2007-03-22 10:30:00 -07:00
|
|
|
"If cloning or not getting a new nodeinfo we shouldn't "
|
|
|
|
"rewrap");
|
2009-09-28 13:33:29 -07:00
|
|
|
NS_PRECONDITION(!aParent || aNode->IsNodeOfType(nsINode::eCONTENT),
|
|
|
|
"Can't insert document or attribute nodes into a parent");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
*aResult = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// First deal with aNode and walk its attributes (and their children). Then,
|
2011-10-17 07:59:28 -07:00
|
|
|
// if aDeep is true, deal with aNode's children (and recurse into their
|
2007-03-22 10:30:00 -07:00
|
|
|
// attributes and children).
|
|
|
|
|
2013-04-08 10:24:21 -07:00
|
|
|
AutoJSContext cx;
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult rv;
|
2010-11-15 17:21:25 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNodeInfoManager *nodeInfoManager = aNewNodeInfoManager;
|
|
|
|
|
|
|
|
// aNode.
|
2014-06-19 19:01:40 -07:00
|
|
|
NodeInfo *nodeInfo = aNode->mNodeInfo;
|
|
|
|
nsRefPtr<NodeInfo> newNodeInfo;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (nodeInfoManager) {
|
2008-10-13 09:12:26 -07:00
|
|
|
|
|
|
|
// Don't allow importing/adopting nodes from non-privileged "scriptable"
|
|
|
|
// documents to "non-scriptable" documents.
|
|
|
|
nsIDocument* newDoc = nodeInfoManager->GetDocument();
|
2008-12-03 02:25:21 -08:00
|
|
|
NS_ENSURE_STATE(newDoc);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasHadScriptHandlingObject = false;
|
2008-10-13 09:12:26 -07:00
|
|
|
if (!newDoc->GetScriptHandlingObject(hasHadScriptHandlingObject) &&
|
|
|
|
!hasHadScriptHandlingObject) {
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* currentDoc = aNode->OwnerDoc();
|
2011-10-18 04:19:44 -07:00
|
|
|
NS_ENSURE_STATE((nsContentUtils::IsChromeDoc(currentDoc) ||
|
2008-12-03 02:25:21 -08:00
|
|
|
(!currentDoc->GetScriptHandlingObject(hasHadScriptHandlingObject) &&
|
|
|
|
!hasHadScriptHandlingObject)));
|
2008-10-13 09:12:26 -07:00
|
|
|
}
|
|
|
|
|
2008-09-12 15:32:18 -07:00
|
|
|
newNodeInfo = nodeInfoManager->GetNodeInfo(nodeInfo->NameAtom(),
|
|
|
|
nodeInfo->GetPrefixAtom(),
|
2011-06-14 00:56:49 -07:00
|
|
|
nodeInfo->NamespaceID(),
|
|
|
|
nodeInfo->NodeType(),
|
|
|
|
nodeInfo->GetExtraName());
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nodeInfo = newNodeInfo;
|
|
|
|
}
|
|
|
|
|
2012-11-14 14:10:08 -08:00
|
|
|
Element *elem = aNode->IsElement() ? aNode->AsElement() : nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> clone;
|
|
|
|
if (aClone) {
|
|
|
|
rv = aNode->Clone(nodeInfo, getter_AddRefs(clone));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (aParent) {
|
|
|
|
// If we're cloning we need to insert the cloned children into the cloned
|
|
|
|
// parent.
|
2009-09-28 13:33:29 -07:00
|
|
|
rv = aParent->AppendChildTo(static_cast<nsIContent*>(clone.get()),
|
2011-10-17 07:59:28 -07:00
|
|
|
false);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else if (aDeep && clone->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// After cloning the document itself, we want to clone the children into
|
|
|
|
// the cloned document (somewhat like cloning and importing them into the
|
|
|
|
// cloned document).
|
|
|
|
nodeInfoManager = clone->mNodeInfo->NodeInfoManager();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nodeInfoManager) {
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* oldDoc = aNode->OwnerDoc();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool wasRegistered = false;
|
2011-10-18 04:19:44 -07:00
|
|
|
if (aNode->IsElement()) {
|
2010-04-30 06:12:05 -07:00
|
|
|
Element* element = aNode->AsElement();
|
|
|
|
oldDoc->ClearBoxObjectFor(element);
|
2014-06-18 19:09:35 -07:00
|
|
|
wasRegistered = oldDoc->UnregisterActivityObserver(element);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
aNode->mNodeInfo.swap(newNodeInfo);
|
2010-09-01 15:48:24 -07:00
|
|
|
if (elem) {
|
|
|
|
elem->NodeInfoChanged(newNodeInfo);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* newDoc = aNode->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (newDoc) {
|
2009-10-01 07:10:13 -07:00
|
|
|
// XXX what if oldDoc is null, we don't know if this should be
|
|
|
|
// registered or not! Can that really happen?
|
2009-05-07 18:32:32 -07:00
|
|
|
if (wasRegistered) {
|
2014-06-18 19:09:35 -07:00
|
|
|
newDoc->RegisterActivityObserver(aNode->AsElement());
|
2009-05-07 18:32:32 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPIDOMWindow* window = newDoc->GetInnerWindow();
|
|
|
|
if (window) {
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager* elm = aNode->GetExistingListenerManager();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (elm) {
|
|
|
|
window->SetMutationListeners(elm->MutationListenerBits());
|
2008-10-15 14:06:32 -07:00
|
|
|
if (elm->MayHavePaintEventListener()) {
|
|
|
|
window->SetHasPaintEventListeners();
|
|
|
|
}
|
2011-04-26 05:31:21 -07:00
|
|
|
if (elm->MayHaveTouchEventListener()) {
|
|
|
|
window->SetHasTouchEventListeners();
|
|
|
|
}
|
2011-09-18 00:45:14 -07:00
|
|
|
if (elm->MayHaveMouseEnterLeaveEventListener()) {
|
|
|
|
window->SetHasMouseEnterLeaveEventListeners();
|
|
|
|
}
|
2014-02-10 22:33:29 -08:00
|
|
|
if (elm->MayHavePointerEnterLeaveEventListener()) {
|
|
|
|
window->SetHasPointerEnterLeaveEventListeners();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-29 23:09:56 -07:00
|
|
|
if (wasRegistered && oldDoc != newDoc) {
|
2009-10-01 07:10:13 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLMediaElement> domMediaElem(do_QueryInterface(aNode));
|
|
|
|
if (domMediaElem) {
|
2013-03-19 05:23:54 -07:00
|
|
|
HTMLMediaElement* mediaElem = static_cast<HTMLMediaElement*>(aNode);
|
2009-10-01 07:10:13 -07:00
|
|
|
mediaElem->NotifyOwnerDocumentActivityChanged();
|
|
|
|
}
|
2012-01-31 13:55:54 -08:00
|
|
|
nsCOMPtr<nsIObjectLoadingContent> objectLoadingContent(do_QueryInterface(aNode));
|
|
|
|
if (objectLoadingContent) {
|
|
|
|
nsObjectLoadingContent* olc = static_cast<nsObjectLoadingContent*>(objectLoadingContent.get());
|
|
|
|
olc->NotifyOwnerDocumentActivityChanged();
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 02:44:19 -07:00
|
|
|
|
2012-08-13 15:11:50 -07:00
|
|
|
if (oldDoc != newDoc && oldDoc->MayHaveDOMMutationObservers()) {
|
|
|
|
newDoc->SetMayHaveDOMMutationObservers();
|
2010-08-26 18:30:03 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (elem) {
|
|
|
|
elem->RecompileScriptEventListeners();
|
|
|
|
}
|
|
|
|
|
2013-04-19 12:57:15 -07:00
|
|
|
if (aReparentScope) {
|
|
|
|
JS::Rooted<JSObject*> wrapper(cx);
|
|
|
|
if ((wrapper = aNode->GetWrapper())) {
|
|
|
|
if (IsDOMObject(wrapper)) {
|
2014-04-08 15:27:15 -07:00
|
|
|
JSAutoCompartment ac(cx, wrapper);
|
2013-04-19 12:57:15 -07:00
|
|
|
rv = ReparentWrapper(cx, wrapper);
|
|
|
|
} else {
|
|
|
|
nsIXPConnect *xpc = nsContentUtils::XPConnect();
|
|
|
|
if (xpc) {
|
|
|
|
rv = xpc->ReparentWrappedNativeIfFound(cx, wrapper, aReparentScope, aNode);
|
2013-06-19 14:03:31 -07:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2013-04-19 12:57:15 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2013-04-19 12:57:15 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aNode->mNodeInfo.swap(nodeInfo);
|
2012-09-26 07:17:46 -07:00
|
|
|
|
2013-04-19 12:57:15 -07:00
|
|
|
return rv;
|
|
|
|
}
|
2012-09-26 07:17:46 -07:00
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 00:25:39 -07:00
|
|
|
if (aDeep && (!aClone || !aNode->IsNodeOfType(nsINode::eATTRIBUTE))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// aNode's children.
|
2011-09-27 00:54:58 -07:00
|
|
|
for (nsIContent* cloneChild = aNode->GetFirstChild();
|
|
|
|
cloneChild;
|
2012-04-03 00:25:39 -07:00
|
|
|
cloneChild = cloneChild->GetNextSibling()) {
|
2009-09-28 13:33:29 -07:00
|
|
|
nsCOMPtr<nsINode> child;
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = CloneAndAdopt(cloneChild, aClone, true, nodeInfoManager,
|
2013-04-08 10:24:21 -07:00
|
|
|
aReparentScope, aNodesWithProperties, clone,
|
2010-12-03 08:43:32 -08:00
|
|
|
getter_AddRefs(child));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 00:15:23 -07:00
|
|
|
// Cloning template element.
|
|
|
|
if (aDeep && aClone && IsTemplateElement(aNode)) {
|
|
|
|
DocumentFragment* origContent =
|
|
|
|
static_cast<HTMLTemplateElement*>(aNode)->Content();
|
|
|
|
DocumentFragment* cloneContent =
|
|
|
|
static_cast<HTMLTemplateElement*>(clone.get())->Content();
|
|
|
|
|
|
|
|
// Clone the children into the clone's template content owner
|
|
|
|
// document's nodeinfo manager.
|
|
|
|
nsNodeInfoManager* ownerNodeInfoManager =
|
|
|
|
cloneContent->mNodeInfo->NodeInfoManager();
|
|
|
|
|
|
|
|
for (nsIContent* cloneChild = origContent->GetFirstChild();
|
|
|
|
cloneChild;
|
|
|
|
cloneChild = cloneChild->GetNextSibling()) {
|
|
|
|
nsCOMPtr<nsINode> child;
|
|
|
|
rv = CloneAndAdopt(cloneChild, aClone, aDeep, ownerNodeInfoManager,
|
2013-04-08 10:24:21 -07:00
|
|
|
aReparentScope, aNodesWithProperties, cloneContent,
|
2013-03-26 00:15:23 -07:00
|
|
|
getter_AddRefs(child));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
// XXX setting document on some nodes not in a document so XBL will bind
|
|
|
|
// and chrome won't break. Make XBL bind to document-less nodes!
|
|
|
|
// XXXbz Once this is fixed, fix up the asserts in all implementations of
|
|
|
|
// BindToTree to assert what they would like to assert, and fix the
|
|
|
|
// ChangeDocumentFor() call in nsXULElement::BindToTree as well. Also,
|
|
|
|
// remove the UnbindFromTree call in ~nsXULElement, and add back in the
|
|
|
|
// precondition in nsXULElement::UnbindFromTree and remove the line in
|
|
|
|
// nsXULElement.h that makes nsNodeUtils a friend of nsXULElement.
|
|
|
|
// Note: Make sure to do this witchery _after_ we've done any deep
|
|
|
|
// cloning, so kids of the new node aren't confused about whether they're
|
|
|
|
// in a document.
|
|
|
|
#ifdef MOZ_XUL
|
2010-04-30 06:12:05 -07:00
|
|
|
if (aClone && !aParent && aNode->IsElement() &&
|
|
|
|
aNode->AsElement()->IsXUL()) {
|
2012-07-19 01:31:47 -07:00
|
|
|
if (!aNode->OwnerDoc()->IsLoadedAsInteractiveData()) {
|
2007-05-15 18:13:47 -07:00
|
|
|
clone->SetFlags(NODE_FORCE_XBL_BINDINGS);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (aNode->HasProperties()) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ok = aNodesWithProperties.AppendObject(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aClone) {
|
|
|
|
ok = ok && aNodesWithProperties.AppendObject(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
|
2009-09-28 13:33:29 -07:00
|
|
|
clone.forget(aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-05-12 08:36:28 -07:00
|
|
|
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsNodeUtils::UnlinkUserData(nsINode *aNode)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aNode->HasProperties(), "Call to UnlinkUserData not needed.");
|
|
|
|
|
|
|
|
// Strong reference to the document so that deleting properties can't
|
|
|
|
// delete the document.
|
2011-10-18 03:53:36 -07:00
|
|
|
nsCOMPtr<nsIDocument> document = aNode->OwnerDoc();
|
2011-10-18 04:19:44 -07:00
|
|
|
document->PropertyTable(DOM_USER_DATA)->DeleteAllPropertiesFor(aNode);
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
2013-03-26 00:15:23 -07:00
|
|
|
|
|
|
|
bool
|
|
|
|
nsNodeUtils::IsTemplateElement(const nsINode *aNode)
|
|
|
|
{
|
|
|
|
return aNode->IsElement() && aNode->AsElement()->IsHTML(nsGkAtoms::_template);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
nsNodeUtils::GetFirstChildOfTemplateOrNode(nsINode* aNode)
|
|
|
|
{
|
|
|
|
if (nsNodeUtils::IsTemplateElement(aNode)) {
|
|
|
|
DocumentFragment* frag =
|
|
|
|
static_cast<HTMLTemplateElement*>(aNode)->Content();
|
|
|
|
return frag->GetFirstChild();
|
|
|
|
}
|
|
|
|
|
|
|
|
return aNode->GetFirstChild();
|
|
|
|
}
|
|
|
|
|