1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 /* atom list for CSS pseudo-classes */
7 
8 #include "mozilla/ArrayUtils.h"
9 
10 #include "nsCSSPseudoClasses.h"
11 #include "nsStaticAtom.h"
12 #include "mozilla/Preferences.h"
13 #include "nsString.h"
14 
15 using namespace mozilla;
16 
17 // define storage for all atoms
18 #define CSS_PSEUDO_CLASS(_name, _value, _flags, _pref) \
19   static nsIAtom* sPseudoClass_##_name;
20 #include "nsCSSPseudoClassList.h"
21 #undef CSS_PSEUDO_CLASS
22 
23 #define CSS_PSEUDO_CLASS(name_, value_, flags_, pref_) \
24   NS_STATIC_ATOM_BUFFER(name_##_pseudo_class_buffer, value_)
25 #include "nsCSSPseudoClassList.h"
26 #undef CSS_PSEUDO_CLASS
27 
28 #define CSS_PSEUDO_CLASS(name_, value_, flags_, pref_) \
29   static_assert(!((flags_) & CSS_PSEUDO_CLASS_ENABLED_IN_CHROME) || \
30                 ((flags_) & CSS_PSEUDO_CLASS_ENABLED_IN_UA_SHEETS), \
31                 "Pseudo-class '" #name_ "' is enabled in chrome, so it " \
32                 "should also be enabled in UA sheets");
33 #include "nsCSSPseudoClassList.h"
34 #undef CSS_PSEUDO_CLASS
35 
36 // Array of nsStaticAtom for each of the pseudo-classes.
37 static const nsStaticAtom CSSPseudoClasses_info[] = {
38 #define CSS_PSEUDO_CLASS(name_, value_, flags_, pref_) \
39   NS_STATIC_ATOM(name_##_pseudo_class_buffer, &sPseudoClass_##name_),
40 #include "nsCSSPseudoClassList.h"
41 #undef CSS_PSEUDO_CLASS
42 };
43 
44 // Flags data for each of the pseudo-classes, which must be separate
45 // from the previous array since there's no place for it in
46 // nsStaticAtom.
47 /* static */ const uint32_t
48 nsCSSPseudoClasses::kPseudoClassFlags[] = {
49 #define CSS_PSEUDO_CLASS(name_, value_, flags_, pref_) \
50   flags_,
51 #include "nsCSSPseudoClassList.h"
52 #undef CSS_PSEUDO_CLASS
53 };
54 
55 /* static */ bool
56 nsCSSPseudoClasses::sPseudoClassEnabled[] = {
57 // If the pseudo class has any "ENABLED_IN" flag set, it is disabled by
58 // default. Note that, if a pseudo class has pref, whatever its default
59 // value is, it'll later be changed in nsCSSPseudoClasses::AddRefAtoms()
60 // If the pseudo class has "ENABLED_IN" flags but doesn't have a pref,
61 // it is an internal pseudo class which is disabled elsewhere.
62 #define IS_ENABLED_BY_DEFAULT(flags_) \
63   (!((flags_) & CSS_PSEUDO_CLASS_ENABLED_MASK))
64 #define CSS_PSEUDO_CLASS(name_, value_, flags_, pref_) \
65   IS_ENABLED_BY_DEFAULT(flags_),
66 #include "nsCSSPseudoClassList.h"
67 #undef CSS_PSEUDO_CLASS
68 #undef IS_ENABLED_BY_DEFAULT
69 };
70 
AddRefAtoms()71 void nsCSSPseudoClasses::AddRefAtoms()
72 {
73   NS_RegisterStaticAtoms(CSSPseudoClasses_info);
74 
75 #define CSS_PSEUDO_CLASS(name_, value_, flags_, pref_)                        \
76   if (pref_[0]) {                                                             \
77     auto idx = static_cast<CSSPseudoElementTypeBase>(Type::name_);            \
78     Preferences::AddBoolVarCache(&sPseudoClassEnabled[idx], pref_);           \
79   }
80 #include "nsCSSPseudoClassList.h"
81 #undef CSS_PSEUDO_CLASS
82 }
83 
84 bool
HasStringArg(Type aType)85 nsCSSPseudoClasses::HasStringArg(Type aType)
86 {
87   return aType == Type::lang ||
88          aType == Type::mozEmptyExceptChildrenWithLocalname ||
89          aType == Type::mozSystemMetric ||
90          aType == Type::mozLocaleDir ||
91          aType == Type::mozDir ||
92          aType == Type::dir;
93 }
94 
95 bool
HasNthPairArg(Type aType)96 nsCSSPseudoClasses::HasNthPairArg(Type aType)
97 {
98   return aType == Type::nthChild ||
99          aType == Type::nthLastChild ||
100          aType == Type::nthOfType ||
101          aType == Type::nthLastOfType;
102 }
103 
104 void
PseudoTypeToString(Type aType,nsAString & aString)105 nsCSSPseudoClasses::PseudoTypeToString(Type aType, nsAString& aString)
106 {
107   MOZ_ASSERT(aType < Type::Count, "Unexpected type");
108   auto idx = static_cast<CSSPseudoClassTypeBase>(aType);
109   (*CSSPseudoClasses_info[idx].mAtom)->ToString(aString);
110 }
111 
112 /* static */ CSSPseudoClassType
GetPseudoType(nsIAtom * aAtom,EnabledState aEnabledState)113 nsCSSPseudoClasses::GetPseudoType(nsIAtom* aAtom, EnabledState aEnabledState)
114 {
115   for (uint32_t i = 0; i < ArrayLength(CSSPseudoClasses_info); ++i) {
116     if (*CSSPseudoClasses_info[i].mAtom == aAtom) {
117       Type type = Type(i);
118       return IsEnabled(type, aEnabledState) ? type : Type::NotPseudo;
119     }
120   }
121   return Type::NotPseudo;
122 }
123 
124 /* static */ bool
IsUserActionPseudoClass(Type aType)125 nsCSSPseudoClasses::IsUserActionPseudoClass(Type aType)
126 {
127   // See http://dev.w3.org/csswg/selectors4/#useraction-pseudos
128   return aType == Type::hover ||
129          aType == Type::active ||
130          aType == Type::focus;
131 }
132