/src/mozilla-central/dom/base/nsNodeUtils.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
2 | | /* vim: set ts=8 sts=2 et sw=2 tw=80: */ |
3 | | /* This Source Code Form is subject to the terms of the Mozilla Public |
4 | | * License, v. 2.0. If a copy of the MPL was not distributed with this |
5 | | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
6 | | |
7 | | #ifndef nsNodeUtils_h___ |
8 | | #define nsNodeUtils_h___ |
9 | | |
10 | | #include "mozilla/Maybe.h" |
11 | | #include "nsIContent.h" // for use in inline function (ParentChainChanged) |
12 | | #include "nsIMutationObserver.h" // for use in inline function (ParentChainChanged) |
13 | | #include "js/TypeDecls.h" |
14 | | #include "nsCOMArray.h" |
15 | | |
16 | | struct CharacterDataChangeInfo; |
17 | | template<class E> class nsCOMArray; |
18 | | class nsCycleCollectionTraversalCallback; |
19 | | namespace mozilla { |
20 | | struct NonOwningAnimationTarget; |
21 | | class ErrorResult; |
22 | | namespace dom { |
23 | | class Animation; |
24 | | } // namespace dom |
25 | | } // namespace mozilla |
26 | | |
27 | | class nsNodeUtils |
28 | | { |
29 | | public: |
30 | | /** |
31 | | * Send CharacterDataWillChange notifications to nsIMutationObservers. |
32 | | * @param aContent Node whose data changed |
33 | | * @param aInfo Struct with information details about the change |
34 | | * @see nsIMutationObserver::CharacterDataWillChange |
35 | | */ |
36 | | static void CharacterDataWillChange(nsIContent* aContent, |
37 | | const CharacterDataChangeInfo&); |
38 | | |
39 | | /** |
40 | | * Send CharacterDataChanged notifications to nsIMutationObservers. |
41 | | * @param aContent Node whose data changed |
42 | | * @param aInfo Struct with information details about the change |
43 | | * @see nsIMutationObserver::CharacterDataChanged |
44 | | */ |
45 | | static void CharacterDataChanged(nsIContent* aContent, |
46 | | const CharacterDataChangeInfo&); |
47 | | |
48 | | /** |
49 | | * Send AttributeWillChange notifications to nsIMutationObservers. |
50 | | * @param aElement Element whose data will change |
51 | | * @param aNameSpaceID Namespace of changing attribute |
52 | | * @param aAttribute Local-name of changing attribute |
53 | | * @param aModType Type of change (add/change/removal) |
54 | | * @param aNewValue The parsed new value, but only if BeforeSetAttr |
55 | | * preparsed it!!! |
56 | | * @see nsIMutationObserver::AttributeWillChange |
57 | | */ |
58 | | static void AttributeWillChange(mozilla::dom::Element* aElement, |
59 | | int32_t aNameSpaceID, |
60 | | nsAtom* aAttribute, |
61 | | int32_t aModType, |
62 | | const nsAttrValue* aNewValue); |
63 | | |
64 | | /** |
65 | | * Send AttributeChanged notifications to nsIMutationObservers. |
66 | | * @param aElement Element whose data changed |
67 | | * @param aNameSpaceID Namespace of changed attribute |
68 | | * @param aAttribute Local-name of changed attribute |
69 | | * @param aModType Type of change (add/change/removal) |
70 | | * @param aOldValue If the old value was StoresOwnData() (or absent), |
71 | | * that value, otherwise null |
72 | | * @see nsIMutationObserver::AttributeChanged |
73 | | */ |
74 | | static void AttributeChanged(mozilla::dom::Element* aElement, |
75 | | int32_t aNameSpaceID, |
76 | | nsAtom* aAttribute, |
77 | | int32_t aModType, |
78 | | const nsAttrValue* aOldValue); |
79 | | |
80 | | /** |
81 | | * Send AttributeSetToCurrentValue notifications to nsIMutationObservers. |
82 | | * @param aElement Element whose data changed |
83 | | * @param aNameSpaceID Namespace of the attribute |
84 | | * @param aAttribute Local-name of the attribute |
85 | | * @see nsIMutationObserver::AttributeSetToCurrentValue |
86 | | */ |
87 | | static void AttributeSetToCurrentValue(mozilla::dom::Element* aElement, |
88 | | int32_t aNameSpaceID, |
89 | | nsAtom* aAttribute); |
90 | | |
91 | | /** |
92 | | * Send ContentAppended notifications to nsIMutationObservers |
93 | | * @param aContainer Node into which new child/children were added |
94 | | * @param aFirstNewContent First new child |
95 | | * @see nsIMutationObserver::ContentAppended |
96 | | */ |
97 | | static void ContentAppended(nsIContent* aContainer, |
98 | | nsIContent* aFirstNewContent); |
99 | | |
100 | | /** |
101 | | * Send NativeAnonymousChildList notifications to nsIMutationObservers |
102 | | * @param aContent Anonymous node that's been added or removed |
103 | | * @param aIsRemove True if it's a removal, false if an addition |
104 | | * @see nsIMutationObserver::NativeAnonymousChildListChange |
105 | | */ |
106 | | static void NativeAnonymousChildListChange(nsIContent* aContent, |
107 | | bool aIsRemove); |
108 | | |
109 | | /** |
110 | | * Send ContentInserted notifications to nsIMutationObservers |
111 | | * @param aContainer Node into which new child was inserted |
112 | | * @param aChild Newly inserted child |
113 | | * @see nsIMutationObserver::ContentInserted |
114 | | */ |
115 | | static void ContentInserted(nsINode* aContainer, |
116 | | nsIContent* aChild); |
117 | | /** |
118 | | * Send ContentRemoved notifications to nsIMutationObservers |
119 | | * @param aContainer Node from which child was removed |
120 | | * @param aChild Removed child |
121 | | * @param aPreviousSibling Previous sibling of the removed child |
122 | | * @see nsIMutationObserver::ContentRemoved |
123 | | */ |
124 | | static void ContentRemoved(nsINode* aContainer, |
125 | | nsIContent* aChild, |
126 | | nsIContent* aPreviousSibling); |
127 | | /** |
128 | | * Send ParentChainChanged notifications to nsIMutationObservers |
129 | | * @param aContent The piece of content that had its parent changed. |
130 | | * @see nsIMutationObserver::ParentChainChanged |
131 | | */ |
132 | | static inline void ParentChainChanged(nsIContent *aContent) |
133 | 0 | { |
134 | 0 | nsINode::nsSlots* slots = aContent->GetExistingSlots(); |
135 | 0 | if (slots && !slots->mMutationObservers.IsEmpty()) { |
136 | 0 | NS_OBSERVER_AUTO_ARRAY_NOTIFY_OBSERVERS(slots->mMutationObservers, |
137 | 0 | nsIMutationObserver, 1, |
138 | 0 | ParentChainChanged, |
139 | 0 | (aContent)); |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | | /** |
144 | | * Utility function to get the target (pseudo-)element associated with an |
145 | | * animation. |
146 | | * @param aAnimation The animation whose target is what we want. |
147 | | */ |
148 | | static mozilla::Maybe<mozilla::NonOwningAnimationTarget> |
149 | | GetTargetForAnimation(const mozilla::dom::Animation* aAnimation); |
150 | | |
151 | | /** |
152 | | * Notify that an animation is added/changed/removed. |
153 | | * @param aAnimation The animation we added/changed/removed. |
154 | | */ |
155 | | static void AnimationAdded(mozilla::dom::Animation* aAnimation); |
156 | | static void AnimationChanged(mozilla::dom::Animation* aAnimation); |
157 | | static void AnimationRemoved(mozilla::dom::Animation* aAnimation); |
158 | | |
159 | | /** |
160 | | * To be called when reference count of aNode drops to zero. |
161 | | * @param aNode The node which is going to be deleted. |
162 | | */ |
163 | | static void LastRelease(nsINode* aNode); |
164 | | |
165 | | /** |
166 | | * Clones aNode, its attributes and, if aDeep is true, its descendant nodes |
167 | | * If aNewNodeInfoManager is not null, it is used to create new nodeinfos for |
168 | | * the clones. aNodesWithProperties will be filled with all the nodes that |
169 | | * have properties, and every node in it will be followed by its clone. |
170 | | * |
171 | | * @param aNode Node to clone. |
172 | | * @param aDeep If true the function will be called recursively on |
173 | | * descendants of the node |
174 | | * @param aNewNodeInfoManager The nodeinfo manager to use to create new |
175 | | * nodeinfos for aNode and its attributes and |
176 | | * descendants. May be null if the nodeinfos |
177 | | * shouldn't be changed. |
178 | | * @param aNodesWithProperties All nodes (from amongst aNode and its |
179 | | * descendants) with properties. Every node will |
180 | | * be followed by its clone. Null can be passed to |
181 | | * prevent this from being used. |
182 | | * @param aError The error, if any. |
183 | | * |
184 | | * @return The newly created node. Null in error conditions. |
185 | | */ |
186 | | static already_AddRefed<nsINode> Clone(nsINode *aNode, bool aDeep, |
187 | | nsNodeInfoManager *aNewNodeInfoManager, |
188 | | nsCOMArray<nsINode> *aNodesWithProperties, |
189 | | mozilla::ErrorResult& aError) |
190 | 0 | { |
191 | 0 | return CloneAndAdopt(aNode, true, aDeep, aNewNodeInfoManager, |
192 | 0 | nullptr, aNodesWithProperties, nullptr, aError); |
193 | 0 | } |
194 | | |
195 | | /** |
196 | | * Walks aNode, its attributes and descendant nodes. If aNewNodeInfoManager is |
197 | | * not null, it is used to create new nodeinfos for the nodes. Also reparents |
198 | | * the XPConnect wrappers for the nodes into aReparentScope if non-null. |
199 | | * aNodesWithProperties will be filled with all the nodes that have |
200 | | * properties. |
201 | | * |
202 | | * @param aNode Node to adopt. |
203 | | * @param aNewNodeInfoManager The nodeinfo manager to use to create new |
204 | | * nodeinfos for aNode and its attributes and |
205 | | * descendants. May be null if the nodeinfos |
206 | | * shouldn't be changed. |
207 | | * @param aReparentScope New scope for the wrappers, or null if no reparenting |
208 | | * should be done. |
209 | | * @param aNodesWithProperties All nodes (from amongst aNode and its |
210 | | * descendants) with properties. |
211 | | * @param aError The error, if any. |
212 | | */ |
213 | | static void Adopt(nsINode *aNode, nsNodeInfoManager *aNewNodeInfoManager, |
214 | | JS::Handle<JSObject*> aReparentScope, |
215 | | nsCOMArray<nsINode>& aNodesWithProperties, |
216 | | mozilla::ErrorResult& aError) |
217 | 0 | { |
218 | 0 | // Just need to store the return value of CloneAndAdopt in a |
219 | 0 | // temporary nsCOMPtr to make sure we release it. |
220 | 0 | nsCOMPtr<nsINode> node = CloneAndAdopt(aNode, false, true, aNewNodeInfoManager, |
221 | 0 | aReparentScope, &aNodesWithProperties, |
222 | 0 | nullptr, aError); |
223 | 0 |
|
224 | 0 | nsMutationGuard::DidMutate(); |
225 | 0 | } |
226 | | |
227 | | /** |
228 | | * A basic implementation of the DOM cloneNode method. Calls nsINode::Clone to |
229 | | * do the actual cloning of the node. |
230 | | * |
231 | | * @param aNode the node to clone |
232 | | * @param aDeep if true all descendants will be cloned too |
233 | | * @param aError the error, if any. |
234 | | * |
235 | | * @return the clone, or null if an error occurs. |
236 | | */ |
237 | | static already_AddRefed<nsINode> CloneNodeImpl(nsINode *aNode, bool aDeep, |
238 | | mozilla::ErrorResult& aError); |
239 | | |
240 | | /** |
241 | | * Returns a true if the node is a HTMLTemplate element. |
242 | | * |
243 | | * @param aNode a node to test for HTMLTemplate elementness. |
244 | | */ |
245 | | static bool IsTemplateElement(const nsINode *aNode); |
246 | | |
247 | | /** |
248 | | * Returns the first child of a node or the first child of |
249 | | * a template element's content if the provided node is a |
250 | | * template element. |
251 | | * |
252 | | * @param aNode A node from which to retrieve the first child. |
253 | | */ |
254 | | static nsIContent* GetFirstChildOfTemplateOrNode(nsINode* aNode); |
255 | | |
256 | | private: |
257 | | /** |
258 | | * Walks aNode, its attributes and, if aDeep is true, its descendant nodes. |
259 | | * If aClone is true the nodes will be cloned. If aNewNodeInfoManager is |
260 | | * not null, it is used to create new nodeinfos for the nodes. Also reparents |
261 | | * the XPConnect wrappers for the nodes into aReparentScope if non-null. |
262 | | * aNodesWithProperties will be filled with all the nodes that have |
263 | | * properties. |
264 | | * |
265 | | * @param aNode Node to adopt/clone. |
266 | | * @param aClone If true the node will be cloned and the cloned node will |
267 | | * be returned. |
268 | | * @param aDeep If true the function will be called recursively on |
269 | | * descendants of the node |
270 | | * @param aNewNodeInfoManager The nodeinfo manager to use to create new |
271 | | * nodeinfos for aNode and its attributes and |
272 | | * descendants. May be null if the nodeinfos |
273 | | * shouldn't be changed. |
274 | | * @param aReparentScope Scope into which wrappers should be reparented, or |
275 | | * null if no reparenting should be done. |
276 | | * @param aNodesWithProperties All nodes (from amongst aNode and its |
277 | | * descendants) with properties. If aClone is |
278 | | * true every node will be followed by its |
279 | | * clone. Null can be passed to prevent this from |
280 | | * being populated. |
281 | | * @param aParent If aClone is true the cloned node will be appended to |
282 | | * aParent's children. May be null. If not null then aNode |
283 | | * must be an nsIContent. |
284 | | * @param aError The error, if any. |
285 | | * |
286 | | * @return If aClone is true then the cloned node will be returned, |
287 | | * unless an error occurred. In error conditions, null |
288 | | * will be returned. |
289 | | */ |
290 | | static already_AddRefed<nsINode> |
291 | | CloneAndAdopt(nsINode *aNode, bool aClone, bool aDeep, |
292 | | nsNodeInfoManager* aNewNodeInfoManager, |
293 | | JS::Handle<JSObject*> aReparentScope, |
294 | | nsCOMArray<nsINode>* aNodesWithProperties, |
295 | | nsINode *aParent, mozilla::ErrorResult& aError); |
296 | | |
297 | | enum class AnimationMutationType |
298 | | { |
299 | | Added, |
300 | | Changed, |
301 | | Removed |
302 | | }; |
303 | | /** |
304 | | * Notify the observers of the target of an animation |
305 | | * @param aAnimation The mutated animation. |
306 | | * @param aMutationType The mutation type of this animation. It could be |
307 | | * Added, Changed, or Removed. |
308 | | */ |
309 | | static void AnimationMutated(mozilla::dom::Animation* aAnimation, |
310 | | AnimationMutationType aMutatedType); |
311 | | |
312 | | }; |
313 | | |
314 | | #endif // nsNodeUtils_h___ |