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___
10 #include "nsCSSPropertyID.h"
11 #include "nsTArrayForwardDeclare.h"
12 #include "nsStringFwd.h"
15 #include "nsGkAtoms.h"
21 struct gfxFontFeature
;
22 struct nsCSSKTableEntry
;
23 struct nsCSSValueList
;
24 struct nsStylePosition
;
31 } // namespace mozilla
33 // Style utility functions
36 static bool DashMatchCompare(const nsAString
& aAttributeValue
,
37 const nsAString
& aSelectorValue
,
38 const nsStringComparator
& aComparator
);
40 static bool LangTagCompare(const nsACString
& aAttributeValue
,
41 const nsACString
& aSelectorValue
);
43 static bool ValueIncludes(const nsAString
& aValueList
,
44 const nsAString
& aValue
,
45 const nsStringComparator
& aComparator
);
47 // Append a quoted (with 'quoteChar') and escaped version of aString
48 // to aResult. 'quoteChar' must be ' or ".
49 static void AppendEscapedCSSString(const nsAString
& aString
,
51 char16_t quoteChar
= '"');
53 // Append the identifier given by |aIdent| to |aResult|, with
54 // appropriate escaping so that it can be reparsed to the same
55 // identifier. An exception is if aIdent contains U+0000, which
56 // will be escaped as U+FFFD and then reparsed back to U+FFFD.
57 static void AppendEscapedCSSIdent(const nsAString
& aIdent
,
61 static void AppendCSSNumber(float aNumber
, nsAString
& aResult
) {
62 aResult
.AppendFloat(aNumber
);
66 * Convert an author-provided floating point number to an integer (0
67 * ... 255) appropriate for use in the alpha component of a color.
69 static uint8_t FloatToColorComponent(float aAlpha
) {
70 NS_ASSERTION(0.0 <= aAlpha
&& aAlpha
<= 1.0, "out of range");
71 return static_cast<uint8_t>(NSToIntRound(aAlpha
* 255));
75 * Convert the alpha component of an nscolor (0 ... 255) to the
76 * floating point number with the least accurate *decimal*
77 * representation that is converted to that color.
79 * Should be used only by serialization code.
81 static float ColorComponentToFloat(uint8_t aAlpha
);
84 * GetSerializedColorValue() computes serialized color value of aColor and
85 * returns it with aSerializedColor.
86 * https://drafts.csswg.org/cssom/#serialize-a-css-component-value
88 static void GetSerializedColorValue(nscolor aColor
,
89 nsAString
& aSerializedColor
);
92 * Does this child count as significant for selector matching?
94 static bool IsSignificantChild(nsIContent
* aChild
,
95 bool aWhitespaceIsSignificant
);
98 * Thread-safe version of IsSignificantChild()
100 static bool ThreadSafeIsSignificantChild(const nsIContent
* aChild
,
101 bool aWhitespaceIsSignificant
);
103 * Returns true if our object-fit & object-position properties might cause
104 * a replaced element's contents to overflow its content-box (requiring
105 * clipping), or false if we can be sure that this won't happen.
107 * This lets us optimize by skipping clipping when we can tell it's
108 * unnecessary (particularly with the default values of these properties).
110 * @param aStylePos The nsStylePosition whose object-fit & object-position
111 * properties should be checked for potential overflow.
112 * @return false if we can be sure that the object-fit & object-position
113 * properties on 'aStylePos' cannot cause a replaced element's
114 * contents to overflow its content-box. Otherwise (if overflow is
115 * is possible), returns true.
117 static bool ObjectPropsMightCauseOverflow(const nsStylePosition
* aStylePos
);
120 * Does the document have a CSP that blocks the application of
121 * inline styles? Returns false if application of the style should
125 * The <style> element that the caller wants to know whether to honor.
126 * Included to check the nonce attribute if one is provided. Allowed to
127 * be null, if this is for something other than a <style> element (in
128 * which case nonces won't be checked).
130 * The document containing the inline style (for querying the CSP);
131 * @param aTriggeringPrincipal
132 * The principal of the scripted caller which added the inline
133 * stylesheet, or null if no scripted caller can be identified.
135 * Line number of inline style element in the containing document (for
136 * reporting violations)
137 * @param aColumnNumber
138 * Column number of inline style element in the containing document (for
139 * reporting violations)
141 * Contents of the inline style element (for reporting violations)
143 * Return error code in case of failure
145 * Does CSP allow application of the specified inline style?
147 static bool CSPAllowsInlineStyle(mozilla::dom::Element
* aContent
,
148 mozilla::dom::Document
* aDocument
,
149 nsIPrincipal
* aTriggeringPrincipal
,
150 uint32_t aLineNumber
, uint32_t aColumnNumber
,
151 const nsAString
& aStyleText
, nsresult
* aRv
);
154 static bool MatchesLanguagePrefix(const char16_t
* aLang
, size_t aLen
,
155 const char16_t (&aPrefix
)[N
]) {
156 return !NS_strncmp(aLang
, aPrefix
, N
- 1) &&
157 (aLen
== N
- 1 || aLang
[N
- 1] == '-');
161 static bool MatchesLanguagePrefix(const nsAtom
* aLang
,
162 const char16_t (&aPrefix
)[N
]) {
164 return MatchesLanguagePrefix(aLang
->GetUTF16String(), aLang
->GetLength(),
169 static bool MatchesLanguagePrefix(const nsAString
& aLang
,
170 const char16_t (&aPrefix
)[N
]) {
171 return MatchesLanguagePrefix(aLang
.Data(), aLang
.Length(), aPrefix
);
175 #endif /* nsStyleUtil_h___ */