Bug 1698786: part 2) Change some compile-time dependent `printf`s to `MOZ_LOG` in...
[gecko.git] / layout / style / nsStyleUtil.h
blob20d5deb61ba5061a4958f6a49b46940fe46f621a
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___
9 #include "nsCoord.h"
10 #include "nsCSSPropertyID.h"
11 #include "nsTArrayForwardDeclare.h"
12 #include "nsStringFwd.h"
13 #include "nsCRT.h"
14 #include "nsColor.h"
15 #include "nsGkAtoms.h"
17 class nsCSSValue;
18 class nsIContent;
19 class nsIPrincipal;
20 class nsIURI;
21 struct gfxFontFeature;
22 struct nsCSSKTableEntry;
23 struct nsCSSValueList;
24 struct nsStylePosition;
26 namespace mozilla {
27 class FontSlantStyle;
28 namespace dom {
29 class Document;
30 class Element;
31 } // namespace dom
32 } // namespace mozilla
34 // Style utility functions
35 class nsStyleUtil {
36 public:
37 static bool DashMatchCompare(const nsAString& aAttributeValue,
38 const nsAString& aSelectorValue,
39 const nsStringComparator& aComparator);
41 static bool ValueIncludes(const nsAString& aValueList,
42 const nsAString& aValue,
43 const nsStringComparator& aComparator);
45 // Append a quoted (with 'quoteChar') and escaped version of aString
46 // to aResult. 'quoteChar' must be ' or ".
47 static void AppendEscapedCSSString(const nsAString& aString,
48 nsAString& aResult,
49 char16_t quoteChar = '"');
51 // Append the identifier given by |aIdent| to |aResult|, with
52 // appropriate escaping so that it can be reparsed to the same
53 // identifier. An exception is if aIdent contains U+0000, which
54 // will be escaped as U+FFFD and then reparsed back to U+FFFD.
55 static void AppendEscapedCSSIdent(const nsAString& aIdent,
56 nsAString& aResult);
58 static void AppendFontSlantStyle(const mozilla::FontSlantStyle&,
59 nsAString& aResult);
61 public:
62 static void AppendCSSNumber(float aNumber, nsAString& aResult) {
63 aResult.AppendFloat(aNumber);
67 * Convert an author-provided floating point number to an integer (0
68 * ... 255) appropriate for use in the alpha component of a color.
70 static uint8_t FloatToColorComponent(float aAlpha) {
71 NS_ASSERTION(0.0 <= aAlpha && aAlpha <= 1.0, "out of range");
72 return static_cast<uint8_t>(NSToIntRound(aAlpha * 255));
76 * Convert the alpha component of an nscolor (0 ... 255) to the
77 * floating point number with the least accurate *decimal*
78 * representation that is converted to that color.
80 * Should be used only by serialization code.
82 static float ColorComponentToFloat(uint8_t aAlpha);
84 /**
85 * GetSerializedColorValue() computes serialized color value of aColor and
86 * returns it with aSerializedColor.
87 * https://drafts.csswg.org/cssom/#serialize-a-css-component-value
89 static void GetSerializedColorValue(nscolor aColor,
90 nsAString& aSerializedColor);
93 * Does this child count as significant for selector matching?
95 static bool IsSignificantChild(nsIContent* aChild,
96 bool aWhitespaceIsSignificant);
99 * Thread-safe version of IsSignificantChild()
101 static bool ThreadSafeIsSignificantChild(const nsIContent* aChild,
102 bool aWhitespaceIsSignificant);
104 * Returns true if our object-fit & object-position properties might cause
105 * a replaced element's contents to overflow its content-box (requiring
106 * clipping), or false if we can be sure that this won't happen.
108 * This lets us optimize by skipping clipping when we can tell it's
109 * unnecessary (particularly with the default values of these properties).
111 * @param aStylePos The nsStylePosition whose object-fit & object-position
112 * properties should be checked for potential overflow.
113 * @return false if we can be sure that the object-fit & object-position
114 * properties on 'aStylePos' cannot cause a replaced element's
115 * contents to overflow its content-box. Otherwise (if overflow is
116 * is possible), returns true.
118 static bool ObjectPropsMightCauseOverflow(const nsStylePosition* aStylePos);
121 * Does the document have a CSP that blocks the application of
122 * inline styles? Returns false if application of the style should
123 * be blocked.
125 * @param aContent
126 * The <style> element that the caller wants to know whether to honor.
127 * Included to check the nonce attribute if one is provided. Allowed to
128 * be null, if this is for something other than a <style> element (in
129 * which case nonces won't be checked).
130 * @param aDocument
131 * The document containing the inline style (for querying the CSP);
132 * @param aTriggeringPrincipal
133 * The principal of the scripted caller which added the inline
134 * stylesheet, or null if no scripted caller can be identified.
135 * @param aLineNumber
136 * Line number of inline style element in the containing document (for
137 * reporting violations)
138 * @param aColumnNumber
139 * Column number of inline style element in the containing document (for
140 * reporting violations)
141 * @param aStyleText
142 * Contents of the inline style element (for reporting violations)
143 * @param aRv
144 * Return error code in case of failure
145 * @return
146 * Does CSP allow application of the specified inline style?
148 static bool CSPAllowsInlineStyle(mozilla::dom::Element* aContent,
149 mozilla::dom::Document* aDocument,
150 nsIPrincipal* aTriggeringPrincipal,
151 uint32_t aLineNumber, uint32_t aColumnNumber,
152 const nsAString& aStyleText, nsresult* aRv);
154 template <size_t N>
155 static bool MatchesLanguagePrefix(const char16_t* aLang, size_t aLen,
156 const char16_t (&aPrefix)[N]) {
157 return !NS_strncmp(aLang, aPrefix, N - 1) &&
158 (aLen == N - 1 || aLang[N - 1] == '-');
161 template <size_t N>
162 static bool MatchesLanguagePrefix(const nsAtom* aLang,
163 const char16_t (&aPrefix)[N]) {
164 MOZ_ASSERT(aLang);
165 return MatchesLanguagePrefix(aLang->GetUTF16String(), aLang->GetLength(),
166 aPrefix);
169 template <size_t N>
170 static bool MatchesLanguagePrefix(const nsAString& aLang,
171 const char16_t (&aPrefix)[N]) {
172 return MatchesLanguagePrefix(aLang.Data(), aLang.Length(), aPrefix);
176 #endif /* nsStyleUtil_h___ */