2006-07-02 11:23:10 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2010-11-16 04:21:25 +03:00
|
|
|
/* vim: set ts=2 sw=2 et tw=99: */
|
2012-05-21 15:12:37 +04: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/. */
|
2006-07-02 11:23:10 +04:00
|
|
|
|
|
|
|
#include "nsNodeUtils.h"
|
2006-08-16 12:44:45 +04:00
|
|
|
#include "nsContentUtils.h"
|
2006-07-02 11:23:10 +04:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIContent.h"
|
2010-05-05 22:18:05 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2006-07-02 11:23:10 +04:00
|
|
|
#include "nsIMutationObserver.h"
|
|
|
|
#include "nsIDocument.h"
|
2014-03-17 10:56:53 +04:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2006-09-05 14:22:54 +04:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "nsIDOMAttr.h"
|
|
|
|
#include "nsCOMArray.h"
|
2007-02-01 01:54:24 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2009-09-10 17:23:40 +04:00
|
|
|
#include "nsDocument.h"
|
2006-11-07 06:13:49 +03:00
|
|
|
#ifdef MOZ_XUL
|
2006-10-31 16:25:40 +03:00
|
|
|
#include "nsXULElement.h"
|
2006-11-07 06:13:49 +03:00
|
|
|
#endif
|
2008-09-05 19:45:03 +04:00
|
|
|
#include "nsBindingManager.h"
|
2009-10-01 02:56:50 +04:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2013-06-18 16:53:23 +04:00
|
|
|
#include "mozilla/dom/HTMLImageElement.h"
|
2013-03-19 16:23:54 +04:00
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2011-05-26 23:58:35 +04:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-02-01 01:55:54 +04:00
|
|
|
#include "nsObjectLoadingContent.h"
|
2012-03-31 20:30:13 +04:00
|
|
|
#include "nsDOMMutationObserver.h"
|
2012-09-26 18:17:46 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-03-26 11:15:23 +04:00
|
|
|
#include "mozilla/dom/HTMLTemplateElement.h"
|
2013-12-02 14:26:11 +04:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2006-07-02 11:23:10 +04:00
|
|
|
|
2014-03-17 10:56:53 +04:00
|
|
|
using namespace mozilla;
|
2010-04-30 17:12:05 +04:00
|
|
|
using namespace mozilla::dom;
|
2013-04-08 21:24:21 +04:00
|
|
|
using mozilla::AutoJSContext;
|
2010-04-30 17:12:05 +04:00
|
|
|
|
2007-12-04 21:37:54 +03:00
|
|
|
// This macro expects the ownerDocument of content_ to be in scope as
|
|
|
|
// |nsIDocument* doc|
|
2006-08-16 02:41:58 +04:00
|
|
|
#define IMPL_MUTATION_NOTIFICATION(func_, content_, params_) \
|
|
|
|
PR_BEGIN_MACRO \
|
2012-03-31 20:30:13 +04:00
|
|
|
bool needsEnterLeave = doc->MayHaveDOMMutationObservers(); \
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
nsDOMMutationObserver::EnterMutationHandling(); \
|
|
|
|
} \
|
2006-07-02 11:23:10 +04:00
|
|
|
nsINode* node = content_; \
|
2012-03-31 20:30:13 +04:00
|
|
|
NS_ASSERTION(node->OwnerDoc() == doc, "Bogus document"); \
|
2007-12-04 21:37:54 +03:00
|
|
|
if (doc) { \
|
2012-10-26 18:48:29 +04:00
|
|
|
doc->BindingManager()->func_ params_; \
|
2007-12-04 21:37:54 +03:00
|
|
|
} \
|
2006-07-02 11:23:10 +04: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. */ \
|
2006-08-16 02:41:58 +04:00
|
|
|
NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS( \
|
|
|
|
slots->mMutationObservers, nsIMutationObserver, \
|
|
|
|
func_, params_); \
|
2006-07-02 11:23:10 +04:00
|
|
|
} \
|
2013-12-02 14:26:11 +04:00
|
|
|
ShadowRoot* shadow = ShadowRoot::FromNode(node); \
|
|
|
|
if (shadow) { \
|
2013-12-21 10:43:58 +04:00
|
|
|
node = shadow->GetPoolHost(); \
|
2013-12-02 14:26:11 +04:00
|
|
|
} else { \
|
|
|
|
node = node->GetParentNode(); \
|
|
|
|
} \
|
2006-08-16 02:41:58 +04:00
|
|
|
} while (node); \
|
2012-03-31 20:30:13 +04:00
|
|
|
if (needsEnterLeave) { \
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling(); \
|
|
|
|
} \
|
2006-08-16 02:41:58 +04:00
|
|
|
PR_END_MACRO
|
2006-07-02 11:23:10 +04:00
|
|
|
|
2007-09-05 12:22:17 +04:00
|
|
|
void
|
|
|
|
nsNodeUtils::CharacterDataWillChange(nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2010-07-21 19:33:32 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataWillChange, aContent,
|
2007-09-05 12:22:17 +04:00
|
|
|
(doc, aContent, aInfo));
|
|
|
|
}
|
|
|
|
|
2006-07-02 11:23:10 +04:00
|
|
|
void
|
2006-11-04 00:51:01 +03:00
|
|
|
nsNodeUtils::CharacterDataChanged(nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
2006-07-02 11:23:10 +04:00
|
|
|
{
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2010-07-21 19:33:32 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataChanged, aContent,
|
2006-11-04 00:51:01 +03:00
|
|
|
(doc, aContent, aInfo));
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
2009-06-29 22:36:25 +04:00
|
|
|
void
|
2010-08-24 11:06:20 +04:00
|
|
|
nsNodeUtils::AttributeWillChange(Element* aElement,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNameSpaceID,
|
2009-06-29 22:36:25 +04:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aModType)
|
2009-06-29 22:36:25 +04:00
|
|
|
{
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
2010-08-24 11:06:20 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeWillChange, aElement,
|
|
|
|
(doc, aElement, aNameSpaceID, aAttribute,
|
2009-06-29 22:36:25 +04:00
|
|
|
aModType));
|
|
|
|
}
|
|
|
|
|
2006-07-02 11:23:10 +04:00
|
|
|
void
|
2010-08-24 11:06:07 +04:00
|
|
|
nsNodeUtils::AttributeChanged(Element* aElement,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNameSpaceID,
|
2006-07-02 11:23:10 +04:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aModType)
|
2006-07-02 11:23:10 +04:00
|
|
|
{
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
2010-08-24 11:05:56 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeChanged, aElement,
|
|
|
|
(doc, aElement, aNameSpaceID, aAttribute,
|
2009-12-11 01:36:04 +03:00
|
|
|
aModType));
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
2012-08-15 14:06:01 +04:00
|
|
|
void
|
|
|
|
nsNodeUtils::AttributeSetToCurrentValue(Element* aElement,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNameSpaceID,
|
2012-08-15 14:06:01 +04:00
|
|
|
nsIAtom* aAttribute)
|
|
|
|
{
|
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeSetToCurrentValue, aElement,
|
|
|
|
(doc, aElement, aNameSpaceID, aAttribute));
|
|
|
|
}
|
|
|
|
|
2006-07-02 11:23:10 +04:00
|
|
|
void
|
|
|
|
nsNodeUtils::ContentAppended(nsIContent* aContainer,
|
2010-05-11 05:12:34 +04:00
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNewIndexInContainer)
|
2006-07-02 11:23:10 +04:00
|
|
|
{
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2006-07-02 11:23:10 +04:00
|
|
|
|
2010-07-21 19:33:32 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentAppended, aContainer,
|
2010-05-11 05:12:34 +04:00
|
|
|
(doc, aContainer, aFirstNewContent,
|
|
|
|
aNewIndexInContainer));
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeUtils::ContentInserted(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndexInContainer)
|
2006-07-02 11:23:10 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aContainer->IsNodeOfType(nsINode::eCONTENT) ||
|
|
|
|
aContainer->IsNodeOfType(nsINode::eDOCUMENT),
|
|
|
|
"container must be an nsIContent or an nsIDocument");
|
|
|
|
nsIContent* container;
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2006-07-02 11:23:10 +04:00
|
|
|
nsIDocument* document;
|
|
|
|
if (aContainer->IsNodeOfType(nsINode::eCONTENT)) {
|
2007-07-08 11:08:04 +04:00
|
|
|
container = static_cast<nsIContent*>(aContainer);
|
2007-12-04 21:37:54 +03:00
|
|
|
document = doc;
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-07-30 18:20:58 +04:00
|
|
|
container = nullptr;
|
2007-07-08 11:08:04 +04:00
|
|
|
document = static_cast<nsIDocument*>(aContainer);
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
2010-07-21 19:33:32 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentInserted, aContainer,
|
2006-08-16 02:41:58 +04:00
|
|
|
(document, container, aChild, aIndexInContainer));
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeUtils::ContentRemoved(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndexInContainer,
|
2010-07-22 02:05:17 +04:00
|
|
|
nsIContent* aPreviousSibling)
|
2006-07-02 11:23:10 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aContainer->IsNodeOfType(nsINode::eCONTENT) ||
|
|
|
|
aContainer->IsNodeOfType(nsINode::eDOCUMENT),
|
|
|
|
"container must be an nsIContent or an nsIDocument");
|
|
|
|
nsIContent* container;
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2006-07-02 11:23:10 +04:00
|
|
|
nsIDocument* document;
|
|
|
|
if (aContainer->IsNodeOfType(nsINode::eCONTENT)) {
|
2007-07-08 11:08:04 +04:00
|
|
|
container = static_cast<nsIContent*>(aContainer);
|
2007-12-04 21:37:54 +03:00
|
|
|
document = doc;
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
else {
|
2012-07-30 18:20:58 +04:00
|
|
|
container = nullptr;
|
2007-07-08 11:08:04 +04:00
|
|
|
document = static_cast<nsIDocument*>(aContainer);
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
2010-07-21 19:37:41 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentRemoved, aContainer,
|
2010-07-22 02:05:17 +04:00
|
|
|
(document, container, aChild, aIndexInContainer,
|
|
|
|
aPreviousSibling));
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-12 19:36:28 +04:00
|
|
|
nsNodeUtils::LastRelease(nsINode* aNode)
|
2006-07-02 11:23:10 +04:00
|
|
|
{
|
|
|
|
nsINode::nsSlots* slots = aNode->GetExistingSlots();
|
2006-08-25 14:00:11 +04:00
|
|
|
if (slots) {
|
|
|
|
if (!slots->mMutationObservers.IsEmpty()) {
|
2010-07-21 19:33:32 +04:00
|
|
|
NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS(slots->mMutationObservers,
|
2006-08-25 14:00:11 +04:00
|
|
|
nsIMutationObserver,
|
|
|
|
NodeWillBeDestroyed, (aNode));
|
|
|
|
}
|
|
|
|
|
2008-02-03 02:41:24 +03:00
|
|
|
delete slots;
|
2012-07-30 18:20:58 +04:00
|
|
|
aNode->mSlots = nullptr;
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
2006-08-16 12:44:45 +04:00
|
|
|
|
2006-09-02 17:21:05 +04: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 19:36:28 +04: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-23 06:41:38 +04:00
|
|
|
static_cast<nsIDocument*>(aNode)->DeleteAllProperties();
|
2007-05-12 19:36:28 +04:00
|
|
|
}
|
2009-10-01 02:56:50 +04:00
|
|
|
else {
|
|
|
|
if (aNode->HasProperties()) {
|
|
|
|
// Strong reference to the document so that deleting properties can't
|
|
|
|
// delete the document.
|
2011-10-18 14:53:36 +04:00
|
|
|
nsCOMPtr<nsIDocument> document = aNode->OwnerDoc();
|
2011-10-18 15:19:44 +04:00
|
|
|
document->DeleteAllPropertiesFor(aNode);
|
2009-10-01 02:56:50 +04: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 18:59:28 +04:00
|
|
|
static_cast<nsGenericHTMLFormElement*>(aNode)->ClearForm(true);
|
2006-08-16 12:44:45 +04:00
|
|
|
}
|
2013-06-18 16:53:23 +04:00
|
|
|
|
2013-07-03 17:21:30 +04:00
|
|
|
if (aNode->IsElement() && aNode->AsElement()->IsHTML(nsGkAtoms::img) &&
|
|
|
|
aNode->HasFlag(ADDED_TO_FORM)) {
|
2013-06-18 16:53:23 +04:00
|
|
|
HTMLImageElement* imageElem = static_cast<HTMLImageElement*>(aNode);
|
|
|
|
imageElem->ClearForm(true);
|
|
|
|
}
|
2006-09-02 17:21:05 +04:00
|
|
|
}
|
2007-05-12 19:36:28 +04:00
|
|
|
aNode->UnsetFlags(NODE_HAS_PROPERTIES);
|
2006-09-02 17:21:05 +04:00
|
|
|
|
2012-02-27 18:03:15 +04:00
|
|
|
if (aNode->NodeType() != nsIDOMNode::DOCUMENT_NODE &&
|
|
|
|
aNode->HasFlag(NODE_HAS_LISTENERMANAGER)) {
|
2006-09-02 17:21:05 +04:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (nsContentUtils::IsInitialized()) {
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* manager =
|
2013-10-23 03:32:04 +04:00
|
|
|
nsContentUtils::GetExistingListenerManagerForNode(aNode);
|
2006-09-02 17:21:05 +04: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 17:12:05 +04:00
|
|
|
if (aNode->IsElement()) {
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* ownerDoc = aNode->OwnerDoc();
|
2010-05-27 02:39:13 +04:00
|
|
|
Element* elem = aNode->AsElement();
|
2011-10-18 15:19:44 +04:00
|
|
|
ownerDoc->ClearBoxObjectFor(elem);
|
2013-12-06 22:17:19 +04:00
|
|
|
|
2010-05-27 02:39:13 +04:00
|
|
|
NS_ASSERTION(aNode->HasFlag(NODE_FORCE_XBL_BINDINGS) ||
|
2013-07-17 20:05:03 +04:00
|
|
|
!elem->GetXBLBinding(),
|
2010-05-27 02:39:13 +04: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 15:19:44 +04:00
|
|
|
ownerDoc->BindingManager()) {
|
2010-06-04 05:09:08 +04:00
|
|
|
ownerDoc->BindingManager()->RemovedFromDocument(elem, ownerDoc);
|
2008-02-14 23:45:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-02 11:04:01 +04:00
|
|
|
aNode->ReleaseWrapper(aNode);
|
2013-12-06 22:17:19 +04:00
|
|
|
|
|
|
|
FragmentOrElement::RemoveBlackMarkedNode(aNode);
|
2007-05-12 19:36:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
NoteUserData(void *aObject, nsIAtom *aKey, void *aXPCOMChild, void *aData)
|
|
|
|
{
|
|
|
|
nsCycleCollectionTraversalCallback* cb =
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<nsCycleCollectionTraversalCallback*>(aData);
|
2014-09-11 14:22:30 +04:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*cb, "[user data]");
|
2007-07-08 11:08:04 +04:00
|
|
|
cb->NoteXPCOMChild(static_cast<nsISupports*>(aXPCOMChild));
|
2007-05-12 19:36:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsNodeUtils::TraverseUserData(nsINode* aNode,
|
|
|
|
nsCycleCollectionTraversalCallback &aCb)
|
|
|
|
{
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* ownerDoc = aNode->OwnerDoc();
|
2010-04-23 06:41:38 +04:00
|
|
|
ownerDoc->PropertyTable(DOM_USER_DATA)->Enumerate(aNode, NoteUserData, &aCb);
|
2007-05-12 19:36:28 +04:00
|
|
|
}
|
|
|
|
|
2006-09-05 14:22:54 +04:00
|
|
|
/* static */
|
|
|
|
nsresult
|
2014-09-11 14:22:30 +04:00
|
|
|
nsNodeUtils::CloneNodeImpl(nsINode *aNode, bool aDeep, nsINode **aResult)
|
2006-09-05 14:22:54 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2006-09-05 14:22:54 +04:00
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsCOMPtr<nsINode> newNode;
|
2006-09-05 14:22:54 +04:00
|
|
|
nsCOMArray<nsINode> nodesWithProperties;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsresult rv = Clone(aNode, aDeep, nullptr, nodesWithProperties,
|
2006-09-05 14:22:54 +04:00
|
|
|
getter_AddRefs(newNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
newNode.swap(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
|
2006-09-05 14:22:54 +04:00
|
|
|
nsNodeInfoManager *aNewNodeInfoManager,
|
2013-05-08 06:34:56 +04:00
|
|
|
JS::Handle<JSObject*> aReparentScope,
|
2006-09-05 14:22:54 +04:00
|
|
|
nsCOMArray<nsINode> &aNodesWithProperties,
|
2009-09-29 00:33:29 +04:00
|
|
|
nsINode *aParent, nsINode **aResult)
|
2006-09-05 14:22:54 +04:00
|
|
|
{
|
2013-04-08 21:24:21 +04:00
|
|
|
NS_PRECONDITION((!aClone && aNewNodeInfoManager) || !aReparentScope,
|
2006-09-05 14:22:54 +04:00
|
|
|
"If cloning or not getting a new nodeinfo we shouldn't "
|
|
|
|
"rewrap");
|
2009-09-29 00:33:29 +04:00
|
|
|
NS_PRECONDITION(!aParent || aNode->IsNodeOfType(nsINode::eCONTENT),
|
|
|
|
"Can't insert document or attribute nodes into a parent");
|
2006-09-05 14:22:54 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2006-09-05 14:22:54 +04:00
|
|
|
|
|
|
|
// First deal with aNode and walk its attributes (and their children). Then,
|
2011-10-17 18:59:28 +04:00
|
|
|
// if aDeep is true, deal with aNode's children (and recurse into their
|
2006-09-05 14:22:54 +04:00
|
|
|
// attributes and children).
|
|
|
|
|
2013-04-08 21:24:21 +04:00
|
|
|
AutoJSContext cx;
|
2006-09-05 14:22:54 +04:00
|
|
|
nsresult rv;
|
2010-11-16 04:21:25 +03:00
|
|
|
|
2006-09-05 14:22:54 +04:00
|
|
|
nsNodeInfoManager *nodeInfoManager = aNewNodeInfoManager;
|
|
|
|
|
|
|
|
// aNode.
|
2014-06-20 06:01:40 +04:00
|
|
|
NodeInfo *nodeInfo = aNode->mNodeInfo;
|
|
|
|
nsRefPtr<NodeInfo> newNodeInfo;
|
2006-09-05 14:22:54 +04:00
|
|
|
if (nodeInfoManager) {
|
2008-10-13 20:12:26 +04:00
|
|
|
|
|
|
|
// Don't allow importing/adopting nodes from non-privileged "scriptable"
|
|
|
|
// documents to "non-scriptable" documents.
|
|
|
|
nsIDocument* newDoc = nodeInfoManager->GetDocument();
|
2008-12-03 13:25:21 +03:00
|
|
|
NS_ENSURE_STATE(newDoc);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasHadScriptHandlingObject = false;
|
2008-10-13 20:12:26 +04:00
|
|
|
if (!newDoc->GetScriptHandlingObject(hasHadScriptHandlingObject) &&
|
|
|
|
!hasHadScriptHandlingObject) {
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* currentDoc = aNode->OwnerDoc();
|
2011-10-18 15:19:44 +04:00
|
|
|
NS_ENSURE_STATE((nsContentUtils::IsChromeDoc(currentDoc) ||
|
2008-12-03 13:25:21 +03:00
|
|
|
(!currentDoc->GetScriptHandlingObject(hasHadScriptHandlingObject) &&
|
|
|
|
!hasHadScriptHandlingObject)));
|
2008-10-13 20:12:26 +04:00
|
|
|
}
|
|
|
|
|
2008-09-13 02:32:18 +04:00
|
|
|
newNodeInfo = nodeInfoManager->GetNodeInfo(nodeInfo->NameAtom(),
|
|
|
|
nodeInfo->GetPrefixAtom(),
|
2011-06-14 11:56:49 +04:00
|
|
|
nodeInfo->NamespaceID(),
|
|
|
|
nodeInfo->NodeType(),
|
|
|
|
nodeInfo->GetExtraName());
|
2006-09-05 14:22:54 +04:00
|
|
|
|
|
|
|
nodeInfo = newNodeInfo;
|
|
|
|
}
|
|
|
|
|
2012-11-15 02:10:08 +04:00
|
|
|
Element *elem = aNode->IsElement() ? aNode->AsElement() : nullptr;
|
2006-10-07 14:27:45 +04:00
|
|
|
|
2006-09-05 14:22:54 +04:00
|
|
|
nsCOMPtr<nsINode> clone;
|
|
|
|
if (aClone) {
|
|
|
|
rv = aNode->Clone(nodeInfo, getter_AddRefs(clone));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-12-23 05:19:08 +03:00
|
|
|
if (clone->IsElement()) {
|
|
|
|
// The cloned node may be a custom element that may require
|
|
|
|
// enqueing created callback and prototype swizzling.
|
|
|
|
Element* elem = clone->AsElement();
|
|
|
|
if (nsContentUtils::IsCustomElementName(nodeInfo->NameAtom())) {
|
|
|
|
elem->OwnerDoc()->SetupCustomElement(elem, nodeInfo->NamespaceID());
|
|
|
|
} else {
|
|
|
|
// Check if node may be custom element by type extension.
|
|
|
|
// ex. <button is="x-button">
|
|
|
|
nsAutoString extension;
|
|
|
|
if (elem->GetAttr(kNameSpaceID_None, nsGkAtoms::is, extension) &&
|
|
|
|
!extension.IsEmpty()) {
|
|
|
|
elem->OwnerDoc()->SetupCustomElement(elem, nodeInfo->NamespaceID(),
|
|
|
|
&extension);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-09-05 14:22:54 +04:00
|
|
|
if (aParent) {
|
|
|
|
// If we're cloning we need to insert the cloned children into the cloned
|
|
|
|
// parent.
|
2009-09-29 00:33:29 +04:00
|
|
|
rv = aParent->AppendChildTo(static_cast<nsIContent*>(clone.get()),
|
2011-10-17 18:59:28 +04:00
|
|
|
false);
|
2006-09-05 14:22:54 +04: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 14:53:36 +04:00
|
|
|
nsIDocument* oldDoc = aNode->OwnerDoc();
|
2011-09-29 10:19:26 +04:00
|
|
|
bool wasRegistered = false;
|
2011-10-18 15:19:44 +04:00
|
|
|
if (aNode->IsElement()) {
|
2010-04-30 17:12:05 +04:00
|
|
|
Element* element = aNode->AsElement();
|
|
|
|
oldDoc->ClearBoxObjectFor(element);
|
2014-06-19 06:09:35 +04:00
|
|
|
wasRegistered = oldDoc->UnregisterActivityObserver(element);
|
2007-03-20 20:39:25 +03:00
|
|
|
}
|
|
|
|
|
2006-09-05 14:22:54 +04:00
|
|
|
aNode->mNodeInfo.swap(newNodeInfo);
|
2010-09-02 02:48:24 +04:00
|
|
|
if (elem) {
|
|
|
|
elem->NodeInfoChanged(newNodeInfo);
|
|
|
|
}
|
2006-09-05 14:22:54 +04:00
|
|
|
|
2011-10-18 14:53:36 +04:00
|
|
|
nsIDocument* newDoc = aNode->OwnerDoc();
|
2007-03-20 20:39:25 +03:00
|
|
|
if (newDoc) {
|
2009-10-01 18:10:13 +04:00
|
|
|
// XXX what if oldDoc is null, we don't know if this should be
|
|
|
|
// registered or not! Can that really happen?
|
2009-05-08 05:32:32 +04:00
|
|
|
if (wasRegistered) {
|
2014-06-19 06:09:35 +04:00
|
|
|
newDoc->RegisterActivityObserver(aNode->AsElement());
|
2009-05-08 05:32:32 +04:00
|
|
|
}
|
|
|
|
|
2007-03-20 20:39:25 +03:00
|
|
|
nsPIDOMWindow* window = newDoc->GetInnerWindow();
|
|
|
|
if (window) {
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* elm = aNode->GetExistingListenerManager();
|
2007-03-20 20:39:25 +03:00
|
|
|
if (elm) {
|
|
|
|
window->SetMutationListeners(elm->MutationListenerBits());
|
2008-10-16 01:06:32 +04:00
|
|
|
if (elm->MayHavePaintEventListener()) {
|
|
|
|
window->SetHasPaintEventListeners();
|
|
|
|
}
|
2011-04-26 16:31:21 +04:00
|
|
|
if (elm->MayHaveTouchEventListener()) {
|
|
|
|
window->SetHasTouchEventListeners();
|
|
|
|
}
|
2014-12-09 13:38:23 +03:00
|
|
|
if (elm->MayHaveScrollWheelEventListener()) {
|
|
|
|
window->SetHasScrollWheelEventListeners();
|
|
|
|
}
|
2011-09-18 11:45:14 +04:00
|
|
|
if (elm->MayHaveMouseEnterLeaveEventListener()) {
|
|
|
|
window->SetHasMouseEnterLeaveEventListeners();
|
|
|
|
}
|
2014-02-11 10:33:29 +04:00
|
|
|
if (elm->MayHavePointerEnterLeaveEventListener()) {
|
|
|
|
window->SetHasPointerEnterLeaveEventListeners();
|
|
|
|
}
|
2007-03-20 20:39:25 +03:00
|
|
|
}
|
2007-02-01 01:54:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-30 10:09:56 +04:00
|
|
|
if (wasRegistered && oldDoc != newDoc) {
|
2009-10-01 18:10:13 +04:00
|
|
|
nsCOMPtr<nsIDOMHTMLMediaElement> domMediaElem(do_QueryInterface(aNode));
|
|
|
|
if (domMediaElem) {
|
2013-03-19 16:23:54 +04:00
|
|
|
HTMLMediaElement* mediaElem = static_cast<HTMLMediaElement*>(aNode);
|
2009-10-01 18:10:13 +04:00
|
|
|
mediaElem->NotifyOwnerDocumentActivityChanged();
|
|
|
|
}
|
2012-02-01 01:55:54 +04:00
|
|
|
nsCOMPtr<nsIObjectLoadingContent> objectLoadingContent(do_QueryInterface(aNode));
|
|
|
|
if (objectLoadingContent) {
|
|
|
|
nsObjectLoadingContent* olc = static_cast<nsObjectLoadingContent*>(objectLoadingContent.get());
|
|
|
|
olc->NotifyOwnerDocumentActivityChanged();
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 13:44:19 +04:00
|
|
|
|
2012-08-14 02:11:50 +04:00
|
|
|
if (oldDoc != newDoc && oldDoc->MayHaveDOMMutationObservers()) {
|
|
|
|
newDoc->SetMayHaveDOMMutationObservers();
|
2010-08-27 05:30:03 +04:00
|
|
|
}
|
|
|
|
|
2006-10-07 14:27:45 +04:00
|
|
|
if (elem) {
|
|
|
|
elem->RecompileScriptEventListeners();
|
|
|
|
}
|
|
|
|
|
2013-04-19 23:57:15 +04:00
|
|
|
if (aReparentScope) {
|
|
|
|
JS::Rooted<JSObject*> wrapper(cx);
|
|
|
|
if ((wrapper = aNode->GetWrapper())) {
|
|
|
|
if (IsDOMObject(wrapper)) {
|
2014-04-09 02:27:15 +04:00
|
|
|
JSAutoCompartment ac(cx, wrapper);
|
2013-04-19 23:57:15 +04:00
|
|
|
rv = ReparentWrapper(cx, wrapper);
|
|
|
|
} else {
|
|
|
|
nsIXPConnect *xpc = nsContentUtils::XPConnect();
|
|
|
|
if (xpc) {
|
|
|
|
rv = xpc->ReparentWrappedNativeIfFound(cx, wrapper, aReparentScope, aNode);
|
2013-06-20 01:03:31 +04:00
|
|
|
} else {
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2013-04-19 23:57:15 +04:00
|
|
|
}
|
2006-09-05 14:22:54 +04:00
|
|
|
}
|
2013-04-19 23:57:15 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aNode->mNodeInfo.swap(nodeInfo);
|
2012-09-26 18:17:46 +04:00
|
|
|
|
2013-04-19 23:57:15 +04:00
|
|
|
return rv;
|
|
|
|
}
|
2012-09-26 18:17:46 +04:00
|
|
|
}
|
2006-09-05 14:22:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 11:25:39 +04:00
|
|
|
if (aDeep && (!aClone || !aNode->IsNodeOfType(nsINode::eATTRIBUTE))) {
|
2006-09-05 14:22:54 +04:00
|
|
|
// aNode's children.
|
2011-09-27 11:54:58 +04:00
|
|
|
for (nsIContent* cloneChild = aNode->GetFirstChild();
|
|
|
|
cloneChild;
|
2012-04-03 11:25:39 +04:00
|
|
|
cloneChild = cloneChild->GetNextSibling()) {
|
2009-09-29 00:33:29 +04:00
|
|
|
nsCOMPtr<nsINode> child;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = CloneAndAdopt(cloneChild, aClone, true, nodeInfoManager,
|
2013-04-08 21:24:21 +04:00
|
|
|
aReparentScope, aNodesWithProperties, clone,
|
2010-12-03 19:43:32 +03:00
|
|
|
getter_AddRefs(child));
|
2006-09-05 14:22:54 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-26 11:15:23 +04: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 21:24:21 +04:00
|
|
|
aReparentScope, aNodesWithProperties, cloneContent,
|
2013-03-26 11:15:23 +04:00
|
|
|
getter_AddRefs(child));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-31 16:25:40 +03: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.
|
2006-11-07 06:13:49 +03:00
|
|
|
#ifdef MOZ_XUL
|
2010-04-30 17:12:05 +04:00
|
|
|
if (aClone && !aParent && aNode->IsElement() &&
|
|
|
|
aNode->AsElement()->IsXUL()) {
|
2012-07-19 12:31:47 +04:00
|
|
|
if (!aNode->OwnerDoc()->IsLoadedAsInteractiveData()) {
|
2007-05-16 05:13:47 +04:00
|
|
|
clone->SetFlags(NODE_FORCE_XBL_BINDINGS);
|
2006-10-31 16:25:40 +03:00
|
|
|
}
|
|
|
|
}
|
2006-11-07 06:13:49 +03:00
|
|
|
#endif
|
2006-10-31 16:25:40 +03:00
|
|
|
|
2006-09-05 14:22:54 +04:00
|
|
|
if (aNode->HasProperties()) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool ok = aNodesWithProperties.AppendObject(aNode);
|
2006-09-05 14:22:54 +04:00
|
|
|
if (aClone) {
|
|
|
|
ok = ok && aNodesWithProperties.AppendObject(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
|
2009-09-29 00:33:29 +04:00
|
|
|
clone.forget(aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
2006-09-05 14:22:54 +04:00
|
|
|
}
|
2007-05-12 19:36:28 +04: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 14:53:36 +04:00
|
|
|
nsCOMPtr<nsIDocument> document = aNode->OwnerDoc();
|
2011-10-18 15:19:44 +04:00
|
|
|
document->PropertyTable(DOM_USER_DATA)->DeleteAllPropertiesFor(aNode);
|
2007-05-12 19:36:28 +04:00
|
|
|
}
|
2013-03-26 11:15:23 +04: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();
|
|
|
|
}
|
|
|
|
|