InspectorUtils.h (13410B)
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 */ 7 8 #ifndef mozilla_dom_InspectorUtils_h 9 #define mozilla_dom_InspectorUtils_h 10 11 #include "mozilla/dom/InspectorUtilsBinding.h" 12 #include "nsLayoutUtils.h" 13 14 class nsAtom; 15 class nsINode; 16 class nsINodeList; 17 class nsRange; 18 19 namespace mozilla { 20 class StyleSheet; 21 namespace css { 22 class Rule; 23 } // namespace css 24 namespace dom { 25 class CharacterData; 26 class Document; 27 class Element; 28 class InspectorFontFace; 29 } // namespace dom 30 } // namespace mozilla 31 32 namespace mozilla::dom { 33 /** 34 * A collection of utility methods for use by devtools. 35 */ 36 class InspectorUtils { 37 public: 38 static void GetAllStyleSheets(GlobalObject& aGlobal, Document& aDocument, 39 bool aDocumentOnly, 40 nsTArray<RefPtr<StyleSheet>>& aResult); 41 static void GetMatchingCSSRules( 42 GlobalObject& aGlobal, Element& aElement, const nsAString& aPseudo, 43 bool aIncludeVisitedStyle, bool aWithStartingStyle, 44 nsTArray<OwningCSSRuleOrInspectorDeclaration>& aResult); 45 46 /** 47 * Get the line number of a rule. 48 * 49 * @param aRule The rule. 50 * @return The rule's line number. Line numbers are 1-based. 51 */ 52 static uint32_t GetRuleLine(GlobalObject& aGlobal, css::Rule& aRule); 53 54 /** 55 * Get the column number of a rule. 56 * 57 * @param aRule The rule. 58 * @return The rule's column number. Column numbers are 1-based. 59 */ 60 static uint32_t GetRuleColumn(GlobalObject& aGlobal, css::Rule& aRule); 61 62 /** 63 * Like getRuleLine, but if the rule is in a <style> element, 64 * returns a line number relative to the start of the element. 65 * 66 * @param aRule the rule to examine 67 * @return the line number of the rule, possibly relative to the 68 * <style> element 69 */ 70 static uint32_t GetRelativeRuleLine(GlobalObject& aGlobal, css::Rule& aRule); 71 72 static void GetRuleIndex(GlobalObject& aGlobal, css::Rule& aRule, 73 nsTArray<uint32_t>& aResult); 74 75 static bool HasRulesModifiedByCSSOM(GlobalObject& aGlobal, 76 StyleSheet& aSheet); 77 78 static void GetStyleSheetRuleCountAndAtRules( 79 GlobalObject& aGlobal, StyleSheet& aSheet, 80 InspectorStyleSheetRuleCountAndAtRulesResult& aResult); 81 82 // Utilities for working with CSS properties 83 // 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); 87 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); 93 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); 98 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, 103 ErrorResult& aRv); 104 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); 108 109 static void RgbToNearestColorName(GlobalObject&, float aR, float aG, float aB, 110 InspectorNearestColor& aResult); 111 112 static void RgbToHsv(GlobalObject&, float aR, float aG, float aB, 113 nsTArray<float>& aResult); 114 115 static void HsvToRgb(GlobalObject&, float aH, float aS, float aV, 116 nsTArray<float>& aResult); 117 118 static float RelativeLuminance(GlobalObject&, float aR, float aG, float aB); 119 120 // Convert a given CSS color string to rgba. Returns null on failure or an 121 // InspectorRGBATuple on success. 122 // 123 // NOTE: Converting a color to RGBA may be lossy when converting from some 124 // formats e.g. CMYK. 125 static void ColorToRGBA(GlobalObject&, const nsACString& aColorString, 126 Nullable<InspectorRGBATuple>& aResult); 127 128 // Convert a given CSS color string to another color space. 129 static void ColorTo(GlobalObject&, const nsACString& aFromColor, 130 const nsACString& aToColorSpace, 131 Nullable<InspectorColorToResult>& aResult); 132 133 // Check whether a given color is a valid CSS color. 134 static bool IsValidCSSColor(GlobalObject& aGlobal, 135 const nsACString& aColorString); 136 137 // Utilities for obtaining information about a CSS property. 138 139 // Get a list of the longhands corresponding to the given CSS property. If 140 // the property is a longhand already, just returns the property itself. 141 // Throws on unsupported property names. 142 static void GetSubpropertiesForCSSProperty(GlobalObject& aGlobal, 143 const nsACString& aProperty, 144 nsTArray<nsString>& aResult, 145 ErrorResult& aRv); 146 147 // Check whether a given CSS property is a shorthand. Throws on unsupported 148 // property names. 149 static bool CssPropertyIsShorthand(GlobalObject& aGlobal, 150 const nsACString& aProperty, 151 ErrorResult& aRv); 152 153 // Check whether values of the given type are valid values for the property. 154 // For shorthands, checks whether there's a corresponding longhand property 155 // that accepts values of this type. Throws on unsupported properties or 156 // unknown types. 157 static bool CssPropertySupportsType(GlobalObject& aGlobal, 158 const nsACString& aProperty, 159 InspectorPropertyType, ErrorResult& aRv); 160 161 static bool Supports(GlobalObject&, const nsACString& aDeclaration, 162 const SupportsOptions&); 163 164 static bool IsIgnorableWhitespace(GlobalObject& aGlobalObject, 165 CharacterData& aDataNode) { 166 return IsIgnorableWhitespace(aDataNode); 167 } 168 static bool IsIgnorableWhitespace(CharacterData& aDataNode); 169 170 // Returns the "parent" of a node. The parent of a document node is the 171 // frame/iframe containing that document. aShowingAnonymousContent says 172 // whether we are showing anonymous content. 173 static nsINode* GetParentForNode(nsINode& aNode, 174 bool aShowingAnonymousContent); 175 static nsINode* GetParentForNode(GlobalObject& aGlobalObject, nsINode& aNode, 176 bool aShowingAnonymousContent) { 177 return GetParentForNode(aNode, aShowingAnonymousContent); 178 } 179 180 static void GetChildrenForNode(GlobalObject&, nsINode& aNode, 181 bool aShowingAnonymousContent, 182 bool aIncludeAssignedNodes, 183 nsTArray<RefPtr<nsINode>>& aResult) { 184 return GetChildrenForNode(aNode, aShowingAnonymousContent, 185 aIncludeAssignedNodes, 186 /* aIncludeSubdocuments = */ true, aResult); 187 } 188 static void GetChildrenForNode(nsINode& aNode, bool aShowingAnonymousContent, 189 bool aIncludeAssignedNodes, 190 bool aIncludeSubdocuments, 191 nsTArray<RefPtr<nsINode>>& aResult); 192 193 /** 194 * Setting and removing content state on an element. Both these functions 195 * call EventStateManager::SetContentState internally; the difference is 196 * that for the remove case we simply pass in nullptr for the element. 197 * Use them accordingly. 198 * 199 * When removing the active state, you may optionally also clear the active 200 * document as well by setting aClearActiveDocument 201 * 202 * @return Returns true if the state was set successfully. See more details 203 * in EventStateManager.h SetContentState. 204 */ 205 static bool SetContentState(GlobalObject& aGlobal, Element& aElement, 206 uint64_t aState, ErrorResult& aRv); 207 static bool RemoveContentState(GlobalObject& aGlobal, Element& aElement, 208 uint64_t aState, bool aClearActiveDocument, 209 ErrorResult& aRv); 210 static uint64_t GetContentState(GlobalObject& aGlobal, Element& aElement); 211 212 static void GetUsedFontFaces(GlobalObject& aGlobal, nsRange& aRange, 213 uint32_t aMaxRanges, // max number of ranges to 214 // record for each face 215 bool aSkipCollapsedWhitespace, 216 nsLayoutUtils::UsedFontFaceList& aResult, 217 ErrorResult& aRv); 218 219 /** 220 * Get the names of all the supported pseudo-elements. 221 * Pseudo-elements which are only accepted in UA style sheets are 222 * not included. 223 */ 224 static void GetCSSPseudoElementNames(GlobalObject&, 225 nsTArray<nsString>& aResult); 226 227 // pseudo-class style locking methods. aPseudoClass must be a valid 228 // pseudo-class selector string, e.g. ":hover". ":any-link" and 229 // non-event-state pseudo-classes are ignored. aEnabled sets whether the 230 // psuedo-class should be locked to on or off. 231 static void AddPseudoClassLock(GlobalObject&, Element&, 232 const nsAString& aPseudoClass, bool aEnabled); 233 static void RemovePseudoClassLock(GlobalObject&, Element&, 234 const nsAString& aPseudoClass); 235 static bool HasPseudoClassLock(GlobalObject&, Element&, 236 const nsAString& aPseudoClass); 237 static void ClearPseudoClassLocks(GlobalObject&, Element&); 238 239 static bool IsElementThemed(GlobalObject&, Element&); 240 241 static bool IsUsedColorSchemeDark(GlobalObject&, Element&); 242 243 static Element* ContainingBlockOf(GlobalObject&, Element&); 244 245 static bool IsBlockContainer(GlobalObject&, Element&); 246 247 static void GetBlockLineCounts(GlobalObject&, Element&, 248 Nullable<nsTArray<uint32_t>>& aResult); 249 250 MOZ_CAN_RUN_SCRIPT 251 static already_AddRefed<nsINodeList> GetOverflowingChildrenOfElement( 252 GlobalObject& aGlobal, Element& element); 253 254 /** 255 * Parse CSS and update the style sheet in place. 256 * 257 * @param DOMCSSStyleSheet aSheet 258 * @param UTF8String aInput 259 * The new source string for the style sheet. 260 */ 261 static void ParseStyleSheet(GlobalObject& aGlobal, StyleSheet& aSheet, 262 const nsACString& aInput, ErrorResult& aRv); 263 264 /** 265 * Check if the provided name can be custom element name. 266 */ 267 static bool IsCustomElementName(GlobalObject&, const nsAString& aName, 268 const nsAString& aNamespaceURI); 269 270 /** 271 * Get the names of registered Highlights 272 */ 273 static void GetRegisteredCssHighlights(GlobalObject& aGlobal, 274 Document& aDocument, bool aActiveOnly, 275 nsTArray<nsString>& aResult); 276 /** 277 * Get registered CSS properties (via CSS.registerProperty or @property) 278 */ 279 static void GetCSSRegisteredProperties( 280 GlobalObject& aGlobal, Document& aDocument, 281 nsTArray<InspectorCSSPropertyDefinition>& aResult); 282 283 /** 284 * Get a single registered CSS property 285 */ 286 static void GetCSSRegisteredProperty( 287 GlobalObject& aGlobal, Document& aDocument, const nsACString& aName, 288 Nullable<InspectorCSSPropertyDefinition>& aResult); 289 290 /** 291 * Returns whether or not a CSS property value is valid for the passed syntax 292 */ 293 static bool ValueMatchesSyntax(GlobalObject&, Document& aDocument, 294 const nsACString& aValue, 295 const nsACString& aSyntax); 296 297 /** 298 * Get the rule body text within aInitialText 299 */ 300 static void GetRuleBodyText(GlobalObject&, const nsACString& aInitialText, 301 nsACString& aBodyText); 302 303 /** 304 * Replace the rule body text in aStyleSheetText at passed line and column 305 */ 306 static void ReplaceBlockRuleBodyTextInStylesheet( 307 GlobalObject&, const nsACString& aStyleSheetText, uint32_t aLine, 308 uint32_t aColumn, const nsACString& aNewBodyText, 309 nsACString& aNewStyleSheetText); 310 311 static void SetVerticalClipping(GlobalObject&, BrowsingContext* aContext, 312 mozilla::CSSCoord aOffset); 313 static void SetDynamicToolbarMaxHeight(GlobalObject&, 314 BrowsingContext* aContext, 315 mozilla::CSSCoord aHeight); 316 static uint16_t GetGridContainerType(GlobalObject&, Element&); 317 }; 318 319 } // namespace mozilla::dom 320 321 #endif // mozilla_dom_InspectorUtils_h