nsStyleUtil.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef nsStyleUtil_h___
  6. #define nsStyleUtil_h___
  7. #include "nsCoord.h"
  8. #include "nsCSSPropertyID.h"
  9. #include "nsString.h"
  10. #include "nsTArrayForwardDeclare.h"
  11. #include "gfxFontFamilyList.h"
  12. #include "nsStyleStruct.h"
  13. #include "nsCRT.h"
  14. class nsCSSValue;
  15. class nsStringComparator;
  16. class nsStyleCoord;
  17. class nsIContent;
  18. class nsIPrincipal;
  19. class nsIURI;
  20. struct gfxFontFeature;
  21. struct gfxAlternateValue;
  22. struct nsCSSValueList;
  23. // Style utility functions
  24. class nsStyleUtil {
  25. public:
  26. static bool DashMatchCompare(const nsAString& aAttributeValue,
  27. const nsAString& aSelectorValue,
  28. const nsStringComparator& aComparator);
  29. static bool ValueIncludes(const nsSubstring& aValueList,
  30. const nsSubstring& aValue,
  31. const nsStringComparator& aComparator);
  32. // Append a quoted (with 'quoteChar') and escaped version of aString
  33. // to aResult. 'quoteChar' must be ' or ".
  34. static void AppendEscapedCSSString(const nsAString& aString,
  35. nsAString& aResult,
  36. char16_t quoteChar = '"');
  37. // Append the identifier given by |aIdent| to |aResult|, with
  38. // appropriate escaping so that it can be reparsed to the same
  39. // identifier. An exception is if aIdent contains U+0000, which
  40. // will be escaped as U+FFFD and then reparsed back to U+FFFD.
  41. static void AppendEscapedCSSIdent(const nsAString& aIdent,
  42. nsAString& aResult);
  43. static void
  44. AppendEscapedCSSFontFamilyList(const mozilla::FontFamilyList& aFamilyList,
  45. nsAString& aResult);
  46. // Append a bitmask-valued property's value(s) (space-separated) to aResult.
  47. static void AppendBitmaskCSSValue(nsCSSPropertyID aProperty,
  48. int32_t aMaskedValue,
  49. int32_t aFirstMask,
  50. int32_t aLastMask,
  51. nsAString& aResult);
  52. static void AppendAngleValue(const nsStyleCoord& aValue, nsAString& aResult);
  53. static void AppendPaintOrderValue(uint8_t aValue, nsAString& aResult);
  54. static void AppendFontFeatureSettings(const nsTArray<gfxFontFeature>& aFeatures,
  55. nsAString& aResult);
  56. static void AppendFontFeatureSettings(const nsCSSValue& src,
  57. nsAString& aResult);
  58. static void AppendUnicodeRange(const nsCSSValue& aValue, nsAString& aResult);
  59. static void AppendCSSNumber(float aNumber, nsAString& aResult)
  60. {
  61. aResult.AppendFloat(aNumber);
  62. }
  63. static void AppendStepsTimingFunction(nsTimingFunction::Type aType,
  64. uint32_t aSteps,
  65. nsAString& aResult);
  66. static void AppendCubicBezierTimingFunction(float aX1, float aY1,
  67. float aX2, float aY2,
  68. nsAString& aResult);
  69. static void AppendCubicBezierKeywordTimingFunction(
  70. nsTimingFunction::Type aType,
  71. nsAString& aResult);
  72. static void AppendSerializedFontSrc(const nsCSSValue& aValue,
  73. nsAString& aResult);
  74. // convert bitmask value to keyword name for a functional alternate
  75. static void GetFunctionalAlternatesName(int32_t aFeature,
  76. nsAString& aFeatureName);
  77. // Append functional font-variant-alternates values to string
  78. static void
  79. SerializeFunctionalAlternates(const nsTArray<gfxAlternateValue>& aAlternates,
  80. nsAString& aResult);
  81. // List of functional font-variant-alternates values to feature/value pairs
  82. static void
  83. ComputeFunctionalAlternates(const nsCSSValueList* aList,
  84. nsTArray<gfxAlternateValue>& aAlternateValues);
  85. /*
  86. * Convert an author-provided floating point number to an integer (0
  87. * ... 255) appropriate for use in the alpha component of a color.
  88. */
  89. static uint8_t FloatToColorComponent(float aAlpha)
  90. {
  91. NS_ASSERTION(0.0 <= aAlpha && aAlpha <= 1.0, "out of range");
  92. return NSToIntRound(aAlpha * 255);
  93. }
  94. /*
  95. * Convert the alpha component of an nscolor (0 ... 255) to the
  96. * floating point number with the least accurate *decimal*
  97. * representation that is converted to that color.
  98. *
  99. * Should be used only by serialization code.
  100. */
  101. static float ColorComponentToFloat(uint8_t aAlpha);
  102. /*
  103. * Does this child count as significant for selector matching?
  104. */
  105. static bool IsSignificantChild(nsIContent* aChild,
  106. bool aTextIsSignificant,
  107. bool aWhitespaceIsSignificant);
  108. /**
  109. * Returns true if our object-fit & object-position properties might cause
  110. * a replaced element's contents to overflow its content-box (requiring
  111. * clipping), or false if we can be sure that this won't happen.
  112. *
  113. * This lets us optimize by skipping clipping when we can tell it's
  114. * unnecessary (particularly with the default values of these properties).
  115. *
  116. * @param aStylePos The nsStylePosition whose object-fit & object-position
  117. * properties should be checked for potential overflow.
  118. * @return false if we can be sure that the object-fit & object-position
  119. * properties on 'aStylePos' cannot cause a replaced element's
  120. * contents to overflow its content-box. Otherwise (if overflow is
  121. * is possible), returns true.
  122. */
  123. static bool ObjectPropsMightCauseOverflow(const nsStylePosition* aStylePos);
  124. /*
  125. * Does this principal have a CSP that blocks the application of
  126. * inline styles? Returns false if application of the style should
  127. * be blocked.
  128. *
  129. * @param aContent
  130. * The <style> element that the caller wants to know whether to honor.
  131. * Included to check the nonce attribute if one is provided. Allowed to
  132. * be null, if this is for something other than a <style> element (in
  133. * which case nonces won't be checked).
  134. * @param aPrincipal
  135. * The principal of the of the document (*not* of the style sheet).
  136. * The document's principal is where any Content Security Policy that
  137. * should be used to block or allow inline styles will be located.
  138. * @param aSourceURI
  139. * URI of document containing inline style (for reporting violations)
  140. * @param aLineNumber
  141. * Line number of inline style element in the containing document (for
  142. * reporting violations)
  143. * @param aStyleText
  144. * Contents of the inline style element (for reporting violations)
  145. * @param aRv
  146. * Return error code in case of failure
  147. * @return
  148. * Does CSP allow application of the specified inline style?
  149. */
  150. static bool CSPAllowsInlineStyle(nsIContent* aContent,
  151. nsIPrincipal* aPrincipal,
  152. nsIURI* aSourceURI,
  153. uint32_t aLineNumber,
  154. const nsSubstring& aStyleText,
  155. nsresult* aRv);
  156. template<size_t N>
  157. static bool MatchesLanguagePrefix(const char16_t* aLang, size_t aLen,
  158. const char16_t (&aPrefix)[N])
  159. {
  160. return !nsCRT::strncmp(aLang, aPrefix, N - 1) &&
  161. (aLen == N - 1 || aLang[N - 1] == '-');
  162. }
  163. template<size_t N>
  164. static bool MatchesLanguagePrefix(const nsIAtom* aLang,
  165. const char16_t (&aPrefix)[N])
  166. {
  167. MOZ_ASSERT(aLang);
  168. return MatchesLanguagePrefix(aLang->GetUTF16String(),
  169. aLang->GetLength(), aPrefix);
  170. }
  171. template<size_t N>
  172. static bool MatchesLanguagePrefix(const nsAString& aLang,
  173. const char16_t (&aPrefix)[N])
  174. {
  175. return MatchesLanguagePrefix(aLang.Data(), aLang.Length(), aPrefix);
  176. }
  177. };
  178. #endif /* nsStyleUtil_h___ */