2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2010-11-15 17:21:25 -08:00
|
|
|
/* vim: set ts=2 sw=2 et tw=99: */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsNodeUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsINode.h"
|
|
|
|
#include "nsIContent.h"
|
2010-05-05 11:18:05 -07:00
|
|
|
#include "mozilla/dom/Element.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIMutationObserver.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMUserDataHandler.h"
|
2011-06-23 19:18:01 -07:00
|
|
|
#include "nsEventListenerManager.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#include "nsIXPConnect.h"
|
|
|
|
#include "nsGenericElement.h"
|
|
|
|
#include "pldhash.h"
|
|
|
|
#include "nsIDOMAttr.h"
|
|
|
|
#include "nsCOMArray.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2009-09-10 06:23:40 -07:00
|
|
|
#include "nsDocument.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef MOZ_XUL
|
|
|
|
#include "nsXULElement.h"
|
|
|
|
#endif
|
2008-09-05 08:45:03 -07:00
|
|
|
#include "nsBindingManager.h"
|
2009-09-30 15:56:50 -07:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2009-10-29 16:55:05 -07:00
|
|
|
#ifdef MOZ_MEDIA
|
2009-10-01 07:10:13 -07:00
|
|
|
#include "nsHTMLMediaElement.h"
|
2009-10-29 16:55:05 -07:00
|
|
|
#endif // MOZ_MEDIA
|
2010-08-26 18:30:03 -07:00
|
|
|
#include "nsImageLoadingContent.h"
|
2010-10-10 15:42:09 -07:00
|
|
|
#include "jsgc.h"
|
2011-05-26 12:58:35 -07:00
|
|
|
#include "nsWrapperCacheInlines.h"
|
2012-01-31 13:55:54 -08:00
|
|
|
#include "nsObjectLoadingContent.h"
|
2012-03-31 09:30:13 -07:00
|
|
|
#include "nsDOMMutationObserver.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2007-12-04 10:37:54 -08:00
|
|
|
// This macro expects the ownerDocument of content_ to be in scope as
|
|
|
|
// |nsIDocument* doc|
|
2007-03-22 10:30:00 -07:00
|
|
|
#define IMPL_MUTATION_NOTIFICATION(func_, content_, params_) \
|
|
|
|
PR_BEGIN_MACRO \
|
2012-03-31 09:30:13 -07:00
|
|
|
bool needsEnterLeave = doc->MayHaveDOMMutationObservers(); \
|
|
|
|
if (needsEnterLeave) { \
|
|
|
|
nsDOMMutationObserver::EnterMutationHandling(); \
|
|
|
|
} \
|
2007-03-22 10:30:00 -07:00
|
|
|
nsINode* node = content_; \
|
2012-03-31 09:30:13 -07:00
|
|
|
NS_ASSERTION(node->OwnerDoc() == doc, "Bogus document"); \
|
2007-12-04 10:37:54 -08:00
|
|
|
if (doc) { \
|
|
|
|
static_cast<nsIMutationObserver*>(doc->BindingManager())-> \
|
|
|
|
func_ params_; \
|
|
|
|
} \
|
2007-03-22 10:30:00 -07:00
|
|
|
do { \
|
|
|
|
nsINode::nsSlots* slots = node->GetExistingSlots(); \
|
|
|
|
if (slots && !slots->mMutationObservers.IsEmpty()) { \
|
|
|
|
/* No need to explicitly notify the first observer first \
|
|
|
|
since that'll happen anyway. */ \
|
|
|
|
NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS( \
|
|
|
|
slots->mMutationObservers, nsIMutationObserver, \
|
|
|
|
func_, params_); \
|
|
|
|
} \
|
|
|
|
node = node->GetNodeParent(); \
|
|
|
|
} while (node); \
|
2012-03-31 09:30:13 -07:00
|
|
|
if (needsEnterLeave) { \
|
|
|
|
nsDOMMutationObserver::LeaveMutationHandling(); \
|
|
|
|
} \
|
2007-03-22 10:30:00 -07:00
|
|
|
PR_END_MACRO
|
|
|
|
|
2007-09-05 01:22:17 -07:00
|
|
|
void
|
|
|
|
nsNodeUtils::CharacterDataWillChange(nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2010-07-21 08:33:32 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataWillChange, aContent,
|
2007-09-05 01:22:17 -07:00
|
|
|
(doc, aContent, aInfo));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
|
|
|
nsNodeUtils::CharacterDataChanged(nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
2010-07-21 08:33:32 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(CharacterDataChanged, aContent,
|
2007-03-22 10:30:00 -07:00
|
|
|
(doc, aContent, aInfo));
|
|
|
|
}
|
|
|
|
|
2009-06-29 11:36:25 -07:00
|
|
|
void
|
2010-08-24 00:06:20 -07:00
|
|
|
nsNodeUtils::AttributeWillChange(Element* aElement,
|
2009-06-29 11:36:25 -07:00
|
|
|
PRInt32 aNameSpaceID,
|
|
|
|
nsIAtom* aAttribute,
|
|
|
|
PRInt32 aModType)
|
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
2010-08-24 00:06:20 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeWillChange, aElement,
|
|
|
|
(doc, aElement, aNameSpaceID, aAttribute,
|
2009-06-29 11:36:25 -07:00
|
|
|
aModType));
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2010-08-24 00:06:07 -07:00
|
|
|
nsNodeUtils::AttributeChanged(Element* aElement,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 aNameSpaceID,
|
|
|
|
nsIAtom* aAttribute,
|
2009-12-10 14:36:04 -08:00
|
|
|
PRInt32 aModType)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aElement->OwnerDoc();
|
2010-08-24 00:05:56 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(AttributeChanged, aElement,
|
|
|
|
(doc, aElement, aNameSpaceID, aAttribute,
|
2009-12-10 14:36:04 -08:00
|
|
|
aModType));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeUtils::ContentAppended(nsIContent* aContainer,
|
2010-05-10 18:12:34 -07:00
|
|
|
nsIContent* aFirstNewContent,
|
2007-03-22 10:30:00 -07:00
|
|
|
PRInt32 aNewIndexInContainer)
|
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-07-21 08:33:32 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentAppended, aContainer,
|
2010-05-10 18:12:34 -07:00
|
|
|
(doc, aContainer, aFirstNewContent,
|
|
|
|
aNewIndexInContainer));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeUtils::ContentInserted(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
|
|
|
PRInt32 aIndexInContainer)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aContainer->IsNodeOfType(nsINode::eCONTENT) ||
|
|
|
|
aContainer->IsNodeOfType(nsINode::eDOCUMENT),
|
|
|
|
"container must be an nsIContent or an nsIDocument");
|
|
|
|
nsIContent* container;
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDocument* document;
|
|
|
|
if (aContainer->IsNodeOfType(nsINode::eCONTENT)) {
|
2007-07-08 00:08:04 -07:00
|
|
|
container = static_cast<nsIContent*>(aContainer);
|
2007-12-04 10:37:54 -08:00
|
|
|
document = doc;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
container = nsnull;
|
2007-07-08 00:08:04 -07:00
|
|
|
document = static_cast<nsIDocument*>(aContainer);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 08:33:32 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentInserted, aContainer,
|
2007-03-22 10:30:00 -07:00
|
|
|
(document, container, aChild, aIndexInContainer));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsNodeUtils::ContentRemoved(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
2010-07-21 15:05:17 -07:00
|
|
|
PRInt32 aIndexInContainer,
|
|
|
|
nsIContent* aPreviousSibling)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aContainer->IsNodeOfType(nsINode::eCONTENT) ||
|
|
|
|
aContainer->IsNodeOfType(nsINode::eDOCUMENT),
|
|
|
|
"container must be an nsIContent or an nsIDocument");
|
|
|
|
nsIContent* container;
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* doc = aContainer->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIDocument* document;
|
|
|
|
if (aContainer->IsNodeOfType(nsINode::eCONTENT)) {
|
2007-07-08 00:08:04 -07:00
|
|
|
container = static_cast<nsIContent*>(aContainer);
|
2007-12-04 10:37:54 -08:00
|
|
|
document = doc;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
container = nsnull;
|
2007-07-08 00:08:04 -07:00
|
|
|
document = static_cast<nsIDocument*>(aContainer);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2010-07-21 08:37:41 -07:00
|
|
|
IMPL_MUTATION_NOTIFICATION(ContentRemoved, aContainer,
|
2010-07-21 15:05:17 -07:00
|
|
|
(document, container, aChild, aIndexInContainer,
|
|
|
|
aPreviousSibling));
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2007-05-12 08:36:28 -07:00
|
|
|
nsNodeUtils::LastRelease(nsINode* aNode)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsINode::nsSlots* slots = aNode->GetExistingSlots();
|
|
|
|
if (slots) {
|
|
|
|
if (!slots->mMutationObservers.IsEmpty()) {
|
2010-07-21 08:33:32 -07:00
|
|
|
NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS(slots->mMutationObservers,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIMutationObserver,
|
|
|
|
NodeWillBeDestroyed, (aNode));
|
|
|
|
}
|
|
|
|
|
2008-02-02 15:41:24 -08:00
|
|
|
delete slots;
|
2011-04-07 19:29:49 -07:00
|
|
|
aNode->mSlots = nsnull;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Kill properties first since that may run external code, so we want to
|
|
|
|
// be in as complete state as possible at that time.
|
2007-05-12 08:36:28 -07:00
|
|
|
if (aNode->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// Delete all properties before tearing down the document. Some of the
|
|
|
|
// properties are bound to nsINode objects and the destructor functions of
|
|
|
|
// the properties may want to use the owner document of the nsINode.
|
2010-04-22 19:41:38 -07:00
|
|
|
static_cast<nsIDocument*>(aNode)->DeleteAllProperties();
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
2009-09-30 15:56:50 -07:00
|
|
|
else {
|
|
|
|
if (aNode->HasProperties()) {
|
|
|
|
// Strong reference to the document so that deleting properties can't
|
|
|
|
// delete the document.
|
2011-10-18 03:53:36 -07:00
|
|
|
nsCOMPtr<nsIDocument> document = aNode->OwnerDoc();
|
2011-10-18 04:19:44 -07:00
|
|
|
document->DeleteAllPropertiesFor(aNode);
|
2009-09-30 15:56:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// I wonder whether it's faster to do the HasFlag check first....
|
|
|
|
if (aNode->IsNodeOfType(nsINode::eHTML_FORM_CONTROL) &&
|
|
|
|
aNode->HasFlag(ADDED_TO_FORM)) {
|
|
|
|
// Tell the form (if any) this node is going away. Don't
|
|
|
|
// notify, since we're being destroyed in any case.
|
2011-10-17 07:59:28 -07:00
|
|
|
static_cast<nsGenericHTMLFormElement*>(aNode)->ClearForm(true);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
2007-05-12 08:36:28 -07:00
|
|
|
aNode->UnsetFlags(NODE_HAS_PROPERTIES);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-02-27 06:03:15 -08:00
|
|
|
if (aNode->NodeType() != nsIDOMNode::DOCUMENT_NODE &&
|
|
|
|
aNode->HasFlag(NODE_HAS_LISTENERMANAGER)) {
|
2007-03-22 10:30:00 -07:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (nsContentUtils::IsInitialized()) {
|
2011-06-23 19:18:01 -07:00
|
|
|
nsEventListenerManager* manager =
|
2011-10-17 07:59:28 -07:00
|
|
|
nsContentUtils::GetListenerManager(aNode, false);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!manager) {
|
|
|
|
NS_ERROR("Huh, our bit says we have a listener manager list, "
|
|
|
|
"but there's nothing in the hash!?!!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
nsContentUtils::RemoveListenerManager(aNode);
|
|
|
|
aNode->UnsetFlags(NODE_HAS_LISTENERMANAGER);
|
|
|
|
}
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
if (aNode->IsElement()) {
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* ownerDoc = aNode->OwnerDoc();
|
2010-05-26 15:39:13 -07:00
|
|
|
Element* elem = aNode->AsElement();
|
2011-10-18 04:19:44 -07:00
|
|
|
ownerDoc->ClearBoxObjectFor(elem);
|
2010-05-26 15:39:13 -07:00
|
|
|
|
|
|
|
NS_ASSERTION(aNode->HasFlag(NODE_FORCE_XBL_BINDINGS) ||
|
|
|
|
!ownerDoc->BindingManager() ||
|
2010-05-26 15:59:23 -07:00
|
|
|
!ownerDoc->BindingManager()->GetBinding(elem),
|
2010-05-26 15:39:13 -07:00
|
|
|
"Non-forced node has binding on destruction");
|
|
|
|
|
|
|
|
// if NODE_FORCE_XBL_BINDINGS is set, the node might still have a binding
|
|
|
|
// attached
|
|
|
|
if (aNode->HasFlag(NODE_FORCE_XBL_BINDINGS) &&
|
2011-10-18 04:19:44 -07:00
|
|
|
ownerDoc->BindingManager()) {
|
2010-06-03 18:09:08 -07:00
|
|
|
ownerDoc->BindingManager()->RemovedFromDocument(elem, ownerDoc);
|
2008-02-14 12:45:07 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-12 13:20:42 -07:00
|
|
|
nsContentUtils::ReleaseWrapper(aNode, aNode);
|
|
|
|
|
2008-02-02 15:41:24 -08:00
|
|
|
delete aNode;
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
|
|
|
|
2008-06-30 18:03:50 -07:00
|
|
|
struct NS_STACK_CLASS nsHandlerData
|
2007-05-12 08:36:28 -07:00
|
|
|
{
|
|
|
|
PRUint16 mOperation;
|
2008-06-30 18:03:50 -07:00
|
|
|
nsCOMPtr<nsIDOMNode> mSource;
|
|
|
|
nsCOMPtr<nsIDOMNode> mDest;
|
2010-04-27 02:51:28 -07:00
|
|
|
nsCxPusher mPusher;
|
2007-05-12 08:36:28 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
CallHandler(void *aObject, nsIAtom *aKey, void *aHandler, void *aData)
|
|
|
|
{
|
2007-07-08 00:08:04 -07:00
|
|
|
nsHandlerData *handlerData = static_cast<nsHandlerData*>(aData);
|
2007-05-12 08:36:28 -07:00
|
|
|
nsCOMPtr<nsIDOMUserDataHandler> handler =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsIDOMUserDataHandler*>(aHandler);
|
|
|
|
nsINode *node = static_cast<nsINode*>(aObject);
|
2007-05-12 08:36:28 -07:00
|
|
|
nsCOMPtr<nsIVariant> data =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsIVariant*>(node->GetProperty(DOM_USER_DATA, aKey));
|
2007-05-12 08:36:28 -07:00
|
|
|
NS_ASSERTION(data, "Handler without data?");
|
|
|
|
|
2010-04-27 02:51:28 -07:00
|
|
|
if (!handlerData->mPusher.RePush(node)) {
|
|
|
|
return;
|
|
|
|
}
|
2007-05-12 08:36:28 -07:00
|
|
|
nsAutoString key;
|
|
|
|
aKey->ToString(key);
|
|
|
|
handler->Handle(handlerData->mOperation, key, data, handlerData->mSource,
|
|
|
|
handlerData->mDest);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
|
|
|
nsNodeUtils::CallUserDataHandlers(nsCOMArray<nsINode> &aNodesWithProperties,
|
|
|
|
nsIDocument *aOwnerDocument,
|
2011-09-28 23:19:26 -07:00
|
|
|
PRUint16 aOperation, bool aCloned)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(!aCloned || (aNodesWithProperties.Count() % 2 == 0),
|
|
|
|
"Expected aNodesWithProperties to contain original and "
|
|
|
|
"cloned nodes.");
|
|
|
|
|
2011-05-09 12:33:03 -07:00
|
|
|
if (!nsContentUtils::IsSafeToRunScript()) {
|
|
|
|
if (nsContentUtils::IsChromeDoc(aOwnerDocument)) {
|
|
|
|
NS_WARNING("Fix the caller! Userdata callback disabled.");
|
|
|
|
} else {
|
|
|
|
NS_ERROR("This is unsafe! Fix the caller! Userdata callback disabled.");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-04-22 19:41:38 -07:00
|
|
|
nsPropertyTable *table = aOwnerDocument->PropertyTable(DOM_USER_DATA_HANDLER);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
// Keep the document alive, just in case one of the handlers causes it to go
|
|
|
|
// away.
|
|
|
|
nsCOMPtr<nsIDocument> ownerDoc = aOwnerDocument;
|
|
|
|
|
|
|
|
nsHandlerData handlerData;
|
|
|
|
handlerData.mOperation = aOperation;
|
|
|
|
|
|
|
|
PRUint32 i, count = aNodesWithProperties.Count();
|
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
nsINode *nodeWithProperties = aNodesWithProperties[i];
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
handlerData.mSource = do_QueryInterface(nodeWithProperties, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (aCloned) {
|
|
|
|
handlerData.mDest = do_QueryInterface(aNodesWithProperties[++i], &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2010-04-22 19:41:38 -07:00
|
|
|
table->Enumerate(nodeWithProperties, CallHandler, &handlerData);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-05-12 08:36:28 -07:00
|
|
|
static void
|
|
|
|
NoteUserData(void *aObject, nsIAtom *aKey, void *aXPCOMChild, void *aData)
|
|
|
|
{
|
|
|
|
nsCycleCollectionTraversalCallback* cb =
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsCycleCollectionTraversalCallback*>(aData);
|
2008-03-17 16:11:08 -07:00
|
|
|
NS_CYCLE_COLLECTION_NOTE_EDGE_NAME(*cb, "[user data (or handler)]");
|
2007-07-08 00:08:04 -07:00
|
|
|
cb->NoteXPCOMChild(static_cast<nsISupports*>(aXPCOMChild));
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsNodeUtils::TraverseUserData(nsINode* aNode,
|
|
|
|
nsCycleCollectionTraversalCallback &aCb)
|
|
|
|
{
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* ownerDoc = aNode->OwnerDoc();
|
2010-04-22 19:41:38 -07:00
|
|
|
ownerDoc->PropertyTable(DOM_USER_DATA)->Enumerate(aNode, NoteUserData, &aCb);
|
|
|
|
ownerDoc->PropertyTable(DOM_USER_DATA_HANDLER)->Enumerate(aNode, NoteUserData, &aCb);
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/* static */
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNodeUtils::CloneNodeImpl(nsINode *aNode, bool aDeep,
|
|
|
|
bool aCallUserDataHandlers,
|
2010-12-08 10:52:39 -08:00
|
|
|
nsIDOMNode **aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
*aResult = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> newNode;
|
|
|
|
nsCOMArray<nsINode> nodesWithProperties;
|
|
|
|
nsresult rv = Clone(aNode, aDeep, nsnull, nodesWithProperties,
|
|
|
|
getter_AddRefs(newNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-18 04:19:44 -07:00
|
|
|
if (aCallUserDataHandlers) {
|
|
|
|
rv = CallUserDataHandlers(nodesWithProperties, aNode->OwnerDoc(),
|
2011-10-17 07:59:28 -07:00
|
|
|
nsIDOMUserDataHandler::NODE_CLONED, true);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
newNode.swap(*aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
2011-09-28 23:19:26 -07:00
|
|
|
nsNodeUtils::CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNodeInfoManager *aNewNodeInfoManager,
|
2010-12-03 08:43:32 -08:00
|
|
|
JSContext *aCx, JSObject *aNewScope,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMArray<nsINode> &aNodesWithProperties,
|
2009-09-28 13:33:29 -07:00
|
|
|
nsINode *aParent, nsINode **aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_PRECONDITION((!aClone && aNewNodeInfoManager) || !aCx,
|
|
|
|
"If cloning or not getting a new nodeinfo we shouldn't "
|
|
|
|
"rewrap");
|
2010-12-03 08:43:32 -08:00
|
|
|
NS_PRECONDITION(!aCx || aNewScope, "Must have new scope");
|
2009-09-28 13:33:29 -07:00
|
|
|
NS_PRECONDITION(!aParent || aNode->IsNodeOfType(nsINode::eCONTENT),
|
|
|
|
"Can't insert document or attribute nodes into a parent");
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
*aResult = nsnull;
|
|
|
|
|
|
|
|
// First deal with aNode and walk its attributes (and their children). Then,
|
2011-10-17 07:59:28 -07:00
|
|
|
// if aDeep is true, deal with aNode's children (and recurse into their
|
2007-03-22 10:30:00 -07:00
|
|
|
// attributes and children).
|
|
|
|
|
|
|
|
nsresult rv;
|
2010-12-03 08:43:32 -08:00
|
|
|
JSObject *wrapper;
|
|
|
|
if (aCx && (wrapper = aNode->GetWrapper())) {
|
2010-11-15 17:21:25 -08:00
|
|
|
rv = xpc_MorphSlimWrapper(aCx, aNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNodeInfoManager *nodeInfoManager = aNewNodeInfoManager;
|
|
|
|
|
|
|
|
// aNode.
|
|
|
|
nsINodeInfo *nodeInfo = aNode->mNodeInfo;
|
|
|
|
nsCOMPtr<nsINodeInfo> newNodeInfo;
|
|
|
|
if (nodeInfoManager) {
|
2008-10-13 09:12:26 -07:00
|
|
|
|
|
|
|
// Don't allow importing/adopting nodes from non-privileged "scriptable"
|
|
|
|
// documents to "non-scriptable" documents.
|
|
|
|
nsIDocument* newDoc = nodeInfoManager->GetDocument();
|
2008-12-03 02:25:21 -08:00
|
|
|
NS_ENSURE_STATE(newDoc);
|
2011-09-28 23:19:26 -07:00
|
|
|
bool hasHadScriptHandlingObject = false;
|
2008-10-13 09:12:26 -07:00
|
|
|
if (!newDoc->GetScriptHandlingObject(hasHadScriptHandlingObject) &&
|
|
|
|
!hasHadScriptHandlingObject) {
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* currentDoc = aNode->OwnerDoc();
|
2011-10-18 04:19:44 -07:00
|
|
|
NS_ENSURE_STATE((nsContentUtils::IsChromeDoc(currentDoc) ||
|
2008-12-03 02:25:21 -08:00
|
|
|
(!currentDoc->GetScriptHandlingObject(hasHadScriptHandlingObject) &&
|
|
|
|
!hasHadScriptHandlingObject)));
|
2008-10-13 09:12:26 -07:00
|
|
|
}
|
|
|
|
|
2008-09-12 15:32:18 -07:00
|
|
|
newNodeInfo = nodeInfoManager->GetNodeInfo(nodeInfo->NameAtom(),
|
|
|
|
nodeInfo->GetPrefixAtom(),
|
2011-06-14 00:56:49 -07:00
|
|
|
nodeInfo->NamespaceID(),
|
|
|
|
nodeInfo->NodeType(),
|
|
|
|
nodeInfo->GetExtraName());
|
2008-09-25 15:46:52 -07:00
|
|
|
NS_ENSURE_TRUE(newNodeInfo, NS_ERROR_OUT_OF_MEMORY);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nodeInfo = newNodeInfo;
|
|
|
|
}
|
|
|
|
|
2010-04-30 06:12:05 -07:00
|
|
|
nsGenericElement *elem = aNode->IsElement() ?
|
2007-07-08 00:08:04 -07:00
|
|
|
static_cast<nsGenericElement*>(aNode) :
|
2007-03-22 10:30:00 -07:00
|
|
|
nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> clone;
|
|
|
|
if (aClone) {
|
|
|
|
rv = aNode->Clone(nodeInfo, getter_AddRefs(clone));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (aParent) {
|
|
|
|
// If we're cloning we need to insert the cloned children into the cloned
|
|
|
|
// parent.
|
2009-09-28 13:33:29 -07:00
|
|
|
rv = aParent->AppendChildTo(static_cast<nsIContent*>(clone.get()),
|
2011-10-17 07:59:28 -07:00
|
|
|
false);
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
else if (aDeep && clone->IsNodeOfType(nsINode::eDOCUMENT)) {
|
|
|
|
// After cloning the document itself, we want to clone the children into
|
|
|
|
// the cloned document (somewhat like cloning and importing them into the
|
|
|
|
// cloned document).
|
|
|
|
nodeInfoManager = clone->mNodeInfo->NodeInfoManager();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (nodeInfoManager) {
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* oldDoc = aNode->OwnerDoc();
|
2011-09-28 23:19:26 -07:00
|
|
|
bool wasRegistered = false;
|
2011-10-18 04:19:44 -07:00
|
|
|
if (aNode->IsElement()) {
|
2010-04-30 06:12:05 -07:00
|
|
|
Element* element = aNode->AsElement();
|
|
|
|
oldDoc->ClearBoxObjectFor(element);
|
|
|
|
wasRegistered = oldDoc->UnregisterFreezableElement(element);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
aNode->mNodeInfo.swap(newNodeInfo);
|
2010-09-01 15:48:24 -07:00
|
|
|
if (elem) {
|
|
|
|
elem->NodeInfoChanged(newNodeInfo);
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-10-18 03:53:36 -07:00
|
|
|
nsIDocument* newDoc = aNode->OwnerDoc();
|
2007-03-22 10:30:00 -07:00
|
|
|
if (newDoc) {
|
2009-10-01 07:10:13 -07:00
|
|
|
// XXX what if oldDoc is null, we don't know if this should be
|
|
|
|
// registered or not! Can that really happen?
|
2009-05-07 18:32:32 -07:00
|
|
|
if (wasRegistered) {
|
2010-04-30 06:12:05 -07:00
|
|
|
newDoc->RegisterFreezableElement(aNode->AsElement());
|
2009-05-07 18:32:32 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsPIDOMWindow* window = newDoc->GetInnerWindow();
|
|
|
|
if (window) {
|
2011-10-17 07:59:28 -07:00
|
|
|
nsEventListenerManager* elm = aNode->GetListenerManager(false);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (elm) {
|
|
|
|
window->SetMutationListeners(elm->MutationListenerBits());
|
2008-10-15 14:06:32 -07:00
|
|
|
if (elm->MayHavePaintEventListener()) {
|
|
|
|
window->SetHasPaintEventListeners();
|
|
|
|
}
|
2010-08-25 06:10:00 -07:00
|
|
|
#ifdef MOZ_MEDIA
|
|
|
|
if (elm->MayHaveAudioAvailableEventListener()) {
|
|
|
|
window->SetHasAudioAvailableEventListeners();
|
|
|
|
}
|
|
|
|
#endif
|
2011-04-26 05:31:21 -07:00
|
|
|
if (elm->MayHaveTouchEventListener()) {
|
|
|
|
window->SetHasTouchEventListeners();
|
|
|
|
}
|
2011-09-18 00:45:14 -07:00
|
|
|
if (elm->MayHaveMouseEnterLeaveEventListener()) {
|
|
|
|
window->SetHasMouseEnterLeaveEventListeners();
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-29 23:09:56 -07:00
|
|
|
if (wasRegistered && oldDoc != newDoc) {
|
2012-01-31 13:55:54 -08:00
|
|
|
#ifdef MOZ_MEDIA
|
2009-10-01 07:10:13 -07:00
|
|
|
nsCOMPtr<nsIDOMHTMLMediaElement> domMediaElem(do_QueryInterface(aNode));
|
|
|
|
if (domMediaElem) {
|
|
|
|
nsHTMLMediaElement* mediaElem = static_cast<nsHTMLMediaElement*>(aNode);
|
|
|
|
mediaElem->NotifyOwnerDocumentActivityChanged();
|
|
|
|
}
|
2011-08-29 23:09:56 -07:00
|
|
|
#endif
|
2012-01-31 13:55:54 -08:00
|
|
|
nsCOMPtr<nsIObjectLoadingContent> objectLoadingContent(do_QueryInterface(aNode));
|
|
|
|
if (objectLoadingContent) {
|
|
|
|
nsObjectLoadingContent* olc = static_cast<nsObjectLoadingContent*>(objectLoadingContent.get());
|
|
|
|
olc->NotifyOwnerDocumentActivityChanged();
|
|
|
|
}
|
|
|
|
}
|
2009-10-01 07:10:13 -07:00
|
|
|
|
2010-08-26 18:30:03 -07:00
|
|
|
// nsImageLoadingContent needs to know when its document changes
|
|
|
|
if (oldDoc != newDoc) {
|
|
|
|
nsCOMPtr<nsIImageLoadingContent> imageContent(do_QueryInterface(aNode));
|
2012-03-31 09:30:13 -07:00
|
|
|
if (imageContent) {
|
2010-08-26 18:30:03 -07:00
|
|
|
imageContent->NotifyOwnerDocumentChanged(oldDoc);
|
2012-03-31 09:30:13 -07:00
|
|
|
}
|
|
|
|
if (oldDoc->MayHaveDOMMutationObservers()) {
|
|
|
|
newDoc->SetMayHaveDOMMutationObservers();
|
|
|
|
}
|
2010-08-26 18:30:03 -07:00
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (elem) {
|
|
|
|
elem->RecompileScriptEventListeners();
|
|
|
|
}
|
|
|
|
|
2010-12-03 08:43:32 -08:00
|
|
|
if (aCx && wrapper) {
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIXPConnect *xpc = nsContentUtils::XPConnect();
|
|
|
|
if (xpc) {
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> oldWrapper;
|
2010-12-03 08:43:32 -08:00
|
|
|
rv = xpc->ReparentWrappedNativeIfFound(aCx, wrapper, aNewScope, aNode,
|
2007-03-22 10:30:00 -07:00
|
|
|
getter_AddRefs(oldWrapper));
|
2010-11-15 17:21:25 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aNode->mNodeInfo.swap(nodeInfo);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-03 06:23:36 -07:00
|
|
|
// XXX If there are any attribute nodes on this element with UserDataHandlers
|
|
|
|
// we should technically adopt/clone/import such attribute nodes and notify
|
|
|
|
// those handlers. However we currently don't have code to do so without
|
|
|
|
// also notifying when it's not safe so we're not doing that at this time.
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-04-03 00:25:39 -07:00
|
|
|
if (aDeep && (!aClone || !aNode->IsNodeOfType(nsINode::eATTRIBUTE))) {
|
2007-03-22 10:30:00 -07:00
|
|
|
// aNode's children.
|
2011-09-27 00:54:58 -07:00
|
|
|
for (nsIContent* cloneChild = aNode->GetFirstChild();
|
|
|
|
cloneChild;
|
2012-04-03 00:25:39 -07:00
|
|
|
cloneChild = cloneChild->GetNextSibling()) {
|
2009-09-28 13:33:29 -07:00
|
|
|
nsCOMPtr<nsINode> child;
|
2011-10-17 07:59:28 -07:00
|
|
|
rv = CloneAndAdopt(cloneChild, aClone, true, nodeInfoManager,
|
2010-12-03 08:43:32 -08:00
|
|
|
aCx, aNewScope, aNodesWithProperties, clone,
|
|
|
|
getter_AddRefs(child));
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX setting document on some nodes not in a document so XBL will bind
|
|
|
|
// and chrome won't break. Make XBL bind to document-less nodes!
|
|
|
|
// XXXbz Once this is fixed, fix up the asserts in all implementations of
|
|
|
|
// BindToTree to assert what they would like to assert, and fix the
|
|
|
|
// ChangeDocumentFor() call in nsXULElement::BindToTree as well. Also,
|
|
|
|
// remove the UnbindFromTree call in ~nsXULElement, and add back in the
|
|
|
|
// precondition in nsXULElement::UnbindFromTree and remove the line in
|
|
|
|
// nsXULElement.h that makes nsNodeUtils a friend of nsXULElement.
|
|
|
|
// Note: Make sure to do this witchery _after_ we've done any deep
|
|
|
|
// cloning, so kids of the new node aren't confused about whether they're
|
|
|
|
// in a document.
|
|
|
|
#ifdef MOZ_XUL
|
2010-04-30 06:12:05 -07:00
|
|
|
if (aClone && !aParent && aNode->IsElement() &&
|
|
|
|
aNode->AsElement()->IsXUL()) {
|
2007-07-08 00:08:04 -07:00
|
|
|
nsXULElement *xulElem = static_cast<nsXULElement*>(elem);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (!xulElem->mPrototype || xulElem->IsInDoc()) {
|
2007-05-15 18:13:47 -07:00
|
|
|
clone->SetFlags(NODE_FORCE_XBL_BINDINGS);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (aNode->HasProperties()) {
|
2011-09-28 23:19:26 -07:00
|
|
|
bool ok = aNodesWithProperties.AppendObject(aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aClone) {
|
|
|
|
ok = ok && aNodesWithProperties.AppendObject(clone);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_TRUE(ok, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
}
|
|
|
|
|
2009-09-28 13:33:29 -07:00
|
|
|
clone.forget(aResult);
|
|
|
|
|
|
|
|
return NS_OK;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2007-05-12 08:36:28 -07:00
|
|
|
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
void
|
|
|
|
nsNodeUtils::UnlinkUserData(nsINode *aNode)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aNode->HasProperties(), "Call to UnlinkUserData not needed.");
|
|
|
|
|
|
|
|
// Strong reference to the document so that deleting properties can't
|
|
|
|
// delete the document.
|
2011-10-18 03:53:36 -07:00
|
|
|
nsCOMPtr<nsIDocument> document = aNode->OwnerDoc();
|
2011-10-18 04:19:44 -07:00
|
|
|
document->PropertyTable(DOM_USER_DATA)->DeleteAllPropertiesFor(aNode);
|
|
|
|
document->PropertyTable(DOM_USER_DATA_HANDLER)->DeleteAllPropertiesFor(aNode);
|
2007-05-12 08:36:28 -07:00
|
|
|
}
|