1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * Base class for all element classes as well as nsDocumentFragment. This
8 * provides an implementation of nsIDOMNode, implements nsIContent, provides
9 * utility methods for subclasses, and so forth.
12 #ifndef FragmentOrElement_h___
13 #define FragmentOrElement_h___
15 #include "mozilla/Attributes.h"
16 #include "mozilla/MemoryReporting.h"
17 #include "nsAttrAndChildArray.h" // member
18 #include "nsCycleCollectionParticipant.h" // NS_DECL_CYCLE_*
19 #include "nsIContent.h" // base class
20 #include "nsINodeList.h" // base class
21 #include "nsIWeakReference.h" // base class
22 #include "nsNodeUtils.h" // class member nsNodeUtils::CloneNodeImpl
23 #include "nsIHTMLCollection.h"
25 class ContentUnbinder
;
27 class nsDOMAttributeMap
;
30 class nsICSSDeclaration
;
42 * Class that implements the nsIDOMNodeList interface (a list of children of
43 * the content), by holding a reference to the content and delegating GetLength
44 * and Item to its existing child list.
47 class nsChildContentList MOZ_FINAL
: public nsINodeList
50 explicit nsChildContentList(nsINode
* aNode
)
55 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
56 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(nsChildContentList
)
59 virtual JSObject
* WrapObject(JSContext
*cx
) MOZ_OVERRIDE
;
61 // nsIDOMNodeList interface
62 NS_DECL_NSIDOMNODELIST
64 // nsINodeList interface
65 virtual int32_t IndexOf(nsIContent
* aContent
) MOZ_OVERRIDE
;
66 virtual nsIContent
* Item(uint32_t aIndex
) MOZ_OVERRIDE
;
73 virtual nsINode
* GetParentObject() MOZ_OVERRIDE
79 ~nsChildContentList() {}
81 // The node whose children make up the list (weak reference)
86 * A class that implements nsIWeakReference
89 class nsNodeWeakReference MOZ_FINAL
: public nsIWeakReference
92 explicit nsNodeWeakReference(nsINode
* aNode
)
101 NS_DECL_NSIWEAKREFERENCE
102 virtual size_t SizeOfOnlyThis(mozilla::MallocSizeOf aMallocSizeOf
) const MOZ_OVERRIDE
;
104 void NoticeNodeDestruction()
110 ~nsNodeWeakReference();
116 * Tearoff to use for nodes to implement nsISupportsWeakReference
118 class nsNodeSupportsWeakRefTearoff MOZ_FINAL
: public nsISupportsWeakReference
121 explicit nsNodeSupportsWeakRefTearoff(nsINode
* aNode
)
127 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
129 // nsISupportsWeakReference
130 NS_DECL_NSISUPPORTSWEAKREFERENCE
132 NS_DECL_CYCLE_COLLECTION_CLASS(nsNodeSupportsWeakRefTearoff
)
135 ~nsNodeSupportsWeakRefTearoff() {}
137 nsCOMPtr
<nsINode
> mNode
;
141 * A generic base class for DOM elements, implementing many nsIContent,
142 * nsIDOMNode and nsIDOMElement methods.
150 class FragmentOrElement
: public nsIContent
153 explicit FragmentOrElement(already_AddRefed
<mozilla::dom::NodeInfo
>& aNodeInfo
);
154 explicit FragmentOrElement(already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
);
156 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
158 NS_DECL_SIZEOF_EXCLUDING_THIS
160 // nsINode interface methods
161 virtual uint32_t GetChildCount() const MOZ_OVERRIDE
;
162 virtual nsIContent
*GetChildAt(uint32_t aIndex
) const MOZ_OVERRIDE
;
163 virtual nsIContent
* const * GetChildArray(uint32_t* aChildCount
) const MOZ_OVERRIDE
;
164 virtual int32_t IndexOf(const nsINode
* aPossibleChild
) const MOZ_OVERRIDE
;
165 virtual nsresult
InsertChildAt(nsIContent
* aKid
, uint32_t aIndex
,
166 bool aNotify
) MOZ_OVERRIDE
;
167 virtual void RemoveChildAt(uint32_t aIndex
, bool aNotify
) MOZ_OVERRIDE
;
168 virtual void GetTextContentInternal(nsAString
& aTextContent
,
169 mozilla::ErrorResult
& aError
) MOZ_OVERRIDE
;
170 virtual void SetTextContentInternal(const nsAString
& aTextContent
,
171 mozilla::ErrorResult
& aError
) MOZ_OVERRIDE
;
173 // nsIContent interface methods
174 virtual already_AddRefed
<nsINodeList
> GetChildren(uint32_t aFilter
) MOZ_OVERRIDE
;
175 virtual const nsTextFragment
*GetText() MOZ_OVERRIDE
;
176 virtual uint32_t TextLength() const MOZ_OVERRIDE
;
177 virtual nsresult
SetText(const char16_t
* aBuffer
, uint32_t aLength
,
178 bool aNotify
) MOZ_OVERRIDE
;
179 // Need to implement this here too to avoid hiding.
180 nsresult
SetText(const nsAString
& aStr
, bool aNotify
)
182 return SetText(aStr
.BeginReading(), aStr
.Length(), aNotify
);
184 virtual nsresult
AppendText(const char16_t
* aBuffer
, uint32_t aLength
,
185 bool aNotify
) MOZ_OVERRIDE
;
186 virtual bool TextIsOnlyWhitespace() MOZ_OVERRIDE
;
187 virtual bool HasTextForTranslation() MOZ_OVERRIDE
;
188 virtual void AppendTextTo(nsAString
& aResult
) MOZ_OVERRIDE
;
189 virtual bool AppendTextTo(nsAString
& aResult
,
190 const mozilla::fallible_t
&) MOZ_OVERRIDE NS_WARN_UNUSED_RESULT
;
191 virtual nsIContent
*GetBindingParent() const MOZ_OVERRIDE
;
192 virtual nsXBLBinding
*GetXBLBinding() const MOZ_OVERRIDE
;
193 virtual void SetXBLBinding(nsXBLBinding
* aBinding
,
194 nsBindingManager
* aOldBindingManager
= nullptr) MOZ_OVERRIDE
;
195 virtual ShadowRoot
*GetShadowRoot() const MOZ_OVERRIDE
;
196 virtual ShadowRoot
*GetContainingShadow() const MOZ_OVERRIDE
;
197 virtual nsTArray
<nsIContent
*> &DestInsertionPoints() MOZ_OVERRIDE
;
198 virtual nsTArray
<nsIContent
*> *GetExistingDestInsertionPoints() const MOZ_OVERRIDE
;
199 virtual void SetShadowRoot(ShadowRoot
* aBinding
) MOZ_OVERRIDE
;
200 virtual nsIContent
*GetXBLInsertionParent() const MOZ_OVERRIDE
;
201 virtual void SetXBLInsertionParent(nsIContent
* aContent
) MOZ_OVERRIDE
;
202 virtual bool IsLink(nsIURI
** aURI
) const MOZ_OVERRIDE
;
204 virtual CustomElementData
*GetCustomElementData() const MOZ_OVERRIDE
;
205 virtual void SetCustomElementData(CustomElementData
* aData
) MOZ_OVERRIDE
;
207 virtual void DestroyContent() MOZ_OVERRIDE
;
208 virtual void SaveSubtreeState() MOZ_OVERRIDE
;
210 NS_IMETHOD
WalkContentStyleRules(nsRuleWalker
* aRuleWalker
) MOZ_OVERRIDE
;
212 nsIHTMLCollection
* Children();
213 uint32_t ChildElementCount()
215 return Children()->Length();
219 * Sets the IsElementInStyleScope flag on each element in the subtree rooted
220 * at this node, including any elements reachable through shadow trees.
222 * @param aInStyleScope The flag value to set.
224 void SetIsElementInStyleScopeFlagOnSubtree(bool aInStyleScope
);
228 * If there are listeners for DOMNodeInserted event, fires the event on all
231 static void FireNodeInserted(nsIDocument
* aDoc
,
233 nsTArray
<nsCOMPtr
<nsIContent
> >& aNodes
);
235 NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(FragmentOrElement
)
238 * Fire a DOMNodeRemoved mutation event for all children of this node
240 void FireNodeRemovedForChildren();
242 virtual bool OwnedOnlyByTheDOMTree() MOZ_OVERRIDE
244 uint32_t rc
= mRefCnt
.get();
248 rc
-= mAttrsAndChildren
.ChildCount();
252 virtual bool IsPurple() MOZ_OVERRIDE
254 return mRefCnt
.IsPurple();
257 virtual void RemovePurple() MOZ_OVERRIDE
259 mRefCnt
.RemovePurple();
262 static void ClearContentUnbinder();
263 static bool CanSkip(nsINode
* aNode
, bool aRemovingAllowed
);
264 static bool CanSkipInCC(nsINode
* aNode
);
265 static bool CanSkipThis(nsINode
* aNode
);
266 static void RemoveBlackMarkedNode(nsINode
* aNode
);
267 static void MarkNodeChildren(nsINode
* aNode
);
268 static void InitCCCallbacks();
269 static void MarkUserData(void* aObject
, nsIAtom
* aKey
, void* aChild
,
273 virtual ~FragmentOrElement();
276 * Copy attributes and state to another element
277 * @param aDest the object to copy to
279 nsresult
CopyInnerTo(FragmentOrElement
* aDest
);
282 // Because of a bug in MS C++ compiler nsDOMSlots must be declared public,
283 // otherwise nsXULElement::nsXULSlots doesn't compile.
285 * There are a set of DOM- and scripting-specific instance variables
286 * that may only be instantiated when a content object is accessed
287 * through the DOM. Rather than burn actual slots in the content
288 * objects for each of these instance variables, we put them off
289 * in a side structure that's only allocated when the content is
290 * accessed through the DOM.
292 class nsDOMSlots
: public nsINode::nsSlots
296 virtual ~nsDOMSlots();
298 void Traverse(nsCycleCollectionTraversalCallback
&cb
, bool aIsXUL
);
299 void Unlink(bool aIsXUL
);
301 size_t SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf
) const;
304 * The .style attribute (an interface that forwards to the actual
306 * @see nsGenericHTMLElement::GetStyle
308 nsCOMPtr
<nsICSSDeclaration
> mStyle
;
311 * The .dataset attribute.
312 * @see nsGenericHTMLElement::GetDataset
314 nsDOMStringMap
* mDataset
; // [Weak]
317 * The .undoManager property.
318 * @see nsGenericHTMLElement::GetUndoManager
320 nsRefPtr
<UndoManager
> mUndoManager
;
323 * SMIL Overridde style rules (for SMIL animation of CSS properties)
324 * @see nsIContent::GetSMILOverrideStyle
326 nsCOMPtr
<nsICSSDeclaration
> mSMILOverrideStyle
;
329 * Holds any SMIL override style rules for this element.
331 nsRefPtr
<mozilla::css::StyleRule
> mSMILOverrideStyleRule
;
334 * An object implementing nsIDOMMozNamedAttrMap for this content (attributes)
335 * @see FragmentOrElement::GetAttributes
337 nsRefPtr
<nsDOMAttributeMap
> mAttributeMap
;
341 * The nearest enclosing content node with a binding that created us.
342 * @see FragmentOrElement::GetBindingParent
344 nsIContent
* mBindingParent
; // [Weak]
347 * The controllers of the XUL Element.
349 nsIControllers
* mControllers
; // [OWNER]
353 * An object implementing the .children property for this element.
355 nsRefPtr
<nsContentList
> mChildrenList
;
358 * An object implementing the .classList property for this element.
360 nsRefPtr
<nsDOMTokenList
> mClassList
;
363 * ShadowRoot bound to the element.
365 nsRefPtr
<ShadowRoot
> mShadowRoot
;
368 * The root ShadowRoot of this element if it is in a shadow tree.
370 nsRefPtr
<ShadowRoot
> mContainingShadow
;
373 * An array of web component insertion points to which this element
376 nsTArray
<nsIContent
*> mDestInsertionPoints
;
379 * XBL binding installed on the element.
381 nsRefPtr
<nsXBLBinding
> mXBLBinding
;
384 * XBL binding installed on the lement.
386 nsCOMPtr
<nsIContent
> mXBLInsertionParent
;
389 * Web components custom element data.
391 nsRefPtr
<CustomElementData
> mCustomElementData
;
395 void GetMarkup(bool aIncludeSelf
, nsAString
& aMarkup
);
396 void SetInnerHTMLInternal(const nsAString
& aInnerHTML
, ErrorResult
& aError
);
398 // Override from nsINode
399 virtual nsINode::nsSlots
* CreateSlots() MOZ_OVERRIDE
;
401 nsDOMSlots
*DOMSlots()
403 return static_cast<nsDOMSlots
*>(Slots());
406 nsDOMSlots
*GetExistingDOMSlots() const
408 return static_cast<nsDOMSlots
*>(GetExistingSlots());
412 * Calls SetIsElementInStyleScopeFlagOnSubtree for each shadow tree attached
413 * to this node, which is assumed to be an Element.
415 * @param aInStyleScope The IsElementInStyleScope flag value to set.
417 void SetIsElementInStyleScopeFlagOnShadowTree(bool aInStyleScope
);
419 friend class ::ContentUnbinder
;
421 * Array containing all attributes and children for this element
423 nsAttrAndChildArray mAttrsAndChildren
;
427 } // namespace mozilla
429 #define NS_ELEMENT_INTERFACE_TABLE_TO_MAP_SEGUE \
430 if (NS_SUCCEEDED(rv)) \
433 rv = FragmentOrElement::QueryInterface(aIID, aInstancePtr); \
434 NS_INTERFACE_TABLE_TO_MAP_SEGUE
436 #endif /* FragmentOrElement_h___ */