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/. */
7 #ifndef mozilla_dom_shadowroot_h__
8 #define mozilla_dom_shadowroot_h__
10 #include "mozilla/DOMEventTargetHelper.h"
11 #include "mozilla/dom/DocumentBinding.h"
12 #include "mozilla/dom/DocumentFragment.h"
13 #include "mozilla/dom/DocumentOrShadowRoot.h"
14 #include "mozilla/dom/NameSpaceConstants.h"
15 #include "mozilla/dom/ShadowRootBinding.h"
16 #include "mozilla/ServoBindings.h"
18 #include "nsCycleCollectionParticipant.h"
19 #include "nsStubMutationObserver.h"
20 #include "nsTHashtable.h"
27 class EventChainPreVisitor
;
28 class ServoStyleRuleMap
;
30 enum class StyleRuleChangeKind
: uint32_t;
40 class HTMLInputElement
;
42 class ShadowRoot final
: public DocumentFragment
, public DocumentOrShadowRoot
{
43 friend class DocumentOrShadowRoot
;
45 using Declarative
= Element::ShadowRootDeclarative
;
46 using IsClonable
= Element::ShadowRootClonable
;
49 NS_IMPL_FROMNODE_HELPER(ShadowRoot
, IsShadowRoot());
51 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ShadowRoot
, DocumentFragment
)
52 NS_DECL_ISUPPORTS_INHERITED
54 ShadowRoot(Element
* aElement
, ShadowRootMode aMode
,
55 Element::DelegatesFocus aDelegatesFocus
,
56 SlotAssignmentMode aSlotAssignment
, IsClonable aClonable
,
57 Declarative aDeclarative
,
58 already_AddRefed
<mozilla::dom::NodeInfo
>&& aNodeInfo
);
60 void AddSizeOfExcludingThis(nsWindowSizes
&, size_t* aNodeSize
) const final
;
62 // Try to reassign an element or text to a slot.
63 void MaybeReassignContent(nsIContent
& aElementOrText
);
64 // Called when an element is inserted as a direct child of our host. Tries to
65 // slot the child in one of our slots.
66 void MaybeSlotHostChild(nsIContent
&);
67 // Called when a direct child of our host is removed. Tries to un-slot the
68 // child from the currently-assigned slot, if any.
69 void MaybeUnslotHostChild(nsIContent
&);
72 Element
* Host() const {
74 "ShadowRoot always has a host, how did we create "
79 ShadowRootMode
Mode() const { return mMode
; }
80 bool DelegatesFocus() const {
81 return mDelegatesFocus
== Element::DelegatesFocus::Yes
;
83 SlotAssignmentMode
SlotAssignment() const { return mSlotAssignment
; }
84 bool Clonable() const { return mIsClonable
== IsClonable::Yes
; }
85 bool IsClosed() const { return mMode
== ShadowRootMode::Closed
; }
87 void RemoveSheetFromStyles(StyleSheet
&);
88 void RuleAdded(StyleSheet
&, css::Rule
&);
89 void RuleRemoved(StyleSheet
&, css::Rule
&);
90 void RuleChanged(StyleSheet
&, css::Rule
*, StyleRuleChangeKind
);
91 void ImportRuleLoaded(CSSImportRule
&, StyleSheet
&);
92 void SheetCloned(StyleSheet
&);
93 void StyleSheetApplicableStateChanged(StyleSheet
&);
96 * Clones internal state, for example stylesheets, of aOther to 'this'.
98 void CloneInternalDataFrom(ShadowRoot
* aOther
);
99 void InsertSheetAt(size_t aIndex
, StyleSheet
&);
101 // Calls UnbindFromTree for each of our kids, and also flags us as no longer
105 // Only intended for UA widgets / special shadow roots, or for handling
106 // failure cases when adopting (see BlastSubtreeToPieces).
108 // Forgets our shadow host and unbinds all our kids.
111 // Calls BindToTree on each of our kids, and also maybe flags us as being
116 * Explicitly invalidates the style and layout of the flattened-tree subtree
117 * rooted at the element.
119 * You need to use this whenever the flat tree is going to be shuffled in a
120 * way that layout doesn't understand via the usual ContentInserted /
121 * ContentAppended / ContentRemoved notifications. For example, if removing an
122 * element will cause a change in the flat tree such that other element will
123 * start showing up (like fallback content), this method needs to be called on
124 * an ancestor of that element.
126 * It is important that this runs _before_ actually shuffling the flat tree
127 * around, so that layout knows the actual tree that it needs to invalidate.
129 void InvalidateStyleAndLayoutOnSubtree(Element
*);
132 void InsertSheetIntoAuthorData(size_t aIndex
, StyleSheet
&,
133 const nsTArray
<RefPtr
<StyleSheet
>>&);
135 void AppendStyleSheet(StyleSheet
& aSheet
) {
136 InsertSheetAt(SheetCount(), aSheet
);
140 * Represents the insertion point in a slot for a given node.
142 struct SlotInsertionPoint
{
143 HTMLSlotElement
* mSlot
= nullptr;
144 Maybe
<uint32_t> mIndex
;
146 SlotInsertionPoint() = default;
147 SlotInsertionPoint(HTMLSlotElement
* aSlot
, const Maybe
<uint32_t>& aIndex
)
148 : mSlot(aSlot
), mIndex(aIndex
) {}
152 * Return the assignment corresponding to the content node at this particular
155 * It's the caller's responsibility to actually call InsertAssignedNode /
156 * AppendAssignedNode in the slot as needed.
158 SlotInsertionPoint
SlotInsertionPointFor(nsIContent
&);
161 * Returns the effective slot name for a given slottable. In most cases, this
162 * is just the value of the slot attribute, if any, or the empty string, but
163 * this also deals with the <details> shadow tree specially.
165 void GetSlotNameFor(const nsIContent
&, nsAString
&) const;
168 * Re-assign the current main summary if it has changed.
170 * Must be called only if mIsDetailsShadowTree is true.
172 enum class SummaryChangeReason
{ Deletion
, Insertion
};
173 void MaybeReassignMainSummary(SummaryChangeReason
);
176 void AddSlot(HTMLSlotElement
* aSlot
);
177 void RemoveSlot(HTMLSlotElement
* aSlot
);
178 bool HasSlots() const { return !mSlotMap
.IsEmpty(); };
179 HTMLSlotElement
* GetDefaultSlot() const {
180 SlotArray
* list
= mSlotMap
.Get(u
""_ns
);
181 return list
? (*list
)->ElementAt(0) : nullptr;
184 void PartAdded(const Element
&);
185 void PartRemoved(const Element
&);
187 IMPL_EVENT_HANDLER(slotchange
);
189 const nsTArray
<const Element
*>& Parts() const { return mParts
; }
191 const StyleAuthorStyles
* GetServoStyles() const { return mServoStyles
.get(); }
193 StyleAuthorStyles
* GetServoStyles() { return mServoStyles
.get(); }
195 mozilla::ServoStyleRuleMap
& ServoStyleRuleMap();
197 JSObject
* WrapNode(JSContext
*, JS::Handle
<JSObject
*> aGivenProto
) final
;
199 void NodeInfoChanged(Document
* aOldDoc
) override
;
201 void AddToIdTable(Element
* aElement
, nsAtom
* aId
);
202 void RemoveFromIdTable(Element
* aElement
, nsAtom
* aId
);
205 using mozilla::dom::DocumentOrShadowRoot::GetElementById
;
207 Element
* GetActiveElement();
210 * These methods allow UA Widget to insert DOM elements into the Shadow ROM
211 * without putting their DOM reflectors to content scope first.
212 * The inserted DOM will have their reflectors in the UA Widget scope.
214 nsINode
* ImportNodeAndAppendChildAt(nsINode
& aParentNode
, nsINode
& aNode
,
215 bool aDeep
, mozilla::ErrorResult
& rv
);
217 nsINode
* CreateElementAndAppendChildAt(nsINode
& aParentNode
,
218 const nsAString
& aTagName
,
219 mozilla::ErrorResult
& rv
);
221 bool IsUAWidget() const { return HasBeenInUAWidget(); }
223 void SetIsUAWidget() {
224 MOZ_ASSERT(!HasChildren());
225 SetIsNativeAnonymousRoot();
226 SetFlags(NODE_HAS_BEEN_IN_UA_WIDGET
);
229 bool IsAvailableToElementInternals() const {
230 return mIsAvailableToElementInternals
;
233 void SetAvailableToElementInternals() {
234 mIsAvailableToElementInternals
= true;
237 void GetEventTargetParent(EventChainPreVisitor
& aVisitor
) override
;
239 bool IsDeclarative() const { return mIsDeclarative
== Declarative::Yes
; }
240 void SetIsDeclarative(Declarative aIsDeclarative
) {
241 mIsDeclarative
= aIsDeclarative
;
243 void SetIsDeclarative(bool aIsDeclarative
) {
244 mIsDeclarative
= aIsDeclarative
? Declarative::Yes
: Declarative::No
;
248 void SetHTMLUnsafe(const nsAString
& aHTML
);
251 // FIXME(emilio): This will need to become more fine-grained.
252 void ApplicableRulesChanged();
254 virtual ~ShadowRoot();
256 // Make sure that the first field is pointer-aligned so it doesn't get packed
257 // in the base class' padding, since otherwise rust-bindgen can't generate
258 // correct bindings for it, see
259 // https://github.com/rust-lang/rust-bindgen/issues/380
261 // The computed data from the style sheets.
262 UniquePtr
<StyleAuthorStyles
> mServoStyles
;
263 UniquePtr
<mozilla::ServoStyleRuleMap
> mStyleRuleMap
;
265 using SlotArray
= TreeOrderedArray
<HTMLSlotElement
*>;
266 // Map from name of slot to an array of all slots in the shadow DOM with with
267 // the given name. The slots are stored as a weak pointer because the elements
268 // are in the shadow tree and should be kept alive by its parent.
269 nsClassHashtable
<nsStringHashKey
, SlotArray
> mSlotMap
;
271 // Unordered array of all elements that have a part attribute in this shadow
273 nsTArray
<const Element
*> mParts
;
275 const ShadowRootMode mMode
;
277 Element::DelegatesFocus mDelegatesFocus
;
279 const SlotAssignmentMode mSlotAssignment
;
281 // Whether this is the <details> internal shadow tree.
282 bool mIsDetailsShadowTree
: 1;
284 // https://dom.spec.whatwg.org/#shadowroot-available-to-element-internals
285 bool mIsAvailableToElementInternals
: 1;
287 // https://dom.spec.whatwg.org/#shadowroot-declarative
288 Declarative mIsDeclarative
;
290 // https://dom.spec.whatwg.org/#shadowroot-clonable
291 const IsClonable mIsClonable
;
293 nsresult
Clone(dom::NodeInfo
*, nsINode
** aResult
) const override
;
297 } // namespace mozilla
299 #endif // mozilla_dom_shadowroot_h__