content/base/src/ShadowRoot.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/base/src/ShadowRoot.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,198 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef mozilla_dom_shadowroot_h__
    1.10 +#define mozilla_dom_shadowroot_h__
    1.11 +
    1.12 +#include "mozilla/dom/DocumentFragment.h"
    1.13 +#include "mozilla/dom/StyleSheetList.h"
    1.14 +#include "nsCOMPtr.h"
    1.15 +#include "nsCycleCollectionParticipant.h"
    1.16 +#include "nsTHashtable.h"
    1.17 +#include "nsDocument.h"
    1.18 +
    1.19 +class nsIAtom;
    1.20 +class nsIContent;
    1.21 +class nsIDocument;
    1.22 +class nsINodeInfo;
    1.23 +class nsPIDOMWindow;
    1.24 +class nsXBLPrototypeBinding;
    1.25 +class nsTagNameMapEntry;
    1.26 +
    1.27 +namespace mozilla {
    1.28 +namespace dom {
    1.29 +
    1.30 +class Element;
    1.31 +class HTMLContentElement;
    1.32 +class HTMLShadowElement;
    1.33 +class ShadowRootStyleSheetList;
    1.34 +
    1.35 +class ShadowRoot : public DocumentFragment,
    1.36 +                   public nsStubMutationObserver
    1.37 +{
    1.38 +  friend class ShadowRootStyleSheetList;
    1.39 +public:
    1.40 +  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ShadowRoot,
    1.41 +                                           DocumentFragment)
    1.42 +  NS_DECL_ISUPPORTS_INHERITED
    1.43 +
    1.44 +  NS_DECL_NSIMUTATIONOBSERVER_ATTRIBUTECHANGED
    1.45 +  NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
    1.46 +  NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
    1.47 +  NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
    1.48 +
    1.49 +  ShadowRoot(nsIContent* aContent, already_AddRefed<nsINodeInfo>&& aNodeInfo,
    1.50 +             nsXBLPrototypeBinding* aProtoBinding);
    1.51 +  virtual ~ShadowRoot();
    1.52 +
    1.53 +  void AddToIdTable(Element* aElement, nsIAtom* aId);
    1.54 +  void RemoveFromIdTable(Element* aElement, nsIAtom* aId);
    1.55 +  void InsertSheet(nsCSSStyleSheet* aSheet, nsIContent* aLinkingContent);
    1.56 +  void RemoveSheet(nsCSSStyleSheet* aSheet);
    1.57 +  bool ApplyAuthorStyles();
    1.58 +  void SetApplyAuthorStyles(bool aApplyAuthorStyles);
    1.59 +  StyleSheetList* StyleSheets();
    1.60 +  HTMLShadowElement* GetShadowElement() { return mShadowElement; }
    1.61 +
    1.62 +  /**
    1.63 +   * Sets the current shadow insertion point where the older
    1.64 +   * ShadowRoot will be projected.
    1.65 +   */
    1.66 +  void SetShadowElement(HTMLShadowElement* aShadowElement);
    1.67 +
    1.68 +  /**
    1.69 +   * Change the node that populates the distribution pool with
    1.70 +   * its children. This is distinct from the ShadowRoot host described
    1.71 +   * in the specifications. The ShadowRoot host is the element
    1.72 +   * which created this ShadowRoot and does not change. The pool host
    1.73 +   * is the same as the ShadowRoot host if this is the youngest
    1.74 +   * ShadowRoot. If this is an older ShadowRoot, the pool host is
    1.75 +   * the <shadow> element in the younger ShadowRoot (if it exists).
    1.76 +   */
    1.77 +  void ChangePoolHost(nsIContent* aNewHost);
    1.78 +
    1.79 +  /**
    1.80 +   * Distributes a single explicit child of the pool host to the content
    1.81 +   * insertion points in this ShadowRoot.
    1.82 +   */
    1.83 +  void DistributeSingleNode(nsIContent* aContent);
    1.84 +
    1.85 +  /**
    1.86 +   * Removes a single explicit child of the pool host from the content
    1.87 +   * insertion points in this ShadowRoot.
    1.88 +   */
    1.89 +  void RemoveDistributedNode(nsIContent* aContent);
    1.90 +
    1.91 +  /**
    1.92 +   * Distributes all the explicit children of the pool host to the content
    1.93 +   * insertion points in this ShadowRoot.
    1.94 +   */
    1.95 +  void DistributeAllNodes();
    1.96 +
    1.97 +  void AddInsertionPoint(HTMLContentElement* aInsertionPoint);
    1.98 +  void RemoveInsertionPoint(HTMLContentElement* aInsertionPoint);
    1.99 +
   1.100 +  void SetYoungerShadow(ShadowRoot* aYoungerShadow);
   1.101 +  ShadowRoot* GetOlderShadow() { return mOlderShadow; }
   1.102 +  ShadowRoot* GetYoungerShadow() { return mYoungerShadow; }
   1.103 +  void SetInsertionPointChanged() { mInsertionPointChanged = true; }
   1.104 +
   1.105 +  void SetAssociatedBinding(nsXBLBinding* aBinding) { mAssociatedBinding = aBinding; }
   1.106 +
   1.107 +  nsISupports* GetParentObject() const { return mPoolHost; }
   1.108 +
   1.109 +  nsIContent* GetPoolHost() { return mPoolHost; }
   1.110 +  nsTArray<HTMLShadowElement*>& ShadowDescendants() { return mShadowDescendants; }
   1.111 +
   1.112 +  JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE;
   1.113 +
   1.114 +  static bool IsPooledNode(nsIContent* aChild, nsIContent* aContainer,
   1.115 +                           nsIContent* aHost);
   1.116 +  static ShadowRoot* FromNode(nsINode* aNode);
   1.117 +  static bool IsShadowInsertionPoint(nsIContent* aContent);
   1.118 +
   1.119 +  // WebIDL methods.
   1.120 +  Element* GetElementById(const nsAString& aElementId);
   1.121 +  already_AddRefed<nsContentList>
   1.122 +    GetElementsByTagName(const nsAString& aNamespaceURI);
   1.123 +  already_AddRefed<nsContentList>
   1.124 +    GetElementsByTagNameNS(const nsAString& aNamespaceURI,
   1.125 +                           const nsAString& aLocalName);
   1.126 +  already_AddRefed<nsContentList>
   1.127 +    GetElementsByClassName(const nsAString& aClasses);
   1.128 +  void GetInnerHTML(nsAString& aInnerHTML);
   1.129 +  void SetInnerHTML(const nsAString& aInnerHTML, ErrorResult& aError);
   1.130 +protected:
   1.131 +  void Restyle();
   1.132 +
   1.133 +  // The pool host is the parent of the nodes that will be distributed
   1.134 +  // into the insertion points in this ShadowRoot. See |ChangeShadowRoot|.
   1.135 +  nsCOMPtr<nsIContent> mPoolHost;
   1.136 +
   1.137 +  // An array of content insertion points that are a descendant of the ShadowRoot
   1.138 +  // sorted in tree order. Insertion points are responsible for notifying
   1.139 +  // the ShadowRoot when they are removed or added as a descendant. The insertion
   1.140 +  // points are kept alive by the parent node, thus weak references are held
   1.141 +  // by the array.
   1.142 +  nsTArray<HTMLContentElement*> mInsertionPoints;
   1.143 +
   1.144 +  // An array of the <shadow> elements that are descendant of the ShadowRoot
   1.145 +  // sorted in tree order. Only the first may be a shadow insertion point.
   1.146 +  nsTArray<HTMLShadowElement*> mShadowDescendants;
   1.147 +
   1.148 +  nsTHashtable<nsIdentifierMapEntry> mIdentifierMap;
   1.149 +  nsXBLPrototypeBinding* mProtoBinding;
   1.150 +
   1.151 +  // It is necessary to hold a reference to the associated nsXBLBinding
   1.152 +  // because the binding holds a reference on the nsXBLDocumentInfo that
   1.153 +  // owns |mProtoBinding|.
   1.154 +  nsRefPtr<nsXBLBinding> mAssociatedBinding;
   1.155 +
   1.156 +  nsRefPtr<ShadowRootStyleSheetList> mStyleSheetList;
   1.157 +
   1.158 +  // The current shadow insertion point of this ShadowRoot.
   1.159 +  HTMLShadowElement* mShadowElement;
   1.160 +
   1.161 +  // The ShadowRoot that was created by the host element before
   1.162 +  // this ShadowRoot was created.
   1.163 +  nsRefPtr<ShadowRoot> mOlderShadow;
   1.164 +
   1.165 +  // The ShadowRoot that was created by the host element after
   1.166 +  // this ShadowRoot was created.
   1.167 +  nsRefPtr<ShadowRoot> mYoungerShadow;
   1.168 +
   1.169 +  // A boolean that indicates that an insertion point was added or removed
   1.170 +  // from this ShadowRoot and that the nodes need to be redistributed into
   1.171 +  // the insertion points. After this flag is set, nodes will be distributed
   1.172 +  // on the next mutation event.
   1.173 +  bool mInsertionPointChanged;
   1.174 +};
   1.175 +
   1.176 +class ShadowRootStyleSheetList : public StyleSheetList
   1.177 +{
   1.178 +public:
   1.179 +  ShadowRootStyleSheetList(ShadowRoot* aShadowRoot);
   1.180 +  virtual ~ShadowRootStyleSheetList();
   1.181 +
   1.182 +  NS_DECL_ISUPPORTS_INHERITED
   1.183 +  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ShadowRootStyleSheetList, StyleSheetList)
   1.184 +
   1.185 +  virtual nsINode* GetParentObject() const MOZ_OVERRIDE
   1.186 +  {
   1.187 +    return mShadowRoot;
   1.188 +  }
   1.189 +
   1.190 +  virtual uint32_t Length() MOZ_OVERRIDE;
   1.191 +  virtual nsCSSStyleSheet* IndexedGetter(uint32_t aIndex, bool& aFound) MOZ_OVERRIDE;
   1.192 +
   1.193 +protected:
   1.194 +  nsRefPtr<ShadowRoot> mShadowRoot;
   1.195 +};
   1.196 +
   1.197 +} // namespace dom
   1.198 +} // namespace mozilla
   1.199 +
   1.200 +#endif // mozilla_dom_shadowroot_h__
   1.201 +

mercurial