CounterStyleManager.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /* -*- Mode: C++; tab-width: 8; 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. #ifndef mozilla_CounterStyleManager_h_
  6. #define mozilla_CounterStyleManager_h_
  7. #include "nsStringFwd.h"
  8. #include "nsRefPtrHashtable.h"
  9. #include "nsHashKeys.h"
  10. #include "nsStyleConsts.h"
  11. #include "mozilla/Attributes.h"
  12. #include "nsCSSValue.h"
  13. class nsPresContext;
  14. namespace mozilla {
  15. class WritingMode;
  16. typedef int32_t CounterValue;
  17. class CounterStyleManager;
  18. class AnonymousCounterStyle;
  19. struct NegativeType;
  20. struct PadType;
  21. class CounterStyle
  22. {
  23. protected:
  24. explicit constexpr CounterStyle(int32_t aStyle)
  25. : mStyle(aStyle)
  26. {
  27. }
  28. private:
  29. CounterStyle(const CounterStyle& aOther) = delete;
  30. void operator=(const CounterStyle& other) = delete;
  31. public:
  32. int32_t GetStyle() const { return mStyle; }
  33. bool IsNone() const { return mStyle == NS_STYLE_LIST_STYLE_NONE; }
  34. bool IsCustomStyle() const { return mStyle == NS_STYLE_LIST_STYLE_CUSTOM; }
  35. // A style is dependent if it depends on the counter style manager.
  36. // Custom styles are certainly dependent. In addition, some builtin
  37. // styles are dependent for fallback.
  38. bool IsDependentStyle() const;
  39. virtual void GetStyleName(nsSubstring& aResult) = 0;
  40. virtual void GetPrefix(nsSubstring& aResult) = 0;
  41. virtual void GetSuffix(nsSubstring& aResult) = 0;
  42. void GetCounterText(CounterValue aOrdinal,
  43. WritingMode aWritingMode,
  44. nsSubstring& aResult,
  45. bool& aIsRTL);
  46. virtual void GetSpokenCounterText(CounterValue aOrdinal,
  47. WritingMode aWritingMode,
  48. nsSubstring& aResult,
  49. bool& aIsBullet);
  50. // XXX This method could be removed once ::-moz-list-bullet and
  51. // ::-moz-list-number are completely merged into ::marker.
  52. virtual bool IsBullet() = 0;
  53. virtual void GetNegative(NegativeType& aResult) = 0;
  54. /**
  55. * This method returns whether an ordinal is in the range of this
  56. * counter style. Note that, it is possible that an ordinal in range
  57. * is rejected by the generating algorithm.
  58. */
  59. virtual bool IsOrdinalInRange(CounterValue aOrdinal) = 0;
  60. /**
  61. * This method returns whether an ordinal is in the default range of
  62. * this counter style. This is the effective range when no 'range'
  63. * descriptor is specified.
  64. */
  65. virtual bool IsOrdinalInAutoRange(CounterValue aOrdinal) = 0;
  66. virtual void GetPad(PadType& aResult) = 0;
  67. virtual CounterStyle* GetFallback() = 0;
  68. virtual uint8_t GetSpeakAs() = 0;
  69. virtual bool UseNegativeSign() = 0;
  70. virtual void CallFallbackStyle(CounterValue aOrdinal,
  71. WritingMode aWritingMode,
  72. nsSubstring& aResult,
  73. bool& aIsRTL);
  74. virtual bool GetInitialCounterText(CounterValue aOrdinal,
  75. WritingMode aWritingMode,
  76. nsSubstring& aResult,
  77. bool& aIsRTL) = 0;
  78. virtual AnonymousCounterStyle* AsAnonymous() { return nullptr; }
  79. NS_IMETHOD_(MozExternalRefCountType) AddRef() = 0;
  80. NS_IMETHOD_(MozExternalRefCountType) Release() = 0;
  81. protected:
  82. int32_t mStyle;
  83. };
  84. class AnonymousCounterStyle final : public CounterStyle
  85. {
  86. public:
  87. explicit AnonymousCounterStyle(const nsSubstring& aContent);
  88. explicit AnonymousCounterStyle(const nsCSSValue::Array* aValue);
  89. virtual void GetStyleName(nsAString& aResult) override;
  90. virtual void GetPrefix(nsAString& aResult) override;
  91. virtual void GetSuffix(nsAString& aResult) override;
  92. virtual bool IsBullet() override;
  93. virtual void GetNegative(NegativeType& aResult) override;
  94. virtual bool IsOrdinalInRange(CounterValue aOrdinal) override;
  95. virtual bool IsOrdinalInAutoRange(CounterValue aOrdinal) override;
  96. virtual void GetPad(PadType& aResult) override;
  97. virtual CounterStyle* GetFallback() override;
  98. virtual uint8_t GetSpeakAs() override;
  99. virtual bool UseNegativeSign() override;
  100. virtual bool GetInitialCounterText(CounterValue aOrdinal,
  101. WritingMode aWritingMode,
  102. nsSubstring& aResult,
  103. bool& aIsRTL) override;
  104. virtual AnonymousCounterStyle* AsAnonymous() override { return this; }
  105. bool IsSingleString() const { return mSingleString; }
  106. uint8_t GetSystem() const { return mSystem; }
  107. const nsTArray<nsString>& GetSymbols() const { return mSymbols; }
  108. NS_INLINE_DECL_REFCOUNTING(AnonymousCounterStyle, override)
  109. private:
  110. ~AnonymousCounterStyle() {}
  111. bool mSingleString;
  112. uint8_t mSystem;
  113. nsTArray<nsString> mSymbols;
  114. };
  115. class CounterStyleManager final
  116. {
  117. private:
  118. ~CounterStyleManager();
  119. public:
  120. explicit CounterStyleManager(nsPresContext* aPresContext);
  121. static void InitializeBuiltinCounterStyles();
  122. void Disconnect();
  123. bool IsInitial() const
  124. {
  125. // only 'none' and 'decimal'
  126. return mCacheTable.Count() == 2;
  127. }
  128. CounterStyle* BuildCounterStyle(const nsSubstring& aName);
  129. static CounterStyle* GetBuiltinStyle(int32_t aStyle);
  130. static CounterStyle* GetNoneStyle()
  131. {
  132. return GetBuiltinStyle(NS_STYLE_LIST_STYLE_NONE);
  133. }
  134. static CounterStyle* GetDecimalStyle()
  135. {
  136. return GetBuiltinStyle(NS_STYLE_LIST_STYLE_DECIMAL);
  137. }
  138. // This method will scan all existing counter styles generated by this
  139. // manager, and remove or mark data dirty accordingly. It returns true
  140. // if any counter style is changed, false elsewise. This method should
  141. // be called when any counter style may be affected.
  142. bool NotifyRuleChanged();
  143. nsPresContext* PresContext() const { return mPresContext; }
  144. NS_INLINE_DECL_REFCOUNTING(CounterStyleManager)
  145. private:
  146. nsPresContext* mPresContext;
  147. nsRefPtrHashtable<nsStringHashKey, CounterStyle> mCacheTable;
  148. };
  149. } // namespace mozilla
  150. #endif /* !defined(mozilla_CounterStyleManager_h_) */