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/.
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"
29 class InspectorFontFace
;
31 } // namespace mozilla
33 namespace mozilla::dom
{
37 * A collection of utility methods for use by devtools.
39 class InspectorUtils
{
41 static void GetAllStyleSheets(GlobalObject
& aGlobal
, Document
& aDocument
,
43 nsTArray
<RefPtr
<StyleSheet
>>& aResult
);
44 static void GetCSSStyleRules(GlobalObject
& aGlobal
, Element
& aElement
,
45 const nsAString
& aPseudo
,
46 bool aIncludeVisitedStyle
,
47 nsTArray
<RefPtr
<CSSStyleRule
>>& aResult
);
50 * Get the line number of a rule.
52 * @param aRule The rule.
53 * @return The rule's line number. Line numbers are 1-based.
55 static uint32_t GetRuleLine(GlobalObject
& aGlobal
, css::Rule
& aRule
);
58 * Get the column number of a rule.
60 * @param aRule The rule.
61 * @return The rule's column number. Column numbers are 1-based.
63 static uint32_t GetRuleColumn(GlobalObject
& aGlobal
, css::Rule
& aRule
);
66 * Like getRuleLine, but if the rule is in a <style> element,
67 * returns a line number relative to the start of the element.
69 * @param aRule the rule to examine
70 * @return the line number of the rule, possibly relative to the
73 static uint32_t GetRelativeRuleLine(GlobalObject
& aGlobal
, css::Rule
& aRule
);
75 static bool HasRulesModifiedByCSSOM(GlobalObject
& aGlobal
,
78 static void GetAllStyleSheetCSSStyleRules(
79 GlobalObject
& aGlobal
, StyleSheet
& aSheet
,
80 nsTArray
<RefPtr
<css::Rule
>>& aResult
);
82 // Utilities for working with CSS properties
84 // Returns true if the string names a property that is inherited by default.
85 static bool IsInheritedProperty(GlobalObject
& aGlobal
, Document
& aDocument
,
86 const nsACString
& aPropertyName
);
88 // Get a list of all our supported property names. Optionally
89 // property aliases included.
90 static void GetCSSPropertyNames(GlobalObject
& aGlobal
,
91 const PropertyNamesOptions
& aOptions
,
92 nsTArray
<nsString
>& aResult
);
94 // Get a list of all properties controlled by preference, as well as
95 // their corresponding preference names.
96 static void GetCSSPropertyPrefs(GlobalObject
& aGlobal
,
97 nsTArray
<PropertyPref
>& aResult
);
99 // Get a list of all valid keywords and colors for aProperty.
100 static void GetCSSValuesForProperty(GlobalObject
& aGlobal
,
101 const nsACString
& aPropertyName
,
102 nsTArray
<nsString
>& aResult
,
105 // Utilities for working with CSS colors
106 static void RgbToColorName(GlobalObject
& aGlobal
, uint8_t aR
, uint8_t aG
,
107 uint8_t aB
, nsACString
& aResult
);
109 // Convert a given CSS color string to rgba. Returns null on failure or an
110 // InspectorRGBATuple on success.
112 // NOTE: Converting a color to RGBA may be lossy when converting from some
113 // formats e.g. CMYK.
114 static void ColorToRGBA(GlobalObject
&, const nsACString
& aColorString
,
116 Nullable
<InspectorRGBATuple
>& aResult
);
118 // Check whether a given color is a valid CSS color.
119 static bool IsValidCSSColor(GlobalObject
& aGlobal
,
120 const nsACString
& aColorString
);
122 // Utilities for obtaining information about a CSS property.
124 // Get a list of the longhands corresponding to the given CSS property. If
125 // the property is a longhand already, just returns the property itself.
126 // Throws on unsupported property names.
127 static void GetSubpropertiesForCSSProperty(GlobalObject
& aGlobal
,
128 const nsACString
& aProperty
,
129 nsTArray
<nsString
>& aResult
,
132 // Check whether a given CSS property is a shorthand. Throws on unsupported
134 static bool CssPropertyIsShorthand(GlobalObject
& aGlobal
,
135 const nsACString
& aProperty
,
138 // Check whether values of the given type are valid values for the property.
139 // For shorthands, checks whether there's a corresponding longhand property
140 // that accepts values of this type. Throws on unsupported properties or
142 static bool CssPropertySupportsType(GlobalObject
& aGlobal
,
143 const nsACString
& aProperty
,
144 InspectorPropertyType
, ErrorResult
& aRv
);
146 static bool Supports(GlobalObject
&, const nsACString
& aDeclaration
,
147 const SupportsOptions
&);
149 static bool IsIgnorableWhitespace(GlobalObject
& aGlobalObject
,
150 CharacterData
& aDataNode
) {
151 return IsIgnorableWhitespace(aDataNode
);
153 static bool IsIgnorableWhitespace(CharacterData
& aDataNode
);
155 // Returns the "parent" of a node. The parent of a document node is the
156 // frame/iframe containing that document. aShowingAnonymousContent says
157 // whether we are showing anonymous content.
158 static nsINode
* GetParentForNode(nsINode
& aNode
,
159 bool aShowingAnonymousContent
);
160 static nsINode
* GetParentForNode(GlobalObject
& aGlobalObject
, nsINode
& aNode
,
161 bool aShowingAnonymousContent
) {
162 return GetParentForNode(aNode
, aShowingAnonymousContent
);
165 static void GetChildrenForNode(GlobalObject
&, nsINode
& aNode
,
166 bool aShowingAnonymousContent
,
167 bool aIncludeAssignedNodes
,
168 nsTArray
<RefPtr
<nsINode
>>& aResult
) {
169 return GetChildrenForNode(aNode
, aShowingAnonymousContent
,
170 aIncludeAssignedNodes
,
171 /* aIncludeSubdocuments = */ true, aResult
);
173 static void GetChildrenForNode(nsINode
& aNode
, bool aShowingAnonymousContent
,
174 bool aIncludeAssignedNodes
,
175 bool aIncludeSubdocuments
,
176 nsTArray
<RefPtr
<nsINode
>>& aResult
);
179 * Setting and removing content state on an element. Both these functions
180 * call EventStateManager::SetContentState internally; the difference is
181 * that for the remove case we simply pass in nullptr for the element.
182 * Use them accordingly.
184 * When removing the active state, you may optionally also clear the active
185 * document as well by setting aClearActiveDocument
187 * @return Returns true if the state was set successfully. See more details
188 * in EventStateManager.h SetContentState.
190 static bool SetContentState(GlobalObject
& aGlobal
, Element
& aElement
,
191 uint64_t aState
, ErrorResult
& aRv
);
192 static bool RemoveContentState(GlobalObject
& aGlobal
, Element
& aElement
,
193 uint64_t aState
, bool aClearActiveDocument
,
195 static uint64_t GetContentState(GlobalObject
& aGlobal
, Element
& aElement
);
197 static void GetUsedFontFaces(GlobalObject
& aGlobal
, nsRange
& aRange
,
198 uint32_t aMaxRanges
, // max number of ranges to
199 // record for each face
200 bool aSkipCollapsedWhitespace
,
201 nsLayoutUtils::UsedFontFaceList
& aResult
,
205 * Get the names of all the supported pseudo-elements.
206 * Pseudo-elements which are only accepted in UA style sheets are
209 static void GetCSSPseudoElementNames(GlobalObject
& aGlobal
,
210 nsTArray
<nsString
>& aResult
);
212 // pseudo-class style locking methods. aPseudoClass must be a valid
213 // pseudo-class selector string, e.g. ":hover". ":any-link" and
214 // non-event-state pseudo-classes are ignored. aEnabled sets whether the
215 // psuedo-class should be locked to on or off.
216 static void AddPseudoClassLock(GlobalObject
& aGlobal
, Element
& aElement
,
217 const nsAString
& aPseudoClass
, bool aEnabled
);
218 static void RemovePseudoClassLock(GlobalObject
& aGlobal
, Element
& aElement
,
219 const nsAString
& aPseudoClass
);
220 static bool HasPseudoClassLock(GlobalObject
& aGlobal
, Element
& aElement
,
221 const nsAString
& aPseudoClass
);
222 static void ClearPseudoClassLocks(GlobalObject
& aGlobal
, Element
& aElement
);
224 static bool IsElementThemed(GlobalObject
& aGlobal
, Element
& aElement
);
226 static Element
* ContainingBlockOf(GlobalObject
&, Element
&);
228 static void GetBlockLineCounts(GlobalObject
& aGlobal
, Element
& aElement
,
229 Nullable
<nsTArray
<uint32_t>>& aResult
);
232 static already_AddRefed
<nsINodeList
> GetOverflowingChildrenOfElement(
233 GlobalObject
& aGlobal
, Element
& element
);
236 * Parse CSS and update the style sheet in place.
238 * @param DOMCSSStyleSheet aSheet
239 * @param UTF8String aInput
240 * The new source string for the style sheet.
242 static void ParseStyleSheet(GlobalObject
& aGlobal
, StyleSheet
& aSheet
,
243 const nsACString
& aInput
, ErrorResult
& aRv
);
246 * Check if the provided name can be custom element name.
248 static bool IsCustomElementName(GlobalObject
&, const nsAString
& aName
,
249 const nsAString
& aNamespaceURI
);
252 * Get the names of registered Highlights
254 static void GetRegisteredCssHighlights(GlobalObject
& aGlobal
,
255 Document
& aDocument
, bool aActiveOnly
,
256 nsTArray
<nsString
>& aResult
);
258 * Get registered CSS properties (via CSS.registerProperty or @property)
260 static void GetCSSRegisteredProperties(
261 GlobalObject
& aGlobal
, Document
& aDocument
,
262 nsTArray
<InspectorCSSPropertyDefinition
>& aResult
);
265 } // namespace mozilla::dom
267 #endif // mozilla_dom_InspectorUtils_h