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"
34 #include "mozilla/ClearOnShutdown.h"
36 using namespace mozilla
;
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::Init() {
101 MOZ_ASSERT(!gFontDescTable
, "pre existing array!");
102 MOZ_ASSERT(!gCounterDescTable
, "pre existing array!");
103 MOZ_ASSERT(!gPropertyIDLNameTable
, "pre existing array!");
105 gFontDescTable
= CreateStaticTable(kCSSRawFontDescs
, eCSSFontDesc_COUNT
);
107 CreateStaticTable(kCSSRawCounterDescs
, eCSSCounterDesc_COUNT
);
109 gPropertyIDLNameTable
= new nsTHashMap
<nsCStringHashKey
, nsCSSPropertyID
>;
110 for (nsCSSPropertyID p
= nsCSSPropertyID(0);
111 size_t(p
) < ArrayLength(kIDLNameTable
); p
= nsCSSPropertyID(p
+ 1)) {
112 if (kIDLNameTable
[p
]) {
113 gPropertyIDLNameTable
->InsertOrUpdate(
114 nsDependentCString(kIDLNameTable
[p
]), p
);
118 ClearOnShutdown(&gFontDescTable
);
119 ClearOnShutdown(&gCounterDescTable
);
120 ClearOnShutdown(&gPropertyIDLNameTable
);
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
, prefName
);
132 RecomputeEnabledState(/* aPrefName = */ nullptr);
136 bool nsCSSProps::IsCustomPropertyName(const nsACString
& aProperty
) {
137 return aProperty
.Length() >= CSS_CUSTOM_NAME_PREFIX_LENGTH
&&
138 StringBeginsWith(aProperty
, "--"_ns
);
141 nsCSSPropertyID
nsCSSProps::LookupPropertyByIDLName(
142 const nsACString
& aPropertyIDLName
, EnabledState aEnabled
) {
143 MOZ_ASSERT(gPropertyIDLNameTable
, "no lookup table, needs addref");
145 if (!gPropertyIDLNameTable
->Get(aPropertyIDLName
, &res
)) {
146 return eCSSProperty_UNKNOWN
;
148 MOZ_ASSERT(res
< eCSSProperty_COUNT
);
149 if (!IsEnabled(res
, aEnabled
)) {
150 return eCSSProperty_UNKNOWN
;
155 nsCSSFontDesc
nsCSSProps::LookupFontDesc(const nsACString
& aFontDesc
) {
156 MOZ_ASSERT(gFontDescTable
, "no lookup table, needs addref");
157 nsCSSFontDesc which
= nsCSSFontDesc(gFontDescTable
->Lookup(aFontDesc
));
159 if (which
== eCSSFontDesc_Display
&&
160 !StaticPrefs::layout_css_font_display_enabled()) {
161 which
= eCSSFontDesc_UNKNOWN
;
166 static constexpr auto sDescNullStr
= ""_ns
;
168 const nsCString
& nsCSSProps::GetStringValue(nsCSSFontDesc aFontDescID
) {
169 MOZ_ASSERT(gFontDescTable
, "no lookup table, needs addref");
170 if (gFontDescTable
) {
171 return gFontDescTable
->GetStringValue(int32_t(aFontDescID
));
176 const nsCString
& nsCSSProps::GetStringValue(nsCSSCounterDesc aCounterDescID
) {
177 MOZ_ASSERT(gCounterDescTable
, "no lookup table, needs addref");
178 if (gCounterDescTable
) {
179 return gCounterDescTable
->GetStringValue(int32_t(aCounterDescID
));
184 CSSPropFlags
nsCSSProps::PropFlags(nsCSSPropertyID aProperty
) {
185 MOZ_ASSERT(0 <= aProperty
&& aProperty
< eCSSProperty_COUNT_with_aliases
,
187 return kFlagsTable
[aProperty
];
191 bool nsCSSProps::gPropertyEnabled
[eCSSProperty_COUNT_with_aliases
] = {
192 // If the property has any "ENABLED_IN" flag set, it is disabled by
193 // default. Note that, if a property has pref, whatever its default
194 // value is, it will later be changed in nsCSSProps::AddRefTable().
195 // If the property has "ENABLED_IN" flags but doesn't have a pref,
196 // it is an internal property which is disabled elsewhere.
197 #define IS_ENABLED_BY_DEFAULT(flags_) \
198 (!((flags_) & (CSSPropFlags::EnabledMask | CSSPropFlags::Inaccessible)))
200 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, ...) \
201 IS_ENABLED_BY_DEFAULT(flags_),
202 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, ...) \
203 IS_ENABLED_BY_DEFAULT(flags_),
204 #define CSS_PROP_ALIAS(name_, aliasid_, id_, method_, flags_, ...) \
205 IS_ENABLED_BY_DEFAULT(flags_),
206 #include "mozilla/ServoCSSPropList.h"
207 #undef CSS_PROP_ALIAS
208 #undef CSS_PROP_SHORTHAND
209 #undef CSS_PROP_LONGHAND
211 #undef IS_ENABLED_BY_DEFAULT
215 * A singleton class to register as a receiver for gfxVars.
216 * Updates the state of backdrop-filter's pref if the gfx
217 * backdrop filter var changes state.
219 class nsCSSPropsGfxVarReceiver final
: public gfx::gfxVarReceiver
{
220 constexpr nsCSSPropsGfxVarReceiver() = default;
222 // Backdrop filter's last known enabled state.
223 static bool sLastKnownAllowBackdropFilter
;
224 static nsCSSPropsGfxVarReceiver sInstance
;
227 static gfx::gfxVarReceiver
& GetInstance() { return sInstance
; }
229 void OnVarChanged(const gfx::GfxVarUpdate
&) override
{
230 bool enabled
= gfx::gfxVars::AllowBackdropFilter();
231 if (sLastKnownAllowBackdropFilter
!= enabled
) {
232 sLastKnownAllowBackdropFilter
= enabled
;
233 nsCSSProps::RecomputeEnabledState(
234 StaticPrefs::GetPrefName_layout_css_backdrop_filter_enabled());
240 nsCSSPropsGfxVarReceiver
nsCSSPropsGfxVarReceiver::sInstance
=
241 nsCSSPropsGfxVarReceiver();
244 bool nsCSSPropsGfxVarReceiver::sLastKnownAllowBackdropFilter
= true;
247 gfx::gfxVarReceiver
& nsCSSProps::GfxVarReceiver() {
248 return nsCSSPropsGfxVarReceiver::GetInstance();
251 #include "nsCSSPropsGenerated.inc"