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 #ifndef nsCSSProps_h___
13 #define nsCSSProps_h___
18 #include "nsCSSPropertyID.h"
19 #include "nsStyleStructFwd.h"
20 #include "mozilla/UseCounter.h"
21 #include "mozilla/CSSEnabledState.h"
22 #include "mozilla/CSSPropFlags.h"
23 #include "mozilla/Preferences.h"
25 // Length of the "--" prefix on custom names (such as custom property names,
26 // and, in the future, custom media query names).
27 #define CSS_CUSTOM_NAME_PREFIX_LENGTH 2
34 } // namespace mozilla
37 nsCSSPropertyID
Servo_ResolveLogicalProperty(nsCSSPropertyID
,
38 const mozilla::ComputedStyle
*);
39 nsCSSPropertyID
Servo_Property_LookupEnabledForAllContent(const nsACString
*);
40 const uint8_t* Servo_Property_GetName(nsCSSPropertyID
, uint32_t* aLength
);
45 using EnabledState
= mozilla::CSSEnabledState
;
46 using Flags
= mozilla::CSSPropFlags
;
50 // Looks up the property with name aProperty and returns its corresponding
51 // nsCSSPropertyID value. If aProperty is the name of a custom property,
52 // then eCSSPropertyExtra_variable will be returned.
54 // This only returns properties enabled for all content, and resolves aliases
55 // to return the aliased property.
56 static nsCSSPropertyID
LookupProperty(const nsACString
& aProperty
) {
57 return Servo_Property_LookupEnabledForAllContent(&aProperty
);
60 // As above, but looked up using a property's IDL name.
61 // eCSSPropertyExtra_variable won't be returned from this method.
62 static nsCSSPropertyID
LookupPropertyByIDLName(
63 const nsACString
& aPropertyIDLName
, EnabledState aEnabled
);
65 // Returns whether aProperty is a custom property name, i.e. begins with
66 // "--". This assumes that the CSS Variables pref has been enabled.
67 static bool IsCustomPropertyName(const nsACString
& aProperty
);
69 static bool IsShorthand(nsCSSPropertyID aProperty
) {
70 if (aProperty
== eCSSPropertyExtra_variable
) {
73 MOZ_ASSERT(0 <= aProperty
&& aProperty
< eCSSProperty_COUNT
,
75 return aProperty
>= eCSSProperty_COUNT_no_shorthands
;
78 // Same but for @font-face descriptors
79 static nsCSSFontDesc
LookupFontDesc(const nsACString
&);
81 // The relevant invariants are asserted in Document.cpp
82 static mozilla::UseCounter
UseCounterFor(nsCSSPropertyID aProperty
) {
83 MOZ_ASSERT(0 <= aProperty
&& aProperty
< eCSSProperty_COUNT_with_aliases
,
85 return mozilla::UseCounter(size_t(mozilla::eUseCounter_FirstCSSProperty
) +
89 // Given a property enum, get the string value
91 // This string is static.
92 static nsDependentCSubstring
GetStringValue(nsCSSPropertyID aProperty
) {
94 const uint8_t* chars
= Servo_Property_GetName(aProperty
, &len
);
95 return nsDependentCSubstring(reinterpret_cast<const char*>(chars
), len
);
98 static const nsCString
& GetStringValue(nsCSSFontDesc aFontDesc
);
99 static const nsCString
& GetStringValue(nsCSSCounterDesc aCounterDesc
);
101 static Flags
PropFlags(nsCSSPropertyID
);
102 static bool PropHasFlags(nsCSSPropertyID aProperty
, Flags aFlags
) {
103 return (PropFlags(aProperty
) & aFlags
) == aFlags
;
106 static nsCSSPropertyID
Physicalize(nsCSSPropertyID aProperty
,
107 const mozilla::ComputedStyle
& aStyle
) {
108 MOZ_ASSERT(!IsShorthand(aProperty
));
109 if (PropHasFlags(aProperty
, Flags::IsLogical
)) {
110 return Servo_ResolveLogicalProperty(aProperty
, &aStyle
);
116 // A table for shorthand properties. The appropriate index is the
117 // property ID minus eCSSProperty_COUNT_no_shorthands.
118 static const nsCSSPropertyID
* const
119 kSubpropertyTable
[eCSSProperty_COUNT
- eCSSProperty_COUNT_no_shorthands
];
123 * Returns true if the backdrop-filter pref and the gfx blocklist are enabled.
125 static bool IsBackdropFilterAvailable(JSContext
*, JSObject
*) {
126 return IsEnabled(eCSSProperty_backdrop_filter
, EnabledState::ForAllContent
);
130 * Recoumputes the enabled state of a pref. If aPrefName is nullptr,
131 * recomputes the state of all prefs in gPropertyEnabled.
132 * aClosure is the pref callback closure data, which is not used.
134 static void RecomputeEnabledState(const char* aPrefName
,
135 void* aClosure
= nullptr);
138 * Retrieve a singleton receiver to register with gfxVars
140 static mozilla::gfx::gfxVarReceiver
& GfxVarReceiver();
142 static const nsCSSPropertyID
* SubpropertyEntryFor(nsCSSPropertyID aProperty
) {
143 MOZ_ASSERT(eCSSProperty_COUNT_no_shorthands
<= aProperty
&&
144 aProperty
< eCSSProperty_COUNT
,
146 return kSubpropertyTable
[aProperty
- eCSSProperty_COUNT_no_shorthands
];
150 static bool gPropertyEnabled
[eCSSProperty_COUNT_with_aliases
];
151 // Defined in the generated nsCSSPropsGenerated.inc.
152 static const char* const kIDLNameTable
[eCSSProperty_COUNT
];
153 static const int32_t kIDLNameSortPositionTable
[eCSSProperty_COUNT
];
157 * Returns the IDL name of the specified property, which must be a
158 * longhand, logical or shorthand property. The IDL name is the property
159 * name with any hyphen-lowercase character pairs replaced by an
160 * uppercase character:
161 * https://drafts.csswg.org/cssom/#css-property-to-idl-attribute
163 * As a special case, the string "cssFloat" is returned for the float
164 * property. nullptr is returned for internal properties.
166 static const char* PropertyIDLName(nsCSSPropertyID aProperty
) {
167 MOZ_ASSERT(0 <= aProperty
&& aProperty
< eCSSProperty_COUNT
,
169 return kIDLNameTable
[aProperty
];
173 * Returns the position of the specified property in a list of all
174 * properties sorted by their IDL name.
176 static int32_t PropertyIDLNameSortPosition(nsCSSPropertyID aProperty
) {
177 MOZ_ASSERT(0 <= aProperty
&& aProperty
< eCSSProperty_COUNT
,
179 return kIDLNameSortPositionTable
[aProperty
];
182 static bool IsEnabled(nsCSSPropertyID aProperty
, EnabledState aEnabled
) {
183 MOZ_ASSERT(0 <= aProperty
&& aProperty
< eCSSProperty_COUNT_with_aliases
,
185 // In the child process, assert that we're not trying to parse stylesheets
186 // before we've gotten all our prefs.
187 MOZ_ASSERT_IF(!XRE_IsParentProcess(),
188 mozilla::Preferences::ArePrefsInitedInContentProcess());
189 if (gPropertyEnabled
[aProperty
]) {
192 if (aEnabled
== EnabledState::IgnoreEnabledState
) {
195 if ((aEnabled
& EnabledState::InUASheets
) &&
196 PropHasFlags(aProperty
, Flags::EnabledInUASheets
)) {
199 if ((aEnabled
& EnabledState::InChrome
) &&
200 PropHasFlags(aProperty
, Flags::EnabledInChrome
)) {
206 struct PropertyPref
{
207 nsCSSPropertyID mPropID
;
210 static const PropertyPref kPropertyPrefTable
[];
212 // Storing the enabledstate_ value in an nsCSSPropertyID variable is a small
213 // hack to avoid needing a separate variable declaration for its real type
214 // (CSSEnabledState), which would then require using a block and
215 // therefore a pair of macros by consumers for the start and end of the loop.
216 #define CSSPROPS_FOR_SHORTHAND_SUBPROPERTIES(it_, prop_, enabledstate_) \
217 for (const nsCSSPropertyID * \
218 it_ = nsCSSProps::SubpropertyEntryFor(prop_), \
219 es_ = (nsCSSPropertyID)((enabledstate_) | CSSEnabledState(0)); \
220 *it_ != eCSSProperty_UNKNOWN; ++it_) \
221 if (nsCSSProps::IsEnabled(*it_, (mozilla::CSSEnabledState)es_))
224 // MOZ_DBG support for nsCSSPropertyID
226 inline std::ostream
& operator<<(std::ostream
& aOut
, nsCSSPropertyID aProperty
) {
227 return aOut
<< nsCSSProps::GetStringValue(aProperty
);
230 #endif /* nsCSSProps_h___ */