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