Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : // vim:cindent:ai:sw=4:ts=4:et:
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/. */
6 :
7 : /* implementation of CSS counters (for numbering things) */
8 :
9 : #ifndef nsCounterManager_h_
10 : #define nsCounterManager_h_
11 :
12 : #include "mozilla/Attributes.h"
13 : #include "nsGenConList.h"
14 : #include "nsClassHashtable.h"
15 : #include "mozilla/Likely.h"
16 : #include "CounterStyleManager.h"
17 :
18 : class nsCounterList;
19 : struct nsCounterUseNode;
20 : struct nsCounterChangeNode;
21 :
22 0 : struct nsCounterNode : public nsGenConNode {
23 : enum Type {
24 : RESET, // a "counter number" pair in 'counter-reset'
25 : INCREMENT, // a "counter number" pair in 'counter-increment'
26 : USE // counter() or counters() in 'content'
27 : };
28 :
29 : Type mType;
30 :
31 : // Counter value after this node
32 : int32_t mValueAfter;
33 :
34 : // mScopeStart points to the node (usually a RESET, but not in the
35 : // case of an implied 'counter-reset') that created the scope for
36 : // this element (for a RESET, its outer scope, i.e., the one it is
37 : // inside rather than the one it creates).
38 :
39 : // May be null for all types, but only when mScopePrev is also null.
40 : // Being null for a non-RESET means that it is an implied
41 : // 'counter-reset'. Being null for a RESET means it has no outer
42 : // scope.
43 : nsCounterNode *mScopeStart;
44 :
45 : // mScopePrev points to the previous node that is in the same scope,
46 : // or for a RESET, the previous node in the scope outside of the
47 : // reset.
48 :
49 : // May be null for all types, but only when mScopeStart is also
50 : // null. Following the mScopePrev links will eventually lead to
51 : // mScopeStart. Being null for a non-RESET means that it is an
52 : // implied 'counter-reset'. Being null for a RESET means it has no
53 : // outer scope.
54 : nsCounterNode *mScopePrev;
55 :
56 : inline nsCounterUseNode* UseNode();
57 : inline nsCounterChangeNode* ChangeNode();
58 :
59 : // For RESET and INCREMENT nodes, aPseudoFrame need not be a
60 : // pseudo-element, and aContentIndex represents the index within the
61 : // 'counter-reset' or 'counter-increment' property instead of within
62 : // the 'content' property but offset to ensure that (reset,
63 : // increment, use) sort in that order. (This slight weirdness
64 : // allows sharing a lot of code with 'quotes'.)
65 0 : nsCounterNode(int32_t aContentIndex, Type aType)
66 0 : : nsGenConNode(aContentIndex)
67 : , mType(aType)
68 : , mValueAfter(0)
69 : , mScopeStart(nullptr)
70 0 : , mScopePrev(nullptr)
71 : {
72 0 : }
73 :
74 : // to avoid virtual function calls in the common case
75 : inline void Calc(nsCounterList* aList);
76 : };
77 :
78 0 : struct nsCounterUseNode : public nsCounterNode {
79 : mozilla::CounterStylePtr mCounterStyle;
80 : nsString mSeparator;
81 :
82 : // false for counter(), true for counters()
83 : bool mAllCounters;
84 :
85 : // args go directly to member variables here and of nsGenConNode
86 0 : nsCounterUseNode(nsStyleContentData::CounterFunction* aCounterFunction,
87 : uint32_t aContentIndex, bool aAllCounters)
88 0 : : nsCounterNode(aContentIndex, USE)
89 : , mCounterStyle(aCounterFunction->mCounterStyle)
90 : , mSeparator(aCounterFunction->mSeparator)
91 0 : , mAllCounters(aAllCounters)
92 : {
93 0 : NS_ASSERTION(aContentIndex <= INT32_MAX, "out of range");
94 0 : }
95 :
96 : virtual bool InitTextFrame(nsGenConList* aList,
97 : nsIFrame* aPseudoFrame, nsIFrame* aTextFrame) override;
98 :
99 : // assign the correct |mValueAfter| value to a node that has been inserted
100 : // Should be called immediately after calling |Insert|.
101 : void Calc(nsCounterList* aList);
102 :
103 : // The text that should be displayed for this counter.
104 : void GetText(nsString& aResult);
105 : };
106 :
107 0 : struct nsCounterChangeNode : public nsCounterNode {
108 : int32_t mChangeValue; // the numeric value of the increment or reset
109 :
110 : // |aPseudoFrame| is not necessarily a pseudo-element's frame, but
111 : // since it is for every other subclass of nsGenConNode, we follow
112 : // the naming convention here.
113 : // |aPropIndex| is the index of the value within the list in the
114 : // 'counter-increment' or 'counter-reset' property.
115 0 : nsCounterChangeNode(nsIFrame* aPseudoFrame,
116 : nsCounterNode::Type aChangeType,
117 : int32_t aChangeValue,
118 : int32_t aPropIndex)
119 0 : : nsCounterNode(// Fake a content index for resets and increments
120 : // that comes before all the real content, with
121 : // the resets first, in order, and then the increments.
122 : aPropIndex + (aChangeType == RESET
123 0 : ? (INT32_MIN)
124 : : (INT32_MIN / 2)),
125 : aChangeType)
126 0 : , mChangeValue(aChangeValue)
127 : {
128 0 : NS_ASSERTION(aPropIndex >= 0, "out of range");
129 0 : NS_ASSERTION(aChangeType == INCREMENT || aChangeType == RESET,
130 : "bad type");
131 0 : mPseudoFrame = aPseudoFrame;
132 0 : CheckFrameAssertions();
133 0 : }
134 :
135 : // assign the correct |mValueAfter| value to a node that has been inserted
136 : // Should be called immediately after calling |Insert|.
137 : void Calc(nsCounterList* aList);
138 : };
139 :
140 0 : inline nsCounterUseNode* nsCounterNode::UseNode()
141 : {
142 0 : NS_ASSERTION(mType == USE, "wrong type");
143 0 : return static_cast<nsCounterUseNode*>(this);
144 : }
145 :
146 0 : inline nsCounterChangeNode* nsCounterNode::ChangeNode()
147 : {
148 0 : NS_ASSERTION(mType == INCREMENT || mType == RESET, "wrong type");
149 0 : return static_cast<nsCounterChangeNode*>(this);
150 : }
151 :
152 0 : inline void nsCounterNode::Calc(nsCounterList* aList)
153 : {
154 0 : if (mType == USE)
155 0 : UseNode()->Calc(aList);
156 : else
157 0 : ChangeNode()->Calc(aList);
158 0 : }
159 :
160 0 : class nsCounterList : public nsGenConList {
161 : public:
162 0 : nsCounterList() : nsGenConList(),
163 0 : mDirty(false)
164 0 : {}
165 :
166 0 : void Insert(nsCounterNode* aNode) {
167 0 : nsGenConList::Insert(aNode);
168 : // Don't SetScope if we're dirty -- we'll reset all the scopes anyway,
169 : // and we can't usefully compute scopes right now.
170 0 : if (MOZ_LIKELY(!IsDirty())) {
171 0 : SetScope(aNode);
172 : }
173 0 : }
174 :
175 0 : nsCounterNode* First() {
176 0 : return static_cast<nsCounterNode*>(mList.getFirst());
177 : }
178 :
179 0 : static nsCounterNode* Next(nsCounterNode* aNode) {
180 0 : return static_cast<nsCounterNode*>(nsGenConList::Next(aNode));
181 : }
182 0 : static nsCounterNode* Prev(nsCounterNode* aNode) {
183 0 : return static_cast<nsCounterNode*>(nsGenConList::Prev(aNode));
184 : }
185 :
186 0 : static int32_t ValueBefore(nsCounterNode* aNode) {
187 0 : return aNode->mScopePrev ? aNode->mScopePrev->mValueAfter : 0;
188 : }
189 :
190 : // Correctly set |aNode->mScopeStart| and |aNode->mScopePrev|
191 : void SetScope(nsCounterNode *aNode);
192 :
193 : // Recalculate |mScopeStart|, |mScopePrev|, and |mValueAfter| for
194 : // all nodes and update text in text content nodes.
195 : void RecalcAll();
196 :
197 0 : bool IsDirty() { return mDirty; }
198 0 : void SetDirty() { mDirty = true; }
199 :
200 : private:
201 : bool mDirty;
202 : };
203 :
204 : /**
205 : * The counter manager maintains an |nsCounterList| for each named
206 : * counter to keep track of all scopes with that name.
207 : */
208 32 : class nsCounterManager {
209 : public:
210 : // Returns true if dirty
211 : bool AddCounterResetsAndIncrements(nsIFrame *aFrame);
212 :
213 : // Gets the appropriate counter list, creating it if necessary.
214 : // Guaranteed to return non-null. (Uses an infallible hashtable API.)
215 : nsCounterList* CounterListFor(const nsAString& aCounterName);
216 :
217 : // Clean up data in any dirty counter lists.
218 : void RecalcAll();
219 :
220 : // Set all counter lists dirty
221 : void SetAllDirty();
222 :
223 : // Destroy nodes for the frame in any lists, and return whether any
224 : // nodes were destroyed.
225 : bool DestroyNodesFor(nsIFrame *aFrame);
226 :
227 : // Clear all data.
228 4 : void Clear() { mNames.Clear(); }
229 :
230 : #ifdef DEBUG
231 : void Dump();
232 : #endif
233 :
234 0 : static int32_t IncrementCounter(int32_t aOldValue, int32_t aIncrement)
235 : {
236 : // Addition of unsigned values is defined to be arithmetic
237 : // modulo 2^bits (C++ 2011, 3.9.1 [basic.fundamental], clause 4);
238 : // addition of signed values is undefined (and clang does
239 : // something very strange if we use it here). Likewise integral
240 : // conversion from signed to unsigned is also defined as modulo
241 : // 2^bits (C++ 2011, 4.7 [conv.integral], clause 2); conversion
242 : // from unsigned to signed is however undefined (ibid., clause 3),
243 : // but to do what we want we must nonetheless depend on that
244 : // small piece of undefined behavior.
245 0 : int32_t newValue = int32_t(uint32_t(aOldValue) + uint32_t(aIncrement));
246 : // The CSS Working Group resolved that a counter-increment that
247 : // exceeds internal limits should not increment at all.
248 : // http://lists.w3.org/Archives/Public/www-style/2013Feb/0392.html
249 : // (This means, for example, that if aIncrement is 5, the
250 : // counter will get stuck at the largest multiple of 5 less than
251 : // the maximum 32-bit integer.)
252 0 : if ((aIncrement > 0) != (newValue > aOldValue)) {
253 0 : newValue = aOldValue;
254 : }
255 0 : return newValue;
256 : }
257 :
258 : private:
259 : // for |AddCounterResetsAndIncrements| only
260 : bool AddResetOrIncrement(nsIFrame* aFrame, int32_t aIndex,
261 : const nsStyleCounterData& aCounterData,
262 : nsCounterNode::Type aType);
263 :
264 : nsClassHashtable<nsStringHashKey, nsCounterList> mNames;
265 : };
266 :
267 : #endif /* nsCounterManager_h_ */
|