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"
34 using namespace mozilla
;
36 static int32_t gPropertyTableRefCount
;
37 static nsStaticCaseInsensitiveNameTable
* gFontDescTable
;
38 static nsStaticCaseInsensitiveNameTable
* gCounterDescTable
;
39 static nsTHashMap
<nsCStringHashKey
, nsCSSPropertyID
>* gPropertyIDLNameTable
;
41 static const char* const kCSSRawFontDescs
[] = {
42 #define CSS_FONT_DESC(name_, method_) #name_,
43 #include "nsCSSFontDescList.h"
47 static const char* const kCSSRawCounterDescs
[] = {
48 #define CSS_COUNTER_DESC(name_, method_) #name_,
49 #include "nsCSSCounterDescList.h"
50 #undef CSS_COUNTER_DESC
53 static nsStaticCaseInsensitiveNameTable
* CreateStaticTable(
54 const char* const aRawTable
[], int32_t aLength
) {
55 auto table
= new nsStaticCaseInsensitiveNameTable(aRawTable
, aLength
);
57 // Partially verify the entries.
58 for (int32_t index
= 0; index
< aLength
; ++index
) {
59 nsAutoCString
temp(aRawTable
[index
]);
60 MOZ_ASSERT(-1 == temp
.FindChar('_'),
61 "underscore char in case insensitive name table");
67 void nsCSSProps::RecomputeEnabledState(const char* aPref
, void*) {
68 MOZ_RELEASE_ASSERT(NS_IsMainThread());
69 DebugOnly
<bool> foundPref
= false;
70 for (const PropertyPref
* pref
= kPropertyPrefTable
;
71 pref
->mPropID
!= eCSSProperty_UNKNOWN
; pref
++) {
72 if (!aPref
|| !strcmp(aPref
, pref
->mPref
)) {
75 gPropertyEnabled
[pref
->mPropID
] = true;
77 gPropertyEnabled
[pref
->mPropID
] = Preferences::GetBool(pref
->mPref
);
79 if (pref
->mPropID
== eCSSProperty_backdrop_filter
) {
80 gPropertyEnabled
[pref
->mPropID
] &=
81 gfx::gfxVars::GetUseWebRenderOrDefault();
85 MOZ_ASSERT(foundPref
);
88 void nsCSSProps::AddRefTable(void) {
89 if (0 == gPropertyTableRefCount
++) {
90 MOZ_ASSERT(!gFontDescTable
, "pre existing array!");
91 MOZ_ASSERT(!gCounterDescTable
, "pre existing array!");
92 MOZ_ASSERT(!gPropertyIDLNameTable
, "pre existing array!");
94 gFontDescTable
= CreateStaticTable(kCSSRawFontDescs
, eCSSFontDesc_COUNT
);
96 CreateStaticTable(kCSSRawCounterDescs
, eCSSCounterDesc_COUNT
);
98 gPropertyIDLNameTable
= new nsTHashMap
<nsCStringHashKey
, nsCSSPropertyID
>;
99 for (nsCSSPropertyID p
= nsCSSPropertyID(0);
100 size_t(p
) < ArrayLength(kIDLNameTable
); p
= nsCSSPropertyID(p
+ 1)) {
101 if (kIDLNameTable
[p
]) {
102 gPropertyIDLNameTable
->InsertOrUpdate(
103 nsDependentCString(kIDLNameTable
[p
]), p
);
107 static bool prefObserversInited
= false;
108 if (!prefObserversInited
) {
109 prefObserversInited
= true;
110 for (const PropertyPref
* pref
= kPropertyPrefTable
;
111 pref
->mPropID
!= eCSSProperty_UNKNOWN
; pref
++) {
112 // https://bugzilla.mozilla.org/show_bug.cgi?id=1472523
113 // We need to use nsCString instead of substring because the preference
114 // callback code stores them. Using AssignLiteral prevents any
115 // unnecessary allocations.
117 prefName
.AssignLiteral(pref
->mPref
, strlen(pref
->mPref
));
118 Preferences::RegisterCallback(nsCSSProps::RecomputeEnabledState
,
121 RecomputeEnabledState(/* aPrefName = */ nullptr);
126 void nsCSSProps::ReleaseTable(void) {
127 if (0 == --gPropertyTableRefCount
) {
128 delete gFontDescTable
;
129 gFontDescTable
= nullptr;
131 delete gCounterDescTable
;
132 gCounterDescTable
= nullptr;
134 delete gPropertyIDLNameTable
;
135 gPropertyIDLNameTable
= nullptr;
140 bool nsCSSProps::IsCustomPropertyName(const nsACString
& aProperty
) {
141 return aProperty
.Length() >= CSS_CUSTOM_NAME_PREFIX_LENGTH
&&
142 StringBeginsWith(aProperty
, "--"_ns
);
145 nsCSSPropertyID
nsCSSProps::LookupPropertyByIDLName(
146 const nsACString
& aPropertyIDLName
, EnabledState aEnabled
) {
147 MOZ_ASSERT(gPropertyIDLNameTable
, "no lookup table, needs addref");
149 if (!gPropertyIDLNameTable
->Get(aPropertyIDLName
, &res
)) {
150 return eCSSProperty_UNKNOWN
;
152 MOZ_ASSERT(res
< eCSSProperty_COUNT
);
153 if (!IsEnabled(res
, aEnabled
)) {
154 return eCSSProperty_UNKNOWN
;
159 nsCSSFontDesc
nsCSSProps::LookupFontDesc(const nsACString
& aFontDesc
) {
160 MOZ_ASSERT(gFontDescTable
, "no lookup table, needs addref");
161 nsCSSFontDesc which
= nsCSSFontDesc(gFontDescTable
->Lookup(aFontDesc
));
163 if (which
== eCSSFontDesc_Display
&&
164 !StaticPrefs::layout_css_font_display_enabled()) {
165 which
= eCSSFontDesc_UNKNOWN
;
170 const nsCString
& nsCSSProps::GetStringValue(nsCSSFontDesc aFontDescID
) {
171 MOZ_ASSERT(gFontDescTable
, "no lookup table, needs addref");
172 if (gFontDescTable
) {
173 return gFontDescTable
->GetStringValue(int32_t(aFontDescID
));
175 static nsDependentCString
sNullStr("");
180 const nsCString
& nsCSSProps::GetStringValue(nsCSSCounterDesc aCounterDesc
) {
181 MOZ_ASSERT(gCounterDescTable
, "no lookup table, needs addref");
182 if (gCounterDescTable
) {
183 return gCounterDescTable
->GetStringValue(int32_t(aCounterDesc
));
185 static nsDependentCString
sNullStr("");
189 const CSSPropFlags
nsCSSProps::kFlagsTable
[eCSSProperty_COUNT
] = {
190 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, ...) flags_,
191 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, ...) flags_,
192 #include "mozilla/ServoCSSPropList.h"
193 #undef CSS_PROP_SHORTHAND
194 #undef CSS_PROP_LONGHAND
198 bool nsCSSProps::gPropertyEnabled
[eCSSProperty_COUNT_with_aliases
] = {
199 // If the property has any "ENABLED_IN" flag set, it is disabled by
200 // default. Note that, if a property has pref, whatever its default
201 // value is, it will later be changed in nsCSSProps::AddRefTable().
202 // If the property has "ENABLED_IN" flags but doesn't have a pref,
203 // it is an internal property which is disabled elsewhere.
204 #define IS_ENABLED_BY_DEFAULT(flags_) \
205 (!((flags_) & (CSSPropFlags::EnabledMask | CSSPropFlags::Inaccessible)))
207 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, ...) \
208 IS_ENABLED_BY_DEFAULT(flags_),
209 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, ...) \
210 IS_ENABLED_BY_DEFAULT(flags_),
211 #define CSS_PROP_ALIAS(...) true,
212 #include "mozilla/ServoCSSPropList.h"
213 #undef CSS_PROP_ALIAS
214 #undef CSS_PROP_SHORTHAND
215 #undef CSS_PROP_LONGHAND
217 #undef IS_ENABLED_BY_DEFAULT
221 * A singleton class to register as a receiver for gfxVars.
222 * Updates the state of backdrop-filter's pref if the gfx
223 * WebRender var changes state.
225 class nsCSSPropsGfxVarReceiver final
: public gfx::gfxVarReceiver
{
226 constexpr nsCSSPropsGfxVarReceiver() = default;
228 // WebRender's last known enabled state.
229 static bool sLastKnownUseWebRender
;
230 static nsCSSPropsGfxVarReceiver sInstance
;
233 static gfx::gfxVarReceiver
& GetInstance() { return sInstance
; }
235 void OnVarChanged(const gfx::GfxVarUpdate
&) override
{
236 bool enabled
= gfxVars::UseWebRender();
237 if (sLastKnownUseWebRender
!= enabled
) {
238 sLastKnownUseWebRender
= enabled
;
239 nsCSSProps::RecomputeEnabledState("layout.css.backdrop-filter.enabled");
245 nsCSSPropsGfxVarReceiver
nsCSSPropsGfxVarReceiver::sInstance
=
246 nsCSSPropsGfxVarReceiver();
249 bool nsCSSPropsGfxVarReceiver::sLastKnownUseWebRender
= false;
252 gfx::gfxVarReceiver
& nsCSSProps::GfxVarReceiver() {
253 return nsCSSPropsGfxVarReceiver::GetInstance();
256 #include "nsCSSPropsGenerated.inc"