Backed out 2 changesets (bug 1943998) for causing wd failures @ phases.py CLOSED...
[gecko.git] / layout / base / nsCounterManager.cpp
blob44ed1ae3286de2e664cb05690c29647db261ade5
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* implementation of CSS counters (for numbering things) */
9 #include "nsCounterManager.h"
11 #include "mozilla/AutoRestore.h"
12 #include "mozilla/ContainStyleScopeManager.h"
13 #include "mozilla/IntegerRange.h"
14 #include "mozilla/Likely.h"
15 #include "mozilla/PresShell.h"
16 #include "mozilla/StaticPrefs_layout.h"
17 #include "mozilla/WritingModes.h"
18 #include "mozilla/dom/Element.h"
19 #include "mozilla/dom/Text.h"
20 #include "nsContainerFrame.h"
21 #include "nsContentUtils.h"
22 #include "nsIContent.h"
23 #include "nsIContentInlines.h"
24 #include "nsIFrame.h"
25 #include "nsTArray.h"
27 using namespace mozilla;
29 bool nsCounterUseNode::InitTextFrame(nsGenConList* aList,
30 nsIFrame* aPseudoFrame,
31 nsIFrame* aTextFrame) {
32 nsCounterNode::InitTextFrame(aList, aPseudoFrame, aTextFrame);
34 auto* counterList = static_cast<nsCounterList*>(aList);
35 counterList->Insert(this);
36 aPseudoFrame->AddStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE);
37 // If the list is already dirty, or the node is not at the end, just start
38 // with an empty string for now and when we recalculate the list we'll change
39 // the value to the right one.
40 if (counterList->IsDirty()) {
41 return false;
43 if (!counterList->IsLast(this)) {
44 counterList->SetDirty();
45 return true;
47 Calc(counterList, /* aNotify = */ false);
48 return false;
51 // assign the correct |mValueAfter| value to a node that has been inserted
52 // Should be called immediately after calling |Insert|.
53 void nsCounterUseNode::Calc(nsCounterList* aList, bool aNotify) {
54 NS_ASSERTION(aList->IsRecalculatingAll() || !aList->IsDirty(),
55 "Why are we calculating with a dirty list?");
57 mValueAfter = nsCounterList::ValueBefore(this);
59 if (mText) {
60 nsAutoString contentString;
61 GetText(contentString);
62 mText->SetText(contentString, aNotify);
66 // assign the correct |mValueAfter| value to a node that has been inserted
67 // Should be called immediately after calling |Insert|.
68 void nsCounterChangeNode::Calc(nsCounterList* aList) {
69 NS_ASSERTION(aList->IsRecalculatingAll() || !aList->IsDirty(),
70 "Why are we calculating with a dirty list?");
71 if (IsContentBasedReset()) {
72 // RecalcAll takes care of this case.
73 } else if (mType == RESET || mType == SET) {
74 mValueAfter = mChangeValue;
75 } else {
76 NS_ASSERTION(mType == INCREMENT, "invalid type");
77 mValueAfter = nsCounterManager::IncrementCounter(
78 nsCounterList::ValueBefore(this), mChangeValue);
82 void nsCounterUseNode::GetText(nsString& aResult) {
83 mPseudoFrame->PresContext()
84 ->CounterStyleManager()
85 ->WithCounterStyleNameOrSymbols(mCounterStyle, [&](CounterStyle* aStyle) {
86 GetText(mPseudoFrame->GetWritingMode(), aStyle, aResult);
87 });
90 void nsCounterUseNode::GetText(WritingMode aWM, CounterStyle* aStyle,
91 nsString& aResult) {
92 const bool isBidiRTL = aWM.IsBidiRTL();
93 auto AppendCounterText = [&aResult, isBidiRTL](const nsAutoString& aText,
94 bool aIsRTL) {
95 if (MOZ_LIKELY(isBidiRTL == aIsRTL)) {
96 aResult.Append(aText);
97 } else {
98 // RLM = 0x200f, LRM = 0x200e
99 const char16_t mark = aIsRTL ? 0x200f : 0x200e;
100 aResult.Append(mark);
101 aResult.Append(aText);
102 aResult.Append(mark);
106 if (mForLegacyBullet) {
107 nsAutoString prefix;
108 aStyle->GetPrefix(prefix);
109 aResult.Assign(prefix);
112 AutoTArray<nsCounterNode*, 8> stack;
113 stack.AppendElement(static_cast<nsCounterNode*>(this));
115 if (mAllCounters && mScopeStart) {
116 for (nsCounterNode* n = mScopeStart; n->mScopePrev; n = n->mScopeStart) {
117 stack.AppendElement(n->mScopePrev);
121 for (nsCounterNode* n : Reversed(stack)) {
122 nsAutoString text;
123 bool isTextRTL;
124 aStyle->GetCounterText(n->mValueAfter, aWM, text, isTextRTL);
125 if (!mForLegacyBullet || aStyle->IsBullet()) {
126 aResult.Append(text);
127 } else {
128 AppendCounterText(text, isTextRTL);
130 if (n == this) {
131 break;
133 aResult.Append(mSeparator);
136 if (mForLegacyBullet) {
137 nsAutoString suffix;
138 aStyle->GetSuffix(suffix);
139 aResult.Append(suffix);
143 static const nsIContent* GetParentContentForScope(nsIFrame* frame) {
144 // We do not want elements with `display: contents` to establish scope for
145 // counters. We'd like to do something like
146 // `nsIFrame::GetClosestFlattenedTreeAncestorPrimaryFrame()` above, but this
147 // may be called before the primary frame is set on frames.
148 nsIContent* content = frame->GetContent()->GetFlattenedTreeParent();
149 while (content && content->IsElement() &&
150 content->AsElement()->IsDisplayContents()) {
151 content = content->GetFlattenedTreeParent();
154 return content;
157 bool nsCounterList::IsDirty() const {
158 return mScope->GetScopeManager().CounterDirty(mCounterName);
161 void nsCounterList::SetDirty() {
162 mScope->GetScopeManager().SetCounterDirty(mCounterName);
165 void nsCounterList::SetScope(nsCounterNode* aNode) {
166 // This function is responsible for setting |mScopeStart| and
167 // |mScopePrev| (whose purpose is described in nsCounterManager.h).
168 // We do this by starting from the node immediately preceding
169 // |aNode| in content tree order, which is reasonably likely to be
170 // the previous element in our scope (or, for a reset, the previous
171 // element in the containing scope, which is what we want). If
172 // we're not in the same scope that it is, then it's too deep in the
173 // frame tree, so we walk up parent scopes until we find something
174 // appropriate.
176 auto setNullScopeFor = [](nsCounterNode* aNode) {
177 aNode->mScopeStart = nullptr;
178 aNode->mScopePrev = nullptr;
179 aNode->mCrossesContainStyleBoundaries = false;
180 if (aNode->IsUnitializedIncrementNode()) {
181 aNode->ChangeNode()->mChangeValue = 1;
185 if (aNode == First() && aNode->mType != nsCounterNode::USE) {
186 setNullScopeFor(aNode);
187 return;
190 auto didSetScopeFor = [this](nsCounterNode* aNode) {
191 if (aNode->mType == nsCounterNode::USE) {
192 return;
194 if (aNode->mScopeStart->IsContentBasedReset()) {
195 SetDirty();
197 if (aNode->IsUnitializedIncrementNode()) {
198 aNode->ChangeNode()->mChangeValue =
199 aNode->mScopeStart->IsReversed() ? -1 : 1;
203 // If there exist an explicit RESET scope created by an ancestor or
204 // the element itself, then we use that scope.
205 // Otherwise, fall through to consider scopes created by siblings (and
206 // their descendants) in reverse document order.
207 // Do this only for the list-item counter, while the CSSWG discusses what the
208 // right thing to do here is, see bug 1548753 and
209 // https://github.com/w3c/csswg-drafts/issues/5477.
210 if (mCounterName == nsGkAtoms::list_item &&
211 aNode->mType != nsCounterNode::USE &&
212 StaticPrefs::layout_css_counter_ancestor_scope_enabled()) {
213 for (auto* p = aNode->mPseudoFrame; p; p = p->GetParent()) {
214 // This relies on the fact that a RESET node is always the first
215 // CounterNode for a frame if it has any.
216 auto* counter = GetFirstNodeFor(p);
217 if (!counter || counter->mType != nsCounterNode::RESET) {
218 continue;
220 if (p == aNode->mPseudoFrame) {
221 break;
223 aNode->mScopeStart = counter;
224 aNode->mScopePrev = counter;
225 aNode->mCrossesContainStyleBoundaries = false;
226 for (nsCounterNode* prev = Prev(aNode); prev; prev = prev->mScopePrev) {
227 if (prev->mScopeStart == counter) {
228 aNode->mScopePrev =
229 prev->mType == nsCounterNode::RESET ? prev->mScopePrev : prev;
230 break;
232 if (prev->mType != nsCounterNode::RESET) {
233 prev = prev->mScopeStart;
234 if (!prev) {
235 break;
239 didSetScopeFor(aNode);
240 return;
244 // Get the content node for aNode's rendering object's *parent*,
245 // since scope includes siblings, so we want a descendant check on
246 // parents. Note here that mPseudoFrame is a bit of a misnomer, as it
247 // might not be a pseudo element at all, but a normal element that
248 // happens to increment a counter. We want to respect the flat tree
249 // here, but skipping any <slot> element that happens to contain
250 // mPseudoFrame. That's why this uses GetInFlowParent() instead
251 // of GetFlattenedTreeParent().
252 const nsIContent* nodeContent = GetParentContentForScope(aNode->mPseudoFrame);
253 if (SetScopeByWalkingBackwardThroughList(aNode, nodeContent, Prev(aNode))) {
254 aNode->mCrossesContainStyleBoundaries = false;
255 didSetScopeFor(aNode);
256 return;
259 // If this is a USE node there's a possibility that its counter scope starts
260 // in a parent `contain: style` scope. Look upward in the `contain: style`
261 // scope tree to find an appropriate node with which this node shares a
262 // counter scope.
263 if (aNode->mType == nsCounterNode::USE && aNode == First()) {
264 for (auto* scope = mScope->GetParent(); scope; scope = scope->GetParent()) {
265 if (auto* counterList =
266 scope->GetCounterManager().GetCounterList(mCounterName)) {
267 if (auto* node = static_cast<nsCounterNode*>(
268 mScope->GetPrecedingElementInGenConList(counterList))) {
269 if (SetScopeByWalkingBackwardThroughList(aNode, nodeContent, node)) {
270 aNode->mCrossesContainStyleBoundaries = true;
271 didSetScopeFor(aNode);
272 return;
279 setNullScopeFor(aNode);
282 bool nsCounterList::SetScopeByWalkingBackwardThroughList(
283 nsCounterNode* aNodeToSetScopeFor, const nsIContent* aNodeContent,
284 nsCounterNode* aNodeToBeginLookingAt) {
285 for (nsCounterNode *prev = aNodeToBeginLookingAt, *start; prev;
286 prev = start->mScopePrev) {
287 // There are two possibilities here:
288 // 1. |prev| starts a new counter scope. This happens when:
289 // a. It's a reset node.
290 // b. It's an implied reset node which we know because mScopeStart is null.
291 // c. It follows one or more USE nodes at the start of the list which have
292 // a scope that starts in a parent `contain: style` context.
293 // In all of these cases, |prev| should be the start of this node's counter
294 // scope.
295 // 2. |prev| does not start a new counter scope and this node should share a
296 // counter scope start with |prev|.
297 start =
298 (prev->mType == nsCounterNode::RESET || !prev->mScopeStart ||
299 (prev->mScopePrev && prev->mScopePrev->mCrossesContainStyleBoundaries))
300 ? prev
301 : prev->mScopeStart;
303 const nsIContent* startContent =
304 GetParentContentForScope(start->mPseudoFrame);
305 NS_ASSERTION(aNodeContent || !startContent,
306 "null check on startContent should be sufficient to "
307 "null check aNodeContent as well, since if aNodeContent "
308 "is for the root, startContent (which is before it) "
309 "must be too");
311 // A reset's outer scope can't be a scope created by a sibling.
312 if (!(aNodeToSetScopeFor->mType == nsCounterNode::RESET &&
313 aNodeContent == startContent) &&
314 // everything is inside the root (except the case above,
315 // a second reset on the root)
316 (!startContent ||
317 aNodeContent->IsInclusiveFlatTreeDescendantOf(startContent))) {
318 // If this node is a USE node and the previous node was also a USE node
319 // which has a scope that starts in a parent `contain: style` context,
320 // this node's scope shares the same scope and crosses `contain: style`
321 // scope boundaries.
322 if (aNodeToSetScopeFor->mType == nsCounterNode::USE) {
323 aNodeToSetScopeFor->mCrossesContainStyleBoundaries =
324 prev->mCrossesContainStyleBoundaries;
327 aNodeToSetScopeFor->mScopeStart = start;
328 aNodeToSetScopeFor->mScopePrev = prev;
329 return true;
333 return false;
336 void nsCounterList::RecalcAll() {
337 AutoRestore<bool> restoreRecalculatingAll(mRecalculatingAll);
338 mRecalculatingAll = true;
340 // Setup the scope and calculate the default start value for content-based
341 // reversed() counters. We need to track the last increment for each of
342 // those scopes so that we can add it in an extra time at the end.
343 // https://drafts.csswg.org/css-lists/#instantiating-counters
344 nsTHashMap<nsPtrHashKey<nsCounterChangeNode>, int32_t> scopes;
345 for (nsCounterNode* node = First(); node; node = Next(node)) {
346 SetScope(node);
347 if (node->IsContentBasedReset()) {
348 node->ChangeNode()->mSeenSetNode = false;
349 node->mValueAfter = 0;
350 scopes.InsertOrUpdate(node->ChangeNode(), 0);
351 } else if (node->mScopeStart && node->mScopeStart->IsContentBasedReset() &&
352 !node->mScopeStart->ChangeNode()->mSeenSetNode) {
353 if (node->mType == nsCounterChangeNode::INCREMENT) {
354 auto incrementNegated = -node->ChangeNode()->mChangeValue;
355 if (auto entry = scopes.Lookup(node->mScopeStart->ChangeNode())) {
356 entry.Data() = incrementNegated;
358 auto* next = Next(node);
359 if (next && next->mPseudoFrame == node->mPseudoFrame &&
360 next->mType == nsCounterChangeNode::SET) {
361 continue;
363 node->mScopeStart->mValueAfter += incrementNegated;
364 } else if (node->mType == nsCounterChangeNode::SET) {
365 node->mScopeStart->mValueAfter += node->ChangeNode()->mChangeValue;
366 // We have a 'counter-set' for this scope so we're done.
367 // The counter is incremented from that value for the remaining nodes.
368 node->mScopeStart->ChangeNode()->mSeenSetNode = true;
373 // For all the content-based reversed() counters we found, add in the
374 // incrementNegated from its last counter-increment.
375 for (auto iter = scopes.ConstIter(); !iter.Done(); iter.Next()) {
376 iter.Key()->mValueAfter += iter.Data();
379 for (nsCounterNode* node = First(); node; node = Next(node)) {
380 node->Calc(this, /* aNotify = */ true);
384 static bool AddCounterChangeNode(nsCounterManager& aManager, nsIFrame* aFrame,
385 int32_t aIndex,
386 const nsStyleContent::CounterPair& aPair,
387 nsCounterNode::Type aType) {
388 auto* node = new nsCounterChangeNode(aFrame, aType, aPair.value, aIndex,
389 aPair.is_reversed);
390 nsCounterList* counterList =
391 aManager.GetOrCreateCounterList(aPair.name.AsAtom());
392 counterList->Insert(node);
393 if (!counterList->IsLast(node)) {
394 // Tell the caller it's responsible for recalculating the entire list.
395 counterList->SetDirty();
396 return true;
399 // Don't call Calc() if the list is already dirty -- it'll be recalculated
400 // anyway, and trying to calculate with a dirty list doesn't work.
401 if (MOZ_LIKELY(!counterList->IsDirty())) {
402 node->Calc(counterList);
404 return counterList->IsDirty();
407 static bool HasCounters(const nsStyleContent& aStyle) {
408 return !aStyle.mCounterIncrement.IsEmpty() ||
409 !aStyle.mCounterReset.IsEmpty() || !aStyle.mCounterSet.IsEmpty();
412 bool nsCounterManager::AddCounterChanges(nsIFrame* aFrame) {
413 // For elements with 'display:list-item' we add a default
414 // 'counter-increment:list-item' unless 'counter-increment' already has a
415 // value for 'list-item'.
417 // https://drafts.csswg.org/css-lists-3/#declaring-a-list-item
419 // We inherit `display` for some anonymous boxes, but we don't want them to
420 // increment the list-item counter.
421 const bool requiresListItemIncrement =
422 aFrame->StyleDisplay()->IsListItem() && !aFrame->Style()->IsAnonBox();
424 const nsStyleContent* styleContent = aFrame->StyleContent();
426 if (!requiresListItemIncrement && !HasCounters(*styleContent)) {
427 MOZ_ASSERT(!aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE));
428 return false;
431 aFrame->AddStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE);
433 bool dirty = false;
434 // Add in order, resets first, so all the comparisons will be optimized
435 // for addition at the end of the list.
437 int32_t i = 0;
438 for (const auto& pair : styleContent->mCounterReset.AsSpan()) {
439 dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
440 nsCounterChangeNode::RESET);
443 bool hasListItemIncrement = false;
445 int32_t i = 0;
446 for (const auto& pair : styleContent->mCounterIncrement.AsSpan()) {
447 hasListItemIncrement |= pair.name.AsAtom() == nsGkAtoms::list_item;
448 if (pair.value != 0) {
449 dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
450 nsCounterChangeNode::INCREMENT);
455 if (requiresListItemIncrement && !hasListItemIncrement) {
456 RefPtr<nsAtom> atom = nsGkAtoms::list_item;
457 // We use a magic value here to signal to SetScope() that it should
458 // set the value to -1 or 1 depending on if the scope is reversed()
459 // or not.
460 auto listItemIncrement = nsStyleContent::CounterPair{
461 {StyleAtom(atom.forget())}, std::numeric_limits<int32_t>::min()};
462 dirty |= AddCounterChangeNode(
463 *this, aFrame, styleContent->mCounterIncrement.Length(),
464 listItemIncrement, nsCounterChangeNode::INCREMENT);
468 int32_t i = 0;
469 for (const auto& pair : styleContent->mCounterSet.AsSpan()) {
470 dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
471 nsCounterChangeNode::SET);
474 return dirty;
477 nsCounterList* nsCounterManager::GetOrCreateCounterList(nsAtom* aCounterName) {
478 MOZ_ASSERT(aCounterName);
479 return mNames.GetOrInsertNew(aCounterName, aCounterName, mScope);
482 nsCounterList* nsCounterManager::GetCounterList(nsAtom* aCounterName) {
483 MOZ_ASSERT(aCounterName);
484 return mNames.Get(aCounterName);
487 void nsCounterManager::RecalcAll() {
488 for (const auto& list : mNames.Values()) {
489 if (list->IsDirty()) {
490 list->RecalcAll();
495 void nsCounterManager::SetAllDirty() {
496 for (const auto& list : mNames.Values()) {
497 list->SetDirty();
501 bool nsCounterManager::DestroyNodesFor(nsIFrame* aFrame) {
502 MOZ_ASSERT(aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE),
503 "why call me?");
504 bool destroyedAny = false;
505 for (const auto& list : mNames.Values()) {
506 if (list->DestroyNodesFor(aFrame)) {
507 destroyedAny = true;
508 list->SetDirty();
511 return destroyedAny;
514 #ifdef ACCESSIBILITY
515 bool nsCounterManager::GetFirstCounterValueForFrame(
516 nsIFrame* aFrame, CounterValue& aOrdinal) const {
517 if (const auto* list = mNames.Get(nsGkAtoms::list_item)) {
518 for (nsCounterNode* n = list->GetFirstNodeFor(aFrame);
519 n && n->mPseudoFrame == aFrame; n = list->Next(n)) {
520 if (n->mType == nsCounterNode::USE) {
521 aOrdinal = n->mValueAfter;
522 return true;
527 return false;
529 #endif
531 #if defined(DEBUG) || defined(MOZ_LAYOUT_DEBUGGER)
532 void nsCounterManager::Dump() const {
533 printf("\n\nCounter Manager Lists:\n");
534 for (const auto& entry : mNames) {
535 printf("Counter named \"%s\":\n", nsAtomCString(entry.GetKey()).get());
537 nsCounterList* list = entry.GetWeak();
538 int32_t i = 0;
539 for (nsCounterNode* node = list->First(); node; node = list->Next(node)) {
540 const char* types[] = {"RESET", "INCREMENT", "SET", "USE"};
541 printf(
542 " Node #%d @%p frame=%p index=%d type=%s valAfter=%d\n"
543 " scope-start=%p scope-prev=%p",
544 i++, (void*)node, (void*)node->mPseudoFrame, node->mContentIndex,
545 types[node->mType], node->mValueAfter, (void*)node->mScopeStart,
546 (void*)node->mScopePrev);
547 if (node->mType == nsCounterNode::USE) {
548 nsAutoString text;
549 node->UseNode()->GetText(text);
550 printf(" text=%s", NS_ConvertUTF16toUTF8(text).get());
552 printf("\n");
555 printf("\n\n");
557 #endif