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/. */
6 
7 #ifndef mozilla_dom_DocumentOrShadowRoot_h__
8 #define mozilla_dom_DocumentOrShadowRoot_h__
9 
10 #include "mozilla/dom/NameSpaceConstants.h"
11 #include "mozilla/IdentifierMapEntry.h"
12 #include "mozilla/RelativeTo.h"
13 #include "mozilla/ReverseIterator.h"
14 #include "nsClassHashtable.h"
15 #include "nsContentListDeclarations.h"
16 #include "nsTArray.h"
17 #include "nsTHashSet.h"
18 #include "RadioGroupManager.h"
19 
20 class nsContentList;
21 class nsCycleCollectionTraversalCallback;
22 class nsINode;
23 class nsINodeList;
24 class nsIRadioVisitor;
25 class nsWindowSizes;
26 
27 namespace mozilla {
28 class ErrorResult;
29 class StyleSheet;
30 class ErrorResult;
31 
32 namespace dom {
33 
34 class Animation;
35 class Element;
36 class Document;
37 class DocumentOrShadowRoot;
38 class HTMLInputElement;
39 class StyleSheetList;
40 class ShadowRoot;
41 template <typename T>
42 class Sequence;
43 
44 /**
45  * A class meant to be shared by ShadowRoot and Document, that holds a list of
46  * stylesheets.
47  *
48  * TODO(emilio, bug 1418159): In the future this should hold most of the
49  * relevant style state, this should allow us to fix bug 548397.
50  */
51 class DocumentOrShadowRoot : public RadioGroupManager {
52   enum class Kind {
53     Document,
54     ShadowRoot,
55   };
56 
57  public:
58   // These should always be non-null, but can't use a reference because
59   // dereferencing `this` on initializer lists is UB, apparently, see
60   // bug 1596499.
61   explicit DocumentOrShadowRoot(Document*);
62   explicit DocumentOrShadowRoot(ShadowRoot*);
63 
64   // Unusual argument naming is because of cycle collection macros.
65   static void Traverse(DocumentOrShadowRoot* tmp,
66                        nsCycleCollectionTraversalCallback& cb);
67   static void Unlink(DocumentOrShadowRoot* tmp);
68 
AsNode()69   nsINode& AsNode() { return *mAsNode; }
70 
AsNode()71   const nsINode& AsNode() const { return *mAsNode; }
72 
SheetAt(size_t aIndex)73   StyleSheet* SheetAt(size_t aIndex) const {
74     return mStyleSheets.SafeElementAt(aIndex);
75   }
76 
SheetCount()77   size_t SheetCount() const { return mStyleSheets.Length(); }
78 
AdoptedSheetCount()79   size_t AdoptedSheetCount() const { return mAdoptedStyleSheets.Length(); }
80 
81   /**
82    * Returns an index for the sheet in relative style order.
83    * If there are non-applicable sheets, then this index may
84    * not match 1:1 with the sheet's actual index in the style set.
85    *
86    * Handles sheets from both mStyleSheets and mAdoptedStyleSheets
87    */
88   int32_t StyleOrderIndexOfSheet(const StyleSheet& aSheet) const;
89 
90   StyleSheetList* StyleSheets();
91 
92   void GetAdoptedStyleSheets(nsTArray<RefPtr<StyleSheet>>&) const;
93 
94   void RemoveStyleSheet(StyleSheet&);
95 
96   Element* GetElementById(const nsAString& aElementId);
97 
98   /**
99    * This method returns _all_ the elements in this scope which have id
100    * aElementId, if there are any.  Otherwise it returns null.
101    *
102    * This is useful for stuff like QuerySelector optimization and such.
103    */
104   inline const nsTArray<Element*>* GetAllElementsForId(
105       const nsAString& aElementId) const;
106 
GetElementsByTagName(const nsAString & aTagName)107   already_AddRefed<nsContentList> GetElementsByTagName(
108       const nsAString& aTagName) {
109     return NS_GetContentList(&AsNode(), kNameSpaceID_Unknown, aTagName);
110   }
111 
112   already_AddRefed<nsContentList> GetElementsByTagNameNS(
113       const nsAString& aNamespaceURI, const nsAString& aLocalName);
114 
115   already_AddRefed<nsContentList> GetElementsByTagNameNS(
116       const nsAString& aNamespaceURI, const nsAString& aLocalName,
117       mozilla::ErrorResult&);
118 
119   already_AddRefed<nsContentList> GetElementsByClassName(
120       const nsAString& aClasses);
121 
122   ~DocumentOrShadowRoot();
123 
124   Element* GetPointerLockElement();
125   Element* GetFullscreenElement() const;
126 
127   Element* ElementFromPoint(float aX, float aY);
128   nsINode* NodeFromPoint(float aX, float aY);
129 
130   void ElementsFromPoint(float aX, float aY, nsTArray<RefPtr<Element>>&);
131   void NodesFromPoint(float aX, float aY, nsTArray<RefPtr<nsINode>>&);
132 
133   /**
134    * Helper for elementFromPoint implementation that allows
135    * ignoring the scroll frame and/or avoiding layout flushes.
136    *
137    * @see nsIDOMWindowUtils::elementFromPoint
138    */
139   Element* ElementFromPointHelper(float aX, float aY,
140                                   bool aIgnoreRootScrollFrame,
141                                   bool aFlushLayout,
142                                   ViewportType aViewportType);
143 
144   void NodesFromRect(float aX, float aY, float aTopSize, float aRightSize,
145                      float aBottomSize, float aLeftSize,
146                      bool aIgnoreRootScrollFrame, bool aFlushLayout,
147                      bool aOnlyVisible, float aVisibleThreshold,
148                      nsTArray<RefPtr<nsINode>>&);
149 
150   /**
151    * This gets fired when the element that an id refers to changes.
152    * This fires at difficult times. It is generally not safe to do anything
153    * which could modify the DOM in any way. Use
154    * nsContentUtils::AddScriptRunner.
155    * @return true to keep the callback in the callback set, false
156    * to remove it.
157    */
158   typedef bool (*IDTargetObserver)(Element* aOldElement, Element* aNewelement,
159                                    void* aData);
160 
161   /**
162    * Add an IDTargetObserver for a specific ID. The IDTargetObserver
163    * will be fired whenever the content associated with the ID changes
164    * in the future. If aForImage is true, mozSetImageElement can override
165    * what content is associated with the ID. In that case the IDTargetObserver
166    * will be notified at those times when the result of LookupImageElement
167    * changes.
168    * At most one (aObserver, aData, aForImage) triple can be
169    * registered for each ID.
170    * @return the content currently associated with the ID.
171    */
172   Element* AddIDTargetObserver(nsAtom* aID, IDTargetObserver aObserver,
173                                void* aData, bool aForImage);
174 
175   /**
176    * Remove the (aObserver, aData, aForImage) triple for a specific ID, if
177    * registered.
178    */
179   void RemoveIDTargetObserver(nsAtom* aID, IDTargetObserver aObserver,
180                               void* aData, bool aForImage);
181 
182   /**
183    * Lookup an image element using its associated ID, which is usually provided
184    * by |-moz-element()|. Similar to GetElementById, with the difference that
185    * elements set using mozSetImageElement have higher priority.
186    * @param aId the ID associated the element we want to lookup
187    * @return the element associated with |aId|
188    */
189   Element* LookupImageElement(const nsAString& aElementId);
190 
191   /**
192    * Check that aId is not empty and log a message to the console
193    * service if it is.
194    * @returns true if aId looks correct, false otherwise.
195    */
CheckGetElementByIdArg(const nsAString & aId)196   inline bool CheckGetElementByIdArg(const nsAString& aId) {
197     if (aId.IsEmpty()) {
198       ReportEmptyGetElementByIdArg();
199       return false;
200     }
201     return true;
202   }
203 
204   void ReportEmptyGetElementByIdArg();
205 
206   // Web Animations
207   MOZ_CAN_RUN_SCRIPT
208   void GetAnimations(nsTArray<RefPtr<Animation>>& aAnimations);
209 
210   nsIContent* Retarget(nsIContent* aContent) const;
211 
212   void SetAdoptedStyleSheets(
213       const Sequence<OwningNonNull<StyleSheet>>& aAdoptedStyleSheets,
214       ErrorResult& aRv);
215 
216   // This is needed because ServoStyleSet / ServoAuthorData don't deal with
217   // duplicate stylesheets (and it's unclear we'd want to support that as it'd
218   // be a bunch of duplicate work), while adopted stylesheets do need to deal
219   // with them.
220   template <typename Callback>
EnumerateUniqueAdoptedStyleSheetsBackToFront(Callback aCallback)221   void EnumerateUniqueAdoptedStyleSheetsBackToFront(Callback aCallback) {
222     StyleSheetSet set(mAdoptedStyleSheets.Length());
223     for (StyleSheet* sheet : Reversed(mAdoptedStyleSheets)) {
224       if (MOZ_UNLIKELY(!set.EnsureInserted(sheet))) {
225         continue;
226       }
227       aCallback(*sheet);
228     }
229   }
230 
231  protected:
232   // Cycle collection helper functions
233   void TraverseSheetRefInStylesIfApplicable(
234       StyleSheet&, nsCycleCollectionTraversalCallback&);
235   void TraverseStyleSheets(nsTArray<RefPtr<StyleSheet>>&, const char*,
236                            nsCycleCollectionTraversalCallback&);
237   void UnlinkStyleSheets(nsTArray<RefPtr<StyleSheet>>&);
238 
239   using StyleSheetSet = nsTHashSet<const StyleSheet*>;
240   void RemoveSheetFromStylesIfApplicable(StyleSheet&);
241   void ClearAdoptedStyleSheets();
242 
243   /**
244    * Clone's the argument's adopted style sheets into this.
245    * This should only be used when cloning a static document for printing.
246    */
247   void CloneAdoptedSheetsFrom(const DocumentOrShadowRoot&);
248 
249   void InsertSheetAt(size_t aIndex, StyleSheet& aSheet);
250 
251   void AddSizeOfExcludingThis(nsWindowSizes&) const;
252   void AddSizeOfOwnedSheetArrayExcludingThis(
253       nsWindowSizes&, const nsTArray<RefPtr<StyleSheet>>&) const;
254 
255   /**
256    * If focused element's subtree root is this document or shadow root, return
257    * focused element, otherwise, get the shadow host recursively until the
258    * shadow host's subtree root is this document or shadow root.
259    */
260   Element* GetRetargetedFocusedElement();
261 
262   nsTArray<RefPtr<StyleSheet>> mStyleSheets;
263   RefPtr<StyleSheetList> mDOMStyleSheets;
264 
265   /**
266    * Style sheets that are adopted by assinging to the `adoptedStyleSheets`
267    * WebIDL atribute. These can only be constructed stylesheets.
268    */
269   nsTArray<RefPtr<StyleSheet>> mAdoptedStyleSheets;
270 
271   /*
272    * mIdentifierMap works as follows for IDs:
273    * 1) Attribute changes affect the table immediately (removing and adding
274    *    entries as needed).
275    * 2) Removals from the DOM affect the table immediately
276    * 3) Additions to the DOM always update existing entries for names, and add
277    *    new ones for IDs.
278    */
279   nsTHashtable<IdentifierMapEntry> mIdentifierMap;
280 
281   // Always non-null, see comment in the constructor as to why a pointer instead
282   // of a reference.
283   nsINode* mAsNode;
284   const Kind mKind;
285 };
286 
GetAllElementsForId(const nsAString & aElementId)287 inline const nsTArray<Element*>* DocumentOrShadowRoot::GetAllElementsForId(
288     const nsAString& aElementId) const {
289   if (aElementId.IsEmpty()) {
290     return nullptr;
291   }
292 
293   IdentifierMapEntry* entry = mIdentifierMap.GetEntry(aElementId);
294   return entry ? &entry->GetIdElements() : nullptr;
295 }
296 
297 }  // namespace dom
298 
299 }  // namespace mozilla
300 
301 #endif
302