Backed out 2 changesets (bug 1900622) for causing Bug 1908553 and ktlint failure...
[gecko.git] / dom / base / ShadowRoot.h
blob9cc8290925ca2eeb4421bd5d0d468350209e6ae8
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"
17 #include "nsCOMPtr.h"
18 #include "nsCycleCollectionParticipant.h"
19 #include "nsStubMutationObserver.h"
20 #include "nsTHashtable.h"
22 class nsAtom;
23 class nsIContent;
25 namespace mozilla {
27 class EventChainPreVisitor;
28 class ServoStyleRuleMap;
30 enum class StyleRuleChangeKind : uint32_t;
32 namespace css {
33 class Rule;
36 namespace dom {
38 class CSSImportRule;
39 class Element;
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;
47 using IsSerializable = Element::ShadowRootSerializable;
49 public:
50 NS_IMPL_FROMNODE_HELPER(ShadowRoot, IsShadowRoot());
52 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ShadowRoot, DocumentFragment)
53 NS_DECL_ISUPPORTS_INHERITED
55 ShadowRoot(Element* aElement, ShadowRootMode aMode,
56 Element::DelegatesFocus aDelegatesFocus,
57 SlotAssignmentMode aSlotAssignment, IsClonable aClonable,
58 IsSerializable aIsSerializable, Declarative aDeclarative,
59 already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo);
61 void AddSizeOfExcludingThis(nsWindowSizes&, size_t* aNodeSize) const final;
63 // Try to reassign an element or text to a slot.
64 void MaybeReassignContent(nsIContent& aElementOrText);
65 // Called when an element is inserted as a direct child of our host. Tries to
66 // slot the child in one of our slots.
67 void MaybeSlotHostChild(nsIContent&);
68 // Called when a direct child of our host is removed. Tries to un-slot the
69 // child from the currently-assigned slot, if any.
70 void MaybeUnslotHostChild(nsIContent&);
72 // Shadow DOM v1
73 Element* Host() const {
74 MOZ_ASSERT(GetHost(),
75 "ShadowRoot always has a host, how did we create "
76 "this ShadowRoot?");
77 return GetHost();
80 ShadowRootMode Mode() const { return mMode; }
81 bool DelegatesFocus() const {
82 return mDelegatesFocus == Element::DelegatesFocus::Yes;
84 SlotAssignmentMode SlotAssignment() const { return mSlotAssignment; }
85 bool Clonable() const { return mIsClonable == IsClonable::Yes; }
86 bool IsClosed() const { return mMode == ShadowRootMode::Closed; }
87 bool Serializable() const { return mIsSerializable == IsSerializable::Yes; }
89 void RemoveSheetFromStyles(StyleSheet&);
90 void RuleAdded(StyleSheet&, css::Rule&);
91 void RuleRemoved(StyleSheet&, css::Rule&);
92 void RuleChanged(StyleSheet&, css::Rule*, StyleRuleChangeKind);
93 void ImportRuleLoaded(CSSImportRule&, StyleSheet&);
94 void SheetCloned(StyleSheet&);
95 void StyleSheetApplicableStateChanged(StyleSheet&);
97 /**
98 * Clones internal state, for example stylesheets, of aOther to 'this'.
100 void CloneInternalDataFrom(ShadowRoot* aOther);
101 void InsertSheetAt(size_t aIndex, StyleSheet&);
103 // Calls UnbindFromTree for each of our kids, and also flags us as no longer
104 // being connected.
105 void Unbind();
107 // Only intended for UA widgets / special shadow roots, or for handling
108 // failure cases when adopting (see BlastSubtreeToPieces).
110 // Forgets our shadow host and unbinds all our kids.
111 void Unattach();
113 // Calls BindToTree on each of our kids, and also maybe flags us as being
114 // connected.
115 nsresult Bind();
118 * Explicitly invalidates the style and layout of the flattened-tree subtree
119 * rooted at the element.
121 * You need to use this whenever the flat tree is going to be shuffled in a
122 * way that layout doesn't understand via the usual ContentInserted /
123 * ContentAppended / ContentRemoved notifications. For example, if removing an
124 * element will cause a change in the flat tree such that other element will
125 * start showing up (like fallback content), this method needs to be called on
126 * an ancestor of that element.
128 * It is important that this runs _before_ actually shuffling the flat tree
129 * around, so that layout knows the actual tree that it needs to invalidate.
131 void InvalidateStyleAndLayoutOnSubtree(Element*);
133 private:
134 void InsertSheetIntoAuthorData(size_t aIndex, StyleSheet&,
135 const nsTArray<RefPtr<StyleSheet>>&);
137 void AppendStyleSheet(StyleSheet& aSheet) {
138 InsertSheetAt(SheetCount(), aSheet);
142 * Represents the insertion point in a slot for a given node.
144 struct SlotInsertionPoint {
145 HTMLSlotElement* mSlot = nullptr;
146 Maybe<uint32_t> mIndex;
148 SlotInsertionPoint() = default;
149 SlotInsertionPoint(HTMLSlotElement* aSlot, const Maybe<uint32_t>& aIndex)
150 : mSlot(aSlot), mIndex(aIndex) {}
154 * Return the assignment corresponding to the content node at this particular
155 * point in time.
157 * It's the caller's responsibility to actually call InsertAssignedNode /
158 * AppendAssignedNode in the slot as needed.
160 SlotInsertionPoint SlotInsertionPointFor(nsIContent&);
163 * Returns the effective slot name for a given slottable. In most cases, this
164 * is just the value of the slot attribute, if any, or the empty string, but
165 * this also deals with the <details> shadow tree specially.
167 void GetSlotNameFor(const nsIContent&, nsAString&) const;
170 * Re-assign the current main summary if it has changed.
172 * Must be called only if mIsDetailsShadowTree is true.
174 enum class SummaryChangeReason { Deletion, Insertion };
175 void MaybeReassignMainSummary(SummaryChangeReason);
177 public:
178 void AddSlot(HTMLSlotElement* aSlot);
179 void RemoveSlot(HTMLSlotElement* aSlot);
180 bool HasSlots() const { return !mSlotMap.IsEmpty(); };
181 HTMLSlotElement* GetDefaultSlot() const {
182 SlotArray* list = mSlotMap.Get(u""_ns);
183 return list ? (*list)->ElementAt(0) : nullptr;
186 void PartAdded(const Element&);
187 void PartRemoved(const Element&);
189 IMPL_EVENT_HANDLER(slotchange);
191 const nsTArray<const Element*>& Parts() const { return mParts; }
193 const StyleAuthorStyles* GetServoStyles() const { return mServoStyles.get(); }
195 StyleAuthorStyles* GetServoStyles() { return mServoStyles.get(); }
197 mozilla::ServoStyleRuleMap& ServoStyleRuleMap();
199 JSObject* WrapNode(JSContext*, JS::Handle<JSObject*> aGivenProto) final;
201 void NodeInfoChanged(Document* aOldDoc) override;
203 void AddToIdTable(Element* aElement, nsAtom* aId);
204 void RemoveFromIdTable(Element* aElement, nsAtom* aId);
206 // WebIDL methods.
207 using mozilla::dom::DocumentOrShadowRoot::GetElementById;
209 Element* GetActiveElement();
212 * These methods allow UA Widget to insert DOM elements into the Shadow ROM
213 * without putting their DOM reflectors to content scope first.
214 * The inserted DOM will have their reflectors in the UA Widget scope.
216 nsINode* ImportNodeAndAppendChildAt(nsINode& aParentNode, nsINode& aNode,
217 bool aDeep, mozilla::ErrorResult& rv);
219 nsINode* CreateElementAndAppendChildAt(nsINode& aParentNode,
220 const nsAString& aTagName,
221 mozilla::ErrorResult& rv);
223 bool IsUAWidget() const { return HasBeenInUAWidget(); }
225 void SetIsUAWidget() {
226 MOZ_ASSERT(!HasChildren());
227 SetIsNativeAnonymousRoot();
228 SetFlags(NODE_HAS_BEEN_IN_UA_WIDGET);
231 bool IsAvailableToElementInternals() const {
232 return mIsAvailableToElementInternals;
235 void SetAvailableToElementInternals() {
236 mIsAvailableToElementInternals = true;
239 void GetEventTargetParent(EventChainPreVisitor& aVisitor) override;
241 bool IsDeclarative() const { return mIsDeclarative == Declarative::Yes; }
242 void SetIsDeclarative(Declarative aIsDeclarative) {
243 mIsDeclarative = aIsDeclarative;
245 void SetIsDeclarative(bool aIsDeclarative) {
246 mIsDeclarative = aIsDeclarative ? Declarative::Yes : Declarative::No;
249 MOZ_CAN_RUN_SCRIPT
250 void SetHTMLUnsafe(const nsAString& aHTML);
252 void GetHTML(const GetHTMLOptions& aOptions, nsAString& aResult);
254 protected:
255 // FIXME(emilio): This will need to become more fine-grained.
256 void ApplicableRulesChanged();
258 virtual ~ShadowRoot();
260 // Make sure that the first field is pointer-aligned so it doesn't get packed
261 // in the base class' padding, since otherwise rust-bindgen can't generate
262 // correct bindings for it, see
263 // https://github.com/rust-lang/rust-bindgen/issues/380
265 // The computed data from the style sheets.
266 UniquePtr<StyleAuthorStyles> mServoStyles;
267 UniquePtr<mozilla::ServoStyleRuleMap> mStyleRuleMap;
269 using SlotArray = TreeOrderedArray<HTMLSlotElement*>;
270 // Map from name of slot to an array of all slots in the shadow DOM with with
271 // the given name. The slots are stored as a weak pointer because the elements
272 // are in the shadow tree and should be kept alive by its parent.
273 nsClassHashtable<nsStringHashKey, SlotArray> mSlotMap;
275 // Unordered array of all elements that have a part attribute in this shadow
276 // tree.
277 nsTArray<const Element*> mParts;
279 const ShadowRootMode mMode;
281 Element::DelegatesFocus mDelegatesFocus;
283 const SlotAssignmentMode mSlotAssignment;
285 // Whether this is the <details> internal shadow tree.
286 bool mIsDetailsShadowTree : 1;
288 // https://dom.spec.whatwg.org/#shadowroot-available-to-element-internals
289 bool mIsAvailableToElementInternals : 1;
291 // https://dom.spec.whatwg.org/#shadowroot-declarative
292 Declarative mIsDeclarative;
294 // https://dom.spec.whatwg.org/#shadowroot-clonable
295 const IsClonable mIsClonable;
297 // https://dom.spec.whatwg.org/#shadowroot-serializable
298 const IsSerializable mIsSerializable;
300 nsresult Clone(dom::NodeInfo*, nsINode** aResult) const override;
303 } // namespace dom
304 } // namespace mozilla
306 #endif // mozilla_dom_shadowroot_h__