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"
21 class BindingStyleRule
;
30 class InspectorFontFace
;
32 } // namespace mozilla
38 * A collection of utility methods for use by devtools.
40 class InspectorUtils
{
42 static void GetAllStyleSheets(GlobalObject
& aGlobal
, Document
& aDocument
,
44 nsTArray
<RefPtr
<StyleSheet
>>& aResult
);
45 static void GetCSSStyleRules(GlobalObject
& aGlobal
, Element
& aElement
,
46 const nsAString
& aPseudo
,
47 bool aIncludeVisitedStyle
,
48 nsTArray
<RefPtr
<BindingStyleRule
>>& aResult
);
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
);
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
);
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
74 static uint32_t GetRelativeRuleLine(GlobalObject
& aGlobal
, css::Rule
& aRule
);
76 static bool HasRulesModifiedByCSSOM(GlobalObject
& aGlobal
,
79 // Utilities for working with selectors. We don't have a JS OM representation
80 // of a single selector or a selector list yet, but given a rule we can index
81 // into the selector list.
83 // These methods would probably make more sense being [ChromeOnly] APIs on
84 // CSSStyleRule itself (bug 1428245).
85 static uint32_t GetSelectorCount(GlobalObject
& aGlobal
,
86 BindingStyleRule
& aRule
);
88 // For all three functions below, aSelectorIndex is 0-based
89 static void GetSelectorText(GlobalObject
& aGlobal
, BindingStyleRule
& aRule
,
90 uint32_t aSelectorIndex
, nsACString
& aText
,
92 static uint64_t GetSpecificity(GlobalObject
& aGlobal
, BindingStyleRule
& aRule
,
93 uint32_t aSelectorIndex
, ErrorResult
& aRv
);
94 // Note: This does not handle scoped selectors correctly, because it has no
95 // idea what the right scope is.
96 static bool SelectorMatchesElement(GlobalObject
& aGlobal
, Element
& aElement
,
97 BindingStyleRule
& aRule
,
98 uint32_t aSelectorIndex
,
99 const nsAString
& aPseudo
,
100 bool aRelevantLinkVisited
,
103 // Utilities for working with CSS properties
105 // Returns true if the string names a property that is inherited by default.
106 static bool IsInheritedProperty(GlobalObject
& aGlobal
,
107 const nsACString
& aPropertyName
);
109 // Get a list of all our supported property names. Optionally
110 // property aliases included.
111 static void GetCSSPropertyNames(GlobalObject
& aGlobal
,
112 const PropertyNamesOptions
& aOptions
,
113 nsTArray
<nsString
>& aResult
);
115 // Get a list of all properties controlled by preference, as well as
116 // their corresponding preference names.
117 static void GetCSSPropertyPrefs(GlobalObject
& aGlobal
,
118 nsTArray
<PropertyPref
>& aResult
);
120 // Get a list of all valid keywords and colors for aProperty.
121 static void GetCSSValuesForProperty(GlobalObject
& aGlobal
,
122 const nsACString
& aPropertyName
,
123 nsTArray
<nsString
>& aResult
,
126 // Utilities for working with CSS colors
127 static void RgbToColorName(GlobalObject
& aGlobal
, uint8_t aR
, uint8_t aG
,
128 uint8_t aB
, nsAString
& aResult
);
130 // Convert a given CSS color string to rgba. Returns null on failure or an
131 // InspectorRGBATuple on success.
133 // NOTE: Converting a color to RGBA may be lossy when converting from some
134 // formats e.g. CMYK.
135 static void ColorToRGBA(GlobalObject
&, const nsACString
& aColorString
,
137 Nullable
<InspectorRGBATuple
>& aResult
);
139 // Check whether a given color is a valid CSS color.
140 static bool IsValidCSSColor(GlobalObject
& aGlobal
,
141 const nsACString
& aColorString
);
143 // Utilities for obtaining information about a CSS property.
145 // Get a list of the longhands corresponding to the given CSS property. If
146 // the property is a longhand already, just returns the property itself.
147 // Throws on unsupported property names.
148 static void GetSubpropertiesForCSSProperty(GlobalObject
& aGlobal
,
149 const nsACString
& aProperty
,
150 nsTArray
<nsString
>& aResult
,
153 // Check whether a given CSS property is a shorthand. Throws on unsupported
155 static bool CssPropertyIsShorthand(GlobalObject
& aGlobal
,
156 const nsACString
& aProperty
,
159 // Check whether values of the given type are valid values for the property.
160 // For shorthands, checks whether there's a corresponding longhand property
161 // that accepts values of this type. Throws on unsupported properties or
163 static bool CssPropertySupportsType(GlobalObject
& aGlobal
,
164 const nsACString
& aProperty
,
165 InspectorPropertyType
, ErrorResult
& aRv
);
167 static bool Supports(GlobalObject
&, const nsACString
& aDeclaration
,
168 const SupportsOptions
&);
170 static bool IsIgnorableWhitespace(GlobalObject
& aGlobalObject
,
171 CharacterData
& aDataNode
) {
172 return IsIgnorableWhitespace(aDataNode
);
174 static bool IsIgnorableWhitespace(CharacterData
& aDataNode
);
176 // Returns the "parent" of a node. The parent of a document node is the
177 // frame/iframe containing that document. aShowingAnonymousContent says
178 // whether we are showing anonymous content.
179 static nsINode
* GetParentForNode(nsINode
& aNode
,
180 bool aShowingAnonymousContent
);
181 static nsINode
* GetParentForNode(GlobalObject
& aGlobalObject
, nsINode
& aNode
,
182 bool aShowingAnonymousContent
) {
183 return GetParentForNode(aNode
, aShowingAnonymousContent
);
186 static already_AddRefed
<nsINodeList
> GetChildrenForNode(
187 GlobalObject
& aGlobalObject
, nsINode
& aNode
,
188 bool aShowingAnonymousContent
) {
189 return GetChildrenForNode(aNode
, aShowingAnonymousContent
);
191 static already_AddRefed
<nsINodeList
> GetChildrenForNode(
192 nsINode
& aNode
, bool aShowingAnonymousContent
);
195 * Setting and removing content state on an element. Both these functions
196 * call EventStateManager::SetContentState internally; the difference is
197 * that for the remove case we simply pass in nullptr for the element.
198 * Use them accordingly.
200 * When removing the active state, you may optionally also clear the active
201 * document as well by setting aClearActiveDocument
203 * @return Returns true if the state was set successfully. See more details
204 * in EventStateManager.h SetContentState.
206 static bool SetContentState(GlobalObject
& aGlobal
, Element
& aElement
,
207 uint64_t aState
, ErrorResult
& aRv
);
208 static bool RemoveContentState(GlobalObject
& aGlobal
, Element
& aElement
,
209 uint64_t aState
, bool aClearActiveDocument
,
211 static uint64_t GetContentState(GlobalObject
& aGlobal
, Element
& aElement
);
213 static void GetUsedFontFaces(GlobalObject
& aGlobal
, nsRange
& aRange
,
214 uint32_t aMaxRanges
, // max number of ranges to
215 // record for each face
216 bool aSkipCollapsedWhitespace
,
217 nsLayoutUtils::UsedFontFaceList
& aResult
,
221 * Get the names of all the supported pseudo-elements.
222 * Pseudo-elements which are only accepted in UA style sheets are
225 static void GetCSSPseudoElementNames(GlobalObject
& aGlobal
,
226 nsTArray
<nsString
>& aResult
);
228 // pseudo-class style locking methods. aPseudoClass must be a valid
229 // pseudo-class selector string, e.g. ":hover". ":any-link" and
230 // non-event-state pseudo-classes are ignored. aEnabled sets whether the
231 // psuedo-class should be locked to on or off.
232 static void AddPseudoClassLock(GlobalObject
& aGlobal
, Element
& aElement
,
233 const nsAString
& aPseudoClass
, bool aEnabled
);
234 static void RemovePseudoClassLock(GlobalObject
& aGlobal
, Element
& aElement
,
235 const nsAString
& aPseudoClass
);
236 static bool HasPseudoClassLock(GlobalObject
& aGlobal
, Element
& aElement
,
237 const nsAString
& aPseudoClass
);
238 static void ClearPseudoClassLocks(GlobalObject
& aGlobal
, Element
& aElement
);
240 static bool IsElementThemed(GlobalObject
& aGlobal
, Element
& aElement
);
242 static Element
* ContainingBlockOf(GlobalObject
&, Element
&);
245 static already_AddRefed
<nsINodeList
> GetOverflowingChildrenOfElement(
246 GlobalObject
& aGlobal
, Element
& element
);
249 * Parse CSS and update the style sheet in place.
251 * @param DOMCSSStyleSheet aSheet
252 * @param UTF8String aInput
253 * The new source string for the style sheet.
255 static void ParseStyleSheet(GlobalObject
& aGlobal
, StyleSheet
& aSheet
,
256 const nsACString
& aInput
, ErrorResult
& aRv
);
259 * Check if the provided name can be custom element name.
261 static bool IsCustomElementName(GlobalObject
&, const nsAString
& aName
,
262 const nsAString
& aNamespaceURI
);
266 } // namespace mozilla
268 #endif // mozilla_dom_InspectorUtils_h