diff options
Diffstat (limited to 'dom/base/ShadowRoot.h')
-rw-r--r-- | dom/base/ShadowRoot.h | 168 |
1 files changed, 61 insertions, 107 deletions
diff --git a/dom/base/ShadowRoot.h b/dom/base/ShadowRoot.h index f84078134..775eaae95 100644 --- a/dom/base/ShadowRoot.h +++ b/dom/base/ShadowRoot.h @@ -1,5 +1,4 @@ /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim: set ts=8 sts=2 et sw=2 tw=80: */ /* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -8,30 +7,30 @@ #define mozilla_dom_shadowroot_h__ #include "mozilla/dom/DocumentFragment.h" -#include "mozilla/dom/StyleSheetList.h" -#include "mozilla/StyleSheet.h" +#include "mozilla/dom/DocumentOrShadowRoot.h" #include "nsCOMPtr.h" #include "nsCycleCollectionParticipant.h" #include "nsIContentInlines.h" +#include "nsIdentifierMapEntry.h" #include "nsTHashtable.h" -#include "nsDocument.h" +#include "nsXBLBinding.h" class nsIAtom; class nsIContent; class nsXBLPrototypeBinding; namespace mozilla { + +class EventChainPreVisitor; + namespace dom { class Element; -class HTMLContentElement; -class HTMLShadowElement; -class ShadowRootStyleSheetList; class ShadowRoot final : public DocumentFragment, + public DocumentOrShadowRoot, public nsStubMutationObserver { - friend class ShadowRootStyleSheetList; public: NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ShadowRoot, DocumentFragment) @@ -42,90 +41,88 @@ public: NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED - ShadowRoot(nsIContent* aContent, already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, + ShadowRoot(Element* aElement, bool aClosed, + already_AddRefed<mozilla::dom::NodeInfo>&& aNodeInfo, nsXBLPrototypeBinding* aProtoBinding); - void AddToIdTable(Element* aElement, nsIAtom* aId); - void RemoveFromIdTable(Element* aElement, nsIAtom* aId); + // Shadow DOM v1 + Element* Host(); + ShadowRootMode Mode() const + { + return mMode; + } + bool IsClosed() const + { + return mMode == ShadowRootMode::Closed; + } + + // [deprecated] Shadow DOM v0 void InsertSheet(StyleSheet* aSheet, nsIContent* aLinkingContent); void RemoveSheet(StyleSheet* aSheet); bool ApplyAuthorStyles(); void SetApplyAuthorStyles(bool aApplyAuthorStyles); - StyleSheetList* StyleSheets(); - HTMLShadowElement* GetShadowElement() { return mShadowElement; } + StyleSheetList* StyleSheets() + { + return &DocumentOrShadowRoot::EnsureDOMStyleSheets(); + } /** - * Sets the current shadow insertion point where the older - * ShadowRoot will be projected. + * Distributes all the explicit children of the pool host to the content + * insertion points in this ShadowRoot. */ - void SetShadowElement(HTMLShadowElement* aShadowElement); + void DistributeAllNodes(); +private: /** - * Change the node that populates the distribution pool with - * its children. This is distinct from the ShadowRoot host described - * in the specifications. The ShadowRoot host is the element - * which created this ShadowRoot and does not change. The pool host - * is the same as the ShadowRoot host if this is the youngest - * ShadowRoot. If this is an older ShadowRoot, the pool host is - * the <shadow> element in the younger ShadowRoot (if it exists). + * Try to reassign an element to a slot and returns whether the assignment + * changed. */ - void ChangePoolHost(nsIContent* aNewHost); + bool MaybeReassignElement(Element* aElement, const nsAttrValue* aOldValue); /** - * Distributes a single explicit child of the pool host to the content - * insertion points in this ShadowRoot. + * Try to assign aContent to a slot in the shadow tree, returns the assigned + * slot if found. */ - void DistributeSingleNode(nsIContent* aContent); + const HTMLSlotElement* AssignSlotFor(nsIContent* aContent); /** - * Removes a single explicit child of the pool host from the content - * insertion points in this ShadowRoot. + * Unassign aContent from the assigned slot in the shadow tree, returns the + * assigned slot if found. + * + * Note: slot attribute of aContent may have changed already, so pass slot + * name explicity here. */ - void RemoveDistributedNode(nsIContent* aContent); + const HTMLSlotElement* UnassignSlotFor(nsIContent* aContent, + const nsAString& aSlotName); /** - * Distributes all the explicit children of the pool host to the content - * insertion points in this ShadowRoot. + * Called when we redistribute content after insertion points have changed. */ - void DistributeAllNodes(); + void DistributionChanged(); + + bool IsPooledNode(nsIContent* aChild) const; - void AddInsertionPoint(HTMLContentElement* aInsertionPoint); - void RemoveInsertionPoint(HTMLContentElement* aInsertionPoint); +public: + void AddSlot(HTMLSlotElement* aSlot); + void RemoveSlot(HTMLSlotElement* aSlot); - void SetYoungerShadow(ShadowRoot* aYoungerShadow); - ShadowRoot* GetYoungerShadowRoot() { return mYoungerShadow; } void SetInsertionPointChanged() { mInsertionPointChanged = true; } void SetAssociatedBinding(nsXBLBinding* aBinding) { mAssociatedBinding = aBinding; } - nsISupports* GetParentObject() const { return mPoolHost; } - - nsIContent* GetPoolHost() { return mPoolHost; } - nsTArray<HTMLShadowElement*>& ShadowDescendants() { return mShadowDescendants; } - JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override; - static bool IsPooledNode(nsIContent* aChild, nsIContent* aContainer, - nsIContent* aHost); static ShadowRoot* FromNode(nsINode* aNode); - static bool IsShadowInsertionPoint(nsIContent* aContent); - static void RemoveDestInsertionPoint(nsIContent* aInsertionPoint, - nsTArray<nsIContent*>& aDestInsertionPoints); + void AddToIdTable(Element* aElement, nsIAtom* aId); + void RemoveFromIdTable(Element* aElement, nsIAtom* aId); // WebIDL methods. - Element* GetElementById(const nsAString& aElementId); - already_AddRefed<nsContentList> - GetElementsByTagName(const nsAString& aNamespaceURI); - already_AddRefed<nsContentList> - GetElementsByTagNameNS(const nsAString& aNamespaceURI, - const nsAString& aLocalName); - already_AddRefed<nsContentList> - GetElementsByClassName(const nsAString& aClasses); + using mozilla::dom::DocumentOrShadowRoot::GetElementById; + + Element* GetActiveElement(); void GetInnerHTML(nsAString& aInnerHTML); void SetInnerHTML(const nsAString& aInnerHTML, ErrorResult& aError); - Element* Host(); - ShadowRoot* GetOlderShadowRoot() { return mOlderShadow; } void StyleSheetChanged(); bool IsComposedDocParticipant() { return mIsComposedDocParticipant; } @@ -134,26 +131,18 @@ public: mIsComposedDocParticipant = aIsComposedDocParticipant; } - virtual void DestroyContent() override; + nsresult GetEventTargetParent(EventChainPreVisitor& aVisitor) override; + protected: virtual ~ShadowRoot(); - // The pool host is the parent of the nodes that will be distributed - // into the insertion points in this ShadowRoot. See |ChangeShadowRoot|. - nsCOMPtr<nsIContent> mPoolHost; - - // An array of content insertion points that are a descendant of the ShadowRoot - // sorted in tree order. Insertion points are responsible for notifying - // the ShadowRoot when they are removed or added as a descendant. The insertion - // points are kept alive by the parent node, thus weak references are held - // by the array. - nsTArray<HTMLContentElement*> mInsertionPoints; + ShadowRootMode mMode; - // An array of the <shadow> elements that are descendant of the ShadowRoot - // sorted in tree order. Only the first may be a shadow insertion point. - nsTArray<HTMLShadowElement*> mShadowDescendants; + // Map from name of slot to an array of all slots in the shadow DOM with with + // the given name. The slots are stored as a weak pointer because the elements + // are in the shadow tree and should be kept alive by its parent. + nsClassHashtable<nsStringHashKey, nsTArray<mozilla::dom::HTMLSlotElement*>> mSlotMap; - nsTHashtable<nsIdentifierMapEntry> mIdentifierMap; nsXBLPrototypeBinding* mProtoBinding; // It is necessary to hold a reference to the associated nsXBLBinding @@ -161,19 +150,6 @@ protected: // owns |mProtoBinding|. RefPtr<nsXBLBinding> mAssociatedBinding; - RefPtr<ShadowRootStyleSheetList> mStyleSheetList; - - // The current shadow insertion point of this ShadowRoot. - HTMLShadowElement* mShadowElement; - - // The ShadowRoot that was created by the host element before - // this ShadowRoot was created. - RefPtr<ShadowRoot> mOlderShadow; - - // The ShadowRoot that was created by the host element after - // this ShadowRoot was created. - RefPtr<ShadowRoot> mYoungerShadow; - // A boolean that indicates that an insertion point was added or removed // from this ShadowRoot and that the nodes need to be redistributed into // the insertion points. After this flag is set, nodes will be distributed @@ -189,28 +165,6 @@ protected: nsresult Clone(mozilla::dom::NodeInfo *aNodeInfo, nsINode **aResult) const override; }; -class ShadowRootStyleSheetList : public StyleSheetList -{ -public: - explicit ShadowRootStyleSheetList(ShadowRoot* aShadowRoot); - - NS_DECL_ISUPPORTS_INHERITED - NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ShadowRootStyleSheetList, StyleSheetList) - - virtual nsINode* GetParentObject() const override - { - return mShadowRoot; - } - - uint32_t Length() override; - StyleSheet* IndexedGetter(uint32_t aIndex, bool& aFound) override; - -protected: - virtual ~ShadowRootStyleSheetList(); - - RefPtr<ShadowRoot> mShadowRoot; -}; - } // namespace dom } // namespace mozilla |