2017-10-27 20:33:53 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2006-03-30 09:56:38 +04:00
|
|
|
/* implementation of CSS counters (for numbering things) */
|
|
|
|
|
2005-04-02 03:07:00 +04:00
|
|
|
#include "nsCounterManager.h"
|
2015-03-26 10:29:31 +03:00
|
|
|
|
2022-06-22 19:16:59 +03:00
|
|
|
#include "mozilla/AutoRestore.h"
|
|
|
|
#include "mozilla/ContainStyleScopeManager.h"
|
2022-06-22 15:35:33 +03:00
|
|
|
#include "mozilla/IntegerRange.h"
|
2022-06-22 19:16:59 +03:00
|
|
|
#include "mozilla/Likely.h"
|
2019-04-06 09:02:28 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2020-08-27 21:26:38 +03:00
|
|
|
#include "mozilla/StaticPrefs_layout.h"
|
2015-03-26 10:29:31 +03:00
|
|
|
#include "mozilla/WritingModes.h"
|
2022-05-26 19:12:46 +03:00
|
|
|
#include "mozilla/dom/Element.h"
|
2022-06-22 19:16:59 +03:00
|
|
|
#include "mozilla/dom/Text.h"
|
|
|
|
#include "nsContainerFrame.h"
|
2005-04-02 03:07:00 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-10-02 01:01:49 +04:00
|
|
|
#include "nsIContent.h"
|
2022-05-26 19:12:46 +03:00
|
|
|
#include "nsIContentInlines.h"
|
2021-09-15 21:48:17 +03:00
|
|
|
#include "nsIFrame.h"
|
2015-03-26 10:29:31 +03:00
|
|
|
#include "nsTArray.h"
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2014-06-12 05:12:00 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2008-08-18 05:16:42 +04:00
|
|
|
bool nsCounterUseNode::InitTextFrame(nsGenConList* aList,
|
2017-06-04 17:45:15 +03:00
|
|
|
nsIFrame* aPseudoFrame,
|
|
|
|
nsIFrame* aTextFrame) {
|
2008-08-18 05:16:42 +04:00
|
|
|
nsCounterNode::InitTextFrame(aList, aPseudoFrame, aTextFrame);
|
|
|
|
|
2019-05-21 20:30:42 +03:00
|
|
|
auto* counterList = static_cast<nsCounterList*>(aList);
|
2008-08-18 05:16:42 +04:00
|
|
|
counterList->Insert(this);
|
2017-06-04 17:45:15 +03:00
|
|
|
aPseudoFrame->AddStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE);
|
2019-05-21 20:30:42 +03:00
|
|
|
// 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;
|
2008-08-18 05:16:42 +04:00
|
|
|
}
|
2019-05-21 20:30:42 +03:00
|
|
|
if (!counterList->IsLast(this)) {
|
|
|
|
counterList->SetDirty();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
Calc(counterList, /* aNotify = */ false);
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2008-08-18 05:16:42 +04: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|.
|
2019-05-21 20:30:42 +03:00
|
|
|
void nsCounterUseNode::Calc(nsCounterList* aList, bool aNotify) {
|
2022-06-22 19:16:59 +03:00
|
|
|
NS_ASSERTION(aList->IsRecalculatingAll() || !aList->IsDirty(),
|
|
|
|
"Why are we calculating with a dirty list?");
|
|
|
|
|
2018-03-16 16:29:15 +03:00
|
|
|
mValueAfter = nsCounterList::ValueBefore(this);
|
2022-06-22 19:16:59 +03:00
|
|
|
|
2019-05-21 20:30:42 +03:00
|
|
|
if (mText) {
|
|
|
|
nsAutoString contentString;
|
|
|
|
GetText(contentString);
|
|
|
|
mText->SetText(contentString, aNotify);
|
|
|
|
}
|
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|.
|
2017-06-04 17:45:15 +03:00
|
|
|
void nsCounterChangeNode::Calc(nsCounterList* aList) {
|
2022-06-22 19:16:59 +03:00
|
|
|
NS_ASSERTION(aList->IsRecalculatingAll() || !aList->IsDirty(),
|
|
|
|
"Why are we calculating with a dirty list?");
|
2019-03-25 01:13:52 +03:00
|
|
|
if (IsContentBasedReset()) {
|
|
|
|
// RecalcAll takes care of this case.
|
|
|
|
} else if (mType == RESET || mType == SET) {
|
2017-06-04 17:45:15 +03:00
|
|
|
mValueAfter = mChangeValue;
|
|
|
|
} else {
|
|
|
|
NS_ASSERTION(mType == INCREMENT, "invalid type");
|
2018-03-16 16:29:15 +03:00
|
|
|
mValueAfter = nsCounterManager::IncrementCounter(
|
|
|
|
nsCounterList::ValueBefore(this), mChangeValue);
|
2017-06-04 17:45:15 +03:00
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsCounterUseNode::GetText(nsString& aResult) {
|
2021-06-14 04:22:04 +03:00
|
|
|
CounterStyle* style =
|
|
|
|
mPseudoFrame->PresContext()->CounterStyleManager()->ResolveCounterStyle(
|
|
|
|
mCounterStyle);
|
|
|
|
GetText(mPseudoFrame->GetWritingMode(), style, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsCounterUseNode::GetText(WritingMode aWM, CounterStyle* aStyle,
|
|
|
|
nsString& aResult) {
|
|
|
|
const bool isBidiRTL = aWM.IsBidiRTL();
|
|
|
|
auto AppendCounterText = [&aResult, isBidiRTL](const nsAutoString& aText,
|
|
|
|
bool aIsRTL) {
|
|
|
|
if (MOZ_LIKELY(isBidiRTL == aIsRTL)) {
|
|
|
|
aResult.Append(aText);
|
|
|
|
} else {
|
|
|
|
// RLM = 0x200f, LRM = 0x200e
|
|
|
|
const char16_t mark = aIsRTL ? 0x200f : 0x200e;
|
|
|
|
aResult.Append(mark);
|
|
|
|
aResult.Append(aText);
|
|
|
|
aResult.Append(mark);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (mForLegacyBullet) {
|
|
|
|
nsAutoString prefix;
|
|
|
|
aStyle->GetPrefix(prefix);
|
|
|
|
aResult.Assign(prefix);
|
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2017-06-04 17:45:15 +03:00
|
|
|
AutoTArray<nsCounterNode*, 8> stack;
|
|
|
|
stack.AppendElement(static_cast<nsCounterNode*>(this));
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2017-06-04 17:45:15 +03:00
|
|
|
if (mAllCounters && mScopeStart) {
|
|
|
|
for (nsCounterNode* n = mScopeStart; n->mScopePrev; n = n->mScopeStart) {
|
|
|
|
stack.AppendElement(n->mScopePrev);
|
|
|
|
}
|
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2021-06-14 04:22:04 +03:00
|
|
|
for (nsCounterNode* n : Reversed(stack)) {
|
2017-06-04 17:45:15 +03:00
|
|
|
nsAutoString text;
|
|
|
|
bool isTextRTL;
|
2021-06-14 04:22:04 +03:00
|
|
|
aStyle->GetCounterText(n->mValueAfter, aWM, text, isTextRTL);
|
|
|
|
if (!mForLegacyBullet || aStyle->IsBullet()) {
|
|
|
|
aResult.Append(text);
|
|
|
|
} else {
|
|
|
|
AppendCounterText(text, isTextRTL);
|
|
|
|
}
|
|
|
|
if (n == this) {
|
2017-06-04 17:45:15 +03:00
|
|
|
break;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
2017-06-04 17:45:15 +03:00
|
|
|
aResult.Append(mSeparator);
|
|
|
|
}
|
2021-06-14 04:22:04 +03:00
|
|
|
|
|
|
|
if (mForLegacyBullet) {
|
|
|
|
nsAutoString suffix;
|
|
|
|
aStyle->GetSuffix(suffix);
|
|
|
|
aResult.Append(suffix);
|
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2022-05-26 19:12:46 +03:00
|
|
|
static const nsIContent* GetParentContentForScope(nsIFrame* frame) {
|
|
|
|
// We do not want elements with `display: contents` to establish scope for
|
|
|
|
// counters. We'd like to do something like
|
|
|
|
// `nsIFrame::GetClosestFlattenedTreeAncestorPrimaryFrame()` above, but this
|
|
|
|
// may be called before the primary frame is set on frames.
|
|
|
|
nsIContent* content = frame->GetContent()->GetFlattenedTreeParent();
|
|
|
|
while (content && content->IsElement() &&
|
|
|
|
content->AsElement()->IsDisplayContents()) {
|
|
|
|
content = content->GetFlattenedTreeParent();
|
|
|
|
}
|
|
|
|
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2022-06-22 19:16:59 +03:00
|
|
|
bool nsCounterList::IsDirty() const {
|
|
|
|
return mScope->GetScopeManager().CounterDirty(mCounterName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsCounterList::SetDirty() {
|
|
|
|
mScope->GetScopeManager().SetCounterDirty(mCounterName);
|
|
|
|
}
|
|
|
|
|
2017-06-04 17:45:15 +03:00
|
|
|
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.
|
|
|
|
|
2021-12-12 21:44:39 +03:00
|
|
|
auto setNullScopeFor = [](nsCounterNode* aNode) {
|
2017-06-04 17:45:15 +03:00
|
|
|
aNode->mScopeStart = nullptr;
|
|
|
|
aNode->mScopePrev = nullptr;
|
2022-06-22 19:16:59 +03:00
|
|
|
aNode->mCrossesContainStyleBoundaries = false;
|
2021-11-14 06:49:36 +03:00
|
|
|
if (aNode->IsUnitializedIncrementNode()) {
|
|
|
|
aNode->ChangeNode()->mChangeValue = 1;
|
|
|
|
}
|
2021-12-12 21:44:39 +03:00
|
|
|
};
|
|
|
|
|
2022-06-22 19:16:59 +03:00
|
|
|
if (aNode == First() && aNode->mType != nsCounterNode::USE) {
|
2021-12-12 21:44:39 +03:00
|
|
|
setNullScopeFor(aNode);
|
2017-06-04 17:45:15 +03:00
|
|
|
return;
|
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2021-11-14 06:49:36 +03:00
|
|
|
auto didSetScopeFor = [this](nsCounterNode* aNode) {
|
|
|
|
if (aNode->mType == nsCounterNode::USE) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (aNode->mScopeStart->IsContentBasedReset()) {
|
2022-06-22 19:16:59 +03:00
|
|
|
SetDirty();
|
2021-11-14 06:49:36 +03:00
|
|
|
}
|
|
|
|
if (aNode->IsUnitializedIncrementNode()) {
|
|
|
|
aNode->ChangeNode()->mChangeValue =
|
|
|
|
aNode->mScopeStart->IsReversed() ? -1 : 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-27 21:26:38 +03:00
|
|
|
// 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()) {
|
2021-10-16 02:58:42 +03:00
|
|
|
for (auto* p = aNode->mPseudoFrame; p; p = p->GetParent()) {
|
|
|
|
// This relies on the fact that a RESET node is always the first
|
|
|
|
// CounterNode for a frame if it has any.
|
|
|
|
auto* counter = GetFirstNodeFor(p);
|
|
|
|
if (!counter || counter->mType != nsCounterNode::RESET) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (p == aNode->mPseudoFrame) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
aNode->mScopeStart = counter;
|
|
|
|
aNode->mScopePrev = counter;
|
2022-06-22 19:16:59 +03:00
|
|
|
aNode->mCrossesContainStyleBoundaries = false;
|
2021-11-14 06:49:36 +03:00
|
|
|
for (nsCounterNode* prev = Prev(aNode); prev; prev = prev->mScopePrev) {
|
2021-10-16 02:58:42 +03:00
|
|
|
if (prev->mScopeStart == counter) {
|
|
|
|
aNode->mScopePrev =
|
|
|
|
prev->mType == nsCounterNode::RESET ? prev->mScopePrev : prev;
|
2020-08-27 21:26:38 +03:00
|
|
|
break;
|
|
|
|
}
|
2021-10-16 02:58:42 +03:00
|
|
|
if (prev->mType != nsCounterNode::RESET) {
|
|
|
|
prev = prev->mScopeStart;
|
|
|
|
if (!prev) {
|
|
|
|
break;
|
|
|
|
}
|
2020-08-27 21:26:38 +03:00
|
|
|
}
|
|
|
|
}
|
2021-11-14 06:49:36 +03:00
|
|
|
didSetScopeFor(aNode);
|
2021-10-16 02:58:42 +03:00
|
|
|
return;
|
2020-08-27 21:26:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-04 17:45:15 +03:00
|
|
|
// Get the content node for aNode's rendering object's *parent*,
|
|
|
|
// since scope includes siblings, so we want a descendant check on
|
2022-05-26 19:12:46 +03:00
|
|
|
// parents. Note here that mPseudoFrame is a bit of a misnomer, as it
|
|
|
|
// might not be a pseudo element at all, but a normal element that
|
|
|
|
// happens to increment a counter. We want to respect the flat tree
|
|
|
|
// here, but skipping any <slot> element that happens to contain
|
|
|
|
// mPseudoFrame. That's why this uses GetInFlowParent() instead
|
|
|
|
// of GetFlattenedTreeParent().
|
|
|
|
const nsIContent* nodeContent = GetParentContentForScope(aNode->mPseudoFrame);
|
2022-06-22 19:16:59 +03:00
|
|
|
if (SetScopeByWalkingBackwardThroughList(aNode, nodeContent, Prev(aNode))) {
|
|
|
|
aNode->mCrossesContainStyleBoundaries = false;
|
|
|
|
didSetScopeFor(aNode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this is a USE node there's a possibility that its counter scope starts
|
|
|
|
// in a parent `contain: style` scope. Look upward in the `contain: style`
|
|
|
|
// scope tree to find an appropriate node with which this node shares a
|
|
|
|
// counter scope.
|
|
|
|
if (aNode->mType == nsCounterNode::USE && aNode == First()) {
|
|
|
|
for (auto* scope = mScope->GetParent(); scope; scope = scope->GetParent()) {
|
|
|
|
if (auto* counterList =
|
|
|
|
scope->GetCounterManager().GetCounterList(mCounterName)) {
|
|
|
|
if (auto* node = static_cast<nsCounterNode*>(
|
|
|
|
mScope->GetPrecedingElementInGenConList(counterList))) {
|
|
|
|
if (SetScopeByWalkingBackwardThroughList(aNode, nodeContent, node)) {
|
|
|
|
aNode->mCrossesContainStyleBoundaries = true;
|
|
|
|
didSetScopeFor(aNode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setNullScopeFor(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsCounterList::SetScopeByWalkingBackwardThroughList(
|
|
|
|
nsCounterNode* aNodeToSetScopeFor, const nsIContent* aNodeContent,
|
|
|
|
nsCounterNode* aNodeToBeginLookingAt) {
|
|
|
|
for (nsCounterNode *prev = aNodeToBeginLookingAt, *start; prev;
|
2017-06-04 17:45:15 +03:00
|
|
|
prev = start->mScopePrev) {
|
2022-06-22 19:16:59 +03:00
|
|
|
// There are two possibilities here:
|
|
|
|
// 1. |prev| starts a new counter scope. This happens when:
|
|
|
|
// a. It's a reset node.
|
|
|
|
// b. It's an implied reset node which we know because mScopeStart is null.
|
|
|
|
// c. It follows one or more USE nodes at the start of the list which have
|
|
|
|
// a scope that starts in a parent `contain: style` context.
|
|
|
|
// In all of these cases, |prev| should be the start of this node's counter
|
|
|
|
// scope.
|
|
|
|
// 2. |prev| does not start a new counter scope and this node should share a
|
|
|
|
// counter scope start with |prev|.
|
|
|
|
start =
|
|
|
|
(prev->mType == nsCounterNode::RESET || !prev->mScopeStart ||
|
|
|
|
(prev->mScopePrev && prev->mScopePrev->mCrossesContainStyleBoundaries))
|
|
|
|
? prev
|
|
|
|
: prev->mScopeStart;
|
|
|
|
|
2022-05-26 19:12:46 +03:00
|
|
|
const nsIContent* startContent =
|
|
|
|
GetParentContentForScope(start->mPseudoFrame);
|
2022-06-22 19:16:59 +03:00
|
|
|
NS_ASSERTION(aNodeContent || !startContent,
|
2017-06-04 17:45:15 +03:00
|
|
|
"null check on startContent should be sufficient to "
|
2022-06-22 19:16:59 +03:00
|
|
|
"null check aNodeContent as well, since if aNodeContent "
|
2017-06-04 17:45:15 +03:00
|
|
|
"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.
|
2022-06-22 19:16:59 +03:00
|
|
|
if (!(aNodeToSetScopeFor->mType == nsCounterNode::RESET &&
|
|
|
|
aNodeContent == startContent) &&
|
2017-06-04 17:45:15 +03:00
|
|
|
// everything is inside the root (except the case above,
|
|
|
|
// a second reset on the root)
|
2022-05-26 19:12:46 +03:00
|
|
|
(!startContent ||
|
2022-06-22 19:16:59 +03:00
|
|
|
aNodeContent->IsInclusiveFlatTreeDescendantOf(startContent))) {
|
|
|
|
// If this node is a USE node and the previous node was also a USE node
|
|
|
|
// which has a scope that starts in a parent `contain: style` context,
|
|
|
|
// this node's scope shares the same scope and crosses `contain: style`
|
|
|
|
// scope boundaries.
|
|
|
|
if (aNodeToSetScopeFor->mType == nsCounterNode::USE) {
|
|
|
|
aNodeToSetScopeFor->mCrossesContainStyleBoundaries =
|
|
|
|
prev->mCrossesContainStyleBoundaries;
|
|
|
|
}
|
|
|
|
|
|
|
|
aNodeToSetScopeFor->mScopeStart = start;
|
|
|
|
aNodeToSetScopeFor->mScopePrev = prev;
|
|
|
|
return true;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
2017-06-04 17:45:15 +03:00
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2022-06-22 19:16:59 +03:00
|
|
|
return false;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsCounterList::RecalcAll() {
|
2022-06-22 19:16:59 +03:00
|
|
|
AutoRestore<bool> restoreRecalculatingAll(mRecalculatingAll);
|
|
|
|
mRecalculatingAll = true;
|
|
|
|
|
2021-11-14 06:49:36 +03:00
|
|
|
// Setup the scope and calculate the default start value for content-based
|
|
|
|
// reversed() counters. We need to track the last increment for each of
|
|
|
|
// those scopes so that we can add it in an extra time at the end.
|
|
|
|
// https://drafts.csswg.org/css-lists/#instantiating-counters
|
|
|
|
nsTHashMap<nsPtrHashKey<nsCounterChangeNode>, int32_t> scopes;
|
2016-10-27 13:07:52 +03:00
|
|
|
for (nsCounterNode* node = First(); node; node = Next(node)) {
|
|
|
|
SetScope(node);
|
2019-03-25 01:13:52 +03:00
|
|
|
if (node->IsContentBasedReset()) {
|
2021-11-14 06:49:36 +03:00
|
|
|
node->ChangeNode()->mSeenSetNode = false;
|
|
|
|
node->mValueAfter = 0;
|
|
|
|
scopes.InsertOrUpdate(node->ChangeNode(), 0);
|
|
|
|
} else if (node->mScopeStart && node->mScopeStart->IsContentBasedReset() &&
|
|
|
|
!node->mScopeStart->ChangeNode()->mSeenSetNode) {
|
|
|
|
if (node->mType == nsCounterChangeNode::INCREMENT) {
|
|
|
|
auto incrementNegated = -node->ChangeNode()->mChangeValue;
|
|
|
|
if (auto entry = scopes.Lookup(node->mScopeStart->ChangeNode())) {
|
|
|
|
entry.Data() = incrementNegated;
|
|
|
|
}
|
|
|
|
auto* next = Next(node);
|
|
|
|
if (next && next->mPseudoFrame == node->mPseudoFrame &&
|
|
|
|
next->mType == nsCounterChangeNode::SET) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
node->mScopeStart->mValueAfter += incrementNegated;
|
|
|
|
} else if (node->mType == nsCounterChangeNode::SET) {
|
|
|
|
node->mScopeStart->mValueAfter += node->ChangeNode()->mChangeValue;
|
|
|
|
// We have a 'counter-set' for this scope so we're done.
|
|
|
|
// The counter is incremented from that value for the remaining nodes.
|
|
|
|
node->mScopeStart->ChangeNode()->mSeenSetNode = true;
|
|
|
|
}
|
2019-03-25 01:13:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-14 06:49:36 +03:00
|
|
|
// For all the content-based reversed() counters we found, add in the
|
|
|
|
// incrementNegated from its last counter-increment.
|
|
|
|
for (auto iter = scopes.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
iter.Key()->mValueAfter += iter.Data();
|
|
|
|
}
|
|
|
|
|
2019-03-25 01:13:52 +03:00
|
|
|
for (nsCounterNode* node = First(); node; node = Next(node)) {
|
2019-05-21 20:30:42 +03:00
|
|
|
node->Calc(this, /* aNotify = */ true);
|
2016-10-27 13:07:52 +03:00
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2019-09-03 02:11:26 +03:00
|
|
|
static bool AddCounterChangeNode(nsCounterManager& aManager, nsIFrame* aFrame,
|
|
|
|
int32_t aIndex,
|
|
|
|
const nsStyleContent::CounterPair& aPair,
|
|
|
|
nsCounterNode::Type aType) {
|
2021-11-14 06:49:36 +03:00
|
|
|
auto* node = new nsCounterChangeNode(aFrame, aType, aPair.value, aIndex,
|
|
|
|
aPair.is_reversed);
|
2022-06-22 19:16:59 +03:00
|
|
|
nsCounterList* counterList =
|
|
|
|
aManager.GetOrCreateCounterList(aPair.name.AsAtom());
|
2019-09-03 02:11:26 +03:00
|
|
|
counterList->Insert(node);
|
|
|
|
if (!counterList->IsLast(node)) {
|
2021-11-14 06:49:36 +03:00
|
|
|
// Tell the caller it's responsible for recalculating the entire list.
|
2019-09-03 02:11:26 +03:00
|
|
|
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);
|
|
|
|
}
|
2021-11-14 06:49:36 +03:00
|
|
|
return counterList->IsDirty();
|
2019-09-03 02:11:26 +03:00
|
|
|
}
|
|
|
|
|
2019-04-11 18:21:17 +03:00
|
|
|
static bool HasCounters(const nsStyleContent& aStyle) {
|
2019-09-03 02:11:26 +03:00
|
|
|
return !aStyle.mCounterIncrement.IsEmpty() ||
|
|
|
|
!aStyle.mCounterReset.IsEmpty() || !aStyle.mCounterSet.IsEmpty();
|
2019-04-11 18:21:17 +03:00
|
|
|
}
|
|
|
|
|
2019-03-25 01:13:52 +03:00
|
|
|
bool nsCounterManager::AddCounterChanges(nsIFrame* aFrame) {
|
2019-04-14 01:22:51 +03:00
|
|
|
// 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 =
|
2019-08-16 01:13:49 +03:00
|
|
|
aFrame->StyleDisplay()->IsListItem() && !aFrame->Style()->IsAnonBox();
|
2019-04-14 01:22:51 +03:00
|
|
|
|
2017-06-04 17:45:15 +03:00
|
|
|
const nsStyleContent* styleContent = aFrame->StyleContent();
|
2019-04-11 18:21:17 +03:00
|
|
|
|
|
|
|
if (!requiresListItemIncrement && !HasCounters(*styleContent)) {
|
2017-06-04 17:45:15 +03:00
|
|
|
MOZ_ASSERT(!aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE));
|
2017-06-04 17:45:15 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-06-04 17:45:15 +03:00
|
|
|
aFrame->AddStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE);
|
|
|
|
|
2019-04-11 18:21:17 +03:00
|
|
|
bool dirty = false;
|
2017-06-04 17:45:15 +03:00
|
|
|
// Add in order, resets first, so all the comparisons will be optimized
|
|
|
|
// for addition at the end of the list.
|
2019-09-03 02:11:26 +03:00
|
|
|
{
|
|
|
|
int32_t i = 0;
|
|
|
|
for (const auto& pair : styleContent->mCounterReset.AsSpan()) {
|
|
|
|
dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
|
|
|
|
nsCounterChangeNode::RESET);
|
|
|
|
}
|
2019-03-25 01:13:52 +03:00
|
|
|
}
|
2019-04-11 18:21:17 +03:00
|
|
|
bool hasListItemIncrement = false;
|
2019-09-03 02:11:26 +03:00
|
|
|
{
|
|
|
|
int32_t i = 0;
|
|
|
|
for (const auto& pair : styleContent->mCounterIncrement.AsSpan()) {
|
|
|
|
hasListItemIncrement |= pair.name.AsAtom() == nsGkAtoms::list_item;
|
2021-03-04 03:37:59 +03:00
|
|
|
if (pair.value != 0) {
|
|
|
|
dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
|
|
|
|
nsCounterChangeNode::INCREMENT);
|
|
|
|
}
|
2019-09-03 02:11:26 +03:00
|
|
|
}
|
2019-04-11 18:21:17 +03:00
|
|
|
}
|
2019-09-03 02:11:26 +03:00
|
|
|
|
2019-04-11 18:21:17 +03:00
|
|
|
if (requiresListItemIncrement && !hasListItemIncrement) {
|
2019-09-03 02:11:26 +03:00
|
|
|
RefPtr<nsAtom> atom = nsGkAtoms::list_item;
|
2021-11-14 06:49:36 +03:00
|
|
|
// We use a magic value here to signal to SetScope() that it should
|
|
|
|
// set the value to -1 or 1 depending on if the scope is reversed()
|
|
|
|
// or not.
|
2019-09-03 02:11:26 +03:00
|
|
|
auto listItemIncrement = nsStyleContent::CounterPair{
|
2021-11-14 06:49:36 +03:00
|
|
|
{StyleAtom(atom.forget())}, std::numeric_limits<int32_t>::min()};
|
2019-09-03 02:11:26 +03:00
|
|
|
dirty |= AddCounterChangeNode(
|
|
|
|
*this, aFrame, styleContent->mCounterIncrement.Length(),
|
|
|
|
listItemIncrement, nsCounterChangeNode::INCREMENT);
|
2017-06-04 17:45:15 +03:00
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
|
2019-09-03 02:11:26 +03:00
|
|
|
{
|
|
|
|
int32_t i = 0;
|
|
|
|
for (const auto& pair : styleContent->mCounterSet.AsSpan()) {
|
|
|
|
dirty |= AddCounterChangeNode(*this, aFrame, i++, pair,
|
|
|
|
nsCounterChangeNode::SET);
|
|
|
|
}
|
2016-08-16 23:57:31 +03:00
|
|
|
}
|
2019-09-03 02:11:26 +03:00
|
|
|
return dirty;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2022-06-22 19:16:59 +03:00
|
|
|
nsCounterList* nsCounterManager::GetOrCreateCounterList(nsAtom* aCounterName) {
|
2019-04-15 23:11:45 +03:00
|
|
|
MOZ_ASSERT(aCounterName);
|
2022-06-22 19:16:59 +03:00
|
|
|
return mNames.GetOrInsertNew(aCounterName, aCounterName, mScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCounterList* nsCounterManager::GetCounterList(nsAtom* aCounterName) {
|
|
|
|
MOZ_ASSERT(aCounterName);
|
|
|
|
return mNames.Get(aCounterName);
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsCounterManager::RecalcAll() {
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& list : mNames.Values()) {
|
2017-06-04 17:45:15 +03:00
|
|
|
if (list->IsDirty()) {
|
|
|
|
list->RecalcAll();
|
2014-06-12 05:12:00 +04:00
|
|
|
}
|
2017-06-04 17:45:15 +03:00
|
|
|
}
|
2014-06-12 05:12:00 +04:00
|
|
|
}
|
|
|
|
|
2017-05-23 03:08:50 +03:00
|
|
|
void nsCounterManager::SetAllDirty() {
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& list : mNames.Values()) {
|
|
|
|
list->SetDirty();
|
2016-08-16 23:57:31 +03:00
|
|
|
}
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2017-06-04 17:45:15 +03:00
|
|
|
bool nsCounterManager::DestroyNodesFor(nsIFrame* aFrame) {
|
2017-06-04 17:45:15 +03:00
|
|
|
MOZ_ASSERT(aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE),
|
|
|
|
"why call me?");
|
2016-08-16 23:57:31 +03:00
|
|
|
bool destroyedAny = false;
|
2021-03-24 20:56:49 +03:00
|
|
|
for (const auto& list : mNames.Values()) {
|
2016-08-16 23:57:31 +03:00
|
|
|
if (list->DestroyNodesFor(aFrame)) {
|
|
|
|
destroyedAny = true;
|
|
|
|
list->SetDirty();
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
2016-08-16 23:57:31 +03:00
|
|
|
}
|
|
|
|
return destroyedAny;
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
|
2021-06-14 04:22:04 +03:00
|
|
|
#ifdef ACCESSIBILITY
|
2022-06-22 19:16:59 +03:00
|
|
|
bool nsCounterManager::GetFirstCounterValueForFrame(
|
|
|
|
nsIFrame* aFrame, CounterValue& aOrdinal) const {
|
2021-06-14 04:22:04 +03:00
|
|
|
if (const auto* list = mNames.Get(nsGkAtoms::list_item)) {
|
|
|
|
for (nsCounterNode* n = list->GetFirstNodeFor(aFrame);
|
|
|
|
n && n->mPseudoFrame == aFrame; n = list->Next(n)) {
|
|
|
|
if (n->mType == nsCounterNode::USE) {
|
2022-06-22 19:16:59 +03:00
|
|
|
aOrdinal = n->mValueAfter;
|
|
|
|
return true;
|
2021-06-14 04:22:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-22 19:16:59 +03:00
|
|
|
|
|
|
|
return false;
|
2021-06-14 04:22:04 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-08-11 02:15:31 +03:00
|
|
|
#if defined(DEBUG) || defined(MOZ_LAYOUT_DEBUGGER)
|
|
|
|
void nsCounterManager::Dump() const {
|
2016-08-16 23:57:31 +03:00
|
|
|
printf("\n\nCounter Manager Lists:\n");
|
2021-03-17 18:49:46 +03:00
|
|
|
for (const auto& entry : mNames) {
|
|
|
|
printf("Counter named \"%s\":\n", nsAtomCString(entry.GetKey()).get());
|
2016-08-16 23:57:31 +03:00
|
|
|
|
2021-03-17 18:49:46 +03:00
|
|
|
nsCounterList* list = entry.GetWeak();
|
2016-10-27 13:07:52 +03:00
|
|
|
int32_t i = 0;
|
|
|
|
for (nsCounterNode* node = list->First(); node; node = list->Next(node)) {
|
2021-03-04 03:37:59 +03:00
|
|
|
const char* types[] = {"RESET", "INCREMENT", "SET", "USE"};
|
2016-10-27 13:07:52 +03:00
|
|
|
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");
|
2015-10-23 08:48:42 +03:00
|
|
|
}
|
2016-08-16 23:57:31 +03:00
|
|
|
}
|
|
|
|
printf("\n\n");
|
2005-04-02 03:07:00 +04:00
|
|
|
}
|
|
|
|
#endif
|