2005-04-01 23:07:00 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
// vim:cindent:ai:sw=4:ts=4:et:
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2006-03-30 05:56:38 +00:00
|
|
|
|
|
|
|
/* implementation of CSS counters (for numbering things) */
|
|
|
|
|
2005-04-01 23:07:00 +00:00
|
|
|
#ifndef nsCounterManager_h_
|
|
|
|
#define nsCounterManager_h_
|
|
|
|
|
|
|
|
#include "nsGenConList.h"
|
|
|
|
#include "nsAutoPtr.h"
|
|
|
|
#include "nsClassHashtable.h"
|
2012-10-26 13:32:10 +00:00
|
|
|
#include "mozilla/Likely.h"
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
class nsCounterList;
|
|
|
|
struct nsCounterUseNode;
|
|
|
|
struct nsCounterChangeNode;
|
|
|
|
|
|
|
|
struct nsCounterNode : public nsGenConNode {
|
|
|
|
enum Type {
|
|
|
|
RESET, // a "counter number" pair in 'counter-reset'
|
|
|
|
INCREMENT, // a "counter number" pair in 'counter-increment'
|
|
|
|
USE // counter() or counters() in 'content'
|
|
|
|
};
|
|
|
|
|
|
|
|
Type mType;
|
|
|
|
|
|
|
|
// Counter value after this node
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mValueAfter;
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
// mScopeStart points to the node (usually a RESET, but not in the
|
|
|
|
// case of an implied 'counter-reset') that created the scope for
|
|
|
|
// this element (for a RESET, its outer scope, i.e., the one it is
|
|
|
|
// inside rather than the one it creates).
|
|
|
|
|
|
|
|
// May be null for all types, but only when mScopePrev is also null.
|
|
|
|
// Being null for a non-RESET means that it is an implied
|
|
|
|
// 'counter-reset'. Being null for a RESET means it has no outer
|
|
|
|
// scope.
|
|
|
|
nsCounterNode *mScopeStart;
|
|
|
|
|
|
|
|
// mScopePrev points to the previous node that is in the same scope,
|
|
|
|
// or for a RESET, the previous node in the scope outside of the
|
|
|
|
// reset.
|
|
|
|
|
|
|
|
// May be null for all types, but only when mScopeStart is also
|
|
|
|
// null. Following the mScopePrev links will eventually lead to
|
|
|
|
// mScopeStart. Being null for a non-RESET means that it is an
|
|
|
|
// implied 'counter-reset'. Being null for a RESET means it has no
|
|
|
|
// outer scope.
|
|
|
|
nsCounterNode *mScopePrev;
|
|
|
|
|
|
|
|
inline nsCounterUseNode* UseNode();
|
|
|
|
inline nsCounterChangeNode* ChangeNode();
|
|
|
|
|
|
|
|
// For RESET and INCREMENT nodes, aPseudoFrame need not be a
|
|
|
|
// pseudo-element, and aContentIndex represents the index within the
|
|
|
|
// 'counter-reset' or 'counter-increment' property instead of within
|
|
|
|
// the 'content' property but offset to ensure that (reset,
|
|
|
|
// increment, use) sort in that order. (This slight weirdness
|
|
|
|
// allows sharing a lot of code with 'quotes'.)
|
2012-08-22 15:56:38 +00:00
|
|
|
nsCounterNode(int32_t aContentIndex, Type aType)
|
2008-08-18 01:16:42 +00:00
|
|
|
: nsGenConNode(aContentIndex)
|
2005-04-01 23:07:00 +00:00
|
|
|
, mType(aType)
|
|
|
|
, mValueAfter(0)
|
2012-07-30 14:20:58 +00:00
|
|
|
, mScopeStart(nullptr)
|
|
|
|
, mScopePrev(nullptr)
|
2005-04-01 23:07:00 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// to avoid virtual function calls in the common case
|
|
|
|
inline void Calc(nsCounterList* aList);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nsCounterUseNode : public nsCounterNode {
|
|
|
|
// The same structure passed through the style system: an array
|
|
|
|
// containing the values in the counter() or counters() in the order
|
|
|
|
// given in the CSS spec.
|
|
|
|
nsRefPtr<nsCSSValue::Array> mCounterStyle;
|
|
|
|
|
|
|
|
// false for counter(), true for counters()
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mAllCounters;
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
// args go directly to member variables here and of nsGenConNode
|
2008-08-18 01:16:42 +00:00
|
|
|
nsCounterUseNode(nsCSSValue::Array* aCounterStyle,
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t aContentIndex, bool aAllCounters)
|
2008-08-18 01:16:42 +00:00
|
|
|
: nsCounterNode(aContentIndex, USE)
|
2005-04-01 23:07:00 +00:00
|
|
|
, mCounterStyle(aCounterStyle)
|
|
|
|
, mAllCounters(aAllCounters)
|
|
|
|
{
|
2012-09-28 06:57:33 +00:00
|
|
|
NS_ASSERTION(aContentIndex <= INT32_MAX, "out of range");
|
2005-04-01 23:07:00 +00:00
|
|
|
}
|
2008-08-18 01:16:42 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
virtual bool InitTextFrame(nsGenConList* aList,
|
2008-08-18 01:16:42 +00:00
|
|
|
nsIFrame* aPseudoFrame, nsIFrame* aTextFrame);
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
// assign the correct |mValueAfter| value to a node that has been inserted
|
|
|
|
// Should be called immediately after calling |Insert|.
|
|
|
|
void Calc(nsCounterList* aList);
|
|
|
|
|
|
|
|
// The text that should be displayed for this counter.
|
|
|
|
void GetText(nsString& aResult);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nsCounterChangeNode : public nsCounterNode {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t mChangeValue; // the numeric value of the increment or reset
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
// |aPseudoFrame| is not necessarily a pseudo-element's frame, but
|
|
|
|
// since it is for every other subclass of nsGenConNode, we follow
|
|
|
|
// the naming convention here.
|
|
|
|
// |aPropIndex| is the index of the value within the list in the
|
|
|
|
// 'counter-increment' or 'counter-reset' property.
|
|
|
|
nsCounterChangeNode(nsIFrame* aPseudoFrame,
|
|
|
|
nsCounterNode::Type aChangeType,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aChangeValue,
|
|
|
|
int32_t aPropIndex)
|
2008-08-18 01:16:42 +00:00
|
|
|
: nsCounterNode(// Fake a content index for resets and increments
|
2005-04-01 23:07:00 +00:00
|
|
|
// that comes before all the real content, with
|
|
|
|
// the resets first, in order, and then the increments.
|
|
|
|
aPropIndex + (aChangeType == RESET
|
2012-09-28 06:57:33 +00:00
|
|
|
? (INT32_MIN)
|
|
|
|
: (INT32_MIN / 2)),
|
2005-04-01 23:07:00 +00:00
|
|
|
aChangeType)
|
|
|
|
, mChangeValue(aChangeValue)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aPropIndex >= 0, "out of range");
|
|
|
|
NS_ASSERTION(aChangeType == INCREMENT || aChangeType == RESET,
|
|
|
|
"bad type");
|
2008-08-18 01:16:42 +00:00
|
|
|
mPseudoFrame = aPseudoFrame;
|
|
|
|
CheckFrameAssertions();
|
2005-04-01 23:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assign the correct |mValueAfter| value to a node that has been inserted
|
|
|
|
// Should be called immediately after calling |Insert|.
|
|
|
|
void Calc(nsCounterList* aList);
|
|
|
|
};
|
|
|
|
|
|
|
|
inline nsCounterUseNode* nsCounterNode::UseNode()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mType == USE, "wrong type");
|
2007-07-08 07:08:04 +00:00
|
|
|
return static_cast<nsCounterUseNode*>(this);
|
2005-04-01 23:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsCounterChangeNode* nsCounterNode::ChangeNode()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mType == INCREMENT || mType == RESET, "wrong type");
|
2007-07-08 07:08:04 +00:00
|
|
|
return static_cast<nsCounterChangeNode*>(this);
|
2005-04-01 23:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline void nsCounterNode::Calc(nsCounterList* aList)
|
|
|
|
{
|
|
|
|
if (mType == USE)
|
|
|
|
UseNode()->Calc(aList);
|
|
|
|
else
|
|
|
|
ChangeNode()->Calc(aList);
|
|
|
|
}
|
|
|
|
|
|
|
|
class nsCounterList : public nsGenConList {
|
|
|
|
public:
|
2007-01-23 23:45:54 +00:00
|
|
|
nsCounterList() : nsGenConList(),
|
2011-10-17 14:59:28 +00:00
|
|
|
mDirty(false)
|
2007-01-23 23:45:54 +00:00
|
|
|
{}
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
void Insert(nsCounterNode* aNode) {
|
|
|
|
nsGenConList::Insert(aNode);
|
2007-01-20 01:18:57 +00:00
|
|
|
// Don't SetScope if we're dirty -- we'll reset all the scopes anyway,
|
|
|
|
// and we can't usefully compute scopes right now.
|
2012-10-26 13:32:10 +00:00
|
|
|
if (MOZ_LIKELY(!IsDirty())) {
|
2007-01-20 01:18:57 +00:00
|
|
|
SetScope(aNode);
|
|
|
|
}
|
2005-04-01 23:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCounterNode* First() {
|
2007-07-08 07:08:04 +00:00
|
|
|
return static_cast<nsCounterNode*>(mFirstNode);
|
2005-04-01 23:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsCounterNode* Next(nsCounterNode* aNode) {
|
2007-07-08 07:08:04 +00:00
|
|
|
return static_cast<nsCounterNode*>(nsGenConList::Next(aNode));
|
2005-04-01 23:07:00 +00:00
|
|
|
}
|
|
|
|
static nsCounterNode* Prev(nsCounterNode* aNode) {
|
2007-07-08 07:08:04 +00:00
|
|
|
return static_cast<nsCounterNode*>(nsGenConList::Prev(aNode));
|
2005-04-01 23:07:00 +00:00
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
static int32_t ValueBefore(nsCounterNode* aNode) {
|
2005-04-01 23:07:00 +00:00
|
|
|
return aNode->mScopePrev ? aNode->mScopePrev->mValueAfter : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correctly set |aNode->mScopeStart| and |aNode->mScopePrev|
|
|
|
|
void SetScope(nsCounterNode *aNode);
|
|
|
|
|
|
|
|
// Recalculate |mScopeStart|, |mScopePrev|, and |mValueAfter| for
|
|
|
|
// all nodes and update text in text content nodes.
|
|
|
|
void RecalcAll();
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool IsDirty() { return mDirty; }
|
2011-10-17 14:59:28 +00:00
|
|
|
void SetDirty() { mDirty = true; }
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
private:
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mDirty;
|
2005-04-01 23:07:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The counter manager maintains an |nsCounterList| for each named
|
|
|
|
* counter to keep track of all scopes with that name.
|
|
|
|
*/
|
|
|
|
class nsCounterManager {
|
|
|
|
public:
|
|
|
|
nsCounterManager();
|
|
|
|
// Returns true if dirty
|
2011-09-29 06:19:26 +00:00
|
|
|
bool AddCounterResetsAndIncrements(nsIFrame *aFrame);
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
// Gets the appropriate counter list, creating it if necessary.
|
|
|
|
// Returns null only on out-of-memory.
|
|
|
|
nsCounterList* CounterListFor(const nsSubstring& aCounterName);
|
|
|
|
|
|
|
|
// Clean up data in any dirty counter lists.
|
|
|
|
void RecalcAll();
|
|
|
|
|
|
|
|
// Destroy nodes for the frame in any lists, and return whether any
|
|
|
|
// nodes were destroyed.
|
2011-09-29 06:19:26 +00:00
|
|
|
bool DestroyNodesFor(nsIFrame *aFrame);
|
2005-04-01 23:07:00 +00:00
|
|
|
|
|
|
|
// Clear all data.
|
|
|
|
void Clear() { mNames.Clear(); }
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void Dump();
|
|
|
|
#endif
|
|
|
|
|
2013-02-22 18:13:37 +00:00
|
|
|
static int32_t IncrementCounter(int32_t aOldValue, int32_t aIncrement)
|
|
|
|
{
|
|
|
|
// Addition of unsigned values is defined to be arithmetic
|
|
|
|
// modulo 2^bits (C++ 2011, 3.9.1 [basic.fundamental], clause 4);
|
|
|
|
// addition of signed values is undefined (and clang does
|
|
|
|
// something very strange if we use it here). Likewise integral
|
2013-02-22 18:29:05 +00:00
|
|
|
// conversion from signed to unsigned is also defined as modulo
|
2013-02-22 18:13:37 +00:00
|
|
|
// 2^bits (C++ 2011, 4.7 [conv.integral], clause 2); conversion
|
2013-02-22 18:29:05 +00:00
|
|
|
// from unsigned to signed is however undefined (ibid., clause 3),
|
2013-02-22 18:13:37 +00:00
|
|
|
// but to do what we want we must nonetheless depend on that
|
|
|
|
// small piece of undefined behavior.
|
|
|
|
int32_t newValue = int32_t(uint32_t(aOldValue) + uint32_t(aIncrement));
|
|
|
|
// The CSS Working Group resolved that a counter-increment that
|
|
|
|
// exceeds internal limits should not increment at all.
|
|
|
|
// http://lists.w3.org/Archives/Public/www-style/2013Feb/0392.html
|
|
|
|
// (This means, for example, that if aIncrement is 5, the
|
|
|
|
// counter will get stuck at the largest multiple of 5 less than
|
|
|
|
// the maximum 32-bit integer.)
|
|
|
|
if ((aIncrement > 0) != (newValue > aOldValue)) {
|
|
|
|
newValue = aOldValue;
|
|
|
|
}
|
|
|
|
return newValue;
|
|
|
|
}
|
|
|
|
|
2005-04-01 23:07:00 +00:00
|
|
|
private:
|
|
|
|
// for |AddCounterResetsAndIncrements| only
|
2012-08-22 15:56:38 +00:00
|
|
|
bool AddResetOrIncrement(nsIFrame *aFrame, int32_t aIndex,
|
2005-04-01 23:07:00 +00:00
|
|
|
const nsStyleCounterData *aCounterData,
|
|
|
|
nsCounterNode::Type aType);
|
|
|
|
|
|
|
|
nsClassHashtable<nsStringHashKey, nsCounterList> mNames;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsCounterManager_h_ */
|