2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
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
|
|
|
|
|
|
|
#ifndef nsNodeUtils_h___
|
|
|
|
#define nsNodeUtils_h___
|
|
|
|
|
2012-08-06 05:02:08 -07:00
|
|
|
#include "nsIContent.h" // for use in inline function (ParentChainChanged)
|
|
|
|
#include "nsIMutationObserver.h" // for use in inline function (ParentChainChanged)
|
2013-08-27 19:59:14 -07:00
|
|
|
#include "js/TypeDecls.h"
|
2013-10-02 04:40:07 -07:00
|
|
|
#include "nsCOMArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2010-04-19 08:40:15 -07:00
|
|
|
struct CharacterDataChangeInfo;
|
2007-05-12 08:36:28 -07:00
|
|
|
class nsIVariant;
|
2010-04-19 08:41:39 -07:00
|
|
|
class nsIDOMNode;
|
2007-03-22 10:30:00 -07:00
|
|
|
template<class E> class nsCOMArray;
|
2007-08-06 07:34:02 -07:00
|
|
|
class nsCycleCollectionTraversalCallback;
|
2015-03-13 22:34:40 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
class AnimationPlayer;
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
class nsNodeUtils
|
|
|
|
{
|
|
|
|
public:
|
2007-09-05 01:22:17 -07:00
|
|
|
/**
|
|
|
|
* Send CharacterDataWillChange notifications to nsIMutationObservers.
|
|
|
|
* @param aContent Node whose data changed
|
|
|
|
* @param aInfo Struct with information details about the change
|
|
|
|
* @see nsIMutationObserver::CharacterDataWillChange
|
|
|
|
*/
|
|
|
|
static void CharacterDataWillChange(nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Send CharacterDataChanged notifications to nsIMutationObservers.
|
|
|
|
* @param aContent Node whose data changed
|
|
|
|
* @param aInfo Struct with information details about the change
|
|
|
|
* @see nsIMutationObserver::CharacterDataChanged
|
|
|
|
*/
|
|
|
|
static void CharacterDataChanged(nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo);
|
|
|
|
|
2009-06-29 11:36:25 -07:00
|
|
|
/**
|
|
|
|
* Send AttributeWillChange notifications to nsIMutationObservers.
|
2010-08-24 00:06:20 -07:00
|
|
|
* @param aElement Element whose data will change
|
2009-06-29 11:36:25 -07:00
|
|
|
* @param aNameSpaceID Namespace of changing attribute
|
|
|
|
* @param aAttribute Local-name of changing attribute
|
|
|
|
* @param aModType Type of change (add/change/removal)
|
|
|
|
* @see nsIMutationObserver::AttributeWillChange
|
|
|
|
*/
|
2010-08-24 00:06:20 -07:00
|
|
|
static void AttributeWillChange(mozilla::dom::Element* aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNameSpaceID,
|
2009-06-29 11:36:25 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aModType);
|
2009-06-29 11:36:25 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Send AttributeChanged notifications to nsIMutationObservers.
|
2010-08-24 00:06:07 -07:00
|
|
|
* @param aElement Element whose data changed
|
2007-03-22 10:30:00 -07:00
|
|
|
* @param aNameSpaceID Namespace of changed attribute
|
|
|
|
* @param aAttribute Local-name of changed attribute
|
|
|
|
* @param aModType Type of change (add/change/removal)
|
|
|
|
* @see nsIMutationObserver::AttributeChanged
|
|
|
|
*/
|
2010-08-24 00:06:07 -07:00
|
|
|
static void AttributeChanged(mozilla::dom::Element* aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNameSpaceID,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsIAtom* aAttribute,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aModType);
|
2012-08-15 03:06:01 -07:00
|
|
|
/**
|
|
|
|
* Send AttributeSetToCurrentValue notifications to nsIMutationObservers.
|
|
|
|
* @param aElement Element whose data changed
|
|
|
|
* @param aNameSpaceID Namespace of the attribute
|
|
|
|
* @param aAttribute Local-name of the attribute
|
|
|
|
* @see nsIMutationObserver::AttributeSetToCurrentValue
|
|
|
|
*/
|
|
|
|
static void AttributeSetToCurrentValue(mozilla::dom::Element* aElement,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNameSpaceID,
|
2012-08-15 03:06:01 -07:00
|
|
|
nsIAtom* aAttribute);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send ContentAppended notifications to nsIMutationObservers
|
|
|
|
* @param aContainer Node into which new child/children were added
|
2010-05-10 18:12:34 -07:00
|
|
|
* @param aFirstNewContent First new child
|
2007-03-22 10:30:00 -07:00
|
|
|
* @param aNewIndexInContainer Index of first new child
|
|
|
|
* @see nsIMutationObserver::ContentAppended
|
|
|
|
*/
|
|
|
|
static void ContentAppended(nsIContent* aContainer,
|
2010-05-10 18:12:34 -07:00
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aNewIndexInContainer);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Send ContentInserted notifications to nsIMutationObservers
|
|
|
|
* @param aContainer Node into which new child was inserted
|
|
|
|
* @param aChild Newly inserted child
|
|
|
|
* @param aIndexInContainer Index of new child
|
|
|
|
* @see nsIMutationObserver::ContentInserted
|
|
|
|
*/
|
|
|
|
static void ContentInserted(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer);
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Send ContentRemoved notifications to nsIMutationObservers
|
|
|
|
* @param aContainer Node from which child was removed
|
|
|
|
* @param aChild Removed child
|
|
|
|
* @param aIndexInContainer Index of removed child
|
|
|
|
* @see nsIMutationObserver::ContentRemoved
|
|
|
|
*/
|
|
|
|
static void ContentRemoved(nsINode* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t aIndexInContainer,
|
2010-07-21 15:05:17 -07:00
|
|
|
nsIContent* aPreviousSibling);
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Send ParentChainChanged notifications to nsIMutationObservers
|
|
|
|
* @param aContent The piece of content that had its parent changed.
|
|
|
|
* @see nsIMutationObserver::ParentChainChanged
|
|
|
|
*/
|
2012-03-21 21:10:51 -07:00
|
|
|
static inline void ParentChainChanged(nsIContent *aContent)
|
|
|
|
{
|
|
|
|
nsINode::nsSlots* slots = aContent->GetExistingSlots();
|
|
|
|
if (slots && !slots->mMutationObservers.IsEmpty()) {
|
|
|
|
NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS(slots->mMutationObservers,
|
|
|
|
nsIMutationObserver,
|
|
|
|
ParentChainChanged,
|
|
|
|
(aContent));
|
|
|
|
}
|
|
|
|
}
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2015-03-13 22:34:40 -07:00
|
|
|
static void AnimationAdded(mozilla::dom::AnimationPlayer* aPlayer);
|
|
|
|
static void AnimationChanged(mozilla::dom::AnimationPlayer* aPlayer);
|
|
|
|
static void AnimationRemoved(mozilla::dom::AnimationPlayer* aPlayer);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* To be called when reference count of aNode drops to zero.
|
|
|
|
* @param aNode The node which is going to be deleted.
|
|
|
|
*/
|
2007-05-12 08:36:28 -07:00
|
|
|
static void LastRelease(nsINode* aNode);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
2011-10-17 07:59:28 -07:00
|
|
|
* Clones aNode, its attributes and, if aDeep is true, its descendant nodes
|
2007-03-22 10:30:00 -07:00
|
|
|
* If aNewNodeInfoManager is not null, it is used to create new nodeinfos for
|
|
|
|
* the clones. aNodesWithProperties will be filled with all the nodes that
|
|
|
|
* have properties, and every node in it will be followed by its clone.
|
|
|
|
*
|
|
|
|
* @param aNode Node to clone.
|
2011-10-17 07:59:28 -07:00
|
|
|
* @param aDeep If true the function will be called recursively on
|
2007-03-22 10:30:00 -07:00
|
|
|
* descendants of the node
|
|
|
|
* @param aNewNodeInfoManager The nodeinfo manager to use to create new
|
|
|
|
* nodeinfos for aNode and its attributes and
|
|
|
|
* descendants. May be null if the nodeinfos
|
|
|
|
* shouldn't be changed.
|
|
|
|
* @param aNodesWithProperties All nodes (from amongst aNode and its
|
|
|
|
* descendants) with properties. Every node will
|
|
|
|
* be followed by its clone.
|
|
|
|
* @param aResult *aResult will contain the cloned node.
|
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
static nsresult Clone(nsINode *aNode, bool aDeep,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsNodeInfoManager *aNewNodeInfoManager,
|
|
|
|
nsCOMArray<nsINode> &aNodesWithProperties,
|
2012-10-09 05:31:24 -07:00
|
|
|
nsINode **aResult)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2013-04-08 10:24:21 -07:00
|
|
|
return CloneAndAdopt(aNode, true, aDeep, aNewNodeInfoManager,
|
2013-05-07 19:34:56 -07:00
|
|
|
JS::NullPtr(), aNodesWithProperties, nullptr, aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2012-11-01 11:18:08 -07:00
|
|
|
/**
|
|
|
|
* Clones aNode, its attributes and, if aDeep is true, its descendant nodes
|
|
|
|
*/
|
|
|
|
static nsresult Clone(nsINode *aNode, bool aDeep, nsINode **aResult)
|
|
|
|
{
|
|
|
|
nsCOMArray<nsINode> dummyNodeWithProperties;
|
2013-05-07 19:34:56 -07:00
|
|
|
return CloneAndAdopt(aNode, true, aDeep, nullptr, JS::NullPtr(),
|
2012-11-01 11:18:08 -07:00
|
|
|
dummyNodeWithProperties, aNode->GetParent(), aResult);
|
|
|
|
}
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* Walks aNode, its attributes and descendant nodes. If aNewNodeInfoManager is
|
|
|
|
* not null, it is used to create new nodeinfos for the nodes. Also reparents
|
2013-04-08 10:24:21 -07:00
|
|
|
* the XPConnect wrappers for the nodes into aReparentScope if non-null.
|
2007-03-22 10:30:00 -07:00
|
|
|
* aNodesWithProperties will be filled with all the nodes that have
|
|
|
|
* properties.
|
|
|
|
*
|
|
|
|
* @param aNode Node to adopt.
|
|
|
|
* @param aNewNodeInfoManager The nodeinfo manager to use to create new
|
|
|
|
* nodeinfos for aNode and its attributes and
|
|
|
|
* descendants. May be null if the nodeinfos
|
|
|
|
* shouldn't be changed.
|
2013-04-08 10:24:21 -07:00
|
|
|
* @param aReparentScope New scope for the wrappers, or null if no reparenting
|
|
|
|
* should be done.
|
2007-03-22 10:30:00 -07:00
|
|
|
* @param aNodesWithProperties All nodes (from amongst aNode and its
|
|
|
|
* descendants) with properties.
|
|
|
|
*/
|
|
|
|
static nsresult Adopt(nsINode *aNode, nsNodeInfoManager *aNewNodeInfoManager,
|
2013-05-07 19:34:56 -07:00
|
|
|
JS::Handle<JSObject*> aReparentScope,
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCOMArray<nsINode> &aNodesWithProperties)
|
|
|
|
{
|
2012-10-09 05:31:24 -07:00
|
|
|
nsCOMPtr<nsINode> node;
|
2011-10-17 07:59:28 -07:00
|
|
|
nsresult rv = CloneAndAdopt(aNode, false, true, aNewNodeInfoManager,
|
2013-04-08 10:24:21 -07:00
|
|
|
aReparentScope, aNodesWithProperties,
|
2012-10-09 05:31:24 -07:00
|
|
|
nullptr, getter_AddRefs(node));
|
2010-03-18 13:03:52 -07:00
|
|
|
|
|
|
|
nsMutationGuard::DidMutate();
|
2010-03-17 07:40:35 -07:00
|
|
|
|
|
|
|
return rv;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-09-11 03:22:30 -07:00
|
|
|
* Helper for the cycle collector to traverse the DOM UserData for aNode.
|
2007-03-22 10:30:00 -07:00
|
|
|
*
|
2014-09-11 03:22:30 -07:00
|
|
|
* @param aNode the node to traverse UserData for
|
2007-05-12 08:36:28 -07:00
|
|
|
* @param aCb the cycle collection callback
|
|
|
|
*/
|
|
|
|
static void TraverseUserData(nsINode* aNode,
|
|
|
|
nsCycleCollectionTraversalCallback &aCb);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/**
|
|
|
|
* A basic implementation of the DOM cloneNode method. Calls nsINode::Clone to
|
|
|
|
* do the actual cloning of the node.
|
|
|
|
*
|
|
|
|
* @param aNode the node to clone
|
|
|
|
* @param aDeep if true all descendants will be cloned too
|
|
|
|
* @param aResult the clone
|
|
|
|
*/
|
2014-09-11 03:22:30 -07:00
|
|
|
static nsresult CloneNodeImpl(nsINode *aNode, bool aDeep, nsINode **aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2007-05-12 08:36:28 -07:00
|
|
|
/**
|
2014-09-11 03:22:30 -07:00
|
|
|
* Release the UserData for aNode.
|
2007-05-12 08:36:28 -07:00
|
|
|
*
|
2014-09-11 03:22:30 -07:00
|
|
|
* @param aNode the node to release the UserData for
|
2007-05-12 08:36:28 -07:00
|
|
|
*/
|
|
|
|
static void UnlinkUserData(nsINode *aNode);
|
|
|
|
|
2013-03-26 00:15:23 -07:00
|
|
|
/**
|
|
|
|
* Returns a true if the node is a HTMLTemplate element.
|
|
|
|
*
|
|
|
|
* @param aNode a node to test for HTMLTemplate elementness.
|
|
|
|
*/
|
|
|
|
static bool IsTemplateElement(const nsINode *aNode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the first child of a node or the first child of
|
|
|
|
* a template element's content if the provided node is a
|
|
|
|
* template element.
|
|
|
|
*
|
|
|
|
* @param aNode A node from which to retrieve the first child.
|
|
|
|
*/
|
|
|
|
static nsIContent* GetFirstChildOfTemplateOrNode(nsINode* aNode);
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
/**
|
2011-10-17 07:59:28 -07:00
|
|
|
* Walks aNode, its attributes and, if aDeep is true, its descendant nodes.
|
|
|
|
* If aClone is true the nodes will be cloned. If aNewNodeInfoManager is
|
2007-03-22 10:30:00 -07:00
|
|
|
* not null, it is used to create new nodeinfos for the nodes. Also reparents
|
2013-04-08 10:24:21 -07:00
|
|
|
* the XPConnect wrappers for the nodes into aReparentScope if non-null.
|
2007-03-22 10:30:00 -07:00
|
|
|
* aNodesWithProperties will be filled with all the nodes that have
|
|
|
|
* properties.
|
|
|
|
*
|
|
|
|
* @param aNode Node to adopt/clone.
|
2011-10-17 07:59:28 -07:00
|
|
|
* @param aClone If true the node will be cloned and the cloned node will
|
2007-03-22 10:30:00 -07:00
|
|
|
* be in aResult.
|
2011-10-17 07:59:28 -07:00
|
|
|
* @param aDeep If true the function will be called recursively on
|
2007-03-22 10:30:00 -07:00
|
|
|
* descendants of the node
|
|
|
|
* @param aNewNodeInfoManager The nodeinfo manager to use to create new
|
|
|
|
* nodeinfos for aNode and its attributes and
|
|
|
|
* descendants. May be null if the nodeinfos
|
|
|
|
* shouldn't be changed.
|
2013-04-08 10:24:21 -07:00
|
|
|
* @param aReparentScope Scope into which wrappers should be reparented, or
|
|
|
|
* null if no reparenting should be done.
|
2007-03-22 10:30:00 -07:00
|
|
|
* @param aNodesWithProperties All nodes (from amongst aNode and its
|
|
|
|
* descendants) with properties. If aClone is
|
2011-10-17 07:59:28 -07:00
|
|
|
* true every node will be followed by its
|
2007-03-22 10:30:00 -07:00
|
|
|
* clone.
|
2011-10-17 07:59:28 -07:00
|
|
|
* @param aParent If aClone is true the cloned node will be appended to
|
2009-09-28 13:33:29 -07:00
|
|
|
* aParent's children. May be null. If not null then aNode
|
|
|
|
* must be an nsIContent.
|
2011-10-17 07:59:28 -07:00
|
|
|
* @param aResult If aClone is true then *aResult will contain the cloned
|
2009-10-16 07:00:38 -07:00
|
|
|
* node.
|
2009-09-28 13:33:29 -07:00
|
|
|
*/
|
2011-09-28 23:19:26 -07:00
|
|
|
static nsresult CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
|
2009-09-28 13:33:29 -07:00
|
|
|
nsNodeInfoManager *aNewNodeInfoManager,
|
2013-05-07 19:34:56 -07:00
|
|
|
JS::Handle<JSObject*> aReparentScope,
|
2009-09-28 13:33:29 -07:00
|
|
|
nsCOMArray<nsINode> &aNodesWithProperties,
|
|
|
|
nsINode *aParent, nsINode **aResult);
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // nsNodeUtils_h___
|