Backed out 2 changesets (bug 903746) for causing non-unified build bustages on nsIPri...
[gecko.git] / layout / style / nsCSSProps.cpp
blob5f2f275d6e824c50c2cb602ce732c3c1f4a65186
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/. */
7 /*
8 * methods for dealing with CSS properties and tables of the keyword
9 * values they accept
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
28 #include "nsString.h"
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"
46 #undef CSS_FONT_DESC
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"
60 #undef CSS_PROP_ALIAS
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);
68 #ifdef DEBUG
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");
75 #endif
76 return 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)) {
85 foundPref = true;
86 #ifdef FUZZING
87 gPropertyEnabled[pref->mPropID] = true;
88 #else
89 gPropertyEnabled[pref->mPropID] = Preferences::GetBool(pref->mPref);
90 if (pref->mPropID == eCSSProperty_backdrop_filter) {
91 gPropertyEnabled[pref->mPropID] &=
92 gfx::gfxVars::GetAllowBackdropFilterOrDefault();
94 #endif
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);
106 gCounterDescTable =
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.
128 nsCString prefName;
129 prefName.AssignLiteral(pref->mPref, strlen(pref->mPref));
130 Preferences::RegisterCallback(nsCSSProps::RecomputeEnabledState, prefName);
132 RecomputeEnabledState(/* aPrefName = */ nullptr);
135 /* static */
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");
144 nsCSSPropertyID res;
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;
152 return res;
155 nsCSSFontDesc nsCSSProps::LookupFontDesc(const nsACString& aFontDesc) {
156 MOZ_ASSERT(gFontDescTable, "no lookup table, needs addref");
157 nsCSSFontDesc which = nsCSSFontDesc(gFontDescTable->Lookup(aFontDesc));
159 return which;
162 static constexpr auto sDescNullStr = ""_ns;
164 const nsCString& nsCSSProps::GetStringValue(nsCSSFontDesc aFontDescID) {
165 MOZ_ASSERT(gFontDescTable, "no lookup table, needs addref");
166 if (gFontDescTable) {
167 return gFontDescTable->GetStringValue(int32_t(aFontDescID));
169 return sDescNullStr;
172 const nsCString& nsCSSProps::GetStringValue(nsCSSCounterDesc aCounterDescID) {
173 MOZ_ASSERT(gCounterDescTable, "no lookup table, needs addref");
174 if (gCounterDescTable) {
175 return gCounterDescTable->GetStringValue(int32_t(aCounterDescID));
177 return sDescNullStr;
180 CSSPropFlags nsCSSProps::PropFlags(nsCSSPropertyID aProperty) {
181 MOZ_ASSERT(0 <= aProperty && aProperty < eCSSProperty_COUNT_with_aliases,
182 "out of range");
183 return kFlagsTable[aProperty];
186 /* static */
187 bool nsCSSProps::gPropertyEnabled[eCSSProperty_COUNT_with_aliases] = {
188 // If the property has any "ENABLED_IN" flag set, it is disabled by
189 // default. Note that, if a property has pref, whatever its default
190 // value is, it will later be changed in nsCSSProps::AddRefTable().
191 // If the property has "ENABLED_IN" flags but doesn't have a pref,
192 // it is an internal property which is disabled elsewhere.
193 #define IS_ENABLED_BY_DEFAULT(flags_) \
194 (!((flags_) & (CSSPropFlags::EnabledMask | CSSPropFlags::Inaccessible)))
196 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, ...) \
197 IS_ENABLED_BY_DEFAULT(flags_),
198 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, ...) \
199 IS_ENABLED_BY_DEFAULT(flags_),
200 #define CSS_PROP_ALIAS(name_, aliasid_, id_, method_, flags_, ...) \
201 IS_ENABLED_BY_DEFAULT(flags_),
202 #include "mozilla/ServoCSSPropList.h"
203 #undef CSS_PROP_ALIAS
204 #undef CSS_PROP_SHORTHAND
205 #undef CSS_PROP_LONGHAND
207 #undef IS_ENABLED_BY_DEFAULT
211 * A singleton class to register as a receiver for gfxVars.
212 * Updates the state of backdrop-filter's pref if the gfx
213 * backdrop filter var changes state.
215 class nsCSSPropsGfxVarReceiver final : public gfx::gfxVarReceiver {
216 constexpr nsCSSPropsGfxVarReceiver() = default;
218 // Backdrop filter's last known enabled state.
219 static bool sLastKnownAllowBackdropFilter;
220 static nsCSSPropsGfxVarReceiver sInstance;
222 public:
223 static gfx::gfxVarReceiver& GetInstance() { return sInstance; }
225 void OnVarChanged(const gfx::GfxVarUpdate&) override {
226 bool enabled = gfx::gfxVars::AllowBackdropFilter();
227 if (sLastKnownAllowBackdropFilter != enabled) {
228 sLastKnownAllowBackdropFilter = enabled;
229 nsCSSProps::RecomputeEnabledState(
230 StaticPrefs::GetPrefName_layout_css_backdrop_filter_enabled());
235 /* static */
236 nsCSSPropsGfxVarReceiver nsCSSPropsGfxVarReceiver::sInstance =
237 nsCSSPropsGfxVarReceiver();
239 /* static */
240 bool nsCSSPropsGfxVarReceiver::sLastKnownAllowBackdropFilter = true;
242 /* static */
243 gfx::gfxVarReceiver& nsCSSProps::GfxVarReceiver() {
244 return nsCSSPropsGfxVarReceiver::GetInstance();
247 #include "nsCSSPropsGenerated.inc"