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
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/
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
16 * The Original Code is nsCounterManager.
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.
23 * L. David Baron <dbaron@dbaron.org> (original author)
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.
37 * ***** END LICENSE BLOCK ***** */
39 /* implementation of CSS counters (for numbering things) */
41 #ifndef nsCounterManager_h_
42 #define nsCounterManager_h_
44 #include "nsGenConList.h"
45 #include "nsAutoPtr.h"
46 #include "nsClassHashtable.h"
49 struct nsCounterUseNode
;
50 struct nsCounterChangeNode
;
52 struct nsCounterNode
: public nsGenConNode
{
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'
61 // Counter value after this node
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).
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
73 nsCounterNode
*mScopeStart
;
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
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
84 nsCounterNode
*mScopePrev
;
86 inline nsCounterUseNode
* UseNode();
87 inline nsCounterChangeNode
* ChangeNode();
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 nsCounterNode(PRInt32 aContentIndex
, Type aType
)
96 : nsGenConNode(aContentIndex
)
104 // to avoid virtual function calls in the common case
105 inline void Calc(nsCounterList
* aList
);
108 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
;
114 // false for counter(), true for counters()
117 // args go directly to member variables here and of nsGenConNode
118 nsCounterUseNode(nsCSSValue::Array
* aCounterStyle
,
119 PRUint32 aContentIndex
, PRBool aAllCounters
)
120 : nsCounterNode(aContentIndex
, USE
)
121 , mCounterStyle(aCounterStyle
)
122 , mAllCounters(aAllCounters
)
124 NS_ASSERTION(aContentIndex
>= 0, "out of range");
127 virtual PRBool
InitTextFrame(nsGenConList
* aList
,
128 nsIFrame
* aPseudoFrame
, nsIFrame
* aTextFrame
);
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
);
134 // The text that should be displayed for this counter.
135 void GetText(nsString
& aResult
);
138 struct nsCounterChangeNode
: public nsCounterNode
{
139 PRInt32 mChangeValue
; // the numeric value of the increment or reset
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 nsCounterChangeNode(nsIFrame
* aPseudoFrame
,
147 nsCounterNode::Type aChangeType
,
148 PRInt32 aChangeValue
,
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
155 : (PR_INT32_MIN
/ 2)),
157 , mChangeValue(aChangeValue
)
159 NS_ASSERTION(aPropIndex
>= 0, "out of range");
160 NS_ASSERTION(aChangeType
== INCREMENT
|| aChangeType
== RESET
,
162 mPseudoFrame
= aPseudoFrame
;
163 CheckFrameAssertions();
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
);
171 inline nsCounterUseNode
* nsCounterNode::UseNode()
173 NS_ASSERTION(mType
== USE
, "wrong type");
174 return static_cast<nsCounterUseNode
*>(this);
177 inline nsCounterChangeNode
* nsCounterNode::ChangeNode()
179 NS_ASSERTION(mType
== INCREMENT
|| mType
== RESET
, "wrong type");
180 return static_cast<nsCounterChangeNode
*>(this);
183 inline void nsCounterNode::Calc(nsCounterList
* aList
)
186 UseNode()->Calc(aList
);
188 ChangeNode()->Calc(aList
);
191 class nsCounterList
: public nsGenConList
{
193 nsCounterList() : nsGenConList(),
197 void Insert(nsCounterNode
* aNode
) {
198 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 if (NS_LIKELY(!IsDirty())) {
206 nsCounterNode
* First() {
207 return static_cast<nsCounterNode
*>(mFirstNode
);
210 static nsCounterNode
* Next(nsCounterNode
* aNode
) {
211 return static_cast<nsCounterNode
*>(nsGenConList::Next(aNode
));
213 static nsCounterNode
* Prev(nsCounterNode
* aNode
) {
214 return static_cast<nsCounterNode
*>(nsGenConList::Prev(aNode
));
217 static PRInt32
ValueBefore(nsCounterNode
* aNode
) {
218 return aNode
->mScopePrev
? aNode
->mScopePrev
->mValueAfter
: 0;
221 // Correctly set |aNode->mScopeStart| and |aNode->mScopePrev|
222 void SetScope(nsCounterNode
*aNode
);
224 // Recalculate |mScopeStart|, |mScopePrev|, and |mValueAfter| for
225 // all nodes and update text in text content nodes.
228 PRBool
IsDirty() { return mDirty
; }
229 void SetDirty() { mDirty
= PR_TRUE
; }
236 * The counter manager maintains an |nsCounterList| for each named
237 * counter to keep track of all scopes with that name.
239 class nsCounterManager
{
242 // Returns true if dirty
243 PRBool
AddCounterResetsAndIncrements(nsIFrame
*aFrame
);
245 // Gets the appropriate counter list, creating it if necessary.
246 // Returns null only on out-of-memory.
247 nsCounterList
* CounterListFor(const nsSubstring
& aCounterName
);
249 // Clean up data in any dirty counter lists.
252 // Destroy nodes for the frame in any lists, and return whether any
253 // nodes were destroyed.
254 PRBool
DestroyNodesFor(nsIFrame
*aFrame
);
257 void Clear() { mNames
.Clear(); }
264 // for |AddCounterResetsAndIncrements| only
265 PRBool
AddResetOrIncrement(nsIFrame
*aFrame
, PRInt32 aIndex
,
266 const nsStyleCounterData
*aCounterData
,
267 nsCounterNode::Type aType
);
269 nsClassHashtable
<nsStringHashKey
, nsCounterList
> mNames
;
272 #endif /* nsCounterManager_h_ */