1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : // vim:cindent:ai:sw=4:ts=4:et:
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is nsCounterManager.
17 : *
18 : * The Initial Developer of the Original Code is the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2004
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * L. David Baron <dbaron@dbaron.org> (original author)
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : /* implementation of CSS counters (for numbering things) */
40 :
41 : #ifndef nsCounterManager_h_
42 : #define nsCounterManager_h_
43 :
44 : #include "nsGenConList.h"
45 : #include "nsAutoPtr.h"
46 : #include "nsClassHashtable.h"
47 :
48 : class nsCounterList;
49 : struct nsCounterUseNode;
50 : struct nsCounterChangeNode;
51 :
52 0 : struct nsCounterNode : public nsGenConNode {
53 : enum Type {
54 : RESET, // a "counter number" pair in 'counter-reset'
55 : INCREMENT, // a "counter number" pair in 'counter-increment'
56 : USE // counter() or counters() in 'content'
57 : };
58 :
59 : Type mType;
60 :
61 : // Counter value after this node
62 : PRInt32 mValueAfter;
63 :
64 : // mScopeStart points to the node (usually a RESET, but not in the
65 : // case of an implied 'counter-reset') that created the scope for
66 : // this element (for a RESET, its outer scope, i.e., the one it is
67 : // inside rather than the one it creates).
68 :
69 : // May be null for all types, but only when mScopePrev is also null.
70 : // Being null for a non-RESET means that it is an implied
71 : // 'counter-reset'. Being null for a RESET means it has no outer
72 : // scope.
73 : nsCounterNode *mScopeStart;
74 :
75 : // mScopePrev points to the previous node that is in the same scope,
76 : // or for a RESET, the previous node in the scope outside of the
77 : // reset.
78 :
79 : // May be null for all types, but only when mScopeStart is also
80 : // null. Following the mScopePrev links will eventually lead to
81 : // mScopeStart. Being null for a non-RESET means that it is an
82 : // implied 'counter-reset'. Being null for a RESET means it has no
83 : // outer scope.
84 : nsCounterNode *mScopePrev;
85 :
86 : inline nsCounterUseNode* UseNode();
87 : inline nsCounterChangeNode* ChangeNode();
88 :
89 : // For RESET and INCREMENT nodes, aPseudoFrame need not be a
90 : // pseudo-element, and aContentIndex represents the index within the
91 : // 'counter-reset' or 'counter-increment' property instead of within
92 : // the 'content' property but offset to ensure that (reset,
93 : // increment, use) sort in that order. (This slight weirdness
94 : // allows sharing a lot of code with 'quotes'.)
95 0 : nsCounterNode(PRInt32 aContentIndex, Type aType)
96 : : nsGenConNode(aContentIndex)
97 : , mType(aType)
98 : , mValueAfter(0)
99 : , mScopeStart(nsnull)
100 0 : , mScopePrev(nsnull)
101 : {
102 0 : }
103 :
104 : // to avoid virtual function calls in the common case
105 : inline void Calc(nsCounterList* aList);
106 : };
107 :
108 0 : struct nsCounterUseNode : public nsCounterNode {
109 : // The same structure passed through the style system: an array
110 : // containing the values in the counter() or counters() in the order
111 : // given in the CSS spec.
112 : nsRefPtr<nsCSSValue::Array> mCounterStyle;
113 :
114 : // false for counter(), true for counters()
115 : bool mAllCounters;
116 :
117 : // args go directly to member variables here and of nsGenConNode
118 0 : nsCounterUseNode(nsCSSValue::Array* aCounterStyle,
119 : PRUint32 aContentIndex, bool aAllCounters)
120 : : nsCounterNode(aContentIndex, USE)
121 : , mCounterStyle(aCounterStyle)
122 0 : , mAllCounters(aAllCounters)
123 : {
124 0 : NS_ASSERTION(aContentIndex <= PR_INT32_MAX, "out of range");
125 0 : }
126 :
127 : virtual bool InitTextFrame(nsGenConList* aList,
128 : nsIFrame* aPseudoFrame, nsIFrame* aTextFrame);
129 :
130 : // assign the correct |mValueAfter| value to a node that has been inserted
131 : // Should be called immediately after calling |Insert|.
132 : void Calc(nsCounterList* aList);
133 :
134 : // The text that should be displayed for this counter.
135 : void GetText(nsString& aResult);
136 : };
137 :
138 0 : struct nsCounterChangeNode : public nsCounterNode {
139 : PRInt32 mChangeValue; // the numeric value of the increment or reset
140 :
141 : // |aPseudoFrame| is not necessarily a pseudo-element's frame, but
142 : // since it is for every other subclass of nsGenConNode, we follow
143 : // the naming convention here.
144 : // |aPropIndex| is the index of the value within the list in the
145 : // 'counter-increment' or 'counter-reset' property.
146 0 : nsCounterChangeNode(nsIFrame* aPseudoFrame,
147 : nsCounterNode::Type aChangeType,
148 : PRInt32 aChangeValue,
149 : PRInt32 aPropIndex)
150 : : nsCounterNode(// Fake a content index for resets and increments
151 : // that comes before all the real content, with
152 : // the resets first, in order, and then the increments.
153 : aPropIndex + (aChangeType == RESET
154 : ? (PR_INT32_MIN)
155 : : (PR_INT32_MIN / 2)),
156 : aChangeType)
157 0 : , mChangeValue(aChangeValue)
158 : {
159 0 : NS_ASSERTION(aPropIndex >= 0, "out of range");
160 0 : NS_ASSERTION(aChangeType == INCREMENT || aChangeType == RESET,
161 : "bad type");
162 0 : mPseudoFrame = aPseudoFrame;
163 0 : CheckFrameAssertions();
164 0 : }
165 :
166 : // assign the correct |mValueAfter| value to a node that has been inserted
167 : // Should be called immediately after calling |Insert|.
168 : void Calc(nsCounterList* aList);
169 : };
170 :
171 0 : inline nsCounterUseNode* nsCounterNode::UseNode()
172 : {
173 0 : NS_ASSERTION(mType == USE, "wrong type");
174 0 : return static_cast<nsCounterUseNode*>(this);
175 : }
176 :
177 0 : inline nsCounterChangeNode* nsCounterNode::ChangeNode()
178 : {
179 0 : NS_ASSERTION(mType == INCREMENT || mType == RESET, "wrong type");
180 0 : return static_cast<nsCounterChangeNode*>(this);
181 : }
182 :
183 0 : inline void nsCounterNode::Calc(nsCounterList* aList)
184 : {
185 0 : if (mType == USE)
186 0 : UseNode()->Calc(aList);
187 : else
188 0 : ChangeNode()->Calc(aList);
189 0 : }
190 :
191 0 : class nsCounterList : public nsGenConList {
192 : public:
193 0 : nsCounterList() : nsGenConList(),
194 0 : mDirty(false)
195 0 : {}
196 :
197 0 : void Insert(nsCounterNode* aNode) {
198 0 : nsGenConList::Insert(aNode);
199 : // Don't SetScope if we're dirty -- we'll reset all the scopes anyway,
200 : // and we can't usefully compute scopes right now.
201 0 : if (NS_LIKELY(!IsDirty())) {
202 0 : SetScope(aNode);
203 : }
204 0 : }
205 :
206 0 : nsCounterNode* First() {
207 0 : return static_cast<nsCounterNode*>(mFirstNode);
208 : }
209 :
210 0 : static nsCounterNode* Next(nsCounterNode* aNode) {
211 0 : return static_cast<nsCounterNode*>(nsGenConList::Next(aNode));
212 : }
213 0 : static nsCounterNode* Prev(nsCounterNode* aNode) {
214 0 : return static_cast<nsCounterNode*>(nsGenConList::Prev(aNode));
215 : }
216 :
217 0 : static PRInt32 ValueBefore(nsCounterNode* aNode) {
218 0 : return aNode->mScopePrev ? aNode->mScopePrev->mValueAfter : 0;
219 : }
220 :
221 : // Correctly set |aNode->mScopeStart| and |aNode->mScopePrev|
222 : void SetScope(nsCounterNode *aNode);
223 :
224 : // Recalculate |mScopeStart|, |mScopePrev|, and |mValueAfter| for
225 : // all nodes and update text in text content nodes.
226 : void RecalcAll();
227 :
228 0 : bool IsDirty() { return mDirty; }
229 0 : void SetDirty() { mDirty = true; }
230 :
231 : private:
232 : bool mDirty;
233 : };
234 :
235 : /**
236 : * The counter manager maintains an |nsCounterList| for each named
237 : * counter to keep track of all scopes with that name.
238 : */
239 0 : class nsCounterManager {
240 : public:
241 : nsCounterManager();
242 : // Returns true if dirty
243 : bool AddCounterResetsAndIncrements(nsIFrame *aFrame);
244 :
245 : // Gets the appropriate counter list, creating it if necessary.
246 : // Returns null only on out-of-memory.
247 : nsCounterList* CounterListFor(const nsSubstring& aCounterName);
248 :
249 : // Clean up data in any dirty counter lists.
250 : void RecalcAll();
251 :
252 : // Destroy nodes for the frame in any lists, and return whether any
253 : // nodes were destroyed.
254 : bool DestroyNodesFor(nsIFrame *aFrame);
255 :
256 : // Clear all data.
257 0 : void Clear() { mNames.Clear(); }
258 :
259 : #ifdef DEBUG
260 : void Dump();
261 : #endif
262 :
263 : private:
264 : // for |AddCounterResetsAndIncrements| only
265 : bool AddResetOrIncrement(nsIFrame *aFrame, PRInt32 aIndex,
266 : const nsStyleCounterData *aCounterData,
267 : nsCounterNode::Type aType);
268 :
269 : nsClassHashtable<nsStringHashKey, nsCounterList> mNames;
270 : };
271 :
272 : #endif /* nsCounterManager_h_ */
|