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 /* atom list for CSS pseudo-elements */
9 #ifndef nsCSSPseudoElements_h___
10 #define nsCSSPseudoElements_h___
12 #include "nsGkAtoms.h"
13 #include "mozilla/CSSEnabledState.h"
14 #include "mozilla/Compiler.h"
15 #include "mozilla/PseudoStyleType.h"
16 #include "mozilla/StaticPrefs_dom.h"
18 // Is this pseudo-element a CSS2 pseudo-element that can be specified
19 // with the single colon syntax (in addition to the double-colon syntax,
20 // which can be used for all pseudo-elements)?
22 // Note: We also rely on this for IsEagerlyCascadedInServo.
23 #define CSS_PSEUDO_ELEMENT_IS_CSS2 (1 << 0)
24 // Is this pseudo-element a pseudo-element that can contain other
26 // (Currently pseudo-elements are either leaves of the tree (relative to
27 // real elements) or they contain other elements in a non-tree-like
28 // manner (i.e., like incorrectly-nested start and end tags). It's
29 // possible that in the future there might be container pseudo-elements
30 // that form a properly nested tree structure. If that happens, we
31 // should probably split this flag into two.)
32 #define CSS_PSEUDO_ELEMENT_CONTAINS_ELEMENTS (1 << 1)
33 // Flag to add the ability to take into account style attribute set for the
34 // pseudo element (by default it's ignored).
35 #define CSS_PSEUDO_ELEMENT_SUPPORTS_STYLE_ATTRIBUTE (1 << 2)
36 // Flag that indicate the pseudo-element supports a user action pseudo-class
37 // following it, such as :active or :hover. This would normally correspond
38 // to whether the pseudo-element is tree-like, but we don't support these
39 // pseudo-classes on ::before and ::after generated content yet. See
40 // http://dev.w3.org/csswg/selectors4/#pseudo-elements.
41 #define CSS_PSEUDO_ELEMENT_SUPPORTS_USER_ACTION_STATE (1 << 3)
42 // Should this pseudo-element be enabled only for UA sheets?
43 #define CSS_PSEUDO_ELEMENT_ENABLED_IN_UA_SHEETS (1 << 4)
44 // Should this pseudo-element be enabled only for UA sheets and chrome
46 #define CSS_PSEUDO_ELEMENT_ENABLED_IN_CHROME (1 << 5)
48 #define CSS_PSEUDO_ELEMENT_ENABLED_IN_UA_SHEETS_AND_CHROME \
49 (CSS_PSEUDO_ELEMENT_ENABLED_IN_UA_SHEETS | \
50 CSS_PSEUDO_ELEMENT_ENABLED_IN_CHROME)
52 // Can we use the ChromeOnly document.createElement(..., { pseudo: "::foo" })
53 // API for creating pseudo-implementing native anonymous content in JS with this
55 #define CSS_PSEUDO_ELEMENT_IS_JS_CREATED_NAC (1 << 6)
56 // Does this pseudo-element act like an item for containers (such as flex and
57 // grid containers) and thus needs parent display-based style fixup?
58 #define CSS_PSEUDO_ELEMENT_IS_FLEX_OR_GRID_ITEM (1 << 7)
60 class nsCSSPseudoElements
{
61 typedef mozilla::PseudoStyleType Type
;
62 typedef mozilla::CSSEnabledState EnabledState
;
65 static bool IsEagerlyCascadedInServo(const Type aType
) {
66 return PseudoElementHasFlags(aType
, CSS_PSEUDO_ELEMENT_IS_CSS2
);
71 static void AssertAtoms();
74 // Alias nsCSSPseudoElements::foo() to nsGkAtoms::foo.
75 #define CSS_PSEUDO_ELEMENT(name_, value_, flags_) \
76 static nsCSSPseudoElementStaticAtom* name_() { \
77 return const_cast<nsCSSPseudoElementStaticAtom*>( \
78 static_cast<const nsCSSPseudoElementStaticAtom*>( \
79 nsGkAtoms::PseudoElement_##name_)); \
81 #include "nsCSSPseudoElementList.h"
82 #undef CSS_PSEUDO_ELEMENT
84 // Returns an empty tuple for a syntactically invalid pseudo-element, and
85 // NotPseudo for the empty / null string.
86 // The second element of the tuple (functional pseudo parameter) is currently
87 // only used for `::highlight()` pseudos and is `nullptr` otherwise.
88 static std::tuple
<mozilla::Maybe
<Type
>, RefPtr
<nsAtom
>> ParsePseudoElement(
89 const nsAString
& aPseudoElement
,
90 EnabledState
= EnabledState::ForAllContent
);
92 // Returns Nothing() for a syntactically invalid pseudo-element, and NotPseudo
93 // for the empty / null string.
94 static mozilla::Maybe
<Type
> GetPseudoType(
95 const nsAString
& aPseudoElement
,
96 EnabledState
= EnabledState::ForAllContent
);
98 // Get the atom for a given Type. aType must be <
99 // PseudoType::CSSPseudoElementsEnd.
100 // This only ever returns static atoms, so it's fine to return a raw pointer.
101 static nsAtom
* GetPseudoAtom(Type aType
);
103 // Get the atom for a given pseudo-element string (e.g. "::before"). This can
104 // return dynamic atoms, for unrecognized pseudo-elements.
105 static already_AddRefed
<nsAtom
> GetPseudoAtom(
106 const nsAString
& aPseudoElement
);
108 static bool PseudoElementContainsElements(const Type aType
) {
109 return PseudoElementHasFlags(aType
, CSS_PSEUDO_ELEMENT_CONTAINS_ELEMENTS
);
112 static bool PseudoElementSupportsStyleAttribute(const Type aType
) {
113 MOZ_ASSERT(aType
< Type::CSSPseudoElementsEnd
);
114 return PseudoElementHasFlags(aType
,
115 CSS_PSEUDO_ELEMENT_SUPPORTS_STYLE_ATTRIBUTE
);
118 static bool PseudoElementSupportsUserActionState(const Type aType
);
120 static bool PseudoElementIsJSCreatedNAC(Type aType
) {
121 return PseudoElementHasFlags(aType
, CSS_PSEUDO_ELEMENT_IS_JS_CREATED_NAC
);
124 static bool PseudoElementIsFlexOrGridItem(const Type aType
) {
125 return PseudoElementHasFlags(aType
,
126 CSS_PSEUDO_ELEMENT_IS_FLEX_OR_GRID_ITEM
);
129 static bool EnabledInContent(Type aType
) {
130 if (aType
== Type::highlight
&&
131 !mozilla::StaticPrefs::dom_customHighlightAPI_enabled()) {
134 return !PseudoElementHasAnyFlag(
135 aType
, CSS_PSEUDO_ELEMENT_ENABLED_IN_UA_SHEETS_AND_CHROME
);
138 static bool IsEnabled(Type aType
, EnabledState aEnabledState
) {
139 if (EnabledInContent(aType
)) {
143 if ((aEnabledState
& EnabledState::InUASheets
) &&
144 PseudoElementHasFlags(aType
, CSS_PSEUDO_ELEMENT_ENABLED_IN_UA_SHEETS
)) {
148 if ((aEnabledState
& EnabledState::InChrome
) &&
149 PseudoElementHasFlags(aType
, CSS_PSEUDO_ELEMENT_ENABLED_IN_CHROME
)) {
156 static nsString
PseudoTypeAsString(Type aPseudoType
);
159 // Does the given pseudo-element have all of the flags given?
160 static bool PseudoElementHasFlags(const Type aType
, uint32_t aFlags
) {
161 MOZ_ASSERT(aType
< Type::CSSPseudoElementsEnd
);
162 return (kPseudoElementFlags
[size_t(aType
)] & aFlags
) == aFlags
;
165 static bool PseudoElementHasAnyFlag(const Type aType
, uint32_t aFlags
) {
166 MOZ_ASSERT(aType
< Type::CSSPseudoElementsEnd
);
167 return (kPseudoElementFlags
[size_t(aType
)] & aFlags
) != 0;
170 static nsStaticAtom
* GetAtomBase();
172 static const uint32_t kPseudoElementFlags
[size_t(Type::CSSPseudoElementsEnd
)];
175 #endif /* nsCSSPseudoElements_h___ */