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/. */
8 * methods for dealing with CSS properties and tables of the keyword
12 #include "nsCSSProps.h"
14 #include "mozilla/ArrayUtils.h"
15 #include "mozilla/Casting.h"
17 #include "gfxPlatform.h"
18 #include "nsLayoutUtils.h"
19 #include "nsIWidget.h"
20 #include "nsStyleConsts.h" // For system widget appearance types
22 #include "mozilla/dom/Animation.h"
23 #include "mozilla/dom/AnimationEffectBinding.h" // for PlaybackDirection
24 #include "mozilla/gfx/gfxVars.h" // for UseWebRender
25 #include "mozilla/gfx/gfxVarReceiver.h"
26 #include "mozilla/LookAndFeel.h" // for system colors
29 #include "nsStaticNameTable.h"
31 #include "mozilla/Preferences.h"
32 #include "mozilla/StaticPrefs_layout.h"
33 #include "mozilla/StaticPtr.h"
35 using namespace mozilla
;
37 static int32_t gPropertyTableRefCount
;
38 static StaticAutoPtr
<nsStaticCaseInsensitiveNameTable
> gFontDescTable
;
39 static StaticAutoPtr
<nsStaticCaseInsensitiveNameTable
> gCounterDescTable
;
40 static StaticAutoPtr
<nsTHashMap
<nsCStringHashKey
, nsCSSPropertyID
>>
41 gPropertyIDLNameTable
;
43 static constexpr const char* const kCSSRawFontDescs
[] = {
44 #define CSS_FONT_DESC(name_, method_) #name_,
45 #include "nsCSSFontDescList.h"
49 static constexpr const char* const kCSSRawCounterDescs
[] = {
50 #define CSS_COUNTER_DESC(name_, method_) #name_,
51 #include "nsCSSCounterDescList.h"
52 #undef CSS_COUNTER_DESC
55 static constexpr CSSPropFlags kFlagsTable
[eCSSProperty_COUNT_with_aliases
] = {
56 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, ...) flags_,
57 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, ...) flags_,
58 #define CSS_PROP_ALIAS(name_, aliasid_, id_, method_, flags_, ...) flags_,
59 #include "mozilla/ServoCSSPropList.h"
61 #undef CSS_PROP_SHORTHAND
62 #undef CSS_PROP_LONGHAND
65 static nsStaticCaseInsensitiveNameTable
* CreateStaticTable(
66 const char* const aRawTable
[], int32_t aLength
) {
67 auto table
= new nsStaticCaseInsensitiveNameTable(aRawTable
, aLength
);
69 // Partially verify the entries.
70 for (int32_t index
= 0; index
< aLength
; ++index
) {
71 nsAutoCString
temp(aRawTable
[index
]);
72 MOZ_ASSERT(-1 == temp
.FindChar('_'),
73 "underscore char in case insensitive name table");
79 void nsCSSProps::RecomputeEnabledState(const char* aPref
, void*) {
80 MOZ_RELEASE_ASSERT(NS_IsMainThread());
81 DebugOnly
<bool> foundPref
= false;
82 for (const PropertyPref
* pref
= kPropertyPrefTable
;
83 pref
->mPropID
!= eCSSProperty_UNKNOWN
; pref
++) {
84 if (!aPref
|| !strcmp(aPref
, pref
->mPref
)) {
87 gPropertyEnabled
[pref
->mPropID
] = true;
89 gPropertyEnabled
[pref
->mPropID
] = Preferences::GetBool(pref
->mPref
);
90 if (pref
->mPropID
== eCSSProperty_backdrop_filter
) {
91 gPropertyEnabled
[pref
->mPropID
] &=
92 gfx::gfxVars::GetAllowBackdropFilterOrDefault();
97 MOZ_ASSERT(foundPref
);
100 void nsCSSProps::AddRefTable(void) {
101 if (0 == gPropertyTableRefCount
++) {
102 MOZ_ASSERT(!gFontDescTable
, "pre existing array!");
103 MOZ_ASSERT(!gCounterDescTable
, "pre existing array!");
104 MOZ_ASSERT(!gPropertyIDLNameTable
, "pre existing array!");
106 gFontDescTable
= CreateStaticTable(kCSSRawFontDescs
, eCSSFontDesc_COUNT
);
108 CreateStaticTable(kCSSRawCounterDescs
, eCSSCounterDesc_COUNT
);
110 gPropertyIDLNameTable
= new nsTHashMap
<nsCStringHashKey
, nsCSSPropertyID
>;
111 for (nsCSSPropertyID p
= nsCSSPropertyID(0);
112 size_t(p
) < ArrayLength(kIDLNameTable
); p
= nsCSSPropertyID(p
+ 1)) {
113 if (kIDLNameTable
[p
]) {
114 gPropertyIDLNameTable
->InsertOrUpdate(
115 nsDependentCString(kIDLNameTable
[p
]), p
);
119 static bool prefObserversInited
= false;
120 if (!prefObserversInited
) {
121 prefObserversInited
= true;
122 for (const PropertyPref
* pref
= kPropertyPrefTable
;
123 pref
->mPropID
!= eCSSProperty_UNKNOWN
; pref
++) {
124 // https://bugzilla.mozilla.org/show_bug.cgi?id=1472523
125 // We need to use nsCString instead of substring because the preference
126 // callback code stores them. Using AssignLiteral prevents any
127 // unnecessary allocations.
129 prefName
.AssignLiteral(pref
->mPref
, strlen(pref
->mPref
));
130 Preferences::RegisterCallback(nsCSSProps::RecomputeEnabledState
,
133 RecomputeEnabledState(/* aPrefName = */ nullptr);
138 void nsCSSProps::ReleaseTable(void) {
139 if (0 == --gPropertyTableRefCount
) {
140 gFontDescTable
= nullptr;
141 gCounterDescTable
= nullptr;
142 gPropertyIDLNameTable
= nullptr;
147 bool nsCSSProps::IsCustomPropertyName(const nsACString
& aProperty
) {
148 return aProperty
.Length() >= CSS_CUSTOM_NAME_PREFIX_LENGTH
&&
149 StringBeginsWith(aProperty
, "--"_ns
);
152 nsCSSPropertyID
nsCSSProps::LookupPropertyByIDLName(
153 const nsACString
& aPropertyIDLName
, EnabledState aEnabled
) {
154 MOZ_ASSERT(gPropertyIDLNameTable
, "no lookup table, needs addref");
156 if (!gPropertyIDLNameTable
->Get(aPropertyIDLName
, &res
)) {
157 return eCSSProperty_UNKNOWN
;
159 MOZ_ASSERT(res
< eCSSProperty_COUNT
);
160 if (!IsEnabled(res
, aEnabled
)) {
161 return eCSSProperty_UNKNOWN
;
166 nsCSSFontDesc
nsCSSProps::LookupFontDesc(const nsACString
& aFontDesc
) {
167 MOZ_ASSERT(gFontDescTable
, "no lookup table, needs addref");
168 nsCSSFontDesc which
= nsCSSFontDesc(gFontDescTable
->Lookup(aFontDesc
));
170 if (which
== eCSSFontDesc_Display
&&
171 !StaticPrefs::layout_css_font_display_enabled()) {
172 which
= eCSSFontDesc_UNKNOWN
;
177 static constexpr auto sDescNullStr
= ""_ns
;
179 const nsCString
& nsCSSProps::GetStringValue(nsCSSFontDesc aFontDescID
) {
180 MOZ_ASSERT(gFontDescTable
, "no lookup table, needs addref");
181 if (gFontDescTable
) {
182 return gFontDescTable
->GetStringValue(int32_t(aFontDescID
));
187 const nsCString
& nsCSSProps::GetStringValue(nsCSSCounterDesc aCounterDescID
) {
188 MOZ_ASSERT(gCounterDescTable
, "no lookup table, needs addref");
189 if (gCounterDescTable
) {
190 return gCounterDescTable
->GetStringValue(int32_t(aCounterDescID
));
195 bool nsCSSProps::PropHasFlags(nsCSSPropertyID aProperty
, Flags aFlags
) {
196 MOZ_ASSERT(0 <= aProperty
&& aProperty
< eCSSProperty_COUNT_with_aliases
,
198 return (kFlagsTable
[aProperty
] & aFlags
) == aFlags
;
202 bool nsCSSProps::gPropertyEnabled
[eCSSProperty_COUNT_with_aliases
] = {
203 // If the property has any "ENABLED_IN" flag set, it is disabled by
204 // default. Note that, if a property has pref, whatever its default
205 // value is, it will later be changed in nsCSSProps::AddRefTable().
206 // If the property has "ENABLED_IN" flags but doesn't have a pref,
207 // it is an internal property which is disabled elsewhere.
208 #define IS_ENABLED_BY_DEFAULT(flags_) \
209 (!((flags_) & (CSSPropFlags::EnabledMask | CSSPropFlags::Inaccessible)))
211 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, ...) \
212 IS_ENABLED_BY_DEFAULT(flags_),
213 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, ...) \
214 IS_ENABLED_BY_DEFAULT(flags_),
215 #define CSS_PROP_ALIAS(name_, aliasid_, id_, method_, flags_, ...) \
216 IS_ENABLED_BY_DEFAULT(flags_),
217 #include "mozilla/ServoCSSPropList.h"
218 #undef CSS_PROP_ALIAS
219 #undef CSS_PROP_SHORTHAND
220 #undef CSS_PROP_LONGHAND
222 #undef IS_ENABLED_BY_DEFAULT
226 * A singleton class to register as a receiver for gfxVars.
227 * Updates the state of backdrop-filter's pref if the gfx
228 * backdrop filter var changes state.
230 class nsCSSPropsGfxVarReceiver final
: public gfx::gfxVarReceiver
{
231 constexpr nsCSSPropsGfxVarReceiver() = default;
233 // Backdrop filter's last known enabled state.
234 static bool sLastKnownAllowBackdropFilter
;
235 static nsCSSPropsGfxVarReceiver sInstance
;
238 static gfx::gfxVarReceiver
& GetInstance() { return sInstance
; }
240 void OnVarChanged(const gfx::GfxVarUpdate
&) override
{
241 bool enabled
= gfx::gfxVars::AllowBackdropFilter();
242 if (sLastKnownAllowBackdropFilter
!= enabled
) {
243 sLastKnownAllowBackdropFilter
= enabled
;
244 nsCSSProps::RecomputeEnabledState(
245 StaticPrefs::GetPrefName_layout_css_backdrop_filter_enabled());
251 nsCSSPropsGfxVarReceiver
nsCSSPropsGfxVarReceiver::sInstance
=
252 nsCSSPropsGfxVarReceiver();
255 bool nsCSSPropsGfxVarReceiver::sLastKnownAllowBackdropFilter
= true;
258 gfx::gfxVarReceiver
& nsCSSProps::GfxVarReceiver() {
259 return nsCSSPropsGfxVarReceiver::GetInstance();
262 #include "nsCSSPropsGenerated.inc"