1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/* implementation of CSS counters (for numbering things) */
#include "nsCounterManager.h"
#include "mozilla/Likely.h"
#include "mozilla/IntegerRange.h"
#include "mozilla/PresShell.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/WritingModes.h"
#include "nsBulletFrame.h" // legacy location for list style type to text code
#include "nsContentUtils.h"
#include "nsIContent.h"
#include "nsTArray.h"
#include "mozilla/dom/Text.h"
using namespace mozilla;
bool nsCounterUseNode::InitTextFrame(nsGenConList* aList,
nsIFrame* aPseudoFrame,
nsIFrame* aTextFrame) {
nsCounterNode::InitTextFrame(aList, aPseudoFrame, aTextFrame);
auto* counterList = static_cast<nsCounterList*>(aList);
counterList->Insert(this);
aPseudoFrame->AddStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE);
// If the list is already dirty, or the node is not at the end, just start
// with an empty string for now and when we recalculate the list we'll change
// the value to the right one.
if (counterList->IsDirty()) {
return false;
}
if (!counterList->IsLast(this)) {
counterList->SetDirty();
return true;
}
Calc(counterList, /* aNotify = */ false);
return false;
}
bool nsCounterUseNode::InitBullet(nsGenConList* aList, nsIFrame* aBullet) {
MOZ_ASSERT(aBullet->IsBulletFrame());
MOZ_ASSERT(aBullet->Style()->GetPseudoType() == PseudoStyleType::marker);
MOZ_ASSERT(mForLegacyBullet);
return InitTextFrame(aList, aBullet, nullptr);
}
// assign the correct |mValueAfter| value to a node that has been inserted
// Should be called immediately after calling |Insert|.
void nsCounterUseNode::Calc(nsCounterList* aList, bool aNotify) {
NS_ASSERTION(!aList->IsDirty(), "Why are we calculating with a dirty list?");
mValueAfter = nsCounterList::ValueBefore(this);
if (mText) {
nsAutoString contentString;
GetText(contentString);
mText->SetText(contentString, aNotify);
} else if (mForLegacyBullet) {
MOZ_ASSERT_IF(mPseudoFrame, mPseudoFrame->IsBulletFrame());
if (nsBulletFrame* f = do_QueryFrame(mPseudoFrame)) {
f->SetOrdinal(mValueAfter, aNotify);
}
}
}
// assign the correct |mValueAfter| value to a node that has been inserted
// Should be called immediately after calling |Insert|.
void nsCounterChangeNode::Calc(nsCounterList* aList) {
NS_ASSERTION(!aList->IsDirty(), "Why are we calculating with a dirty list?");
if (IsContentBasedReset()) {
// RecalcAll takes care of this case.
} else if (mType == RESET || mType == SET) {
mValueAfter = mChangeValue;
} else {
NS_ASSERTION(mType == INCREMENT, "invalid type");
mValueAfter = nsCounterManager::IncrementCounter(
nsCounterList::ValueBefore(this), mChangeValue);
}
}
// The text that should be displayed for this counter.
void nsCounterUseNode::GetText(nsString& aResult) {
aResult.Truncate();
AutoTArray<nsCounterNode*, 8> stack;
stack.AppendElement(static_cast<nsCounterNode*>(this));
if (mAllCounters && mScopeStart) {
for (nsCounterNode* n = mScopeStart; n->mScopePrev; n = n->mScopeStart) {
stack.AppendElement(n->mScopePrev);
}
}
WritingMode wm = mPseudoFrame->GetWritingMode();
CounterStyle* style =
mPseudoFrame->PresContext()->CounterStyleManager()->ResolveCounterStyle(
mCounterStyle);
for (uint32_t i = stack.Length() - 1;; --i) {
nsCounterNode* n = stack[i];
nsAutoString text;
bool isTextRTL;
style->GetCounterText(n->mValueAfter, wm, text, isTextRTL);
aResult.Append(text);
if (i == 0) {
break;
}
aResult.Append(mSeparator);
}
}
void nsCounterList::SetScope(nsCounterNode* aNode) {
// This function is responsible for setting |mScopeStart| and
// |mScopePrev| (whose purpose is described in nsCounterManager.h).
// We do this by starting from the node immediately preceding
// |aNode| in content tree order, which is reasonably likely to be
// the previous element in our scope (or, for a reset, the previous
// element in the containing scope, which is what we want). If
// we're not in the same scope that it is, then it's too deep in the
// frame tree, so we walk up parent scopes until we find something
// appropriate.
if (aNode == First()) {
aNode->mScopeStart = nullptr;
aNode->mScopePrev = nullptr;
return;
}
// If there exist an explicit RESET scope created by an ancestor or
// the element itself, then we use that scope.
// Otherwise, fall through to consider scopes created by siblings (and
// their descendants) in reverse document order.
if (aNode->mType != nsCounterNode::USE &&
StaticPrefs::layout_css_counter_ancestor_scope_enabled()) {
nsIContent* const counterNode = aNode->mPseudoFrame->GetContent();
nsCounterNode* lastPrev = nullptr;
for (nsCounterNode* prev = Prev(aNode); prev; prev = prev->mScopePrev) {
if (prev->mType == nsCounterNode::RESET) {
if (aNode->mPseudoFrame == prev->mPseudoFrame) {
break;
}
// FIXME(bug 1477524): should use flattened tree here:
nsIContent* resetNode = prev->mPseudoFrame->GetContent();
if (counterNode->IsInclusiveDescendantOf(resetNode)) {
aNode->mScopeStart = prev;
aNode->mScopePrev = lastPrev ? lastPrev : prev;
return;
}
lastPrev = prev->mScopePrev;
} else if (!lastPrev) {
lastPrev = prev;
}
}
}
// Get the content node for aNode's rendering object's *parent*,
// since scope includes siblings, so we want a descendant check on
// parents.
nsIContent* nodeContent = aNode->mPseudoFrame->GetContent()->GetParent();
for (nsCounterNode *prev = Prev(aNode), *start; prev;
prev = start->mScopePrev) {
// If |prev| starts a scope (because it's a real or implied
// reset), we want it as the scope start rather than the start
// of its enclosing scope. Otherwise, there's no enclosing
// scope, so the next thing in prev's scope shares its scope
// start.
start = (prev->mType == nsCounterNode::RESET || !prev->mScopeStart)
? prev
: prev->mScopeStart;
// |startContent| is analogous to |nodeContent| (see above).
nsIContent* startContent = start->mPseudoFrame->GetContent()->GetParent();
NS_ASSERTION(nodeContent || !startContent,
"null check on startContent should be sufficient to "
"null check nodeContent as well, since if nodeContent "
"is for the root, startContent (which is before it) "
"must be too");
// A reset's outer scope can't be a scope created by a sibling.
if (!(aNode->mType == nsCounterNode::RESET &&
nodeContent == startContent) &&
// everything is inside the root (except the case above,
// a second reset on the root)
// FIXME(bug 1477524): should use flattened tree here:
(!startContent || nodeContent->IsInclusiveDescendantOf(startContent))) {
aNode->mScopeStart = start;
aNode->mScopePrev = prev;
return;
}
}
aNode->mScopeStart = nullptr;
aNode->mScopePrev = nullptr;
}
void nsCounterList::RecalcAll() {
mDirty = false;
// Setup the scope and calculate the default start value for <ol reversed>.
for (nsCounterNode* node = First(); node; node = Next(node)) {
SetScope(node);
if (node->IsContentBasedReset()) {
node->mValueAfter = 1;
} else if (node->mType == nsCounterChangeNode::INCREMENT &&
node->mScopeStart && node->mScopeStart->IsContentBasedReset() &&
node->mPseudoFrame->StyleDisplay()->IsListItem()) {
++node->mScopeStart->mValueAfter;
}
}
for (nsCounterNode* node = First(); node; node = Next(node)) {
node->Calc(this, /* aNotify = */ true);
}
}
static bool AddCounterChangeNode(nsCounterManager& aManager, nsIFrame* aFrame,
int32_t aIndex,
const nsStyleContent::CounterPair& aPair,
nsCounterNode::Type aType) {
auto* node = new nsCounterChangeNode(aFrame, aType, aPair.value, aIndex);
nsCounterList* counterList = aManager.CounterListFor(aPair.name.AsAtom());
counterList->Insert(node);
if (!counterList->IsLast(node)) {
// Tell the caller it's responsible for recalculating the entire
// list.
counterList->SetDirty();
return true;
}
// Don't call Calc() if the list is already dirty -- it'll be recalculated
// anyway, and trying to calculate with a dirty list doesn't work.
if (MOZ_LIKELY(!counterList->IsDirty())) {
node->Calc(counterList);
}
return false;
}
static bool HasCounters(const nsStyleContent& aStyle) {
return !aStyle.mCounterIncrement.IsEmpty() ||
!aStyle.mCounterReset.IsEmpty() || !aStyle.mCounterSet.IsEmpty();
}
bool nsCounterManager::AddCounterChanges(nsIFrame* aFrame) {
// For elements with 'display:list-item' we add a default
// 'counter-increment:list-item' unless 'counter-increment' already has a
// value for 'list-item'.
//
// https://drafts.csswg.org/css-lists-3/#declaring-a-list-item
//
// We inherit `display` for some anonymous boxes, but we don't want them to
// increment the list-item counter.
const bool requiresListItemIncrement =
aFrame->StyleDisplay()->IsListItem() && !aFrame->Style()->IsAnonBox();
const nsStyleContent* styleContent = aFrame->StyleContent();
if (!requiresListItemIncrement && !HasCounters(*styleContent)) {
MOZ_ASSERT(!aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE));
return false;
}
aFrame->AddStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE);
bool dirty = false;
// Add in order, resets first, so all the comparisons will be optimized
// for addition at the end of the list.
{
int32_t i = 0;
for (const auto& pair : styleContent->mCounterReset.AsSpan()) {
dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
nsCounterChangeNode::RESET);
}
}
bool hasListItemIncrement = false;
{
int32_t i = 0;
for (const auto& pair : styleContent->mCounterIncrement.AsSpan()) {
hasListItemIncrement |= pair.name.AsAtom() == nsGkAtoms::list_item;
dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
nsCounterChangeNode::INCREMENT);
}
}
if (requiresListItemIncrement && !hasListItemIncrement) {
bool reversed =
aFrame->StyleList()->mMozListReversed == StyleMozListReversed::True;
RefPtr<nsAtom> atom = nsGkAtoms::list_item;
auto listItemIncrement = nsStyleContent::CounterPair{
{StyleAtom(atom.forget())}, reversed ? -1 : 1};
dirty |= AddCounterChangeNode(
*this, aFrame, styleContent->mCounterIncrement.Length(),
listItemIncrement, nsCounterChangeNode::INCREMENT);
}
{
int32_t i = 0;
for (const auto& pair : styleContent->mCounterSet.AsSpan()) {
dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
nsCounterChangeNode::SET);
}
}
return dirty;
}
nsCounterList* nsCounterManager::CounterListFor(nsAtom* aCounterName) {
MOZ_ASSERT(aCounterName);
return mNames.LookupForAdd(aCounterName)
.OrInsert([]() { return new nsCounterList(); })
.get();
}
void nsCounterManager::RecalcAll() {
for (auto iter = mNames.Iter(); !iter.Done(); iter.Next()) {
nsCounterList* list = iter.UserData();
if (list->IsDirty()) {
list->RecalcAll();
}
}
}
void nsCounterManager::SetAllDirty() {
for (auto iter = mNames.Iter(); !iter.Done(); iter.Next()) {
iter.UserData()->SetDirty();
}
}
bool nsCounterManager::DestroyNodesFor(nsIFrame* aFrame) {
MOZ_ASSERT(aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE),
"why call me?");
bool destroyedAny = false;
for (auto iter = mNames.Iter(); !iter.Done(); iter.Next()) {
nsCounterList* list = iter.UserData();
if (list->DestroyNodesFor(aFrame)) {
destroyedAny = true;
list->SetDirty();
}
}
return destroyedAny;
}
#ifdef DEBUG
void nsCounterManager::Dump() {
printf("\n\nCounter Manager Lists:\n");
for (auto iter = mNames.Iter(); !iter.Done(); iter.Next()) {
printf("Counter named \"%s\":\n", nsAtomCString(iter.Key()).get());
nsCounterList* list = iter.UserData();
int32_t i = 0;
for (nsCounterNode* node = list->First(); node; node = list->Next(node)) {
const char* types[] = {"RESET", "SET", "INCREMENT", "USE"};
printf(
" Node #%d @%p frame=%p index=%d type=%s valAfter=%d\n"
" scope-start=%p scope-prev=%p",
i++, (void*)node, (void*)node->mPseudoFrame, node->mContentIndex,
types[node->mType], node->mValueAfter, (void*)node->mScopeStart,
(void*)node->mScopePrev);
if (node->mType == nsCounterNode::USE) {
nsAutoString text;
node->UseNode()->GetText(text);
printf(" text=%s", NS_ConvertUTF16toUTF8(text).get());
}
printf("\n");
}
}
printf("\n\n");
}
#endif
|