nsCSSPseudoElements.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /* atom list for CSS pseudo-elements */
  6. #include "mozilla/ArrayUtils.h"
  7. #include "nsCSSPseudoElements.h"
  8. #include "nsAtomListUtils.h"
  9. #include "nsStaticAtom.h"
  10. #include "nsCSSAnonBoxes.h"
  11. using namespace mozilla;
  12. // define storage for all atoms
  13. #define CSS_PSEUDO_ELEMENT(name_, value_, flags_) \
  14. nsICSSPseudoElement* nsCSSPseudoElements::name_;
  15. #include "nsCSSPseudoElementList.h"
  16. #undef CSS_PSEUDO_ELEMENT
  17. #define CSS_PSEUDO_ELEMENT(name_, value_, flags_) \
  18. NS_STATIC_ATOM_BUFFER(name_##_pseudo_element_buffer, value_)
  19. #include "nsCSSPseudoElementList.h"
  20. #undef CSS_PSEUDO_ELEMENT
  21. // Array of nsStaticAtom for each of the pseudo-elements.
  22. static const nsStaticAtom CSSPseudoElements_info[] = {
  23. #define CSS_PSEUDO_ELEMENT(name_, value_, flags_) \
  24. NS_STATIC_ATOM(name_##_pseudo_element_buffer, (nsIAtom**)&nsCSSPseudoElements::name_),
  25. #include "nsCSSPseudoElementList.h"
  26. #undef CSS_PSEUDO_ELEMENT
  27. };
  28. // Flags data for each of the pseudo-elements, which must be separate
  29. // from the previous array since there's no place for it in
  30. // nsStaticAtom.
  31. /* static */ const uint32_t
  32. nsCSSPseudoElements::kPseudoElementFlags[] = {
  33. #define CSS_PSEUDO_ELEMENT(name_, value_, flags_) \
  34. flags_,
  35. #include "nsCSSPseudoElementList.h"
  36. #undef CSS_PSEUDO_ELEMENT
  37. };
  38. void nsCSSPseudoElements::AddRefAtoms()
  39. {
  40. NS_RegisterStaticAtoms(CSSPseudoElements_info);
  41. }
  42. bool nsCSSPseudoElements::IsPseudoElement(nsIAtom *aAtom)
  43. {
  44. return nsAtomListUtils::IsMember(aAtom, CSSPseudoElements_info,
  45. ArrayLength(CSSPseudoElements_info));
  46. }
  47. /* static */ bool
  48. nsCSSPseudoElements::IsCSS2PseudoElement(nsIAtom *aAtom)
  49. {
  50. // We don't implement this using PseudoElementHasFlags because callers
  51. // want to pass things that could be anon boxes.
  52. NS_ASSERTION(nsCSSPseudoElements::IsPseudoElement(aAtom) ||
  53. nsCSSAnonBoxes::IsAnonBox(aAtom),
  54. "must be pseudo element or anon box");
  55. bool result = aAtom == nsCSSPseudoElements::after ||
  56. aAtom == nsCSSPseudoElements::before ||
  57. aAtom == nsCSSPseudoElements::firstLetter ||
  58. aAtom == nsCSSPseudoElements::firstLine;
  59. NS_ASSERTION(nsCSSAnonBoxes::IsAnonBox(aAtom) ||
  60. result == PseudoElementHasFlags(
  61. GetPseudoType(aAtom, EnabledState::eIgnoreEnabledState),
  62. CSS_PSEUDO_ELEMENT_IS_CSS2),
  63. "result doesn't match flags");
  64. return result;
  65. }
  66. /* static */ CSSPseudoElementType
  67. nsCSSPseudoElements::GetPseudoType(nsIAtom *aAtom, EnabledState aEnabledState)
  68. {
  69. for (CSSPseudoElementTypeBase i = 0;
  70. i < ArrayLength(CSSPseudoElements_info);
  71. ++i) {
  72. if (*CSSPseudoElements_info[i].mAtom == aAtom) {
  73. auto type = static_cast<Type>(i);
  74. // ::moz-placeholder is an alias for ::placeholder
  75. if (type == CSSPseudoElementType::mozPlaceholder) {
  76. type = CSSPseudoElementType::placeholder;
  77. }
  78. return IsEnabled(type, aEnabledState) ? type : Type::NotPseudo;
  79. }
  80. }
  81. if (nsCSSAnonBoxes::IsAnonBox(aAtom)) {
  82. #ifdef MOZ_XUL
  83. if (nsCSSAnonBoxes::IsTreePseudoElement(aAtom)) {
  84. return Type::XULTree;
  85. }
  86. #endif
  87. return Type::AnonBox;
  88. }
  89. return Type::NotPseudo;
  90. }
  91. /* static */ nsIAtom*
  92. nsCSSPseudoElements::GetPseudoAtom(Type aType)
  93. {
  94. NS_ASSERTION(aType < Type::Count, "Unexpected type");
  95. return *CSSPseudoElements_info[
  96. static_cast<CSSPseudoElementTypeBase>(aType)].mAtom;
  97. }
  98. /* static */ bool
  99. nsCSSPseudoElements::PseudoElementSupportsUserActionState(const Type aType)
  100. {
  101. return PseudoElementHasFlags(aType,
  102. CSS_PSEUDO_ELEMENT_SUPPORTS_USER_ACTION_STATE);
  103. }