2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2005-04-02 03:07:00 +04:00
|
|
|
// vim:cindent:ai:sw=4:ts=4:et:
|
2012-05-21 15:12:37 +04: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 09:56:38 +04:00
|
|
|
|
|
|
|
/* implementation of CSS counters (for numbering things) */
|
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
#ifndef nsCounterManager_h_
|
|
|
|
#define nsCounterManager_h_
|
|
|
|
|
2013-05-14 20:33:23 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2005-04-02 03:07:00 +04:00
|
|
|
#include "nsGenConList.h"
|
|
|
|
#include "nsClassHashtable.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2014-06-12 05:12:00 +04:00
|
|
|
#include "CounterStyleManager.h"
|
2005-04-02 03:07:00 +04: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'
|
2019-04-07 14:45:57 +03:00
|
|
|
SET, // a "counter number" pair in 'counter-set'
|
2005-04-02 03:07:00 +04:00
|
|
|
USE // counter() or counters() in 'content'
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
Type mType;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// Counter value after this node
|
2019-05-21 20:30:42 +03:00
|
|
|
int32_t mValueAfter = 0;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04: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).
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// 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.
|
2019-05-21 20:30:42 +03:00
|
|
|
nsCounterNode* mScopeStart = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// 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.
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// 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.
|
2019-05-21 20:30:42 +03:00
|
|
|
nsCounterNode* mScopePrev = nullptr;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
inline nsCounterUseNode* UseNode();
|
|
|
|
inline nsCounterChangeNode* ChangeNode();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-04-07 14:45:57 +03:00
|
|
|
// For RESET, INCREMENT and SET nodes, aPseudoFrame need not be a
|
2005-04-02 03:07:00 +04:00
|
|
|
// pseudo-element, and aContentIndex represents the index within the
|
2019-04-07 14:45:57 +03:00
|
|
|
// 'counter-reset', 'counter-increment' or 'counter-set' property
|
2019-03-25 01:13:52 +03:00
|
|
|
// instead of within the 'content' property but offset to ensure
|
2019-04-07 14:45:57 +03:00
|
|
|
// that (reset, increment, set, use) sort in that order.
|
2019-03-25 01:13:52 +03:00
|
|
|
// (This slight weirdness allows sharing a lot of code with 'quotes'.)
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCounterNode(int32_t aContentIndex, Type aType)
|
2019-05-21 20:30:42 +03:00
|
|
|
: nsGenConNode(aContentIndex), mType(aType) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// to avoid virtual function calls in the common case
|
2019-05-21 20:30:42 +03:00
|
|
|
inline void Calc(nsCounterList* aList, bool aNotify);
|
2019-03-25 01:13:52 +03:00
|
|
|
|
|
|
|
// Is this a <ol reversed> RESET node?
|
|
|
|
inline bool IsContentBasedReset();
|
2005-04-02 03:07:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct nsCounterUseNode : public nsCounterNode {
|
2017-05-13 14:42:23 +03:00
|
|
|
mozilla::CounterStylePtr mCounterStyle;
|
2017-05-22 15:51:20 +03:00
|
|
|
nsString mSeparator;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// false for counter(), true for counters()
|
2019-05-21 20:30:42 +03:00
|
|
|
bool mAllCounters = false;
|
|
|
|
|
|
|
|
bool mForLegacyBullet = false;
|
|
|
|
|
|
|
|
enum ForLegacyBullet { ForLegacyBullet };
|
|
|
|
explicit nsCounterUseNode(enum ForLegacyBullet)
|
|
|
|
: nsCounterNode(0, USE), mForLegacyBullet(true) {
|
|
|
|
mCounterStyle = nsGkAtoms::list_item;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// args go directly to member variables here and of nsGenConNode
|
2020-01-05 16:10:39 +03:00
|
|
|
nsCounterUseNode(mozilla::CounterStylePtr aCounterStyle, nsString aSeparator,
|
|
|
|
uint32_t aContentIndex, bool aAllCounters)
|
2008-08-18 05:16:42 +04:00
|
|
|
: nsCounterNode(aContentIndex, USE),
|
2020-01-05 16:10:39 +03:00
|
|
|
mCounterStyle(std::move(aCounterStyle)),
|
|
|
|
mSeparator(std::move(aSeparator)),
|
2005-04-02 03:07:00 +04:00
|
|
|
mAllCounters(aAllCounters) {
|
2012-09-28 10:57:33 +04:00
|
|
|
NS_ASSERTION(aContentIndex <= INT32_MAX, "out of range");
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
virtual bool InitTextFrame(nsGenConList* aList, nsIFrame* aPseudoFrame,
|
2015-03-21 19:28:04 +03:00
|
|
|
nsIFrame* aTextFrame) override;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2019-05-21 20:30:42 +03:00
|
|
|
bool InitBullet(nsGenConList* aList, nsIFrame* aBulletFrame);
|
|
|
|
|
|
|
|
// assign the correct |mValueAfter| value to a node that has been inserted,
|
|
|
|
// and update the value of the text node, notifying if `aNotify` is true.
|
2005-04-02 03:07:00 +04:00
|
|
|
// Should be called immediately after calling |Insert|.
|
2019-05-21 20:30:42 +03:00
|
|
|
void Calc(nsCounterList* aList, bool aNotify);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// The text that should be displayed for this counter.
|
|
|
|
void GetText(nsString& aResult);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct nsCounterChangeNode : public nsCounterNode {
|
2019-03-25 01:13:52 +03:00
|
|
|
int32_t mChangeValue; // the numeric value of the increment, set or reset
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04: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
|
2019-04-07 14:45:57 +03:00
|
|
|
// 'counter-increment', 'counter-reset' or 'counter-set' property.
|
2005-04-02 03:07:00 +04:00
|
|
|
nsCounterChangeNode(nsIFrame* aPseudoFrame, nsCounterNode::Type aChangeType,
|
2019-05-21 20:30:42 +03:00
|
|
|
int32_t aChangeValue, int32_t aPropIndex)
|
2019-04-07 14:45:57 +03:00
|
|
|
: nsCounterNode( // Fake a content index for resets, increments and sets
|
2005-04-02 03:07:00 +04:00
|
|
|
// that comes before all the real content, with
|
2019-04-07 14:45:57 +03:00
|
|
|
// the resets first, in order, and then the increments
|
|
|
|
// and then the sets.
|
2019-03-25 01:13:52 +03:00
|
|
|
aPropIndex + (aChangeType == RESET ? (INT32_MIN)
|
2019-04-07 14:45:57 +03:00
|
|
|
: (aChangeType == INCREMENT
|
|
|
|
? ((INT32_MIN / 3) * 2)
|
|
|
|
: INT32_MIN / 3)),
|
2005-04-02 03:07:00 +04:00
|
|
|
aChangeType),
|
|
|
|
mChangeValue(aChangeValue) {
|
|
|
|
NS_ASSERTION(aPropIndex >= 0, "out of range");
|
2019-03-25 01:13:52 +03:00
|
|
|
NS_ASSERTION(
|
|
|
|
aChangeType == INCREMENT || aChangeType == SET || aChangeType == RESET,
|
|
|
|
"bad type");
|
2008-08-18 05:16:42 +04:00
|
|
|
mPseudoFrame = aPseudoFrame;
|
|
|
|
CheckFrameAssertions();
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04: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 11:08:04 +04:00
|
|
|
return static_cast<nsCounterUseNode*>(this);
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
inline nsCounterChangeNode* nsCounterNode::ChangeNode() {
|
2019-03-25 01:13:52 +03:00
|
|
|
MOZ_ASSERT(mType == INCREMENT || mType == SET || mType == RESET);
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<nsCounterChangeNode*>(this);
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2019-05-21 20:30:42 +03:00
|
|
|
inline void nsCounterNode::Calc(nsCounterList* aList, bool aNotify) {
|
2005-04-02 03:07:00 +04:00
|
|
|
if (mType == USE)
|
2019-05-21 20:30:42 +03:00
|
|
|
UseNode()->Calc(aList, aNotify);
|
2005-04-02 03:07:00 +04:00
|
|
|
else
|
|
|
|
ChangeNode()->Calc(aList);
|
|
|
|
}
|
|
|
|
|
2019-03-25 01:13:52 +03:00
|
|
|
inline bool nsCounterNode::IsContentBasedReset() {
|
|
|
|
return mType == RESET &&
|
|
|
|
ChangeNode()->mChangeValue == std::numeric_limits<int32_t>::min();
|
|
|
|
}
|
2019-03-31 18:12:55 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
class nsCounterList : public nsGenConList {
|
|
|
|
public:
|
2007-01-24 02:45:54 +03:00
|
|
|
nsCounterList() : nsGenConList(), mDirty(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
void Insert(nsCounterNode* aNode) {
|
|
|
|
nsGenConList::Insert(aNode);
|
2007-01-20 04:18:57 +03: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 17:32:10 +04:00
|
|
|
if (MOZ_LIKELY(!IsDirty())) {
|
2007-01-20 04:18:57 +03:00
|
|
|
SetScope(aNode);
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
|
|
|
|
nsCounterNode* First() {
|
2016-10-27 13:07:52 +03:00
|
|
|
return static_cast<nsCounterNode*>(mList.getFirst());
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static nsCounterNode* Next(nsCounterNode* aNode) {
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<nsCounterNode*>(nsGenConList::Next(aNode));
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
static nsCounterNode* Prev(nsCounterNode* aNode) {
|
2007-07-08 11:08:04 +04:00
|
|
|
return static_cast<nsCounterNode*>(nsGenConList::Prev(aNode));
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
static int32_t ValueBefore(nsCounterNode* aNode) {
|
2005-04-02 03:07:00 +04:00
|
|
|
return aNode->mScopePrev ? aNode->mScopePrev->mValueAfter : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correctly set |aNode->mScopeStart| and |aNode->mScopePrev|
|
|
|
|
void SetScope(nsCounterNode* aNode);
|
2014-09-12 09:24:00 +04:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// Recalculate |mScopeStart|, |mScopePrev|, and |mValueAfter| for
|
|
|
|
// all nodes and update text in text content nodes.
|
|
|
|
void RecalcAll();
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsDirty() { return mDirty; }
|
2011-10-17 18:59:28 +04:00
|
|
|
void SetDirty() { mDirty = true; }
|
2005-04-02 03:07:00 +04:00
|
|
|
|
|
|
|
private:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mDirty;
|
2005-04-02 03:07:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The counter manager maintains an |nsCounterList| for each named
|
|
|
|
* counter to keep track of all scopes with that name.
|
|
|
|
*/
|
|
|
|
class nsCounterManager {
|
|
|
|
public:
|
|
|
|
// Returns true if dirty
|
2019-03-25 01:13:52 +03:00
|
|
|
bool AddCounterChanges(nsIFrame* aFrame);
|
2005-04-02 03:07:00 +04:00
|
|
|
|
|
|
|
// Gets the appropriate counter list, creating it if necessary.
|
2014-09-12 09:25:00 +04:00
|
|
|
// Guaranteed to return non-null. (Uses an infallible hashtable API.)
|
2019-04-15 23:11:45 +03:00
|
|
|
nsCounterList* CounterListFor(nsAtom* aCounterName);
|
2005-04-02 03:07:00 +04:00
|
|
|
|
|
|
|
// Clean up data in any dirty counter lists.
|
|
|
|
void RecalcAll();
|
|
|
|
|
2017-05-23 03:08:50 +03:00
|
|
|
// Set all counter lists dirty
|
|
|
|
void SetAllDirty();
|
2014-06-12 05:12:00 +04:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
// Destroy nodes for the frame in any lists, and return whether any
|
|
|
|
// nodes were destroyed.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool DestroyNodesFor(nsIFrame* aFrame);
|
2005-04-02 03:07:00 +04:00
|
|
|
|
|
|
|
// Clear all data.
|
|
|
|
void Clear() { mNames.Clear(); }
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void Dump();
|
|
|
|
#endif
|
|
|
|
|
2013-02-22 22:13:37 +04: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 22:29:05 +04:00
|
|
|
// conversion from signed to unsigned is also defined as modulo
|
2013-02-22 22:13:37 +04:00
|
|
|
// 2^bits (C++ 2011, 4.7 [conv.integral], clause 2); conversion
|
2013-02-22 22:29:05 +04:00
|
|
|
// from unsigned to signed is however undefined (ibid., clause 3),
|
2013-02-22 22:13:37 +04: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;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2013-02-22 22:13:37 +04:00
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
private:
|
2019-04-15 23:11:45 +03:00
|
|
|
nsClassHashtable<nsRefPtrHashKey<nsAtom>, nsCounterList> mNames;
|
2005-04-02 03:07:00 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* nsCounterManager_h_ */
|