nsStyleUtil.h (6823B)
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 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #ifndef nsStyleUtil_h___ 7 #define nsStyleUtil_h___ 8 9 #include "NonCustomCSSPropertyId.h" 10 #include "nsCRT.h" 11 #include "nsColor.h" 12 #include "nsCoord.h" 13 #include "nsGkAtoms.h" 14 #include "nsStringFwd.h" 15 #include "nsTArrayForwardDeclare.h" 16 17 class nsCSSValue; 18 class nsIContent; 19 class nsIPrincipal; 20 class nsIURI; 21 struct nsCSSKTableEntry; 22 struct nsCSSValueList; 23 struct nsStylePosition; 24 25 namespace mozilla { 26 namespace dom { 27 class Document; 28 class Element; 29 } // namespace dom 30 } // namespace mozilla 31 32 // Style utility functions 33 class nsStyleUtil { 34 public: 35 static bool DashMatchCompare(const nsAString& aAttributeValue, 36 const nsAString& aSelectorValue, 37 const nsStringComparator& aComparator); 38 39 static bool LangTagCompare(const nsACString& aAttributeValue, 40 const nsACString& aSelectorValue); 41 42 static bool ValueIncludes(const nsAString& aValueList, 43 const nsAString& aValue, 44 const nsStringComparator& aComparator); 45 46 // Append a quoted (with 'quoteChar') version of aString to aResult. 47 // 'aQuoteChar' must be ' or ". 48 static void AppendQuotedCSSString(const nsACString& aString, 49 nsACString& aResult, char aQuoteChar = '"'); 50 51 // Append the identifier given by |aIdent| to |aResult|, with 52 // appropriate escaping so that it can be reparsed to the same 53 // identifier. An exception is if aIdent contains U+0000, which 54 // will be escaped as U+FFFD and then reparsed back to U+FFFD. 55 static void AppendEscapedCSSIdent(const nsAString& aIdent, 56 nsAString& aResult); 57 58 public: 59 static void AppendCSSNumber(float aNumber, nsAString& aResult) { 60 aResult.AppendFloat(aNumber); 61 } 62 63 /* 64 * Convert an author-provided floating point number to an integer (0 65 * ... 255) appropriate for use in the alpha component of a color. 66 */ 67 static uint8_t FloatToColorComponent(float aAlpha) { 68 NS_ASSERTION(0.0 <= aAlpha && aAlpha <= 1.0, "out of range"); 69 return static_cast<uint8_t>(NSToIntRound(aAlpha * 255)); 70 } 71 72 /* 73 * Convert the alpha component of an nscolor (0 ... 255) to the 74 * floating point number with the least accurate *decimal* 75 * representation that is converted to that color. 76 * 77 * Should be used only by serialization code. 78 */ 79 static float ColorComponentToFloat(uint8_t aAlpha); 80 81 /** 82 * GetSerializedColorValue() computes serialized color value of aColor and 83 * returns it with aSerializedColor. 84 * https://drafts.csswg.org/cssom/#serialize-a-css-component-value 85 */ 86 static void GetSerializedColorValue(nscolor aColor, 87 nsAString& aSerializedColor); 88 89 /* 90 * Does this child count as significant for selector matching? 91 */ 92 static bool IsSignificantChild(nsIContent* aChild, 93 bool aWhitespaceIsSignificant); 94 95 /* 96 * Thread-safe version of IsSignificantChild() 97 */ 98 static bool ThreadSafeIsSignificantChild(const nsIContent* aChild, 99 bool aWhitespaceIsSignificant); 100 /** 101 * Returns true if our object-fit & object-position properties might cause 102 * a replaced element's contents to overflow its content-box (requiring 103 * clipping), or false if we can be sure that this won't happen. 104 * 105 * This lets us optimize by skipping clipping when we can tell it's 106 * unnecessary (particularly with the default values of these properties). 107 * 108 * @param aStylePos The nsStylePosition whose object-fit & object-position 109 * properties should be checked for potential overflow. 110 * @return false if we can be sure that the object-fit & object-position 111 * properties on 'aStylePos' cannot cause a replaced element's 112 * contents to overflow its content-box. Otherwise (if overflow is 113 * is possible), returns true. 114 */ 115 static bool ObjectPropsMightCauseOverflow(const nsStylePosition* aStylePos); 116 117 /* 118 * Does the document have a CSP that blocks the application of 119 * inline styles? Returns false if application of the style should 120 * be blocked. 121 * 122 * @param aContent 123 * The <style> element that the caller wants to know whether to honor. 124 * Included to check the nonce attribute if one is provided. Allowed to 125 * be null, if this is for something other than a <style> element (in 126 * which case nonces won't be checked). 127 * @param aDocument 128 * The document containing the inline style (for querying the CSP); 129 * @param aTriggeringPrincipal 130 * The principal of the scripted caller which added the inline 131 * stylesheet, or null if no scripted caller can be identified. 132 * @param aLineNumber 133 * Line number of inline style element in the containing document (for 134 * reporting violations) 135 * @param aColumnNumber 136 * Column number of inline style element in the containing document (for 137 * reporting violations) 138 * @param aStyleText 139 * Contents of the inline style element (for reporting violations) 140 * @param aRv 141 * Return error code in case of failure 142 * @return 143 * Does CSP allow application of the specified inline style? 144 */ 145 static bool CSPAllowsInlineStyle(mozilla::dom::Element* aContent, 146 mozilla::dom::Document* aDocument, 147 nsIPrincipal* aTriggeringPrincipal, 148 uint32_t aLineNumber, uint32_t aColumnNumber, 149 const nsAString& aStyleText, nsresult* aRv); 150 151 template <size_t N> 152 static bool MatchesLanguagePrefix(const char16_t* aLang, size_t aLen, 153 const char16_t (&aPrefix)[N]) { 154 return !NS_strncmp(aLang, aPrefix, N - 1) && 155 (aLen == N - 1 || aLang[N - 1] == '-'); 156 } 157 158 template <size_t N> 159 static bool MatchesLanguagePrefix(const nsAtom* aLang, 160 const char16_t (&aPrefix)[N]) { 161 MOZ_ASSERT(aLang); 162 return MatchesLanguagePrefix(aLang->GetUTF16String(), aLang->GetLength(), 163 aPrefix); 164 } 165 166 template <size_t N> 167 static bool MatchesLanguagePrefix(const nsAString& aLang, 168 const char16_t (&aPrefix)[N]) { 169 return MatchesLanguagePrefix(aLang.Data(), aLang.Length(), aPrefix); 170 } 171 }; 172 173 #endif /* nsStyleUtil_h___ */