File: counter_style.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (216 lines) | stat: -rw-r--r-- 7,598 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_CSS_COUNTER_STYLE_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_CSS_COUNTER_STYLE_H_

#include "base/check_op.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/collection_support/heap_hash_set.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"

namespace blink {

class StyleRuleCounterStyle;
class CSSValue;

enum class CounterStyleSystem {
  kCyclic,
  kFixed,
  kSymbolic,
  kAlphabetic,
  kNumeric,
  kAdditive,
  kHebrew,
  kSimpChineseInformal,
  kSimpChineseFormal,
  kTradChineseInformal,
  kTradChineseFormal,
  kKoreanHangulFormal,
  kKoreanHanjaInformal,
  kKoreanHanjaFormal,
  kLowerArmenian,
  kUpperArmenian,
  kEthiopicNumeric,
  kUnresolvedExtends,
};

enum class CounterStyleSpeakAs {
  kAuto,
  kBullets,
  kNumbers,
  kWords,
  kReference,
};

// Represents a valid counter style defined in a tree scope.
class CORE_EXPORT CounterStyle final : public GarbageCollected<CounterStyle> {
 public:
  static CounterStyle& GetDecimal();

  static CounterStyleSystem ToCounterStyleSystemEnum(const CSSValue* value);

  // Returns nullptr if the @counter-style rule is invalid.
  static CounterStyle* Create(const StyleRuleCounterStyle&);

  const StyleRuleCounterStyle& GetStyleRule() const { return *style_rule_; }

  AtomicString GetName() const;
  CounterStyleSystem GetSystem() const { return system_; }

  bool IsPredefined() const { return is_predefined_; }
  void SetIsPredefined() { is_predefined_ = true; }

  // Returns true for the predefined symbolic counter styles 'disc', 'circle',
  // 'square', 'disclosure-open' and 'disclosure-closed'.
  bool IsPredefinedSymbolMarker() const { return is_predefined_symbol_marker_; }
  void SetIsPredefinedSymbolMarker() { is_predefined_symbol_marker_ = true; }

  // A CounterStyle object is dirtied when the information it holds becomes
  // stale, e.g., when the style rule mutated or the 'extends' or 'fallback'
  // counter styles mutated, etc. Once dirtied, it will never be reused, and
  // will be removed or replaced by a newly created clean CounterStyle.
  // Elements using dirty CounterStyles should update style and layout.
  bool IsDirty() const { return is_dirty_; }
  void SetIsDirty() { is_dirty_ = true; }

  void TraverseAndMarkDirtyIfNeeded(HeapHashSet<Member<CounterStyle>>& visited);

  // Set to true when there's no counter style matching 'extends', 'fallback' or
  // 'speak-as', so this style must be dirtied when new styles are added.
  void SetHasInexistentReferences() { has_inexistent_references_ = true; }

  // https://drafts.csswg.org/css-counter-styles/#generate-a-counter
  String GenerateRepresentation(int value) const;

  String GetPrefix() const { return prefix_; }
  String GetSuffix() const { return suffix_; }

  String GenerateRepresentationWithPrefixAndSuffix(int value) const {
    return prefix_ + GenerateRepresentation(value) + suffix_;
  }

  AtomicString GetExtendsName() const { return extends_name_; }
  const CounterStyle& GetExtendedStyle() const { return *extended_style_; }
  bool HasUnresolvedExtends() const {
    return system_ == CounterStyleSystem::kUnresolvedExtends;
  }
  void ResolveExtends(CounterStyle& extended);

  AtomicString GetFallbackName() const { return fallback_name_; }
  const CounterStyle& GetFallbackStyle() const { return *fallback_style_; }
  bool HasUnresolvedFallback() const { return !fallback_style_; }
  void ResolveFallback(CounterStyle& fallback) { fallback_style_ = &fallback; }

  CounterStyleSpeakAs GetSpeakAs() const { return speak_as_; }
  AtomicString GetSpeakAsName() const { return speak_as_name_; }
  bool HasUnresolvedSpeakAsReference() const {
    return speak_as_ == CounterStyleSpeakAs::kReference && !speak_as_style_;
  }
  void ResolveInvalidSpeakAsReference() {
    speak_as_ = CounterStyleSpeakAs::kAuto;
    speak_as_style_ = nullptr;
  }
  void ResolveSpeakAsReference(CounterStyle& speak_as) {
    DCHECK_NE(CounterStyleSpeakAs::kReference, speak_as.speak_as_);
    speak_as_style_ = speak_as;
  }
  const CounterStyle& GetSpeakAsStyle() const {
    DCHECK_EQ(CounterStyleSpeakAs::kReference, speak_as_);
    return *speak_as_style_;
  }

  // Converts kReference and kAuto to one of the remaining values.
  CounterStyleSpeakAs EffectiveSpeakAs() const;

  // Generates the alternative text for the given counter value according to the
  // 'speak-as' descriptor. Consumed by accessibility.
  String GenerateTextAlternative(int value) const;

  void Trace(Visitor*) const;

  explicit CounterStyle(const StyleRuleCounterStyle& rule);
  ~CounterStyle();

 private:
  // https://drafts.csswg.org/css-counter-styles/#counter-style-range
  bool RangeContains(int value) const;

  // Returns true if a negative sign is needed for the value.
  // https://drafts.csswg.org/css-counter-styles/#counter-style-negative
  bool NeedsNegativeSign(int value) const;

  // https://drafts.csswg.org/css-counter-styles/#initial-representation-for-the-counter-value
  // Returns nullptr if the counter value cannot be represented with the given
  // 'system', 'range' and 'symbols'/'additive-symbols' descriptor values.
  String GenerateInitialRepresentation(int value) const;

  // Uses the fallback counter style to generate a representation for the value.
  // It may recurse, and if it enters a loop, it uses 'decimal' instead.
  String GenerateFallbackRepresentation(int value) const;

  String IndexesToString(const Vector<wtf_size_t>& symbol_indexes) const;

  String GenerateTextAlternativeWithoutPrefixSuffix(int value) const;

  // The corresponding style rule in CSS.
  Member<const StyleRuleCounterStyle> style_rule_;

  // Tracks mutations of |style_rule_|.
  int style_rule_version_;

  // The actual system of the counter style with 'extends' resolved. The value
  // is kUnresolvedExtends temporarily before the resolution.
  CounterStyleSystem system_ = CounterStyleSystem::kSymbolic;

  AtomicString extends_name_;
  Member<CounterStyle> extended_style_;

  AtomicString fallback_name_{"decimal"};
  Member<CounterStyle> fallback_style_;

  CounterStyleSpeakAs speak_as_ = CounterStyleSpeakAs::kAuto;

  // These two members are set if 'speak-as' references another counter style.
  AtomicString speak_as_name_;
  Member<CounterStyle> speak_as_style_;

  // True if we are looking for a fallback counter style to generate a counter
  // value. Supports cycle detection in fallback.
  mutable bool is_in_fallback_ = false;

  // Value of 'symbols' for non-additive systems; Or symbol values in
  // 'additive-symbols' for the 'additive' system.
  Vector<String> symbols_;

  // Additive weights, for the 'additive' system only.
  Vector<unsigned> additive_weights_;

  // Value of 'range' descriptor. Empty vector means 'auto'.
  Vector<std::pair<int, int>> range_;

  String prefix_;
  String suffix_ = ". ";

  String negative_prefix_ = "-";
  String negative_suffix_;

  String pad_symbol_;
  wtf_size_t pad_length_ = 0;

  // First symbol value, for 'fixed' system only.
  wtf_size_t first_symbol_value_ = 1;

  bool is_predefined_ = false;
  bool is_predefined_symbol_marker_ = false;
  bool has_inexistent_references_ = false;
  bool is_dirty_ = false;

  friend class CounterStyleMapTest;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_CSS_COUNTER_STYLE_H_