2015-05-03 22:32:37 +03: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 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
|
|
|
|
2019-11-06 12:08:06 +03:00
|
|
|
#include "MutationObservers.h"
|
|
|
|
|
2006-08-16 12:44:45 +04:00
|
|
|
#include "nsContentUtils.h"
|
2016-02-17 23:37:00 +03:00
|
|
|
#include "nsCSSPseudoElements.h"
|
2006-07-02 11:23:10 +04:00
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIContent.h"
|
2017-12-31 22:57:32 +03:00
|
|
|
#include "nsIContentInlines.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2019-03-29 18:11:04 +03:00
|
|
|
#include "mozilla/dom/DocumentInlines.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"
|
2014-03-17 10:56:53 +04:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2015-09-16 06:49:53 +03:00
|
|
|
#include "PLDHashTable.h"
|
2006-09-05 14:22:54 +04:00
|
|
|
#include "nsCOMArray.h"
|
2007-02-01 01:54:24 +03:00
|
|
|
#include "nsPIDOMWindow.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
|
2009-10-01 02:56:50 +04:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2016-04-28 18:22:43 +03:00
|
|
|
#include "mozilla/AnimationTarget.h"
|
2015-02-13 22:34:53 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2017-09-13 20:34:55 +03:00
|
|
|
#include "mozilla/ErrorResult.h"
|
2015-04-21 04:22:09 +03:00
|
|
|
#include "mozilla/dom/Animation.h"
|
2018-05-07 05:07:06 +03:00
|
|
|
#include "mozilla/dom/KeyframeEffect.h"
|
2019-03-29 18:12:47 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2011-05-26 23:58:35 +04:00
|
|
|
#include "nsWrapperCacheInlines.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"
|
2018-12-26 05:35:30 +03:00
|
|
|
#include "mozilla/dom/CustomElementRegistry.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
|
|
|
|
2017-12-11 08:42:38 +03:00
|
|
|
enum class IsRemoveNotification {
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
};
|
|
|
|
|
2017-12-11 12:14:36 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
# define COMPOSED_DOC_DECL \
|
|
|
|
const bool wasInComposedDoc = !!node->GetComposedDoc();
|
|
|
|
#else
|
|
|
|
# define COMPOSED_DOC_DECL
|
|
|
|
#endif
|
|
|
|
|
2019-11-07 03:35:25 +03:00
|
|
|
#define CALL_BINDING_MANAGER(func_, params_) \
|
|
|
|
do { \
|
|
|
|
} while (0)
|
2019-10-09 02:52:14 +03:00
|
|
|
|
2007-12-04 21:37:54 +03:00
|
|
|
// This macro expects the ownerDocument of content_ to be in scope as
|
2019-01-02 16:05:23 +03:00
|
|
|
// |Document* doc|
|
2017-12-11 08:42:38 +03: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 12:14:36 +03:00
|
|
|
COMPOSED_DOC_DECL \
|
2017-12-11 08:42:38 +03:00
|
|
|
NS_ASSERTION(node->OwnerDoc() == doc, "Bogus document"); \
|
|
|
|
if (remove_ == IsRemoveNotification::Yes && node->GetComposedDoc()) { \
|
2019-03-29 18:12:47 +03:00
|
|
|
if (PresShell* presShell = doc->GetObservingPresShell()) { \
|
|
|
|
presShell->func_ params_; \
|
2017-12-11 08:42:38 +03:00
|
|
|
} \
|
|
|
|
} \
|
2019-10-09 02:52:14 +03:00
|
|
|
CALL_BINDING_MANAGER(func_, params_); \
|
2017-12-11 08:42:38 +03:00
|
|
|
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 12:14:36 +03: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 08:42:38 +03:00
|
|
|
if (remove_ == IsRemoveNotification::No && last == doc) { \
|
2019-03-29 18:12:47 +03:00
|
|
|
if (PresShell* presShell = doc->GetObservingPresShell()) { \
|
|
|
|
presShell->func_ params_; \
|
2017-12-11 08:42:38 +03:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling(); \
|
|
|
|
} \
|
2006-08-16 02:41:58 +04:00
|
|
|
PR_END_MACRO
|
2006-07-02 11:23:10 +04:00
|
|
|
|
2015-03-14 08:34:40 +03: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 08:07:05 +03:00
|
|
|
NS_OBSERVER_AUTO_ARRAY_NOTIFY_OBSERVERS_WITH_QI( \
|
|
|
|
slots->mMutationObservers, nsIMutationObserver, 1, \
|
2015-03-14 08:34:40 +03:00
|
|
|
nsIAnimationObserver, func_, params_); \
|
|
|
|
} \
|
2017-12-31 22:57:32 +03:00
|
|
|
if (ShadowRoot* shadow = ShadowRoot::FromNode(node)) { \
|
2017-09-25 18:09:26 +03:00
|
|
|
node = shadow->GetHost(); \
|
2015-03-14 08:34:40 +03:00
|
|
|
} else { \
|
|
|
|
node = node->GetParentNode(); \
|
|
|
|
} \
|
|
|
|
} while (node); \
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling(); \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
namespace mozilla {
|
|
|
|
void MutationObservers::NotifyCharacterDataWillChange(
|
2018-02-27 17:30:27 +03:00
|
|
|
nsIContent* aContent, const CharacterDataChangeInfo& aInfo) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aContent->OwnerDoc();
|
2010-07-21 19:33:32 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataWillChange, aContent,
|
2018-03-01 14:36:58 +03:00
|
|
|
(aContent, aInfo), IsRemoveNotification::No);
|
2007-09-05 12:22:17 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
void MutationObservers::NotifyCharacterDataChanged(
|
|
|
|
nsIContent* aContent, const CharacterDataChangeInfo& aInfo) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aContent->OwnerDoc();
|
2019-02-25 19:39:46 +03:00
|
|
|
doc->Changed();
|
2010-07-21 19:33:32 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataChanged, aContent, (aContent, aInfo),
|
2018-03-01 14:36:58 +03:00
|
|
|
IsRemoveNotification::No);
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
void MutationObservers::NotifyAttributeWillChange(Element* aElement,
|
|
|
|
int32_t aNameSpaceID,
|
|
|
|
nsAtom* aAttribute,
|
|
|
|
int32_t aModType) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aElement->OwnerDoc();
|
2019-01-29 18:27:02 +03:00
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeWillChange, aElement,
|
|
|
|
(aElement, aNameSpaceID, aAttribute, aModType),
|
|
|
|
IsRemoveNotification::No);
|
2009-06-29 22:36:25 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
void MutationObservers::NotifyAttributeChanged(Element* aElement,
|
|
|
|
int32_t aNameSpaceID,
|
|
|
|
nsAtom* aAttribute,
|
|
|
|
int32_t aModType,
|
|
|
|
const nsAttrValue* aOldValue) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aElement->OwnerDoc();
|
2019-02-25 19:39:46 +03:00
|
|
|
doc->Changed();
|
2010-08-24 11:05:56 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(
|
|
|
|
AttributeChanged, aElement,
|
2018-03-01 14:36:58 +03:00
|
|
|
(aElement, aNameSpaceID, aAttribute, aModType, aOldValue),
|
2017-12-11 08:42:38 +03:00
|
|
|
IsRemoveNotification::No);
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
void MutationObservers::NotifyAttributeSetToCurrentValue(Element* aElement,
|
|
|
|
int32_t aNameSpaceID,
|
|
|
|
nsAtom* aAttribute) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aElement->OwnerDoc();
|
2012-08-15 14:06:01 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeSetToCurrentValue, aElement,
|
2018-03-01 14:36:58 +03:00
|
|
|
(aElement, aNameSpaceID, aAttribute),
|
2017-12-11 08:42:38 +03:00
|
|
|
IsRemoveNotification::No);
|
2012-08-15 14:06:01 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
void MutationObservers::NotifyContentAppended(nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aContainer->OwnerDoc();
|
2019-02-25 19:39:46 +03:00
|
|
|
doc->Changed();
|
2010-07-21 19:33:32 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentAppended, aContainer, (aFirstNewContent),
|
2017-12-11 08:42:38 +03:00
|
|
|
IsRemoveNotification::No);
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
void MutationObservers::NotifyNativeAnonymousChildListChange(
|
|
|
|
nsIContent* aContent, bool aIsRemove) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = aContent->OwnerDoc();
|
2017-12-11 12:14:36 +03:00
|
|
|
auto isRemove =
|
|
|
|
aIsRemove ? IsRemoveNotification::Yes : IsRemoveNotification::No;
|
2015-09-24 18:23:32 +03:00
|
|
|
IMPL_MUTATION_NOTIFICATION(NativeAnonymousChildListChange, aContent,
|
2018-03-01 14:36:58 +03:00
|
|
|
(aContent, aIsRemove), isRemove);
|
2015-09-24 18:23:32 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
void MutationObservers::NotifyContentInserted(nsINode* aContainer,
|
|
|
|
nsIContent* aChild) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aContainer->IsContent() || aContainer->IsDocument(),
|
2019-01-02 16:05:23 +03:00
|
|
|
"container must be an nsIContent or an Document");
|
|
|
|
Document* doc = aContainer->OwnerDoc();
|
2019-02-25 19:39:46 +03:00
|
|
|
doc->Changed();
|
2018-03-01 14:36:58 +03:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentInserted, aContainer, (aChild),
|
2017-12-11 08:42:38 +03:00
|
|
|
IsRemoveNotification::No);
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 00:32:53 +03:00
|
|
|
void MutationObservers::NotifyContentRemoved(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
nsIContent* aPreviousSibling) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aContainer->IsContent() || aContainer->IsDocument(),
|
2019-01-02 16:05:23 +03:00
|
|
|
"container must be an nsIContent or an Document");
|
|
|
|
Document* doc = aContainer->OwnerDoc();
|
2019-02-25 19:39:46 +03:00
|
|
|
doc->Changed();
|
2018-03-01 14:36:58 +03:00
|
|
|
MOZ_ASSERT(aChild->GetParentNode() == aContainer,
|
|
|
|
"We expect the parent link to be still around at this point");
|
2010-07-21 19:37:41 +04:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentRemoved, aContainer,
|
2018-03-01 14:36:58 +03:00
|
|
|
(aChild, aPreviousSibling),
|
2017-12-11 08:42:38 +03:00
|
|
|
IsRemoveNotification::Yes);
|
2006-07-02 11:23:10 +04:00
|
|
|
}
|
2019-11-06 00:32:53 +03:00
|
|
|
} // namespace mozilla
|
2006-07-02 11:23:10 +04:00
|
|
|
|
2019-11-05 23:39:22 +03:00
|
|
|
void MutationObservers::NotifyAnimationMutated(
|
2019-11-05 17:23:13 +03:00
|
|
|
dom::Animation* aAnimation, AnimationMutationType aMutatedType) {
|
|
|
|
MOZ_ASSERT(aAnimation);
|
2015-03-14 08:34:40 +03:00
|
|
|
|
2020-02-18 23:44:14 +03:00
|
|
|
NonOwningAnimationTarget target = aAnimation->GetTargetForAnimation();
|
2015-03-14 08:34:40 +03:00
|
|
|
if (!target) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-21 11:49:50 +03:00
|
|
|
// A pseudo element and its parent element use the same owner doc.
|
2020-02-18 23:44:14 +03:00
|
|
|
Document* doc = target.mElement->OwnerDoc();
|
2015-03-14 08:34:40 +03:00
|
|
|
if (doc->MayHaveAnimationObservers()) {
|
2016-03-21 11:49:50 +03:00
|
|
|
// we use the its parent element as the subject in DOM Mutation Observer.
|
2020-02-18 23:44:14 +03:00
|
|
|
Element* elem = target.mElement;
|
2016-03-21 11:49:50 +03:00
|
|
|
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 08:34:40 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-05 23:39:22 +03:00
|
|
|
void MutationObservers::NotifyAnimationAdded(dom::Animation* aAnimation) {
|
|
|
|
NotifyAnimationMutated(aAnimation, AnimationMutationType::Added);
|
2016-03-21 11:49:50 +03:00
|
|
|
}
|
2015-03-14 08:34:40 +03:00
|
|
|
|
2019-11-05 23:39:22 +03:00
|
|
|
void MutationObservers::NotifyAnimationChanged(dom::Animation* aAnimation) {
|
|
|
|
NotifyAnimationMutated(aAnimation, AnimationMutationType::Changed);
|
2015-03-14 08:34:40 +03:00
|
|
|
}
|
|
|
|
|
2019-11-05 23:39:22 +03:00
|
|
|
void MutationObservers::NotifyAnimationRemoved(dom::Animation* aAnimation) {
|
|
|
|
NotifyAnimationMutated(aAnimation, AnimationMutationType::Removed);
|
2015-03-14 08:34:40 +03:00
|
|
|
}
|