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