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