content/base/src/nsNodeUtils.h

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef nsNodeUtils_h___
michael@0 7 #define nsNodeUtils_h___
michael@0 8
michael@0 9 #include "nsIContent.h" // for use in inline function (ParentChainChanged)
michael@0 10 #include "nsIMutationObserver.h" // for use in inline function (ParentChainChanged)
michael@0 11 #include "js/TypeDecls.h"
michael@0 12 #include "nsCOMArray.h"
michael@0 13
michael@0 14 struct CharacterDataChangeInfo;
michael@0 15 class nsIVariant;
michael@0 16 class nsIDOMNode;
michael@0 17 class nsIDOMUserDataHandler;
michael@0 18 template<class E> class nsCOMArray;
michael@0 19 class nsCycleCollectionTraversalCallback;
michael@0 20
michael@0 21 class nsNodeUtils
michael@0 22 {
michael@0 23 public:
michael@0 24 /**
michael@0 25 * Send CharacterDataWillChange notifications to nsIMutationObservers.
michael@0 26 * @param aContent Node whose data changed
michael@0 27 * @param aInfo Struct with information details about the change
michael@0 28 * @see nsIMutationObserver::CharacterDataWillChange
michael@0 29 */
michael@0 30 static void CharacterDataWillChange(nsIContent* aContent,
michael@0 31 CharacterDataChangeInfo* aInfo);
michael@0 32
michael@0 33 /**
michael@0 34 * Send CharacterDataChanged notifications to nsIMutationObservers.
michael@0 35 * @param aContent Node whose data changed
michael@0 36 * @param aInfo Struct with information details about the change
michael@0 37 * @see nsIMutationObserver::CharacterDataChanged
michael@0 38 */
michael@0 39 static void CharacterDataChanged(nsIContent* aContent,
michael@0 40 CharacterDataChangeInfo* aInfo);
michael@0 41
michael@0 42 /**
michael@0 43 * Send AttributeWillChange notifications to nsIMutationObservers.
michael@0 44 * @param aElement Element whose data will change
michael@0 45 * @param aNameSpaceID Namespace of changing attribute
michael@0 46 * @param aAttribute Local-name of changing attribute
michael@0 47 * @param aModType Type of change (add/change/removal)
michael@0 48 * @see nsIMutationObserver::AttributeWillChange
michael@0 49 */
michael@0 50 static void AttributeWillChange(mozilla::dom::Element* aElement,
michael@0 51 int32_t aNameSpaceID,
michael@0 52 nsIAtom* aAttribute,
michael@0 53 int32_t aModType);
michael@0 54
michael@0 55 /**
michael@0 56 * Send AttributeChanged notifications to nsIMutationObservers.
michael@0 57 * @param aElement Element whose data changed
michael@0 58 * @param aNameSpaceID Namespace of changed attribute
michael@0 59 * @param aAttribute Local-name of changed attribute
michael@0 60 * @param aModType Type of change (add/change/removal)
michael@0 61 * @see nsIMutationObserver::AttributeChanged
michael@0 62 */
michael@0 63 static void AttributeChanged(mozilla::dom::Element* aElement,
michael@0 64 int32_t aNameSpaceID,
michael@0 65 nsIAtom* aAttribute,
michael@0 66 int32_t aModType);
michael@0 67 /**
michael@0 68 * Send AttributeSetToCurrentValue notifications to nsIMutationObservers.
michael@0 69 * @param aElement Element whose data changed
michael@0 70 * @param aNameSpaceID Namespace of the attribute
michael@0 71 * @param aAttribute Local-name of the attribute
michael@0 72 * @see nsIMutationObserver::AttributeSetToCurrentValue
michael@0 73 */
michael@0 74 static void AttributeSetToCurrentValue(mozilla::dom::Element* aElement,
michael@0 75 int32_t aNameSpaceID,
michael@0 76 nsIAtom* aAttribute);
michael@0 77
michael@0 78 /**
michael@0 79 * Send ContentAppended notifications to nsIMutationObservers
michael@0 80 * @param aContainer Node into which new child/children were added
michael@0 81 * @param aFirstNewContent First new child
michael@0 82 * @param aNewIndexInContainer Index of first new child
michael@0 83 * @see nsIMutationObserver::ContentAppended
michael@0 84 */
michael@0 85 static void ContentAppended(nsIContent* aContainer,
michael@0 86 nsIContent* aFirstNewContent,
michael@0 87 int32_t aNewIndexInContainer);
michael@0 88
michael@0 89 /**
michael@0 90 * Send ContentInserted notifications to nsIMutationObservers
michael@0 91 * @param aContainer Node into which new child was inserted
michael@0 92 * @param aChild Newly inserted child
michael@0 93 * @param aIndexInContainer Index of new child
michael@0 94 * @see nsIMutationObserver::ContentInserted
michael@0 95 */
michael@0 96 static void ContentInserted(nsINode* aContainer,
michael@0 97 nsIContent* aChild,
michael@0 98 int32_t aIndexInContainer);
michael@0 99 /**
michael@0 100 * Send ContentRemoved notifications to nsIMutationObservers
michael@0 101 * @param aContainer Node from which child was removed
michael@0 102 * @param aChild Removed child
michael@0 103 * @param aIndexInContainer Index of removed child
michael@0 104 * @see nsIMutationObserver::ContentRemoved
michael@0 105 */
michael@0 106 static void ContentRemoved(nsINode* aContainer,
michael@0 107 nsIContent* aChild,
michael@0 108 int32_t aIndexInContainer,
michael@0 109 nsIContent* aPreviousSibling);
michael@0 110 /**
michael@0 111 * Send ParentChainChanged notifications to nsIMutationObservers
michael@0 112 * @param aContent The piece of content that had its parent changed.
michael@0 113 * @see nsIMutationObserver::ParentChainChanged
michael@0 114 */
michael@0 115 static inline void ParentChainChanged(nsIContent *aContent)
michael@0 116 {
michael@0 117 nsINode::nsSlots* slots = aContent->GetExistingSlots();
michael@0 118 if (slots && !slots->mMutationObservers.IsEmpty()) {
michael@0 119 NS_OBSERVER_ARRAY_NOTIFY_OBSERVERS(slots->mMutationObservers,
michael@0 120 nsIMutationObserver,
michael@0 121 ParentChainChanged,
michael@0 122 (aContent));
michael@0 123 }
michael@0 124 }
michael@0 125
michael@0 126 /**
michael@0 127 * To be called when reference count of aNode drops to zero.
michael@0 128 * @param aNode The node which is going to be deleted.
michael@0 129 */
michael@0 130 static void LastRelease(nsINode* aNode);
michael@0 131
michael@0 132 /**
michael@0 133 * Clones aNode, its attributes and, if aDeep is true, its descendant nodes
michael@0 134 * If aNewNodeInfoManager is not null, it is used to create new nodeinfos for
michael@0 135 * the clones. aNodesWithProperties will be filled with all the nodes that
michael@0 136 * have properties, and every node in it will be followed by its clone.
michael@0 137 *
michael@0 138 * @param aNode Node to clone.
michael@0 139 * @param aDeep If true the function will be called recursively on
michael@0 140 * descendants of the node
michael@0 141 * @param aNewNodeInfoManager The nodeinfo manager to use to create new
michael@0 142 * nodeinfos for aNode and its attributes and
michael@0 143 * descendants. May be null if the nodeinfos
michael@0 144 * shouldn't be changed.
michael@0 145 * @param aNodesWithProperties All nodes (from amongst aNode and its
michael@0 146 * descendants) with properties. Every node will
michael@0 147 * be followed by its clone.
michael@0 148 * @param aResult *aResult will contain the cloned node.
michael@0 149 */
michael@0 150 static nsresult Clone(nsINode *aNode, bool aDeep,
michael@0 151 nsNodeInfoManager *aNewNodeInfoManager,
michael@0 152 nsCOMArray<nsINode> &aNodesWithProperties,
michael@0 153 nsINode **aResult)
michael@0 154 {
michael@0 155 return CloneAndAdopt(aNode, true, aDeep, aNewNodeInfoManager,
michael@0 156 JS::NullPtr(), aNodesWithProperties, nullptr, aResult);
michael@0 157 }
michael@0 158
michael@0 159 /**
michael@0 160 * Clones aNode, its attributes and, if aDeep is true, its descendant nodes
michael@0 161 */
michael@0 162 static nsresult Clone(nsINode *aNode, bool aDeep, nsINode **aResult)
michael@0 163 {
michael@0 164 nsCOMArray<nsINode> dummyNodeWithProperties;
michael@0 165 return CloneAndAdopt(aNode, true, aDeep, nullptr, JS::NullPtr(),
michael@0 166 dummyNodeWithProperties, aNode->GetParent(), aResult);
michael@0 167 }
michael@0 168
michael@0 169 /**
michael@0 170 * Walks aNode, its attributes and descendant nodes. If aNewNodeInfoManager is
michael@0 171 * not null, it is used to create new nodeinfos for the nodes. Also reparents
michael@0 172 * the XPConnect wrappers for the nodes into aReparentScope if non-null.
michael@0 173 * aNodesWithProperties will be filled with all the nodes that have
michael@0 174 * properties.
michael@0 175 *
michael@0 176 * @param aNode Node to adopt.
michael@0 177 * @param aNewNodeInfoManager The nodeinfo manager to use to create new
michael@0 178 * nodeinfos for aNode and its attributes and
michael@0 179 * descendants. May be null if the nodeinfos
michael@0 180 * shouldn't be changed.
michael@0 181 * @param aReparentScope New scope for the wrappers, or null if no reparenting
michael@0 182 * should be done.
michael@0 183 * @param aNodesWithProperties All nodes (from amongst aNode and its
michael@0 184 * descendants) with properties.
michael@0 185 */
michael@0 186 static nsresult Adopt(nsINode *aNode, nsNodeInfoManager *aNewNodeInfoManager,
michael@0 187 JS::Handle<JSObject*> aReparentScope,
michael@0 188 nsCOMArray<nsINode> &aNodesWithProperties)
michael@0 189 {
michael@0 190 nsCOMPtr<nsINode> node;
michael@0 191 nsresult rv = CloneAndAdopt(aNode, false, true, aNewNodeInfoManager,
michael@0 192 aReparentScope, aNodesWithProperties,
michael@0 193 nullptr, getter_AddRefs(node));
michael@0 194
michael@0 195 nsMutationGuard::DidMutate();
michael@0 196
michael@0 197 return rv;
michael@0 198 }
michael@0 199
michael@0 200 /**
michael@0 201 * Call registered userdata handlers for operation aOperation for the nodes in
michael@0 202 * aNodesWithProperties. If aCloned is true aNodesWithProperties should
michael@0 203 * contain both the original and the cloned nodes (and only the userdata
michael@0 204 * handlers registered for the original nodes will be called).
michael@0 205 *
michael@0 206 * @param aNodesWithProperties Contains the nodes that might have properties
michael@0 207 * registered on them. If aCloned is true every
michael@0 208 * one of those nodes should be immediately
michael@0 209 * followed in the array by the cloned node.
michael@0 210 * @param aOwnerDocument The ownerDocument of the original nodes.
michael@0 211 * @param aOperation The operation to call a userdata handler for.
michael@0 212 * @param aCloned If true aNodesWithProperties will contain both original
michael@0 213 * and cloned nodes.
michael@0 214 */
michael@0 215 static nsresult CallUserDataHandlers(nsCOMArray<nsINode> &aNodesWithProperties,
michael@0 216 nsIDocument *aOwnerDocument,
michael@0 217 uint16_t aOperation, bool aCloned);
michael@0 218
michael@0 219 /**
michael@0 220 * Helper for the cycle collector to traverse the DOM UserData and
michael@0 221 * UserDataHandlers for aNode.
michael@0 222 *
michael@0 223 * @param aNode the node to traverse UserData and UserDataHandlers for
michael@0 224 * @param aCb the cycle collection callback
michael@0 225 */
michael@0 226 static void TraverseUserData(nsINode* aNode,
michael@0 227 nsCycleCollectionTraversalCallback &aCb);
michael@0 228
michael@0 229 /**
michael@0 230 * A basic implementation of the DOM cloneNode method. Calls nsINode::Clone to
michael@0 231 * do the actual cloning of the node.
michael@0 232 *
michael@0 233 * @param aNode the node to clone
michael@0 234 * @param aDeep if true all descendants will be cloned too
michael@0 235 * @param aCallUserDataHandlers if true, user data handlers will be called
michael@0 236 * @param aResult the clone
michael@0 237 */
michael@0 238 static nsresult CloneNodeImpl(nsINode *aNode, bool aDeep,
michael@0 239 bool aCallUserDataHandlers,
michael@0 240 nsINode **aResult);
michael@0 241
michael@0 242 /**
michael@0 243 * Release the UserData and UserDataHandlers for aNode.
michael@0 244 *
michael@0 245 * @param aNode the node to release the UserData and UserDataHandlers for
michael@0 246 */
michael@0 247 static void UnlinkUserData(nsINode *aNode);
michael@0 248
michael@0 249 /**
michael@0 250 * Returns a true if the node is a HTMLTemplate element.
michael@0 251 *
michael@0 252 * @param aNode a node to test for HTMLTemplate elementness.
michael@0 253 */
michael@0 254 static bool IsTemplateElement(const nsINode *aNode);
michael@0 255
michael@0 256 /**
michael@0 257 * Returns the first child of a node or the first child of
michael@0 258 * a template element's content if the provided node is a
michael@0 259 * template element.
michael@0 260 *
michael@0 261 * @param aNode A node from which to retrieve the first child.
michael@0 262 */
michael@0 263 static nsIContent* GetFirstChildOfTemplateOrNode(nsINode* aNode);
michael@0 264
michael@0 265 private:
michael@0 266 /**
michael@0 267 * Walks aNode, its attributes and, if aDeep is true, its descendant nodes.
michael@0 268 * If aClone is true the nodes will be cloned. If aNewNodeInfoManager is
michael@0 269 * not null, it is used to create new nodeinfos for the nodes. Also reparents
michael@0 270 * the XPConnect wrappers for the nodes into aReparentScope if non-null.
michael@0 271 * aNodesWithProperties will be filled with all the nodes that have
michael@0 272 * properties.
michael@0 273 *
michael@0 274 * @param aNode Node to adopt/clone.
michael@0 275 * @param aClone If true the node will be cloned and the cloned node will
michael@0 276 * be in aResult.
michael@0 277 * @param aDeep If true the function will be called recursively on
michael@0 278 * descendants of the node
michael@0 279 * @param aNewNodeInfoManager The nodeinfo manager to use to create new
michael@0 280 * nodeinfos for aNode and its attributes and
michael@0 281 * descendants. May be null if the nodeinfos
michael@0 282 * shouldn't be changed.
michael@0 283 * @param aReparentScope Scope into which wrappers should be reparented, or
michael@0 284 * null if no reparenting should be done.
michael@0 285 * @param aNodesWithProperties All nodes (from amongst aNode and its
michael@0 286 * descendants) with properties. If aClone is
michael@0 287 * true every node will be followed by its
michael@0 288 * clone.
michael@0 289 * @param aParent If aClone is true the cloned node will be appended to
michael@0 290 * aParent's children. May be null. If not null then aNode
michael@0 291 * must be an nsIContent.
michael@0 292 * @param aResult If aClone is true then *aResult will contain the cloned
michael@0 293 * node.
michael@0 294 */
michael@0 295 static nsresult CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep,
michael@0 296 nsNodeInfoManager *aNewNodeInfoManager,
michael@0 297 JS::Handle<JSObject*> aReparentScope,
michael@0 298 nsCOMArray<nsINode> &aNodesWithProperties,
michael@0 299 nsINode *aParent, nsINode **aResult);
michael@0 300 };
michael@0 301
michael@0 302 #endif // nsNodeUtils_h___

mercurial