|
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 |
|
8 #include "nsCoord.h" |
|
9 #include "nsCSSProperty.h" |
|
10 #include "nsString.h" |
|
11 #include "nsTArrayForwardDeclare.h" |
|
12 |
|
13 class nsCSSValue; |
|
14 class nsStringComparator; |
|
15 class nsStyleCoord; |
|
16 class nsIContent; |
|
17 class nsIPrincipal; |
|
18 class nsIURI; |
|
19 struct gfxFontFeature; |
|
20 struct gfxAlternateValue; |
|
21 class nsCSSValueList; |
|
22 |
|
23 // Style utility functions |
|
24 class nsStyleUtil { |
|
25 public: |
|
26 |
|
27 static bool DashMatchCompare(const nsAString& aAttributeValue, |
|
28 const nsAString& aSelectorValue, |
|
29 const nsStringComparator& aComparator); |
|
30 |
|
31 // Append a quoted (with 'quoteChar') and escaped version of aString |
|
32 // to aResult. 'quoteChar' must be ' or ". |
|
33 static void AppendEscapedCSSString(const nsAString& aString, |
|
34 nsAString& aResult, |
|
35 char16_t quoteChar = '"'); |
|
36 |
|
37 // Append the identifier given by |aIdent| to |aResult|, with |
|
38 // appropriate escaping so that it can be reparsed to the same |
|
39 // identifier. |
|
40 // Returns false if |aIdent| contains U+0000 |
|
41 // Returns true for all other cases |
|
42 static bool AppendEscapedCSSIdent(const nsAString& aIdent, |
|
43 nsAString& aResult); |
|
44 |
|
45 // Append a bitmask-valued property's value(s) (space-separated) to aResult. |
|
46 static void AppendBitmaskCSSValue(nsCSSProperty aProperty, |
|
47 int32_t aMaskedValue, |
|
48 int32_t aFirstMask, |
|
49 int32_t aLastMask, |
|
50 nsAString& aResult); |
|
51 |
|
52 static void AppendAngleValue(const nsStyleCoord& aValue, nsAString& aResult); |
|
53 |
|
54 static void AppendPaintOrderValue(uint8_t aValue, nsAString& aResult); |
|
55 |
|
56 static void AppendFontFeatureSettings(const nsTArray<gfxFontFeature>& aFeatures, |
|
57 nsAString& aResult); |
|
58 |
|
59 static void AppendFontFeatureSettings(const nsCSSValue& src, |
|
60 nsAString& aResult); |
|
61 |
|
62 static void AppendCSSNumber(float aNumber, nsAString& aResult) |
|
63 { |
|
64 aResult.AppendFloat(aNumber); |
|
65 } |
|
66 |
|
67 // convert bitmask value to keyword name for a functional alternate |
|
68 static void GetFunctionalAlternatesName(int32_t aFeature, |
|
69 nsAString& aFeatureName); |
|
70 |
|
71 // Append functional font-variant-alternates values to string |
|
72 static void |
|
73 SerializeFunctionalAlternates(const nsTArray<gfxAlternateValue>& aAlternates, |
|
74 nsAString& aResult); |
|
75 |
|
76 // List of functional font-variant-alternates values to feature/value pairs |
|
77 static void |
|
78 ComputeFunctionalAlternates(const nsCSSValueList* aList, |
|
79 nsTArray<gfxAlternateValue>& aAlternateValues); |
|
80 |
|
81 /* |
|
82 * Convert an author-provided floating point number to an integer (0 |
|
83 * ... 255) appropriate for use in the alpha component of a color. |
|
84 */ |
|
85 static uint8_t FloatToColorComponent(float aAlpha) |
|
86 { |
|
87 NS_ASSERTION(0.0 <= aAlpha && aAlpha <= 1.0, "out of range"); |
|
88 return NSToIntRound(aAlpha * 255); |
|
89 } |
|
90 |
|
91 /* |
|
92 * Convert the alpha component of an nscolor (0 ... 255) to the |
|
93 * floating point number with the least accurate *decimal* |
|
94 * representation that is converted to that color. |
|
95 * |
|
96 * Should be used only by serialization code. |
|
97 */ |
|
98 static float ColorComponentToFloat(uint8_t aAlpha); |
|
99 |
|
100 /* |
|
101 * Does this child count as significant for selector matching? |
|
102 */ |
|
103 static bool IsSignificantChild(nsIContent* aChild, |
|
104 bool aTextIsSignificant, |
|
105 bool aWhitespaceIsSignificant); |
|
106 /* |
|
107 * Does this principal have a CSP that blocks the application of |
|
108 * inline styles? Returns false if application of the style should |
|
109 * be blocked. |
|
110 * |
|
111 * @param aContent |
|
112 * The <style> element that the caller wants to know whether to honor. |
|
113 * Included to check the nonce attribute if one is provided. Allowed to |
|
114 * be null, if this is for something other than a <style> element (in |
|
115 * which case nonces won't be checked). |
|
116 * @param aPrincipal |
|
117 * The principal of the of the document (*not* of the style sheet). |
|
118 * The document's principal is where any Content Security Policy that |
|
119 * should be used to block or allow inline styles will be located. |
|
120 * @param aSourceURI |
|
121 * URI of document containing inline style (for reporting violations) |
|
122 * @param aLineNumber |
|
123 * Line number of inline style element in the containing document (for |
|
124 * reporting violations) |
|
125 * @param aStyleText |
|
126 * Contents of the inline style element (for reporting violations) |
|
127 * @param aRv |
|
128 * Return error code in case of failure |
|
129 * @return |
|
130 * Does CSP allow application of the specified inline style? |
|
131 */ |
|
132 static bool CSPAllowsInlineStyle(nsIContent* aContent, |
|
133 nsIPrincipal* aPrincipal, |
|
134 nsIURI* aSourceURI, |
|
135 uint32_t aLineNumber, |
|
136 const nsSubstring& aStyleText, |
|
137 nsresult* aRv); |
|
138 |
|
139 }; |
|
140 |
|
141 |
|
142 #endif /* nsStyleUtil_h___ */ |