Bug 1661283 Part 2: Make InspectorUtils::GetOverflowingChildrenOfElement correctly...
[gecko.git] / layout / inspector / InspectorUtils.h
blobd87340e43203b8f9db1c87b30358c461e8df52c8
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/.
6 */
8 #ifndef mozilla_dom_InspectorUtils_h
9 #define mozilla_dom_InspectorUtils_h
11 #include "mozilla/dom/InspectorUtilsBinding.h"
12 #include "mozilla/UniquePtr.h"
13 #include "nsLayoutUtils.h"
15 class nsAtom;
16 class nsINode;
17 class nsINodeList;
18 class nsRange;
19 class ComputedStyle;
21 namespace mozilla {
22 class BindingStyleRule;
23 class StyleSheet;
24 namespace css {
25 class Rule;
26 } // namespace css
27 namespace dom {
28 class CharacterData;
29 class Document;
30 class Element;
31 class InspectorFontFace;
32 } // namespace dom
33 } // namespace mozilla
35 namespace mozilla {
36 namespace dom {
38 /**
39 * A collection of utility methods for use by devtools.
41 class InspectorUtils {
42 public:
43 static void GetAllStyleSheets(GlobalObject& aGlobal, Document& aDocument,
44 bool aDocumentOnly,
45 nsTArray<RefPtr<StyleSheet>>& aResult);
46 static void GetCSSStyleRules(GlobalObject& aGlobal, Element& aElement,
47 const nsAString& aPseudo,
48 bool aIncludeVisitedStyle,
49 nsTArray<RefPtr<BindingStyleRule>>& aResult);
51 /**
52 * Get the line number of a rule.
54 * @param aRule The rule.
55 * @return The rule's line number. Line numbers are 1-based.
57 static uint32_t GetRuleLine(GlobalObject& aGlobal, css::Rule& aRule);
59 /**
60 * Get the column number of a rule.
62 * @param aRule The rule.
63 * @return The rule's column number. Column numbers are 1-based.
65 static uint32_t GetRuleColumn(GlobalObject& aGlobal, css::Rule& aRule);
67 /**
68 * Like getRuleLine, but if the rule is in a <style> element,
69 * returns a line number relative to the start of the element.
71 * @param aRule the rule to examine
72 * @return the line number of the rule, possibly relative to the
73 * <style> element
75 static uint32_t GetRelativeRuleLine(GlobalObject& aGlobal, css::Rule& aRule);
77 static bool HasRulesModifiedByCSSOM(GlobalObject& aGlobal,
78 StyleSheet& aSheet);
80 // Utilities for working with selectors. We don't have a JS OM representation
81 // of a single selector or a selector list yet, but given a rule we can index
82 // into the selector list.
84 // These methods would probably make more sense being [ChromeOnly] APIs on
85 // CSSStyleRule itself (bug 1428245).
86 static uint32_t GetSelectorCount(GlobalObject& aGlobal,
87 BindingStyleRule& aRule);
89 // For all three functions below, aSelectorIndex is 0-based
90 static void GetSelectorText(GlobalObject& aGlobal, BindingStyleRule& aRule,
91 uint32_t aSelectorIndex, nsString& aText,
92 ErrorResult& aRv);
93 static uint64_t GetSpecificity(GlobalObject& aGlobal, BindingStyleRule& aRule,
94 uint32_t aSelectorIndex, ErrorResult& aRv);
95 // Note: This does not handle scoped selectors correctly, because it has no
96 // idea what the right scope is.
97 static bool SelectorMatchesElement(GlobalObject& aGlobal, Element& aElement,
98 BindingStyleRule& aRule,
99 uint32_t aSelectorIndex,
100 const nsAString& aPseudo,
101 bool aRelevantLinkVisited,
102 ErrorResult& aRv);
104 // Utilities for working with CSS properties
106 // Returns true if the string names a property that is inherited by default.
107 static bool IsInheritedProperty(GlobalObject& aGlobal,
108 const nsACString& aPropertyName);
110 // Get a list of all our supported property names. Optionally
111 // property aliases included.
112 static void GetCSSPropertyNames(GlobalObject& aGlobal,
113 const PropertyNamesOptions& aOptions,
114 nsTArray<nsString>& aResult);
116 // Get a list of all properties controlled by preference, as well as
117 // their corresponding preference names.
118 static void GetCSSPropertyPrefs(GlobalObject& aGlobal,
119 nsTArray<PropertyPref>& aResult);
121 // Get a list of all valid keywords and colors for aProperty.
122 static void GetCSSValuesForProperty(GlobalObject& aGlobal,
123 const nsACString& aPropertyName,
124 nsTArray<nsString>& aResult,
125 ErrorResult& aRv);
127 // Utilities for working with CSS colors
128 static void RgbToColorName(GlobalObject& aGlobal, uint8_t aR, uint8_t aG,
129 uint8_t aB, nsAString& aResult, ErrorResult& aRv);
131 // Convert a given CSS color string to rgba. Returns null on failure or an
132 // InspectorRGBATuple on success.
134 // NOTE: Converting a color to RGBA may be lossy when converting from some
135 // formats e.g. CMYK.
136 static void ColorToRGBA(GlobalObject&, const nsACString& aColorString,
137 const Document*,
138 Nullable<InspectorRGBATuple>& aResult);
140 // Check whether a given color is a valid CSS color.
141 static bool IsValidCSSColor(GlobalObject& aGlobal,
142 const nsACString& aColorString);
144 // Utilities for obtaining information about a CSS property.
146 // Get a list of the longhands corresponding to the given CSS property. If
147 // the property is a longhand already, just returns the property itself.
148 // Throws on unsupported property names.
149 static void GetSubpropertiesForCSSProperty(GlobalObject& aGlobal,
150 const nsACString& aProperty,
151 nsTArray<nsString>& aResult,
152 ErrorResult& aRv);
154 // Check whether a given CSS property is a shorthand. Throws on unsupported
155 // property names.
156 static bool CssPropertyIsShorthand(GlobalObject& aGlobal,
157 const nsACString& aProperty,
158 ErrorResult& aRv);
160 // Check whether values of the given type are valid values for the property.
161 // For shorthands, checks whether there's a corresponding longhand property
162 // that accepts values of this type. Throws on unsupported properties or
163 // unknown types.
164 static bool CssPropertySupportsType(GlobalObject& aGlobal,
165 const nsACString& aProperty,
166 InspectorPropertyType, ErrorResult& aRv);
168 static bool IsIgnorableWhitespace(GlobalObject& aGlobalObject,
169 CharacterData& aDataNode) {
170 return IsIgnorableWhitespace(aDataNode);
172 static bool IsIgnorableWhitespace(CharacterData& aDataNode);
174 // Returns the "parent" of a node. The parent of a document node is the
175 // frame/iframe containing that document. aShowingAnonymousContent says
176 // whether we are showing anonymous content.
177 static nsINode* GetParentForNode(nsINode& aNode,
178 bool aShowingAnonymousContent);
179 static nsINode* GetParentForNode(GlobalObject& aGlobalObject, nsINode& aNode,
180 bool aShowingAnonymousContent) {
181 return GetParentForNode(aNode, aShowingAnonymousContent);
184 static already_AddRefed<nsINodeList> GetChildrenForNode(
185 GlobalObject& aGlobalObject, nsINode& aNode,
186 bool aShowingAnonymousContent) {
187 return GetChildrenForNode(aNode, aShowingAnonymousContent);
189 static already_AddRefed<nsINodeList> GetChildrenForNode(
190 nsINode& aNode, bool aShowingAnonymousContent);
193 * Setting and removing content state on an element. Both these functions
194 * call EventStateManager::SetContentState internally; the difference is
195 * that for the remove case we simply pass in nullptr for the element.
196 * Use them accordingly.
198 * When removing the active state, you may optionally also clear the active
199 * document as well by setting aClearActiveDocument
201 * @return Returns true if the state was set successfully. See more details
202 * in EventStateManager.h SetContentState.
204 static bool SetContentState(GlobalObject& aGlobal, Element& aElement,
205 uint64_t aState, ErrorResult& aRv);
206 static bool RemoveContentState(GlobalObject& aGlobal, Element& aElement,
207 uint64_t aState, bool aClearActiveDocument,
208 ErrorResult& aRv);
209 static uint64_t GetContentState(GlobalObject& aGlobal, Element& aElement);
211 static void GetUsedFontFaces(GlobalObject& aGlobal, nsRange& aRange,
212 uint32_t aMaxRanges, // max number of ranges to
213 // record for each face
214 bool aSkipCollapsedWhitespace,
215 nsLayoutUtils::UsedFontFaceList& aResult,
216 ErrorResult& aRv);
219 * Get the names of all the supported pseudo-elements.
220 * Pseudo-elements which are only accepted in UA style sheets are
221 * not included.
223 static void GetCSSPseudoElementNames(GlobalObject& aGlobal,
224 nsTArray<nsString>& aResult);
226 // pseudo-class style locking methods. aPseudoClass must be a valid
227 // pseudo-class selector string, e.g. ":hover". ":any-link" and
228 // non-event-state pseudo-classes are ignored. aEnabled sets whether the
229 // psuedo-class should be locked to on or off.
230 static void AddPseudoClassLock(GlobalObject& aGlobal, Element& aElement,
231 const nsAString& aPseudoClass, bool aEnabled);
232 static void RemovePseudoClassLock(GlobalObject& aGlobal, Element& aElement,
233 const nsAString& aPseudoClass);
234 static bool HasPseudoClassLock(GlobalObject& aGlobal, Element& aElement,
235 const nsAString& aPseudoClass);
236 static void ClearPseudoClassLocks(GlobalObject& aGlobal, Element& aElement);
238 static bool IsElementThemed(GlobalObject& aGlobal, Element& aElement);
240 static Element* ContainingBlockOf(GlobalObject&, Element&);
242 MOZ_CAN_RUN_SCRIPT
243 static already_AddRefed<nsINodeList> GetOverflowingChildrenOfElement(
244 GlobalObject& aGlobal, Element& element);
247 * Parse CSS and update the style sheet in place.
249 * @param DOMCSSStyleSheet aSheet
250 * @param UTF8String aInput
251 * The new source string for the style sheet.
253 static void ParseStyleSheet(GlobalObject& aGlobal, StyleSheet& aSheet,
254 const nsACString& aInput, ErrorResult& aRv);
257 * Check if the provided name can be custom element name.
259 static bool IsCustomElementName(GlobalObject&, const nsAString& aName,
260 const nsAString& aNamespaceURI);
262 private:
263 static already_AddRefed<ComputedStyle> GetCleanComputedStyleForElement(
264 Element* aElement, nsAtom* aPseudo);
267 } // namespace dom
268 } // namespace mozilla
270 #endif // mozilla_dom_InspectorUtils_h