Bug 1913305 - Add test. r=mtigley
[gecko.git] / layout / inspector / InspectorUtils.h
blob0f4ed05b770ff36abc3f397cccf99f6708f377ba
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;
20 namespace mozilla {
21 class StyleSheet;
22 namespace css {
23 class Rule;
24 } // namespace css
25 namespace dom {
26 class CharacterData;
27 class Document;
28 class Element;
29 class InspectorFontFace;
30 } // namespace dom
31 } // namespace mozilla
33 namespace mozilla::dom {
34 class CSSStyleRule;
36 /**
37 * A collection of utility methods for use by devtools.
39 class InspectorUtils {
40 public:
41 static void GetAllStyleSheets(GlobalObject& aGlobal, Document& aDocument,
42 bool aDocumentOnly,
43 nsTArray<RefPtr<StyleSheet>>& aResult);
44 static void GetCSSStyleRules(GlobalObject& aGlobal, Element& aElement,
45 const nsAString& aPseudo,
46 bool aIncludeVisitedStyle,
47 bool aWithStartingStyle,
48 nsTArray<RefPtr<CSSStyleRule>>& aResult);
50 /**
51 * Get the line number of a rule.
53 * @param aRule The rule.
54 * @return The rule's line number. Line numbers are 1-based.
56 static uint32_t GetRuleLine(GlobalObject& aGlobal, css::Rule& aRule);
58 /**
59 * Get the column number of a rule.
61 * @param aRule The rule.
62 * @return The rule's column number. Column numbers are 1-based.
64 static uint32_t GetRuleColumn(GlobalObject& aGlobal, css::Rule& aRule);
66 /**
67 * Like getRuleLine, but if the rule is in a <style> element,
68 * returns a line number relative to the start of the element.
70 * @param aRule the rule to examine
71 * @return the line number of the rule, possibly relative to the
72 * <style> element
74 static uint32_t GetRelativeRuleLine(GlobalObject& aGlobal, css::Rule& aRule);
76 static void GetRuleIndex(GlobalObject& aGlobal, css::Rule& aRule,
77 nsTArray<uint32_t>& aResult);
79 static bool HasRulesModifiedByCSSOM(GlobalObject& aGlobal,
80 StyleSheet& aSheet);
82 static void GetStyleSheetRuleCountAndAtRules(
83 GlobalObject& aGlobal, StyleSheet& aSheet,
84 InspectorStyleSheetRuleCountAndAtRulesResult& aResult);
86 // Utilities for working with CSS properties
88 // Returns true if the string names a property that is inherited by default.
89 static bool IsInheritedProperty(GlobalObject& aGlobal, Document& aDocument,
90 const nsACString& aPropertyName);
92 // Get a list of all our supported property names. Optionally
93 // property aliases included.
94 static void GetCSSPropertyNames(GlobalObject& aGlobal,
95 const PropertyNamesOptions& aOptions,
96 nsTArray<nsString>& aResult);
98 // Get a list of all properties controlled by preference, as well as
99 // their corresponding preference names.
100 static void GetCSSPropertyPrefs(GlobalObject& aGlobal,
101 nsTArray<PropertyPref>& aResult);
103 // Get a list of all valid keywords and colors for aProperty.
104 static void GetCSSValuesForProperty(GlobalObject& aGlobal,
105 const nsACString& aPropertyName,
106 nsTArray<nsString>& aResult,
107 ErrorResult& aRv);
109 // Utilities for working with CSS colors
110 static void RgbToColorName(GlobalObject& aGlobal, uint8_t aR, uint8_t aG,
111 uint8_t aB, nsACString& aResult);
113 // Convert a given CSS color string to rgba. Returns null on failure or an
114 // InspectorRGBATuple on success.
116 // NOTE: Converting a color to RGBA may be lossy when converting from some
117 // formats e.g. CMYK.
118 static void ColorToRGBA(GlobalObject&, const nsACString& aColorString,
119 const Document*,
120 Nullable<InspectorRGBATuple>& aResult);
122 // Convert a given CSS color string to another color space.
123 static void ColorTo(GlobalObject&, const nsACString& aFromColor,
124 const nsACString& aToColorSpace,
125 Nullable<InspectorColorToResult>& aResult);
127 // Check whether a given color is a valid CSS color.
128 static bool IsValidCSSColor(GlobalObject& aGlobal,
129 const nsACString& aColorString);
131 // Utilities for obtaining information about a CSS property.
133 // Get a list of the longhands corresponding to the given CSS property. If
134 // the property is a longhand already, just returns the property itself.
135 // Throws on unsupported property names.
136 static void GetSubpropertiesForCSSProperty(GlobalObject& aGlobal,
137 const nsACString& aProperty,
138 nsTArray<nsString>& aResult,
139 ErrorResult& aRv);
141 // Check whether a given CSS property is a shorthand. Throws on unsupported
142 // property names.
143 static bool CssPropertyIsShorthand(GlobalObject& aGlobal,
144 const nsACString& aProperty,
145 ErrorResult& aRv);
147 // Check whether values of the given type are valid values for the property.
148 // For shorthands, checks whether there's a corresponding longhand property
149 // that accepts values of this type. Throws on unsupported properties or
150 // unknown types.
151 static bool CssPropertySupportsType(GlobalObject& aGlobal,
152 const nsACString& aProperty,
153 InspectorPropertyType, ErrorResult& aRv);
155 static bool Supports(GlobalObject&, const nsACString& aDeclaration,
156 const SupportsOptions&);
158 static bool IsIgnorableWhitespace(GlobalObject& aGlobalObject,
159 CharacterData& aDataNode) {
160 return IsIgnorableWhitespace(aDataNode);
162 static bool IsIgnorableWhitespace(CharacterData& aDataNode);
164 // Returns the "parent" of a node. The parent of a document node is the
165 // frame/iframe containing that document. aShowingAnonymousContent says
166 // whether we are showing anonymous content.
167 static nsINode* GetParentForNode(nsINode& aNode,
168 bool aShowingAnonymousContent);
169 static nsINode* GetParentForNode(GlobalObject& aGlobalObject, nsINode& aNode,
170 bool aShowingAnonymousContent) {
171 return GetParentForNode(aNode, aShowingAnonymousContent);
174 static void GetChildrenForNode(GlobalObject&, nsINode& aNode,
175 bool aShowingAnonymousContent,
176 bool aIncludeAssignedNodes,
177 nsTArray<RefPtr<nsINode>>& aResult) {
178 return GetChildrenForNode(aNode, aShowingAnonymousContent,
179 aIncludeAssignedNodes,
180 /* aIncludeSubdocuments = */ true, aResult);
182 static void GetChildrenForNode(nsINode& aNode, bool aShowingAnonymousContent,
183 bool aIncludeAssignedNodes,
184 bool aIncludeSubdocuments,
185 nsTArray<RefPtr<nsINode>>& aResult);
188 * Setting and removing content state on an element. Both these functions
189 * call EventStateManager::SetContentState internally; the difference is
190 * that for the remove case we simply pass in nullptr for the element.
191 * Use them accordingly.
193 * When removing the active state, you may optionally also clear the active
194 * document as well by setting aClearActiveDocument
196 * @return Returns true if the state was set successfully. See more details
197 * in EventStateManager.h SetContentState.
199 static bool SetContentState(GlobalObject& aGlobal, Element& aElement,
200 uint64_t aState, ErrorResult& aRv);
201 static bool RemoveContentState(GlobalObject& aGlobal, Element& aElement,
202 uint64_t aState, bool aClearActiveDocument,
203 ErrorResult& aRv);
204 static uint64_t GetContentState(GlobalObject& aGlobal, Element& aElement);
206 static void GetUsedFontFaces(GlobalObject& aGlobal, nsRange& aRange,
207 uint32_t aMaxRanges, // max number of ranges to
208 // record for each face
209 bool aSkipCollapsedWhitespace,
210 nsLayoutUtils::UsedFontFaceList& aResult,
211 ErrorResult& aRv);
214 * Get the names of all the supported pseudo-elements.
215 * Pseudo-elements which are only accepted in UA style sheets are
216 * not included.
218 static void GetCSSPseudoElementNames(GlobalObject&,
219 nsTArray<nsString>& aResult);
221 // pseudo-class style locking methods. aPseudoClass must be a valid
222 // pseudo-class selector string, e.g. ":hover". ":any-link" and
223 // non-event-state pseudo-classes are ignored. aEnabled sets whether the
224 // psuedo-class should be locked to on or off.
225 static void AddPseudoClassLock(GlobalObject&, Element&,
226 const nsAString& aPseudoClass, bool aEnabled);
227 static void RemovePseudoClassLock(GlobalObject&, Element&,
228 const nsAString& aPseudoClass);
229 static bool HasPseudoClassLock(GlobalObject&, Element&,
230 const nsAString& aPseudoClass);
231 static void ClearPseudoClassLocks(GlobalObject&, Element&);
233 static bool IsElementThemed(GlobalObject&, Element&);
235 static bool IsUsedColorSchemeDark(GlobalObject&, Element&);
237 static Element* ContainingBlockOf(GlobalObject&, Element&);
239 static void GetBlockLineCounts(GlobalObject&, Element&,
240 Nullable<nsTArray<uint32_t>>& aResult);
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);
263 * Get the names of registered Highlights
265 static void GetRegisteredCssHighlights(GlobalObject& aGlobal,
266 Document& aDocument, bool aActiveOnly,
267 nsTArray<nsString>& aResult);
269 * Get registered CSS properties (via CSS.registerProperty or @property)
271 static void GetCSSRegisteredProperties(
272 GlobalObject& aGlobal, Document& aDocument,
273 nsTArray<InspectorCSSPropertyDefinition>& aResult);
276 * Get a single registered CSS property
278 static void GetCSSRegisteredProperty(
279 GlobalObject& aGlobal, Document& aDocument, const nsACString& aName,
280 Nullable<InspectorCSSPropertyDefinition>& aResult);
283 * Returns whether or not a CSS property value is valid for the passed syntax
285 static bool ValueMatchesSyntax(GlobalObject&, Document& aDocument,
286 const nsACString& aValue,
287 const nsACString& aSyntax);
290 * Get the rule body text within aInitialText
292 static void GetRuleBodyText(GlobalObject&, const nsACString& aInitialText,
293 nsACString& aBodyText);
296 * Replace the rule body text in aStyleSheetText at passed line and column
298 static void ReplaceBlockRuleBodyTextInStylesheet(
299 GlobalObject&, const nsACString& aStyleSheetText, uint32_t aLine,
300 uint32_t aColumn, const nsACString& aNewBodyText,
301 nsACString& aNewStyleSheetText);
304 } // namespace mozilla::dom
306 #endif // mozilla_dom_InspectorUtils_h