2012-03-31 09:30:13 -07:00
|
|
|
/* -*- Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 8; -*- */
|
|
|
|
/* vim: set sw=4 ts=8 et tw=80 : */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "nsDOMMutationObserver.h"
|
|
|
|
#include "nsDOMClassInfoID.h"
|
2012-07-27 07:03:27 -07:00
|
|
|
#include "nsError.h"
|
2012-03-31 09:30:13 -07:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
#include "nsIDOMMutationEvent.h"
|
|
|
|
#include "nsTextFragment.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
nsTArray<nsRefPtr<nsDOMMutationObserver> >*
|
2012-07-30 07:20:58 -07:00
|
|
|
nsDOMMutationObserver::sScheduledMutationObservers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
nsDOMMutationObserver* nsDOMMutationObserver::sCurrentObserver = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t nsDOMMutationObserver::sMutationLevel = 0;
|
|
|
|
uint64_t nsDOMMutationObserver::sCount = 0;
|
2012-03-31 09:30:13 -07:00
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
nsAutoTArray<nsTArray<nsRefPtr<nsDOMMutationObserver> >, 4>*
|
2012-07-30 07:20:58 -07:00
|
|
|
nsDOMMutationObserver::sCurrentlyHandlingObservers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
nsINodeList*
|
|
|
|
nsDOMMutationRecord::AddedNodes()
|
|
|
|
{
|
|
|
|
if (!mAddedNodes) {
|
|
|
|
mAddedNodes = new nsSimpleContentList(mTarget);
|
|
|
|
}
|
|
|
|
return mAddedNodes;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINodeList*
|
|
|
|
nsDOMMutationRecord::RemovedNodes()
|
|
|
|
{
|
|
|
|
if (!mRemovedNodes) {
|
|
|
|
mRemovedNodes = new nsSimpleContentList(mTarget);
|
|
|
|
}
|
|
|
|
return mRemovedNodes;
|
|
|
|
}
|
2012-03-31 09:30:13 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMMutationRecord)
|
2012-12-18 06:50:52 -08:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMMutationRecord)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMMutationRecord)
|
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsDOMMutationRecord)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMMutationRecord)
|
2012-12-18 06:50:52 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPreviousSibling)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNextSibling)
|
2012-07-30 07:20:58 -07:00
|
|
|
tmp->mAddedNodes = nullptr;
|
|
|
|
tmp->mRemovedNodes = nullptr;
|
2012-12-18 06:50:52 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMMutationRecord)
|
2012-12-18 06:50:52 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTarget)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPreviousSibling)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNextSibling)
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mAddedNodes");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIDOMNodeList*>(tmp->mAddedNodes));
|
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(cb, "mRemovedNodes");
|
|
|
|
cb.NoteXPCOMChild(static_cast<nsIDOMNodeList*>(tmp->mRemovedNodes));
|
2012-12-18 06:50:52 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
// Observer
|
|
|
|
|
2012-07-09 13:44:33 -07:00
|
|
|
NS_IMPL_ADDREF(nsMutationReceiver)
|
|
|
|
NS_IMPL_RELEASE(nsMutationReceiver)
|
2012-03-31 09:30:13 -07:00
|
|
|
|
2012-07-09 13:44:33 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsMutationReceiver)
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsMutationReceiver)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
nsMutationReceiver::nsMutationReceiver(nsINode* aTarget,
|
|
|
|
nsDOMMutationObserver* aObserver)
|
|
|
|
: nsMutationReceiverBase(aTarget, aObserver)
|
|
|
|
{
|
|
|
|
mTarget->BindObject(aObserver);
|
|
|
|
}
|
|
|
|
|
2012-04-04 10:36:40 -07:00
|
|
|
void
|
|
|
|
nsMutationReceiver::Disconnect(bool aRemoveFromObserver)
|
|
|
|
{
|
|
|
|
if (mRegisterTarget) {
|
|
|
|
mRegisterTarget->RemoveMutationObserver(this);
|
2012-07-30 07:20:58 -07:00
|
|
|
mRegisterTarget = nullptr;
|
2012-04-04 10:36:40 -07:00
|
|
|
}
|
|
|
|
|
2012-07-30 07:20:58 -07:00
|
|
|
mParent = nullptr;
|
2012-04-04 10:36:40 -07:00
|
|
|
nsINode* target = mTarget;
|
2012-07-30 07:20:58 -07:00
|
|
|
mTarget = nullptr;
|
2012-12-18 06:50:52 -08:00
|
|
|
nsDOMMutationObserver* observer = mObserver;
|
2012-07-30 07:20:58 -07:00
|
|
|
mObserver = nullptr;
|
2012-04-04 10:36:40 -07:00
|
|
|
RemoveClones();
|
|
|
|
|
|
|
|
if (target && observer) {
|
|
|
|
if (aRemoveFromObserver) {
|
|
|
|
static_cast<nsDOMMutationObserver*>(observer)->RemoveReceiver(this);
|
|
|
|
}
|
|
|
|
// UnbindObject may delete 'this'!
|
|
|
|
target->UnbindObject(observer);
|
|
|
|
}
|
|
|
|
}
|
2012-03-31 09:30:13 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsMutationReceiver::AttributeWillChange(nsIDocument* aDocument,
|
|
|
|
mozilla::dom::Element* aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNameSpaceID,
|
2012-03-31 09:30:13 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aModType)
|
2012-03-31 09:30:13 -07:00
|
|
|
{
|
|
|
|
if (nsAutoMutationBatch::IsBatching() ||
|
|
|
|
!ObservesAttr(aElement, aNameSpaceID, aAttribute) ||
|
2012-10-10 12:04:42 -07:00
|
|
|
aElement->ChromeOnlyAccess()) {
|
2012-03-31 09:30:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMutationRecord* m =
|
|
|
|
Observer()->CurrentRecord(NS_LITERAL_STRING("attributes"));
|
|
|
|
|
|
|
|
NS_ASSERTION(!m->mTarget || m->mTarget == aElement,
|
|
|
|
"Wrong target!");
|
|
|
|
NS_ASSERTION(m->mAttrName.IsVoid() ||
|
|
|
|
m->mAttrName.Equals(nsDependentAtomString(aAttribute)),
|
|
|
|
"Wrong attribute!");
|
|
|
|
if (!m->mTarget) {
|
|
|
|
m->mTarget = aElement;
|
|
|
|
m->mAttrName = nsAtomString(aAttribute);
|
|
|
|
if (aNameSpaceID == kNameSpaceID_None) {
|
|
|
|
m->mAttrNamespace.SetIsVoid(true);
|
|
|
|
} else {
|
|
|
|
nsContentUtils::NameSpaceManager()->GetNameSpaceURI(aNameSpaceID,
|
|
|
|
m->mAttrNamespace);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AttributeOldValue() && m->mPrevValue.IsVoid()) {
|
|
|
|
if (!aElement->GetAttr(aNameSpaceID, aAttribute, m->mPrevValue)) {
|
|
|
|
m->mPrevValue.SetIsVoid(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMutationReceiver::CharacterDataWillChange(nsIDocument *aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
|
|
|
if (nsAutoMutationBatch::IsBatching() ||
|
|
|
|
!CharacterData() || !(Subtree() || aContent == Target()) ||
|
2012-10-10 12:04:42 -07:00
|
|
|
aContent->ChromeOnlyAccess()) {
|
2012-03-31 09:30:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMutationRecord* m =
|
|
|
|
Observer()->CurrentRecord(NS_LITERAL_STRING("characterData"));
|
|
|
|
|
|
|
|
NS_ASSERTION(!m->mTarget || m->mTarget == aContent,
|
|
|
|
"Wrong target!");
|
|
|
|
|
|
|
|
if (!m->mTarget) {
|
|
|
|
m->mTarget = aContent;
|
|
|
|
}
|
|
|
|
if (CharacterDataOldValue() && m->mPrevValue.IsVoid()) {
|
|
|
|
aContent->GetText()->AppendTo(m->mPrevValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMutationReceiver::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNewIndexInContainer)
|
2012-03-31 09:30:13 -07:00
|
|
|
{
|
|
|
|
nsINode* parent = NODE_FROM(aContainer, aDocument);
|
|
|
|
bool wantsChildList = ChildList() && (Subtree() || parent == Target());
|
2012-10-10 12:04:42 -07:00
|
|
|
if (!wantsChildList || aFirstNewContent->ChromeOnlyAccess()) {
|
2012-03-31 09:30:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsAutoMutationBatch::IsBatching()) {
|
|
|
|
if (parent == nsAutoMutationBatch::GetBatchTarget()) {
|
|
|
|
nsAutoMutationBatch::UpdateObserver(Observer(), wantsChildList);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMutationRecord* m =
|
|
|
|
Observer()->CurrentRecord(NS_LITERAL_STRING("childList"));
|
|
|
|
NS_ASSERTION(!m->mTarget || m->mTarget == parent,
|
|
|
|
"Wrong target!");
|
|
|
|
if (m->mTarget) {
|
|
|
|
// Already handled case.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m->mTarget = parent;
|
|
|
|
m->mAddedNodes = new nsSimpleContentList(parent);
|
|
|
|
|
|
|
|
nsINode* n = aFirstNewContent;
|
|
|
|
while (n) {
|
|
|
|
m->mAddedNodes->AppendElement(static_cast<nsIContent*>(n));
|
|
|
|
n = n->GetNextSibling();
|
|
|
|
}
|
|
|
|
m->mPreviousSibling = aFirstNewContent->GetPreviousSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMutationReceiver::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer)
|
2012-03-31 09:30:13 -07:00
|
|
|
{
|
|
|
|
nsINode* parent = NODE_FROM(aContainer, aDocument);
|
|
|
|
bool wantsChildList = ChildList() && (Subtree() || parent == Target());
|
2012-10-10 12:04:42 -07:00
|
|
|
if (!wantsChildList || aChild->ChromeOnlyAccess()) {
|
2012-03-31 09:30:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nsAutoMutationBatch::IsBatching()) {
|
|
|
|
if (parent == nsAutoMutationBatch::GetBatchTarget()) {
|
|
|
|
nsAutoMutationBatch::UpdateObserver(Observer(), wantsChildList);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMutationRecord* m =
|
|
|
|
Observer()->CurrentRecord(NS_LITERAL_STRING("childList"));
|
|
|
|
if (m->mTarget) {
|
|
|
|
// Already handled case.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m->mTarget = parent;
|
|
|
|
m->mAddedNodes = new nsSimpleContentList(parent);
|
|
|
|
m->mAddedNodes->AppendElement(aChild);
|
|
|
|
m->mPreviousSibling = aChild->GetPreviousSibling();
|
|
|
|
m->mNextSibling = aChild->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsMutationReceiver::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer,
|
2012-03-31 09:30:13 -07:00
|
|
|
nsIContent* aPreviousSibling)
|
|
|
|
{
|
2012-10-10 12:04:42 -07:00
|
|
|
if (aChild->ChromeOnlyAccess()) {
|
2012-03-31 09:30:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* parent = NODE_FROM(aContainer, aDocument);
|
|
|
|
if (nsAutoMutationBatch::IsBatching()) {
|
|
|
|
if (nsAutoMutationBatch::IsRemovalDone()) {
|
|
|
|
// This can happen for example if HTML parser parses to
|
|
|
|
// context node, but needs to move elements around.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (nsAutoMutationBatch::GetBatchTarget() != parent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wantsChildList = ChildList() && (Subtree() || parent == Target());
|
|
|
|
if (wantsChildList || Subtree()) {
|
|
|
|
nsAutoMutationBatch::NodeRemoved(aChild);
|
|
|
|
nsAutoMutationBatch::UpdateObserver(Observer(), wantsChildList);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Subtree()) {
|
|
|
|
// Try to avoid creating transient observer if the node
|
|
|
|
// already has an observer observing the same set of nodes.
|
|
|
|
nsMutationReceiver* orig = GetParent() ? GetParent() : this;
|
|
|
|
if (Observer()->GetReceiverFor(aChild, false) != orig) {
|
|
|
|
bool transientExists = false;
|
2012-07-30 07:20:58 -07:00
|
|
|
nsCOMArray<nsMutationReceiver>* transientReceivers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
Observer()->mTransientReceivers.Get(aChild, &transientReceivers);
|
|
|
|
if (!transientReceivers) {
|
|
|
|
transientReceivers = new nsCOMArray<nsMutationReceiver>();
|
|
|
|
Observer()->mTransientReceivers.Put(aChild, transientReceivers);
|
|
|
|
} else {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < transientReceivers->Count(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
nsMutationReceiver* r = transientReceivers->ObjectAt(i);
|
|
|
|
if (r->GetParent() == orig) {
|
|
|
|
transientExists = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!transientExists) {
|
|
|
|
// Make sure the elements which are removed from the
|
|
|
|
// subtree are kept in the same observation set.
|
|
|
|
transientReceivers->AppendObject(new nsMutationReceiver(aChild, orig));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ChildList() && (Subtree() || parent == Target())) {
|
|
|
|
nsDOMMutationRecord* m =
|
|
|
|
Observer()->CurrentRecord(NS_LITERAL_STRING("childList"));
|
|
|
|
if (m->mTarget) {
|
|
|
|
// Already handled case.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m->mTarget = parent;
|
|
|
|
m->mRemovedNodes = new nsSimpleContentList(parent);
|
|
|
|
m->mRemovedNodes->AppendElement(aChild);
|
|
|
|
m->mPreviousSibling = aPreviousSibling;
|
|
|
|
m->mNextSibling = parent->GetChildAt(aIndexInContainer);
|
|
|
|
}
|
|
|
|
// We need to schedule always, so that after microtask mTransientReceivers
|
|
|
|
// can be cleared correctly.
|
|
|
|
Observer()->ScheduleForRun();
|
|
|
|
}
|
|
|
|
|
2012-04-06 02:12:59 -07:00
|
|
|
void nsMutationReceiver::NodeWillBeDestroyed(const nsINode *aNode)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mParent, "Shouldn't have mParent here!");
|
|
|
|
Disconnect(true);
|
|
|
|
}
|
|
|
|
|
2012-03-31 09:30:13 -07:00
|
|
|
// Observer
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsDOMMutationObserver)
|
2012-08-28 08:25:19 -07:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
2012-12-18 06:50:52 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsDOMMutationObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsDOMMutationObserver)
|
|
|
|
|
2012-08-28 08:25:19 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsDOMMutationObserver)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsDOMMutationObserver)
|
2012-08-28 08:25:19 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < tmp->mReceivers.Count(); ++i) {
|
2012-04-04 10:36:40 -07:00
|
|
|
tmp->mReceivers[i]->Disconnect(false);
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
tmp->mReceivers.Clear();
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPendingMutations)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallback)
|
2012-03-31 09:30:13 -07:00
|
|
|
// No need to handle mTransientReceivers
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsDOMMutationObserver)
|
2012-08-28 08:25:19 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
2012-11-14 23:32:40 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mReceivers)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPendingMutations)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallback)
|
2012-03-31 09:30:13 -07:00
|
|
|
// No need to handle mTransientReceivers
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
nsMutationReceiver*
|
|
|
|
nsDOMMutationObserver::GetReceiverFor(nsINode* aNode, bool aMayCreate)
|
|
|
|
{
|
|
|
|
if (!aMayCreate && !aNode->MayHaveDOMMutationObserver()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < mReceivers.Count(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
if (mReceivers[i]->Target() == aNode) {
|
|
|
|
return mReceivers[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!aMayCreate) {
|
2012-07-30 07:20:58 -07:00
|
|
|
return nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsMutationReceiver* r = new nsMutationReceiver(aNode, this);
|
|
|
|
mReceivers.AppendObject(r);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-04-04 10:36:40 -07:00
|
|
|
void
|
|
|
|
nsDOMMutationObserver::RemoveReceiver(nsMutationReceiver* aReceiver)
|
|
|
|
{
|
|
|
|
mReceivers.RemoveObject(aReceiver);
|
|
|
|
}
|
|
|
|
|
2012-03-31 09:30:13 -07:00
|
|
|
void
|
|
|
|
nsDOMMutationObserver::GetAllSubtreeObserversFor(nsINode* aNode,
|
|
|
|
nsTArray<nsMutationReceiver*>&
|
|
|
|
aReceivers)
|
|
|
|
{
|
|
|
|
nsINode* n = aNode;
|
|
|
|
while (n) {
|
|
|
|
if (n->MayHaveDOMMutationObserver()) {
|
|
|
|
nsMutationReceiver* r = GetReceiverFor(n, false);
|
|
|
|
if (r && r->Subtree() && !aReceivers.Contains(r)) {
|
|
|
|
aReceivers.AppendElement(r);
|
|
|
|
// If we've found all the receivers the observer has,
|
|
|
|
// no need to search for more.
|
2012-08-22 08:56:38 -07:00
|
|
|
if (mReceivers.Count() == int32_t(aReceivers.Length())) {
|
2012-03-31 09:30:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
nsCOMArray<nsMutationReceiver>* transientReceivers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
if (mTransientReceivers.Get(n, &transientReceivers) && transientReceivers) {
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < transientReceivers->Count(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
nsMutationReceiver* r = transientReceivers->ObjectAt(i);
|
|
|
|
nsMutationReceiver* parent = r->GetParent();
|
|
|
|
if (r->Subtree() && parent && !aReceivers.Contains(parent)) {
|
|
|
|
aReceivers.AppendElement(parent);
|
|
|
|
}
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
if (mReceivers.Count() == int32_t(aReceivers.Length())) {
|
2012-03-31 09:30:13 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-10-09 05:31:24 -07:00
|
|
|
n = n->GetParentNode();
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMMutationObserver::ScheduleForRun()
|
|
|
|
{
|
|
|
|
nsDOMMutationObserver::AddCurrentlyHandlingObserver(this);
|
|
|
|
|
|
|
|
if (mWaitingForRun) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mWaitingForRun = true;
|
|
|
|
RescheduleForRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMMutationObserver::RescheduleForRun()
|
|
|
|
{
|
|
|
|
if (!sScheduledMutationObservers) {
|
2012-12-18 06:50:52 -08:00
|
|
|
sScheduledMutationObservers = new nsTArray<nsRefPtr<nsDOMMutationObserver> >;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool didInsert = false;
|
2012-12-18 06:50:52 -08:00
|
|
|
for (uint32_t i = 0; i < sScheduledMutationObservers->Length(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
if (static_cast<nsDOMMutationObserver*>((*sScheduledMutationObservers)[i])
|
|
|
|
->mId > mId) {
|
2012-12-18 06:50:52 -08:00
|
|
|
sScheduledMutationObservers->InsertElementAt(i, this);
|
2012-03-31 09:30:13 -07:00
|
|
|
didInsert = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!didInsert) {
|
2012-12-18 06:50:52 -08:00
|
|
|
sScheduledMutationObservers->AppendElement(this);
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
void
|
|
|
|
nsDOMMutationObserver::Observe(nsINode& aTarget,
|
|
|
|
const mozilla::dom::MutationObserverInit& aOptions,
|
|
|
|
mozilla::ErrorResult& aRv)
|
2012-03-31 09:30:13 -07:00
|
|
|
{
|
|
|
|
|
2012-12-18 08:56:45 -08:00
|
|
|
if (!(aOptions.mChildList || aOptions.mAttributes || aOptions.mCharacterData)) {
|
2012-12-18 06:50:52 -08:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-18 08:56:45 -08:00
|
|
|
if (aOptions.mAttributeOldValue && !aOptions.mAttributes) {
|
2012-12-18 06:50:52 -08:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2012-12-18 08:56:45 -08:00
|
|
|
if (aOptions.mCharacterDataOldValue && !aOptions.mCharacterData) {
|
2012-12-18 06:50:52 -08:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2012-03-31 09:30:13 -07:00
|
|
|
|
|
|
|
nsCOMArray<nsIAtom> filters;
|
|
|
|
bool allAttrs = true;
|
2012-12-18 08:56:45 -08:00
|
|
|
if (aOptions.mAttributeFilter.WasPassed()) {
|
2012-03-31 09:30:13 -07:00
|
|
|
allAttrs = false;
|
2012-12-18 06:50:52 -08:00
|
|
|
const mozilla::dom::Sequence<nsString>& filtersAsString =
|
2012-12-18 08:56:45 -08:00
|
|
|
aOptions.mAttributeFilter.Value();
|
2012-12-18 06:50:52 -08:00
|
|
|
uint32_t len = filtersAsString.Length();
|
|
|
|
|
2012-12-18 08:56:45 -08:00
|
|
|
if (len != 0 && !aOptions.mAttributes) {
|
2012-12-18 06:50:52 -08:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!filters.SetCapacity(len)) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
|
|
nsCOMPtr<nsIAtom> a = do_GetAtom(filtersAsString[i]);
|
|
|
|
filters.AppendObject(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMutationReceiver* r = GetReceiverFor(&aTarget, true);
|
2012-12-18 08:56:45 -08:00
|
|
|
r->SetChildList(aOptions.mChildList);
|
|
|
|
r->SetAttributes(aOptions.mAttributes);
|
|
|
|
r->SetCharacterData(aOptions.mCharacterData);
|
|
|
|
r->SetSubtree(aOptions.mSubtree);
|
|
|
|
r->SetAttributeOldValue(aOptions.mAttributeOldValue);
|
|
|
|
r->SetCharacterDataOldValue(aOptions.mCharacterDataOldValue);
|
2012-03-31 09:30:13 -07:00
|
|
|
r->SetAttributeFilter(filters);
|
|
|
|
r->SetAllAttributes(allAttrs);
|
|
|
|
r->RemoveClones();
|
2012-04-04 10:36:40 -07:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < mReceivers.Count(); ++i) {
|
2012-04-04 10:36:40 -07:00
|
|
|
NS_WARN_IF_FALSE(mReceivers[i]->Target(),
|
|
|
|
"All the receivers should have a target!");
|
|
|
|
}
|
|
|
|
#endif
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
void
|
2012-03-31 09:30:13 -07:00
|
|
|
nsDOMMutationObserver::Disconnect()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < mReceivers.Count(); ++i) {
|
2012-04-04 10:36:40 -07:00
|
|
|
mReceivers[i]->Disconnect(false);
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
mReceivers.Clear();
|
|
|
|
mCurrentMutations.Clear();
|
|
|
|
mPendingMutations.Clear();
|
2012-04-10 22:24:18 -07:00
|
|
|
}
|
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
void
|
|
|
|
nsDOMMutationObserver::TakeRecords(
|
|
|
|
nsTArray<nsRefPtr<nsDOMMutationRecord> >& aRetVal)
|
2012-04-10 22:24:18 -07:00
|
|
|
{
|
2012-12-18 06:50:52 -08:00
|
|
|
aRetVal.Clear();
|
|
|
|
mPendingMutations.SwapElements(aRetVal);
|
2012-04-10 22:24:18 -07:00
|
|
|
}
|
2012-03-31 09:30:13 -07:00
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
// static
|
|
|
|
already_AddRefed<nsDOMMutationObserver>
|
|
|
|
nsDOMMutationObserver::Constructor(nsISupports* aGlobal,
|
|
|
|
mozilla::dom::MutationCallback& aCb,
|
|
|
|
mozilla::ErrorResult& aRv)
|
2012-03-31 09:30:13 -07:00
|
|
|
{
|
2012-12-18 06:50:52 -08:00
|
|
|
nsCOMPtr<nsPIDOMWindow> window = do_QueryInterface(aGlobal);
|
|
|
|
if (!window) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
2012-12-18 06:50:52 -08:00
|
|
|
MOZ_ASSERT(window->IsInnerWindow());
|
|
|
|
nsRefPtr<nsDOMMutationObserver> observer =
|
|
|
|
new nsDOMMutationObserver(window.forget(), aCb);
|
|
|
|
return observer.forget();
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMMutationObserver::HandleMutation()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(nsContentUtils::IsSafeToRunScript(), "Whaat!");
|
2012-04-03 00:25:38 -07:00
|
|
|
NS_ASSERTION(mCurrentMutations.IsEmpty(),
|
2012-03-31 09:30:13 -07:00
|
|
|
"Still generating MutationRecords?");
|
|
|
|
|
|
|
|
mWaitingForRun = false;
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < mReceivers.Count(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
mReceivers[i]->RemoveClones();
|
|
|
|
}
|
|
|
|
mTransientReceivers.Clear();
|
|
|
|
|
|
|
|
nsPIDOMWindow* outer = mOwner->GetOuterWindow();
|
2012-12-18 06:50:52 -08:00
|
|
|
if (!mPendingMutations.Length() || !outer ||
|
2012-03-31 09:30:13 -07:00
|
|
|
outer->GetCurrentInnerWindow() != mOwner) {
|
|
|
|
mPendingMutations.Clear();
|
|
|
|
return;
|
|
|
|
}
|
2012-04-10 22:24:18 -07:00
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
nsTArray<nsRefPtr<nsDOMMutationRecord> > mutationsArray;
|
|
|
|
TakeRecords(mutationsArray);
|
|
|
|
mozilla::dom::Sequence<mozilla::dom::OwningNonNull<nsDOMMutationRecord> >
|
|
|
|
mutations;
|
|
|
|
uint32_t len = mutationsArray.Length();
|
|
|
|
NS_ENSURE_TRUE_VOID(mutations.SetCapacity(len));
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
|
|
|
*mutations.AppendElement() = mutationsArray[i].forget();
|
|
|
|
}
|
|
|
|
mozilla::ErrorResult rv;
|
|
|
|
mCallback->Call(this, mutations, *this, rv);
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
class AsyncMutationHandler : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
nsDOMMutationObserver::HandleMutations();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMMutationObserver::HandleMutationsInternal()
|
|
|
|
{
|
|
|
|
if (!nsContentUtils::IsSafeToRunScript()) {
|
|
|
|
nsContentUtils::AddScriptRunner(new AsyncMutationHandler());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
static nsRefPtr<nsDOMMutationObserver> sCurrentObserver;
|
|
|
|
if (sCurrentObserver && !sCurrentObserver->Suppressed()) {
|
|
|
|
// In normal cases sScheduledMutationObservers will be handled
|
|
|
|
// after previous mutations are handled. But in case some
|
|
|
|
// callback calls a sync API, which spins the eventloop, we need to still
|
|
|
|
// process other mutations happening during that sync call.
|
|
|
|
// This does *not* catch all cases, but should work for stuff running
|
|
|
|
// in separate tabs.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-12-18 06:50:52 -08:00
|
|
|
nsTArray<nsRefPtr<nsDOMMutationObserver> >* suppressedObservers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
|
|
|
|
while (sScheduledMutationObservers) {
|
2012-12-18 06:50:52 -08:00
|
|
|
nsTArray<nsRefPtr<nsDOMMutationObserver> >* observers = sScheduledMutationObservers;
|
2012-07-30 07:20:58 -07:00
|
|
|
sScheduledMutationObservers = nullptr;
|
2012-12-18 06:50:52 -08:00
|
|
|
for (uint32_t i = 0; i < observers->Length(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
sCurrentObserver = static_cast<nsDOMMutationObserver*>((*observers)[i]);
|
|
|
|
if (!sCurrentObserver->Suppressed()) {
|
|
|
|
sCurrentObserver->HandleMutation();
|
|
|
|
} else {
|
|
|
|
if (!suppressedObservers) {
|
2012-12-18 06:50:52 -08:00
|
|
|
suppressedObservers = new nsTArray<nsRefPtr<nsDOMMutationObserver> >;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
2012-12-18 06:50:52 -08:00
|
|
|
if (!suppressedObservers->Contains(sCurrentObserver)) {
|
|
|
|
suppressedObservers->AppendElement(sCurrentObserver);
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete observers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (suppressedObservers) {
|
2012-12-18 06:50:52 -08:00
|
|
|
for (uint32_t i = 0; i < suppressedObservers->Length(); ++i) {
|
|
|
|
static_cast<nsDOMMutationObserver*>(suppressedObservers->ElementAt(i))->
|
2012-03-31 09:30:13 -07:00
|
|
|
RescheduleForRun();
|
|
|
|
}
|
|
|
|
delete suppressedObservers;
|
2012-07-30 07:20:58 -07:00
|
|
|
suppressedObservers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
2012-07-30 07:20:58 -07:00
|
|
|
sCurrentObserver = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMutationRecord*
|
|
|
|
nsDOMMutationObserver::CurrentRecord(const nsAString& aType)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sMutationLevel > 0, "Unexpected mutation level!");
|
|
|
|
|
|
|
|
while (mCurrentMutations.Length() < sMutationLevel) {
|
2012-07-30 07:20:58 -07:00
|
|
|
mCurrentMutations.AppendElement(static_cast<nsDOMMutationRecord*>(nullptr));
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t last = sMutationLevel - 1;
|
2012-03-31 09:30:13 -07:00
|
|
|
if (!mCurrentMutations[last]) {
|
2012-12-18 06:50:52 -08:00
|
|
|
nsDOMMutationRecord* r = new nsDOMMutationRecord(aType, GetParentObject());
|
2012-03-31 09:30:13 -07:00
|
|
|
mCurrentMutations[last] = r;
|
2012-12-18 06:50:52 -08:00
|
|
|
mPendingMutations.AppendElement(r);
|
2012-03-31 09:30:13 -07:00
|
|
|
ScheduleForRun();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(mCurrentMutations[last]->mType.Equals(aType),
|
|
|
|
"Unexpected MutationRecord type!");
|
|
|
|
|
|
|
|
return mCurrentMutations[last];
|
|
|
|
}
|
|
|
|
|
|
|
|
nsDOMMutationObserver::~nsDOMMutationObserver()
|
|
|
|
{
|
2012-08-22 08:56:38 -07:00
|
|
|
for (int32_t i = 0; i < mReceivers.Count(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
mReceivers[i]->RemoveClones();
|
|
|
|
}
|
2012-12-18 06:50:52 -08:00
|
|
|
}
|
2012-03-31 09:30:13 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMMutationObserver::EnterMutationHandling()
|
|
|
|
{
|
|
|
|
++sMutationLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Leave the current mutation level (there can be several levels if in case
|
|
|
|
// of nested calls to the nsIMutationObserver methods).
|
|
|
|
// The most recent mutation record is removed from mCurrentMutations, so
|
|
|
|
// that is doesn't get modified anymore by receivers.
|
|
|
|
void
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling()
|
|
|
|
{
|
|
|
|
if (sCurrentlyHandlingObservers &&
|
|
|
|
sCurrentlyHandlingObservers->Length() == sMutationLevel) {
|
2012-12-18 06:50:52 -08:00
|
|
|
nsTArray<nsRefPtr<nsDOMMutationObserver> >& obs =
|
2012-03-31 09:30:13 -07:00
|
|
|
sCurrentlyHandlingObservers->ElementAt(sMutationLevel - 1);
|
2012-12-18 06:50:52 -08:00
|
|
|
for (uint32_t i = 0; i < obs.Length(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
nsDOMMutationObserver* o =
|
|
|
|
static_cast<nsDOMMutationObserver*>(obs[i]);
|
|
|
|
if (o->mCurrentMutations.Length() == sMutationLevel) {
|
|
|
|
// It is already in pending mutations.
|
|
|
|
o->mCurrentMutations.RemoveElementAt(sMutationLevel - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sCurrentlyHandlingObservers->RemoveElementAt(sMutationLevel - 1);
|
|
|
|
}
|
|
|
|
--sMutationLevel;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMMutationObserver::AddCurrentlyHandlingObserver(nsDOMMutationObserver* aObserver)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(sMutationLevel > 0, "Unexpected mutation level!");
|
|
|
|
|
|
|
|
if (!sCurrentlyHandlingObservers) {
|
|
|
|
sCurrentlyHandlingObservers =
|
2012-12-18 06:50:52 -08:00
|
|
|
new nsAutoTArray<nsTArray<nsRefPtr<nsDOMMutationObserver> >, 4>;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
while (sCurrentlyHandlingObservers->Length() < sMutationLevel) {
|
|
|
|
sCurrentlyHandlingObservers->InsertElementAt(
|
|
|
|
sCurrentlyHandlingObservers->Length());
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t last = sMutationLevel - 1;
|
2012-12-18 06:50:52 -08:00
|
|
|
if (!sCurrentlyHandlingObservers->ElementAt(last).Contains(aObserver)) {
|
|
|
|
sCurrentlyHandlingObservers->ElementAt(last).AppendElement(aObserver);
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsDOMMutationObserver::Shutdown()
|
|
|
|
{
|
|
|
|
delete sCurrentlyHandlingObservers;
|
2012-07-30 07:20:58 -07:00
|
|
|
sCurrentlyHandlingObservers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
delete sScheduledMutationObservers;
|
2012-07-30 07:20:58 -07:00
|
|
|
sScheduledMutationObservers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoMutationBatch*
|
2012-07-30 07:20:58 -07:00
|
|
|
nsAutoMutationBatch::sCurrentBatch = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
nsAutoMutationBatch::Done()
|
|
|
|
{
|
|
|
|
if (sCurrentBatch != this) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
sCurrentBatch = mPreviousBatch;
|
|
|
|
if (mObservers.IsEmpty()) {
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling();
|
|
|
|
// Nothing to do.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t len = mObservers.Length();
|
|
|
|
for (uint32_t i = 0; i < len; ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
nsDOMMutationObserver* ob = mObservers[i].mObserver;
|
|
|
|
bool wantsChildList = mObservers[i].mWantsChildList;
|
|
|
|
|
|
|
|
nsRefPtr<nsSimpleContentList> removedList;
|
|
|
|
if (wantsChildList) {
|
|
|
|
removedList = new nsSimpleContentList(mBatchTarget);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsMutationReceiver*> allObservers;
|
|
|
|
ob->GetAllSubtreeObserversFor(mBatchTarget, allObservers);
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t j = mFromFirstToLast ? 0 : mRemovedNodes.Length() - 1;
|
|
|
|
int32_t end = mFromFirstToLast ? mRemovedNodes.Length() : -1;
|
2012-03-31 09:30:13 -07:00
|
|
|
for (; j != end; mFromFirstToLast ? ++j : --j) {
|
|
|
|
nsCOMPtr<nsIContent> removed = mRemovedNodes[j];
|
|
|
|
if (removedList) {
|
|
|
|
removedList->AppendElement(removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (allObservers.Length()) {
|
2012-07-30 07:20:58 -07:00
|
|
|
nsCOMArray<nsMutationReceiver>* transientReceivers = nullptr;
|
2012-03-31 09:30:13 -07:00
|
|
|
ob->mTransientReceivers.Get(removed, &transientReceivers);
|
|
|
|
if (!transientReceivers) {
|
|
|
|
transientReceivers = new nsCOMArray<nsMutationReceiver>();
|
|
|
|
ob->mTransientReceivers.Put(removed, transientReceivers);
|
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t k = 0; k < allObservers.Length(); ++k) {
|
2012-03-31 09:30:13 -07:00
|
|
|
nsMutationReceiver* r = allObservers[k];
|
|
|
|
nsMutationReceiver* orig = r->GetParent() ? r->GetParent() : r;
|
|
|
|
if (ob->GetReceiverFor(removed, false) != orig) {
|
|
|
|
// Make sure the elements which are removed from the
|
|
|
|
// subtree are kept in the same observation set.
|
|
|
|
transientReceivers->AppendObject(new nsMutationReceiver(removed, orig));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (wantsChildList && (mRemovedNodes.Length() || mAddedNodes.Length())) {
|
|
|
|
nsRefPtr<nsSimpleContentList> addedList =
|
|
|
|
new nsSimpleContentList(mBatchTarget);
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mAddedNodes.Length(); ++i) {
|
2012-03-31 09:30:13 -07:00
|
|
|
addedList->AppendElement(mAddedNodes[i]);
|
|
|
|
}
|
|
|
|
nsDOMMutationRecord* m =
|
2012-12-18 06:50:52 -08:00
|
|
|
new nsDOMMutationRecord(NS_LITERAL_STRING("childList"),
|
|
|
|
ob->GetParentObject());
|
|
|
|
ob->mPendingMutations.AppendElement(m);
|
2012-03-31 09:30:13 -07:00
|
|
|
m->mTarget = mBatchTarget;
|
|
|
|
m->mRemovedNodes = removedList;
|
|
|
|
m->mAddedNodes = addedList;
|
|
|
|
m->mPreviousSibling = mPrevSibling;
|
|
|
|
m->mNextSibling = mNextSibling;
|
|
|
|
}
|
2012-04-03 23:22:16 -07:00
|
|
|
// Always schedule the observer so that transient receivers are
|
|
|
|
// removed correctly.
|
|
|
|
ob->ScheduleForRun();
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling();
|
|
|
|
}
|