2015-05-03 19:32:37 +00:00
|
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 11:12:37 +00: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 07:23:10 +00:00
|
|
|
|
|
|
|
|
|
#include "nsNodeUtils.h"
|
2006-08-16 08:44:45 +00:00
|
|
|
|
#include "nsContentUtils.h"
|
2016-02-17 20:37:00 +00:00
|
|
|
|
#include "nsCSSPseudoElements.h"
|
2006-07-02 07:23:10 +00:00
|
|
|
|
#include "nsINode.h"
|
|
|
|
|
#include "nsIContent.h"
|
2017-12-31 19:57:32 +00:00
|
|
|
|
#include "nsIContentInlines.h"
|
2010-05-05 18:18:05 +00:00
|
|
|
|
#include "mozilla/dom/Element.h"
|
2006-07-02 07:23:10 +00:00
|
|
|
|
#include "nsIMutationObserver.h"
|
|
|
|
|
#include "nsIDocument.h"
|
2014-03-17 06:56:53 +00:00
|
|
|
|
#include "mozilla/EventListenerManager.h"
|
2006-09-05 10:22:54 +00:00
|
|
|
|
#include "nsIXPConnect.h"
|
2015-09-16 03:49:53 +00:00
|
|
|
|
#include "PLDHashTable.h"
|
2006-09-05 10:22:54 +00:00
|
|
|
|
#include "nsCOMArray.h"
|
2007-01-31 22:54:24 +00:00
|
|
|
|
#include "nsPIDOMWindow.h"
|
2009-09-10 13:23:40 +00:00
|
|
|
|
#include "nsDocument.h"
|
2006-11-07 03:13:49 +00:00
|
|
|
|
#ifdef MOZ_XUL
|
2006-10-31 13:25:40 +00:00
|
|
|
|
#include "nsXULElement.h"
|
2006-11-07 03:13:49 +00:00
|
|
|
|
#endif
|
2008-09-05 15:45:03 +00:00
|
|
|
|
#include "nsBindingManager.h"
|
2009-09-30 22:56:50 +00:00
|
|
|
|
#include "nsGenericHTMLElement.h"
|
2016-04-28 15:22:43 +00:00
|
|
|
|
#include "mozilla/AnimationTarget.h"
|
2015-02-13 19:34:53 +00:00
|
|
|
|
#include "mozilla/Assertions.h"
|
2017-09-13 17:34:55 +00:00
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2015-04-21 01:22:09 +00:00
|
|
|
|
#include "mozilla/dom/Animation.h"
|
2013-06-18 12:53:23 +00:00
|
|
|
|
#include "mozilla/dom/HTMLImageElement.h"
|
2013-03-19 12:23:54 +00:00
|
|
|
|
#include "mozilla/dom/HTMLMediaElement.h"
|
2018-05-07 02:07:06 +00:00
|
|
|
|
#include "mozilla/dom/KeyframeEffect.h"
|
2011-05-26 19:58:35 +00:00
|
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-01-31 21:55:54 +00:00
|
|
|
|
#include "nsObjectLoadingContent.h"
|
2012-03-31 16:30:13 +00:00
|
|
|
|
#include "nsDOMMutationObserver.h"
|
2012-09-26 14:17:46 +00:00
|
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2013-03-26 07:15:23 +00:00
|
|
|
|
#include "mozilla/dom/HTMLTemplateElement.h"
|
2013-12-02 10:26:11 +00:00
|
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2006-07-02 07:23:10 +00:00
|
|
|
|
|
2014-03-17 06:56:53 +00:00
|
|
|
|
using namespace mozilla;
|
2010-04-30 13:12:05 +00:00
|
|
|
|
using namespace mozilla::dom;
|
2013-04-08 17:24:21 +00:00
|
|
|
|
using mozilla::AutoJSContext;
|
2010-04-30 13:12:05 +00:00
|
|
|
|
|
2017-12-11 05:42:38 +00:00
|
|
|
|
enum class IsRemoveNotification
|
|
|
|
|
{
|
|
|
|
|
Yes,
|
|
|
|
|
No,
|
|
|
|
|
};
|
|
|
|
|
|
2017-12-11 09:14:36 +00:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
#define COMPOSED_DOC_DECL \
|
|
|
|
|
const bool wasInComposedDoc = !!node->GetComposedDoc();
|
|
|
|
|
#else
|
|
|
|
|
#define COMPOSED_DOC_DECL
|
|
|
|
|
#endif
|
|
|
|
|
|
2007-12-04 18:37:54 +00:00
|
|
|
|
// This macro expects the ownerDocument of content_ to be in scope as
|
|
|
|
|
// |nsIDocument* doc|
|
2017-12-11 05:42:38 +00:00
|
|
|
|
#define IMPL_MUTATION_NOTIFICATION(func_, content_, params_, remove_) \
|
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
|
bool needsEnterLeave = doc->MayHaveDOMMutationObservers(); \
|
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
|
nsDOMMutationObserver::EnterMutationHandling(); \
|
|
|
|
|
} \
|
|
|
|
|
nsINode* node = content_; \
|
2017-12-11 09:14:36 +00:00
|
|
|
|
COMPOSED_DOC_DECL \
|
2017-12-11 05:42:38 +00:00
|
|
|
|
NS_ASSERTION(node->OwnerDoc() == doc, "Bogus document"); \
|
|
|
|
|
if (remove_ == IsRemoveNotification::Yes && node->GetComposedDoc()) { \
|
|
|
|
|
if (nsIPresShell* shell = doc->GetObservingShell()) { \
|
|
|
|
|
shell->func_ params_; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
doc->BindingManager()->func_ params_; \
|
|
|
|
|
nsINode* last; \
|
|
|
|
|
do { \
|
|
|
|
|
nsINode::nsSlots* slots = node->GetExistingSlots(); \
|
|
|
|
|
if (slots && !slots->mMutationObservers.IsEmpty()) { \
|
|
|
|
|
NS_OBSERVER_AUTO_ARRAY_NOTIFY_OBSERVERS( \
|
|
|
|
|
slots->mMutationObservers, nsIMutationObserver, 1, \
|
|
|
|
|
func_, params_); \
|
|
|
|
|
} \
|
|
|
|
|
last = node; \
|
|
|
|
|
if (ShadowRoot* shadow = ShadowRoot::FromNode(node)) { \
|
|
|
|
|
node = shadow->GetHost(); \
|
|
|
|
|
} else { \
|
|
|
|
|
node = node->GetParentNode(); \
|
|
|
|
|
} \
|
|
|
|
|
} while (node); \
|
2017-12-11 09:14:36 +00:00
|
|
|
|
/* Whitelist NativeAnonymousChildListChange removal notifications from \
|
|
|
|
|
* the assertion since it runs from UnbindFromTree, and thus we don't \
|
|
|
|
|
* reach the document, but doesn't matter. */ \
|
|
|
|
|
MOZ_ASSERT((last == doc) == wasInComposedDoc || \
|
|
|
|
|
(remove_ == IsRemoveNotification::Yes && \
|
|
|
|
|
!strcmp(#func_, "NativeAnonymousChildListChange"))); \
|
2017-12-11 05:42:38 +00:00
|
|
|
|
if (remove_ == IsRemoveNotification::No && last == doc) { \
|
|
|
|
|
if (nsIPresShell* shell = doc->GetObservingShell()) { \
|
|
|
|
|
shell->func_ params_; \
|
|
|
|
|
} \
|
|
|
|
|
} \
|
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling(); \
|
|
|
|
|
} \
|
2006-08-15 22:41:58 +00:00
|
|
|
|
PR_END_MACRO
|
2006-07-02 07:23:10 +00:00
|
|
|
|
|
2015-03-14 05:34:40 +00:00
|
|
|
|
#define IMPL_ANIMATION_NOTIFICATION(func_, content_, params_) \
|
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
|
bool needsEnterLeave = doc->MayHaveDOMMutationObservers(); \
|
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
|
nsDOMMutationObserver::EnterMutationHandling(); \
|
|
|
|
|
} \
|
|
|
|
|
nsINode* node = content_; \
|
|
|
|
|
do { \
|
|
|
|
|
nsINode::nsSlots* slots = node->GetExistingSlots(); \
|
|
|
|
|
if (slots && !slots->mMutationObservers.IsEmpty()) { \
|
2017-07-29 05:07:05 +00:00
|
|
|
|
NS_OBSERVER_AUTO_ARRAY_NOTIFY_OBSERVERS_WITH_QI( \
|
|
|
|
|
slots->mMutationObservers, nsIMutationObserver, 1, \
|
2015-03-14 05:34:40 +00:00
|
|
|
|
nsIAnimationObserver, func_, params_); \
|
|
|
|
|
} \
|
2017-12-31 19:57:32 +00:00
|
|
|
|
if (ShadowRoot* shadow = ShadowRoot::FromNode(node)) { \
|
2017-09-25 15:09:26 +00:00
|
|
|
|
node = shadow->GetHost(); \
|
2015-03-14 05:34:40 +00:00
|
|
|
|
} else { \
|
|
|
|
|
node = node->GetParentNode(); \
|
|
|
|
|
} \
|
|
|
|
|
} while (node); \
|
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling(); \
|
|
|
|
|
} \
|
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
2007-09-05 08:22:17 +00:00
|
|
|
|
void
|
|
|
|
|
nsNodeUtils::CharacterDataWillChange(nsIContent* aContent,
|
2018-02-27 14:30:27 +00:00
|
|
|
|
const CharacterDataChangeInfo& aInfo)
|
2007-09-05 08:22:17 +00:00
|
|
|
|
{
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2010-07-21 15:33:32 +00:00
|
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataWillChange, aContent,
|
2018-03-01 11:36:58 +00:00
|
|
|
|
(aContent, aInfo), IsRemoveNotification::No);
|
2007-09-05 08:22:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-07-02 07:23:10 +00:00
|
|
|
|
void
|
2006-11-03 21:51:01 +00:00
|
|
|
|
nsNodeUtils::CharacterDataChanged(nsIContent* aContent,
|
2018-02-27 14:30:27 +00:00
|
|
|
|
const CharacterDataChangeInfo& aInfo)
|
2006-07-02 07:23:10 +00:00
|
|
|
|
{
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2010-07-21 15:33:32 +00:00
|
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataChanged, aContent,
|
2018-03-01 11:36:58 +00:00
|
|
|
|
(aContent, aInfo), IsRemoveNotification::No);
|
2006-07-02 07:23:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-06-29 18:36:25 +00:00
|
|
|
|
void
|
2010-08-24 07:06:20 +00:00
|
|
|
|
nsNodeUtils::AttributeWillChange(Element* aElement,
|
2012-08-22 15:56:38 +00:00
|
|
|
|
int32_t aNameSpaceID,
|
2017-10-02 22:05:19 +00:00
|
|
|
|
nsAtom* aAttribute,
|
2015-07-22 03:53:35 +00:00
|
|
|
|
int32_t aModType,
|
|
|
|
|
const nsAttrValue* aNewValue)
|
2009-06-29 18:36:25 +00:00
|
|
|
|
{
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
2010-08-24 07:06:20 +00:00
|
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeWillChange, aElement,
|
2018-03-01 11:36:58 +00:00
|
|
|
|
(aElement, aNameSpaceID, aAttribute,
|
2017-12-11 05:42:38 +00:00
|
|
|
|
aModType, aNewValue), IsRemoveNotification::No);
|
2009-06-29 18:36:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-07-02 07:23:10 +00:00
|
|
|
|
void
|
2010-08-24 07:06:07 +00:00
|
|
|
|
nsNodeUtils::AttributeChanged(Element* aElement,
|
2012-08-22 15:56:38 +00:00
|
|
|
|
int32_t aNameSpaceID,
|
2017-10-02 22:05:19 +00:00
|
|
|
|
nsAtom* aAttribute,
|
2015-07-21 04:13:53 +00:00
|
|
|
|
int32_t aModType,
|
|
|
|
|
const nsAttrValue* aOldValue)
|
2006-07-02 07:23:10 +00:00
|
|
|
|
{
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
2010-08-24 07:05:56 +00:00
|
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeChanged, aElement,
|
2018-03-01 11:36:58 +00:00
|
|
|
|
(aElement, aNameSpaceID, aAttribute,
|
2017-12-11 05:42:38 +00:00
|
|
|
|
aModType, aOldValue), IsRemoveNotification::No);
|
2006-07-02 07:23:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-08-15 10:06:01 +00:00
|
|
|
|
void
|
|
|
|
|
nsNodeUtils::AttributeSetToCurrentValue(Element* aElement,
|
2012-08-22 15:56:38 +00:00
|
|
|
|
int32_t aNameSpaceID,
|
2017-10-02 22:05:19 +00:00
|
|
|
|
nsAtom* aAttribute)
|
2012-08-15 10:06:01 +00:00
|
|
|
|
{
|
|
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
|
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeSetToCurrentValue, aElement,
|
2018-03-01 11:36:58 +00:00
|
|
|
|
(aElement, aNameSpaceID, aAttribute),
|
2017-12-11 05:42:38 +00:00
|
|
|
|
IsRemoveNotification::No);
|
2012-08-15 10:06:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-07-02 07:23:10 +00:00
|
|
|
|
void
|
|
|
|
|
nsNodeUtils::ContentAppended(nsIContent* aContainer,
|
2017-07-27 13:49:52 +00:00
|
|
|
|
nsIContent* aFirstNewContent)
|
2006-07-02 07:23:10 +00:00
|
|
|
|
{
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2006-07-02 07:23:10 +00:00
|
|
|
|
|
2010-07-21 15:33:32 +00:00
|
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentAppended, aContainer,
|
2018-03-01 11:36:58 +00:00
|
|
|
|
(aFirstNewContent),
|
2017-12-11 05:42:38 +00:00
|
|
|
|
IsRemoveNotification::No);
|
2006-07-02 07:23:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-09-24 15:23:32 +00:00
|
|
|
|
void
|
|
|
|
|
nsNodeUtils::NativeAnonymousChildListChange(nsIContent* aContent,
|
|
|
|
|
bool aIsRemove)
|
|
|
|
|
{
|
|
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2017-12-11 09:14:36 +00:00
|
|
|
|
auto isRemove = aIsRemove
|
|
|
|
|
? IsRemoveNotification::Yes : IsRemoveNotification::No;
|
2015-09-24 15:23:32 +00:00
|
|
|
|
IMPL_MUTATION_NOTIFICATION(NativeAnonymousChildListChange, aContent,
|
2018-03-01 11:36:58 +00:00
|
|
|
|
(aContent, aIsRemove),
|
2017-12-11 09:14:36 +00:00
|
|
|
|
isRemove);
|
2015-09-24 15:23:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-07-02 07:23:10 +00:00
|
|
|
|
void
|
|
|
|
|
nsNodeUtils::ContentInserted(nsINode* aContainer,
|
2017-07-27 13:49:52 +00:00
|
|
|
|
nsIContent* aChild)
|
2006-07-02 07:23:10 +00:00
|
|
|
|
{
|
2018-04-28 19:50:58 +00:00
|
|
|
|
MOZ_ASSERT(aContainer->IsContent() || aContainer->IsDocument(),
|
|
|
|
|
"container must be an nsIContent or an nsIDocument");
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2018-03-01 11:36:58 +00:00
|
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentInserted, aContainer, (aChild),
|
2017-12-11 05:42:38 +00:00
|
|
|
|
IsRemoveNotification::No);
|
2006-07-02 07:23:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
nsNodeUtils::ContentRemoved(nsINode* aContainer,
|
|
|
|
|
nsIContent* aChild,
|
2010-07-21 22:05:17 +00:00
|
|
|
|
nsIContent* aPreviousSibling)
|
2006-07-02 07:23:10 +00:00
|
|
|
|
{
|
2018-04-28 19:50:58 +00:00
|
|
|
|
MOZ_ASSERT(aContainer->IsContent() || aContainer->IsDocument(),
|
|
|
|
|
"container must be an nsIContent or an nsIDocument");
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2018-03-01 11:36:58 +00:00
|
|
|
|
MOZ_ASSERT(aChild->GetParentNode() == aContainer,
|
|
|
|
|
"We expect the parent link to be still around at this point");
|
2010-07-21 15:37:41 +00:00
|
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentRemoved, aContainer,
|
2018-03-01 11:36:58 +00:00
|
|
|
|
(aChild, aPreviousSibling),
|
2017-12-11 05:42:38 +00:00
|
|
|
|
IsRemoveNotification::Yes);
|
2006-07-02 07:23:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-21 08:49:50 +00:00
|
|
|
|
Maybe<NonOwningAnimationTarget>
|
2015-10-22 06:16:18 +00:00
|
|
|
|
nsNodeUtils::GetTargetForAnimation(const Animation* aAnimation)
|
2015-03-14 05:34:40 +00:00
|
|
|
|
{
|
2018-05-07 02:15:16 +00:00
|
|
|
|
AnimationEffect* effect = aAnimation->GetEffect();
|
2016-07-25 08:56:34 +00:00
|
|
|
|
if (!effect || !effect->AsKeyframeEffect()) {
|
|
|
|
|
return Nothing();
|
|
|
|
|
}
|
|
|
|
|
return effect->AsKeyframeEffect()->GetTarget();
|
2015-03-14 05:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-03-21 08:49:50 +00:00
|
|
|
|
nsNodeUtils::AnimationMutated(Animation* aAnimation,
|
|
|
|
|
AnimationMutationType aMutatedType)
|
2015-03-14 05:34:40 +00:00
|
|
|
|
{
|
2016-03-21 08:49:50 +00:00
|
|
|
|
Maybe<NonOwningAnimationTarget> target = GetTargetForAnimation(aAnimation);
|
2015-03-14 05:34:40 +00:00
|
|
|
|
if (!target) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-21 08:49:50 +00:00
|
|
|
|
// A pseudo element and its parent element use the same owner doc.
|
|
|
|
|
nsIDocument* doc = target->mElement->OwnerDoc();
|
2015-03-14 05:34:40 +00:00
|
|
|
|
if (doc->MayHaveAnimationObservers()) {
|
2016-03-21 08:49:50 +00:00
|
|
|
|
// we use the its parent element as the subject in DOM Mutation Observer.
|
2016-03-21 08:49:50 +00:00
|
|
|
|
Element* elem = target->mElement;
|
|
|
|
|
switch (aMutatedType) {
|
|
|
|
|
case AnimationMutationType::Added:
|
|
|
|
|
IMPL_ANIMATION_NOTIFICATION(AnimationAdded, elem, (aAnimation));
|
|
|
|
|
break;
|
|
|
|
|
case AnimationMutationType::Changed:
|
|
|
|
|
IMPL_ANIMATION_NOTIFICATION(AnimationChanged, elem, (aAnimation));
|
|
|
|
|
break;
|
|
|
|
|
case AnimationMutationType::Removed:
|
|
|
|
|
IMPL_ANIMATION_NOTIFICATION(AnimationRemoved, elem, (aAnimation));
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
MOZ_ASSERT_UNREACHABLE("unexpected mutation type");
|
|
|
|
|
}
|
2015-03-14 05:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2016-03-21 08:49:50 +00:00
|
|
|
|
nsNodeUtils::AnimationAdded(Animation* aAnimation)
|
2015-03-14 05:34:40 +00:00
|
|
|
|
{
|
2016-03-21 08:49:50 +00:00
|
|
|
|
AnimationMutated(aAnimation, AnimationMutationType::Added);
|
|
|
|
|
}
|
2015-03-14 05:34:40 +00:00
|
|
|
|
|
2016-03-21 08:49:50 +00:00
|
|
|
|
void
|
|
|
|
|
nsNodeUtils::AnimationChanged(Animation* aAnimation)
|
|
|
|
|
{
|
|
|
|
|
AnimationMutated(aAnimation, AnimationMutationType::Changed);
|
2015-03-14 05:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2015-04-21 01:22:10 +00:00
|
|
|
|
nsNodeUtils::AnimationRemoved(Animation* aAnimation)
|
2015-03-14 05:34:40 +00:00
|
|
|
|
{
|
2016-03-21 08:49:50 +00:00
|
|
|
|
AnimationMutated(aAnimation, AnimationMutationType::Removed);
|
2015-03-14 05:34:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-07-02 07:23:10 +00:00
|
|
|
|
void
|
2007-05-12 15:36:28 +00:00
|
|
|
|
nsNodeUtils::LastRelease(nsINode* aNode)
|
2006-07-02 07:23:10 +00:00
|
|
|
|
{
|
|
|
|
|
nsINode::nsSlots* slots = aNode->GetExistingSlots();
|
2006-08-25 10:00:11 +00:00
|
|
|
|
if (slots) {
|
|
|
|
|
if (!slots->mMutationObservers.IsEmpty()) {
|
2017-07-29 05:07:05 +00:00
|
|
|
|
NS_OBSERVER_AUTO_ARRAY_NOTIFY_OBSERVERS(slots->mMutationObservers,
|
|
|
|
|
nsIMutationObserver, 1,
|
|
|
|
|
NodeWillBeDestroyed, (aNode));
|
2006-08-25 10:00:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-02-02 23:41:24 +00:00
|
|
|
|
delete slots;
|
2012-07-30 14:20:58 +00:00
|
|
|
|
aNode->mSlots = nullptr;
|
2006-07-02 07:23:10 +00:00
|
|
|
|
}
|
2006-08-16 08:44:45 +00:00
|
|
|
|
|
2006-09-02 13:21:05 +00:00
|
|
|
|
// Kill properties first since that may run external code, so we want to
|
|
|
|
|
// be in as complete state as possible at that time.
|
2018-04-15 09:43:15 +00:00
|
|
|
|
if (aNode->IsDocument()) {
|
2007-05-12 15:36:28 +00:00
|
|
|
|
// 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.
|
2018-04-15 09:43:15 +00:00
|
|
|
|
aNode->AsDocument()->DeleteAllProperties();
|
2007-05-12 15:36:28 +00:00
|
|
|
|
}
|
2009-09-30 22:56:50 +00:00
|
|
|
|
else {
|
|
|
|
|
if (aNode->HasProperties()) {
|
|
|
|
|
// Strong reference to the document so that deleting properties can't
|
|
|
|
|
// delete the document.
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsCOMPtr<nsIDocument> document = aNode->OwnerDoc();
|
2011-10-18 11:19:44 +00:00
|
|
|
|
document->DeleteAllPropertiesFor(aNode);
|
2009-09-30 22:56:50 +00: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.
|
2017-05-01 21:10:00 +00:00
|
|
|
|
static_cast<nsGenericHTMLFormElement*>(aNode)->ClearForm(true, true);
|
2006-08-16 08:44:45 +00:00
|
|
|
|
}
|
2013-06-18 12:53:23 +00:00
|
|
|
|
|
2015-03-03 11:08:59 +00:00
|
|
|
|
if (aNode->IsHTMLElement(nsGkAtoms::img) &&
|
2013-07-03 13:21:30 +00:00
|
|
|
|
aNode->HasFlag(ADDED_TO_FORM)) {
|
2013-06-18 12:53:23 +00:00
|
|
|
|
HTMLImageElement* imageElem = static_cast<HTMLImageElement*>(aNode);
|
|
|
|
|
imageElem->ClearForm(true);
|
|
|
|
|
}
|
2006-09-02 13:21:05 +00:00
|
|
|
|
}
|
2007-05-12 15:36:28 +00:00
|
|
|
|
aNode->UnsetFlags(NODE_HAS_PROPERTIES);
|
2006-09-02 13:21:05 +00:00
|
|
|
|
|
2018-01-30 04:10:53 +00:00
|
|
|
|
if (aNode->NodeType() != nsINode::DOCUMENT_NODE &&
|
2012-02-27 14:03:15 +00:00
|
|
|
|
aNode->HasFlag(NODE_HAS_LISTENERMANAGER)) {
|
2006-09-02 13:21:05 +00:00
|
|
|
|
#ifdef DEBUG
|
|
|
|
|
if (nsContentUtils::IsInitialized()) {
|
2014-03-17 06:56:53 +00:00
|
|
|
|
EventListenerManager* manager =
|
2013-10-22 23:32:04 +00:00
|
|
|
|
nsContentUtils::GetExistingListenerManagerForNode(aNode);
|
2006-09-02 13:21:05 +00: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 13:12:05 +00:00
|
|
|
|
if (aNode->IsElement()) {
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* ownerDoc = aNode->OwnerDoc();
|
2010-05-26 22:39:13 +00:00
|
|
|
|
Element* elem = aNode->AsElement();
|
2011-10-18 11:19:44 +00:00
|
|
|
|
ownerDoc->ClearBoxObjectFor(elem);
|
2013-12-06 18:17:19 +00:00
|
|
|
|
|
2018-03-12 20:24:10 +00:00
|
|
|
|
NS_ASSERTION(!elem->GetXBLBinding(),
|
|
|
|
|
"Node has binding on destruction");
|
2008-02-14 20:45:07 +00:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-02 07:04:01 +00:00
|
|
|
|
aNode->ReleaseWrapper(aNode);
|
2013-12-06 18:17:19 +00:00
|
|
|
|
|
|
|
|
|
FragmentOrElement::RemoveBlackMarkedNode(aNode);
|
2007-05-12 15:36:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-09-05 10:22:54 +00:00
|
|
|
|
/* static */
|
2017-09-13 17:34:55 +00:00
|
|
|
|
already_AddRefed<nsINode>
|
|
|
|
|
nsNodeUtils::CloneNodeImpl(nsINode *aNode, bool aDeep, ErrorResult& aError)
|
2006-09-05 10:22:54 +00:00
|
|
|
|
{
|
2017-09-13 17:34:55 +00:00
|
|
|
|
return Clone(aNode, aDeep, nullptr, nullptr, aError);
|
2006-09-05 10:22:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* static */
|
2017-09-13 17:34:55 +00:00
|
|
|
|
already_AddRefed<nsINode>
|
2011-09-29 06:19:26 +00:00
|
|
|
|
nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
|
2006-09-05 10:22:54 +00:00
|
|
|
|
nsNodeInfoManager *aNewNodeInfoManager,
|
2013-05-08 02:34:56 +00:00
|
|
|
|
JS::Handle<JSObject*> aReparentScope,
|
2017-06-21 18:55:04 +00:00
|
|
|
|
nsCOMArray<nsINode> *aNodesWithProperties,
|
2017-09-13 17:34:55 +00:00
|
|
|
|
nsINode* aParent, ErrorResult& aError)
|
2006-09-05 10:22:54 +00:00
|
|
|
|
{
|
2018-04-28 19:50:58 +00:00
|
|
|
|
MOZ_ASSERT((!aClone && aNewNodeInfoManager) || !aReparentScope,
|
|
|
|
|
"If cloning or not getting a new nodeinfo we shouldn't rewrap");
|
|
|
|
|
MOZ_ASSERT(!aParent || aNode->IsContent(),
|
|
|
|
|
"Can't insert document or attribute nodes into a parent");
|
2006-09-05 10:22:54 +00:00
|
|
|
|
|
|
|
|
|
// First deal with aNode and walk its attributes (and their children). Then,
|
2011-10-17 14:59:28 +00:00
|
|
|
|
// if aDeep is true, deal with aNode's children (and recurse into their
|
2006-09-05 10:22:54 +00:00
|
|
|
|
// attributes and children).
|
|
|
|
|
|
2015-01-06 01:00:27 +00:00
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2010-11-16 01:21:25 +00:00
|
|
|
|
|
2006-09-05 10:22:54 +00:00
|
|
|
|
nsNodeInfoManager *nodeInfoManager = aNewNodeInfoManager;
|
|
|
|
|
|
|
|
|
|
// aNode.
|
2014-06-20 02:01:40 +00:00
|
|
|
|
NodeInfo *nodeInfo = aNode->mNodeInfo;
|
2015-10-18 05:24:48 +00:00
|
|
|
|
RefPtr<NodeInfo> newNodeInfo;
|
2006-09-05 10:22:54 +00:00
|
|
|
|
if (nodeInfoManager) {
|
2008-10-13 16:12:26 +00:00
|
|
|
|
|
|
|
|
|
// Don't allow importing/adopting nodes from non-privileged "scriptable"
|
|
|
|
|
// documents to "non-scriptable" documents.
|
|
|
|
|
nsIDocument* newDoc = nodeInfoManager->GetDocument();
|
2017-09-13 17:34:55 +00:00
|
|
|
|
if (NS_WARN_IF(!newDoc)) {
|
|
|
|
|
aError.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2011-09-29 06:19:26 +00:00
|
|
|
|
bool hasHadScriptHandlingObject = false;
|
2008-10-13 16:12:26 +00:00
|
|
|
|
if (!newDoc->GetScriptHandlingObject(hasHadScriptHandlingObject) &&
|
|
|
|
|
!hasHadScriptHandlingObject) {
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* currentDoc = aNode->OwnerDoc();
|
2017-09-13 17:34:55 +00:00
|
|
|
|
if (NS_WARN_IF(!nsContentUtils::IsChromeDoc(currentDoc) &&
|
|
|
|
|
(currentDoc->GetScriptHandlingObject(hasHadScriptHandlingObject) ||
|
|
|
|
|
hasHadScriptHandlingObject))) {
|
|
|
|
|
aError.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2008-10-13 16:12:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
2008-09-12 22:32:18 +00:00
|
|
|
|
newNodeInfo = nodeInfoManager->GetNodeInfo(nodeInfo->NameAtom(),
|
|
|
|
|
nodeInfo->GetPrefixAtom(),
|
2011-06-14 07:56:49 +00:00
|
|
|
|
nodeInfo->NamespaceID(),
|
|
|
|
|
nodeInfo->NodeType(),
|
|
|
|
|
nodeInfo->GetExtraName());
|
2006-09-05 10:22:54 +00:00
|
|
|
|
|
|
|
|
|
nodeInfo = newNodeInfo;
|
|
|
|
|
}
|
|
|
|
|
|
2012-11-14 22:10:08 +00:00
|
|
|
|
Element *elem = aNode->IsElement() ? aNode->AsElement() : nullptr;
|
2006-10-07 10:27:45 +00:00
|
|
|
|
|
2006-09-05 10:22:54 +00:00
|
|
|
|
nsCOMPtr<nsINode> clone;
|
|
|
|
|
if (aClone) {
|
2017-09-13 17:34:55 +00:00
|
|
|
|
nsresult rv = aNode->Clone(nodeInfo, getter_AddRefs(clone), aDeep);
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
aError.Throw(rv);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2006-09-05 10:22:54 +00:00
|
|
|
|
|
2017-10-20 18:02:33 +00:00
|
|
|
|
if (CustomElementRegistry::IsCustomElementEnabled(nodeInfo->GetDocument()) &&
|
2017-10-10 22:25:10 +00:00
|
|
|
|
(clone->IsHTMLElement() || clone->IsXULElement())) {
|
2014-12-23 02:19:08 +00:00
|
|
|
|
// The cloned node may be a custom element that may require
|
2017-10-03 05:34:00 +00:00
|
|
|
|
// enqueing upgrade reaction.
|
2017-11-14 12:01:00 +00:00
|
|
|
|
Element* cloneElem = clone->AsElement();
|
2017-10-02 22:05:19 +00:00
|
|
|
|
RefPtr<nsAtom> tagAtom = nodeInfo->NameAtom();
|
2017-11-14 12:01:00 +00:00
|
|
|
|
CustomElementData* data = elem->GetCustomElementData();
|
|
|
|
|
|
|
|
|
|
// Check if node may be custom element by type extension.
|
|
|
|
|
// ex. <button is="x-button">
|
|
|
|
|
nsAutoString extension;
|
|
|
|
|
if (!data || data->GetCustomElementType() != tagAtom) {
|
|
|
|
|
cloneElem->GetAttr(kNameSpaceID_None, nsGkAtoms::is, extension);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-07 16:45:00 +00:00
|
|
|
|
if ((data && data->GetCustomElementType() == tagAtom) ||
|
|
|
|
|
!extension.IsEmpty()) {
|
|
|
|
|
// The typeAtom can be determined by extension, because we only need to
|
|
|
|
|
// consider two cases: 1) Original node is a autonomous custom element
|
|
|
|
|
// which has CustomElementData. 2) Original node is a built-in custom
|
|
|
|
|
// element or normal element, but it has `is` attribute when it is being
|
|
|
|
|
// cloned.
|
2017-11-14 12:01:00 +00:00
|
|
|
|
RefPtr<nsAtom> typeAtom = extension.IsEmpty() ? tagAtom : NS_Atomize(extension);
|
|
|
|
|
cloneElem->SetCustomElementData(new CustomElementData(typeAtom));
|
2018-01-17 06:55:13 +00:00
|
|
|
|
|
|
|
|
|
MOZ_ASSERT(nodeInfo->NameAtom()->Equals(nodeInfo->LocalName()));
|
2017-11-14 12:01:00 +00:00
|
|
|
|
CustomElementDefinition* definition =
|
2017-10-03 05:34:00 +00:00
|
|
|
|
nsContentUtils::LookupCustomElementDefinition(nodeInfo->GetDocument(),
|
2018-01-17 06:55:13 +00:00
|
|
|
|
nodeInfo->NameAtom(),
|
2017-11-14 12:01:00 +00:00
|
|
|
|
nodeInfo->NamespaceID(),
|
2017-11-14 11:23:00 +00:00
|
|
|
|
typeAtom);
|
2017-10-03 05:34:00 +00:00
|
|
|
|
if (definition) {
|
2017-11-14 12:01:00 +00:00
|
|
|
|
nsContentUtils::EnqueueUpgradeReaction(cloneElem, definition);
|
2014-12-23 02:19:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-05 10:22:54 +00:00
|
|
|
|
if (aParent) {
|
|
|
|
|
// If we're cloning we need to insert the cloned children into the cloned
|
|
|
|
|
// parent.
|
2009-09-28 20:33:29 +00:00
|
|
|
|
rv = aParent->AppendChildTo(static_cast<nsIContent*>(clone.get()),
|
2011-10-17 14:59:28 +00:00
|
|
|
|
false);
|
2017-09-13 17:34:55 +00:00
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
|
aError.Throw(rv);
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2006-09-05 10:22:54 +00:00
|
|
|
|
}
|
2018-04-15 09:43:15 +00:00
|
|
|
|
else if (aDeep && clone->IsDocument()) {
|
2006-09-05 10:22:54 +00:00
|
|
|
|
// 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 10:53:36 +00:00
|
|
|
|
nsIDocument* oldDoc = aNode->OwnerDoc();
|
2011-09-29 06:19:26 +00:00
|
|
|
|
bool wasRegistered = false;
|
2011-10-18 11:19:44 +00:00
|
|
|
|
if (aNode->IsElement()) {
|
2010-04-30 13:12:05 +00:00
|
|
|
|
Element* element = aNode->AsElement();
|
|
|
|
|
oldDoc->ClearBoxObjectFor(element);
|
2014-06-19 02:09:35 +00:00
|
|
|
|
wasRegistered = oldDoc->UnregisterActivityObserver(element);
|
2007-03-20 17:39:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-09-05 10:22:54 +00:00
|
|
|
|
aNode->mNodeInfo.swap(newNodeInfo);
|
2010-09-01 22:48:24 +00:00
|
|
|
|
if (elem) {
|
2017-02-23 01:19:04 +00:00
|
|
|
|
elem->NodeInfoChanged(oldDoc);
|
2010-09-01 22:48:24 +00:00
|
|
|
|
}
|
2006-09-05 10:22:54 +00:00
|
|
|
|
|
2011-10-18 10:53:36 +00:00
|
|
|
|
nsIDocument* newDoc = aNode->OwnerDoc();
|
2007-03-20 17:39:25 +00:00
|
|
|
|
if (newDoc) {
|
2017-10-20 18:02:33 +00:00
|
|
|
|
if (CustomElementRegistry::IsCustomElementEnabled(newDoc)) {
|
2017-10-02 04:42:00 +00:00
|
|
|
|
// Adopted callback must be enqueued whenever a node’s
|
|
|
|
|
// shadow-including inclusive descendants that is custom.
|
|
|
|
|
Element* element = aNode->IsElement() ? aNode->AsElement() : nullptr;
|
|
|
|
|
if (element) {
|
2017-11-02 18:52:00 +00:00
|
|
|
|
CustomElementData* data = element->GetCustomElementData();
|
2017-10-02 04:42:00 +00:00
|
|
|
|
if (data && data->mState == CustomElementData::State::eCustom) {
|
|
|
|
|
LifecycleAdoptedCallbackArgs args = {
|
|
|
|
|
oldDoc,
|
|
|
|
|
newDoc
|
|
|
|
|
};
|
|
|
|
|
nsContentUtils::EnqueueLifecycleCallback(nsIDocument::eAdopted,
|
|
|
|
|
element, nullptr, &args);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-01 14:10:13 +00: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 01:32:32 +00:00
|
|
|
|
if (wasRegistered) {
|
2014-06-19 02:09:35 +00:00
|
|
|
|
newDoc->RegisterActivityObserver(aNode->AsElement());
|
2009-05-08 01:32:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-01-30 17:05:36 +00:00
|
|
|
|
if (nsPIDOMWindowInner* window = newDoc->GetInnerWindow()) {
|
2014-03-17 06:56:53 +00:00
|
|
|
|
EventListenerManager* elm = aNode->GetExistingListenerManager();
|
2007-03-20 17:39:25 +00:00
|
|
|
|
if (elm) {
|
|
|
|
|
window->SetMutationListeners(elm->MutationListenerBits());
|
2008-10-15 21:06:32 +00:00
|
|
|
|
if (elm->MayHavePaintEventListener()) {
|
|
|
|
|
window->SetHasPaintEventListeners();
|
|
|
|
|
}
|
2015-04-03 00:45:50 +00:00
|
|
|
|
if (elm->MayHaveTouchEventListener()) {
|
|
|
|
|
window->SetHasTouchEventListeners();
|
|
|
|
|
}
|
2011-09-18 07:45:14 +00:00
|
|
|
|
if (elm->MayHaveMouseEnterLeaveEventListener()) {
|
|
|
|
|
window->SetHasMouseEnterLeaveEventListeners();
|
|
|
|
|
}
|
2014-02-11 06:33:29 +00:00
|
|
|
|
if (elm->MayHavePointerEnterLeaveEventListener()) {
|
|
|
|
|
window->SetHasPointerEnterLeaveEventListeners();
|
|
|
|
|
}
|
2017-06-22 18:01:27 +00:00
|
|
|
|
if (elm->MayHaveSelectionChangeEventListener()) {
|
|
|
|
|
window->SetHasSelectionChangeEventListeners();
|
|
|
|
|
}
|
2007-03-20 17:39:25 +00:00
|
|
|
|
}
|
2007-01-31 22:54:24 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-30 06:09:56 +00:00
|
|
|
|
if (wasRegistered && oldDoc != newDoc) {
|
2018-02-15 05:17:45 +00:00
|
|
|
|
nsIContent* content = aNode->AsContent();
|
2018-03-21 21:39:04 +00:00
|
|
|
|
if (auto mediaElem = HTMLMediaElement::FromNodeOrNull(content)) {
|
2009-10-01 14:10:13 +00:00
|
|
|
|
mediaElem->NotifyOwnerDocumentActivityChanged();
|
|
|
|
|
}
|
2012-01-31 21:55:54 +00:00
|
|
|
|
nsCOMPtr<nsIObjectLoadingContent> objectLoadingContent(do_QueryInterface(aNode));
|
|
|
|
|
if (objectLoadingContent) {
|
|
|
|
|
nsObjectLoadingContent* olc = static_cast<nsObjectLoadingContent*>(objectLoadingContent.get());
|
|
|
|
|
olc->NotifyOwnerDocumentActivityChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-05-09 09:44:19 +00:00
|
|
|
|
|
2012-08-13 22:11:50 +00:00
|
|
|
|
if (oldDoc != newDoc && oldDoc->MayHaveDOMMutationObservers()) {
|
|
|
|
|
newDoc->SetMayHaveDOMMutationObservers();
|
2010-08-27 01:30:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-14 05:34:40 +00:00
|
|
|
|
if (oldDoc != newDoc && oldDoc->MayHaveAnimationObservers()) {
|
|
|
|
|
newDoc->SetMayHaveAnimationObservers();
|
|
|
|
|
}
|
|
|
|
|
|
2006-10-07 10:27:45 +00:00
|
|
|
|
if (elem) {
|
|
|
|
|
elem->RecompileScriptEventListeners();
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-19 19:57:15 +00:00
|
|
|
|
if (aReparentScope) {
|
2016-05-26 23:43:42 +00:00
|
|
|
|
AutoJSContext cx;
|
2013-04-19 19:57:15 +00:00
|
|
|
|
JS::Rooted<JSObject*> wrapper(cx);
|
|
|
|
|
if ((wrapper = aNode->GetWrapper())) {
|
2015-02-13 19:34:53 +00:00
|
|
|
|
MOZ_ASSERT(IsDOMObject(wrapper));
|
2018-05-16 08:53:16 +00:00
|
|
|
|
JSAutoRealm ar(cx, wrapper);
|
2017-09-13 17:34:55 +00:00
|
|
|
|
ReparentWrapper(cx, wrapper, aError);
|
|
|
|
|
if (aError.Failed()) {
|
2016-11-17 13:54:12 +00:00
|
|
|
|
if (wasRegistered) {
|
|
|
|
|
aNode->OwnerDoc()->UnregisterActivityObserver(aNode->AsElement());
|
|
|
|
|
}
|
2016-08-04 14:28:15 +00:00
|
|
|
|
aNode->mNodeInfo.swap(newNodeInfo);
|
2017-10-12 02:19:06 +00:00
|
|
|
|
if (elem) {
|
|
|
|
|
elem->NodeInfoChanged(newDoc);
|
|
|
|
|
}
|
2016-11-17 13:54:12 +00:00
|
|
|
|
if (wasRegistered) {
|
|
|
|
|
aNode->OwnerDoc()->RegisterActivityObserver(aNode->AsElement());
|
|
|
|
|
}
|
2017-09-13 17:34:55 +00:00
|
|
|
|
return nullptr;
|
2013-04-19 19:57:15 +00:00
|
|
|
|
}
|
2012-09-26 14:17:46 +00:00
|
|
|
|
}
|
2006-09-05 10:22:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-20 10:02:29 +00:00
|
|
|
|
if (aNodesWithProperties && aNode->HasProperties()) {
|
|
|
|
|
bool ok = aNodesWithProperties->AppendObject(aNode);
|
|
|
|
|
MOZ_RELEASE_ASSERT(ok, "Out of memory");
|
|
|
|
|
if (aClone) {
|
|
|
|
|
ok = aNodesWithProperties->AppendObject(clone);
|
|
|
|
|
MOZ_RELEASE_ASSERT(ok, "Out of memory");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-15 13:12:02 +00:00
|
|
|
|
if (aDeep && (!aClone || !aNode->IsAttr())) {
|
2006-09-05 10:22:54 +00:00
|
|
|
|
// aNode's children.
|
2011-09-27 07:54:58 +00:00
|
|
|
|
for (nsIContent* cloneChild = aNode->GetFirstChild();
|
|
|
|
|
cloneChild;
|
2012-04-03 07:25:39 +00:00
|
|
|
|
cloneChild = cloneChild->GetNextSibling()) {
|
2017-09-13 17:34:55 +00:00
|
|
|
|
nsCOMPtr<nsINode> child =
|
|
|
|
|
CloneAndAdopt(cloneChild, aClone, true, nodeInfoManager,
|
|
|
|
|
aReparentScope, aNodesWithProperties, clone,
|
|
|
|
|
aError);
|
|
|
|
|
if (NS_WARN_IF(aError.Failed())) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2006-09-05 10:22:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-04 10:41:13 +00:00
|
|
|
|
if (aDeep && aNode->IsElement()) {
|
|
|
|
|
if (aClone) {
|
|
|
|
|
if (clone->OwnerDoc()->IsStaticDocument()) {
|
|
|
|
|
ShadowRoot* originalShadowRoot = aNode->AsElement()->GetShadowRoot();
|
|
|
|
|
if (originalShadowRoot) {
|
|
|
|
|
ShadowRootInit init;
|
|
|
|
|
init.mMode = originalShadowRoot->Mode();
|
|
|
|
|
RefPtr<ShadowRoot> newShadowRoot =
|
|
|
|
|
clone->AsElement()->AttachShadow(init, aError);
|
|
|
|
|
if (NS_WARN_IF(aError.Failed())) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newShadowRoot->CloneInternalDataFrom(originalShadowRoot);
|
|
|
|
|
for (nsIContent* origChild = originalShadowRoot->GetFirstChild();
|
|
|
|
|
origChild;
|
|
|
|
|
origChild = origChild->GetNextSibling()) {
|
|
|
|
|
nsCOMPtr<nsINode> child =
|
|
|
|
|
CloneAndAdopt(origChild, aClone, aDeep, nodeInfoManager,
|
|
|
|
|
aReparentScope, aNodesWithProperties, newShadowRoot,
|
|
|
|
|
aError);
|
|
|
|
|
if (NS_WARN_IF(aError.Failed())) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if (ShadowRoot* shadowRoot = aNode->AsElement()->GetShadowRoot()) {
|
|
|
|
|
nsCOMPtr<nsINode> child =
|
|
|
|
|
CloneAndAdopt(shadowRoot, aClone, aDeep, nodeInfoManager,
|
|
|
|
|
aReparentScope, aNodesWithProperties, clone,
|
|
|
|
|
aError);
|
|
|
|
|
if (NS_WARN_IF(aError.Failed())) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2017-11-06 17:33:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2013-03-26 07:15:23 +00: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()) {
|
2017-09-13 17:34:55 +00:00
|
|
|
|
nsCOMPtr<nsINode> child =
|
|
|
|
|
CloneAndAdopt(cloneChild, aClone, aDeep, ownerNodeInfoManager,
|
|
|
|
|
aReparentScope, aNodesWithProperties, cloneContent,
|
|
|
|
|
aError);
|
|
|
|
|
if (NS_WARN_IF(aError.Failed())) {
|
|
|
|
|
return nullptr;
|
|
|
|
|
}
|
2013-03-26 07:15:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-13 17:34:55 +00:00
|
|
|
|
return clone.forget();
|
2006-09-05 10:22:54 +00:00
|
|
|
|
}
|
2007-05-12 15:36:28 +00:00
|
|
|
|
|
2013-03-26 07:15:23 +00:00
|
|
|
|
bool
|
|
|
|
|
nsNodeUtils::IsTemplateElement(const nsINode *aNode)
|
|
|
|
|
{
|
2015-03-03 11:08:59 +00:00
|
|
|
|
return aNode->IsHTMLElement(nsGkAtoms::_template);
|
2013-03-26 07:15:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
nsIContent*
|
|
|
|
|
nsNodeUtils::GetFirstChildOfTemplateOrNode(nsINode* aNode)
|
|
|
|
|
{
|
|
|
|
|
if (nsNodeUtils::IsTemplateElement(aNode)) {
|
|
|
|
|
DocumentFragment* frag =
|
|
|
|
|
static_cast<HTMLTemplateElement*>(aNode)->Content();
|
|
|
|
|
return frag->GetFirstChild();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return aNode->GetFirstChild();
|
|
|
|
|
}
|