Bug 1463600 - Implement CSS 'contain: style' r=emilio

Add an implementation of CSS `contain: style`. This introduces two new
data structures, the ContainStyleScope and ContainStyleScopeManager.

ContainStyleScope manages one `contain: style` "world" which has its own
counter and quote lists. The contents of these lists depend on their
parent scopes, but are not affected by their children.
ContainStyleScopeManager manages a tree of scopes starting at a root
scope which is outside of any `contain: style` element.

Scopes are stored in a hash table that is keyed off of the nsIContent
which establishes the `contain: style` scope. When modifying quote or
content lists, the ContainStyleScopeManager is responsible for finding
the appropriate `contain: style` scope to modify.

Perhaps the most complex part of this is that counters and quotes have
read access to the state of counters and quotes that are in ancestor
`contain: style` scopes. In the case of counters, USE nodes that are at
the beginning of counter lists might have a counter scope that starts in
an ancestor `contain: style` scope. When nsCounterNode::SetScope() is
called, the code may look upward in the `contain: style` scope tree to
find the start of the counter scope. In the case of quotes, the first
node in the quote list must look for the state of quotes in ancestor
`contain: style` scopes.

Differential Revision: https://phabricator.services.mozilla.com/D149508
This commit is contained in:
Martin Robinson
2022-06-22 16:16:59 +00:00
parent 4fb8d02cda
commit 797bb26bc0
45 changed files with 703 additions and 210 deletions

View File

@@ -1566,18 +1566,23 @@ nsCSSFrameConstructor::nsCSSFrameConstructor(Document* aDocument,
}
void nsCSSFrameConstructor::NotifyDestroyingFrame(nsIFrame* aFrame) {
if (aFrame->HasAnyStateBits(NS_FRAME_GENERATED_CONTENT)) {
if (mQuoteList.DestroyNodesFor(aFrame)) QuotesDirty();
if (aFrame->HasAnyStateBits(NS_FRAME_GENERATED_CONTENT) &&
mContainStyleScopeManager.DestroyQuoteNodesFor(aFrame)) {
QuotesDirty();
}
if (aFrame->HasAnyStateBits(NS_FRAME_HAS_CSS_COUNTER_STYLE) &&
mCounterManager.DestroyNodesFor(aFrame)) {
mContainStyleScopeManager.DestroyCounterNodesFor(aFrame)) {
// Technically we don't need to update anything if we destroyed only
// USE nodes. However, this is unlikely to happen in the real world
// since USE nodes generally go along with INCREMENT nodes.
CountersDirty();
}
if (aFrame->StyleDisplay()->IsContainStyle()) {
mContainStyleScopeManager.DestroyScopesFor(aFrame);
}
RestyleManager()->NotifyDestroyingFrame(aFrame);
}
@@ -1608,7 +1613,7 @@ already_AddRefed<nsIContent> nsCSSFrameConstructor::CreateGenConTextNode(
}
already_AddRefed<nsIContent> nsCSSFrameConstructor::CreateGeneratedContent(
nsFrameConstructorState& aState, const Element& aOriginatingElement,
nsFrameConstructorState& aState, Element& aOriginatingElement,
ComputedStyle& aPseudoStyle, uint32_t aContentIndex) {
using Type = StyleContentItem::Tag;
// Get the content value
@@ -1660,7 +1665,8 @@ already_AddRefed<nsIContent> nsCSSFrameConstructor::CreateGeneratedContent(
ptr = CounterStylePtr::FromStyle(counters._2);
}
nsCounterList* counterList = mCounterManager.CounterListFor(name);
auto* counterList = mContainStyleScopeManager.GetOrCreateCounterList(
aOriginatingElement, name);
auto node = MakeUnique<nsCounterUseNode>(
std::move(ptr), std::move(separator), aContentIndex,
/* aAllCounters = */ type == Type::Counters);
@@ -1674,8 +1680,10 @@ already_AddRefed<nsIContent> nsCSSFrameConstructor::CreateGeneratedContent(
case Type::NoOpenQuote:
case Type::NoCloseQuote: {
auto node = MakeUnique<nsQuoteNode>(type, aContentIndex);
auto* quoteList =
mContainStyleScopeManager.QuoteListFor(aOriginatingElement);
auto initializer = MakeUnique<nsGenConInitializer>(
std::move(node), &mQuoteList, &nsCSSFrameConstructor::QuotesDirty);
std::move(node), quoteList, &nsCSSFrameConstructor::QuotesDirty);
return CreateGenConTextNode(aState, u""_ns, std::move(initializer));
}
@@ -1715,7 +1723,8 @@ already_AddRefed<nsIContent> nsCSSFrameConstructor::CreateGeneratedContent(
}
void nsCSSFrameConstructor::CreateGeneratedContentFromListStyle(
nsFrameConstructorState& aState, const ComputedStyle& aPseudoStyle,
nsFrameConstructorState& aState, Element& aOriginatingElement,
const ComputedStyle& aPseudoStyle,
const FunctionRef<void(nsIContent*)> aAddChild) {
const nsStyleList* styleList = aPseudoStyle.StyleList();
if (!styleList->mListStyleImage.IsNone()) {
@@ -1726,11 +1735,13 @@ void nsCSSFrameConstructor::CreateGeneratedContentFromListStyle(
aAddChild(child);
return;
}
CreateGeneratedContentFromListStyleType(aState, aPseudoStyle, aAddChild);
CreateGeneratedContentFromListStyleType(aState, aOriginatingElement,
aPseudoStyle, aAddChild);
}
void nsCSSFrameConstructor::CreateGeneratedContentFromListStyleType(
nsFrameConstructorState& aState, const ComputedStyle& aPseudoStyle,
nsFrameConstructorState& aState, Element& aOriginatingElement,
const ComputedStyle& aPseudoStyle,
const FunctionRef<void(nsIContent*)> aAddChild) {
const nsStyleList* styleList = aPseudoStyle.StyleList();
CounterStyle* counterStyle =
@@ -1765,8 +1776,8 @@ void nsCSSFrameConstructor::CreateGeneratedContentFromListStyleType(
return;
}
nsCounterList* counterList =
mCounterManager.CounterListFor(nsGkAtoms::list_item);
auto* counterList = mContainStyleScopeManager.GetOrCreateCounterList(
aOriginatingElement, nsGkAtoms::list_item);
auto initializer = MakeUnique<nsGenConInitializer>(
std::move(node), counterList, &nsCSSFrameConstructor::CountersDirty);
RefPtr<nsIContent> child =
@@ -1906,7 +1917,8 @@ void nsCSSFrameConstructor::CreateGeneratedContentItem(
}
// If a ::marker has no 'content' then generate it from its 'list-style-*'.
if (contentCount == 0 && aPseudoElement == PseudoStyleType::marker) {
CreateGeneratedContentFromListStyle(aState, *pseudoStyle, AppendChild);
CreateGeneratedContentFromListStyle(aState, aOriginatingElement,
*pseudoStyle, AppendChild);
}
auto flags = ItemFlags{ItemFlag::IsGeneratedContent} + aExtraFlags;
AddFrameConstructionItemsInternal(aState, container, aParentFrame, true,
@@ -4680,7 +4692,8 @@ void nsCSSFrameConstructor::InitAndRestoreFrame(
RestoreFrameStateFor(aNewFrame, aState.mFrameState);
}
if (aAllowCounters && mCounterManager.AddCounterChanges(aNewFrame)) {
if (aAllowCounters &&
mContainStyleScopeManager.AddCounterChanges(aNewFrame)) {
CountersDirty();
}
}
@@ -7271,7 +7284,8 @@ void nsCSSFrameConstructor::ContentRangeInserted(nsIContent* aStartChild,
}
};
CreateGeneratedContentFromListStyleType(
state, *insertion.mParentFrame->Style(), InsertChild);
state, *insertion.mContainer->AsElement(),
*insertion.mParentFrame->Style(), InsertChild);
if (!firstNewChild) {
// No fallback content - we're done.
return;
@@ -7932,12 +7946,12 @@ void nsCSSFrameConstructor::RecalcQuotesAndCounters() {
if (mQuotesDirty) {
mQuotesDirty = false;
mQuoteList.RecalcAll();
mContainStyleScopeManager.RecalcAllQuotes();
}
if (mCountersDirty) {
mCountersDirty = false;
mCounterManager.RecalcAll();
mContainStyleScopeManager.RecalcAllCounters();
}
NS_ASSERTION(!mQuotesDirty, "Quotes updates will be lost");
@@ -7945,20 +7959,19 @@ void nsCSSFrameConstructor::RecalcQuotesAndCounters() {
}
void nsCSSFrameConstructor::NotifyCounterStylesAreDirty() {
mCounterManager.SetAllDirty();
mContainStyleScopeManager.SetAllCountersDirty();
CountersDirty();
}
void nsCSSFrameConstructor::WillDestroyFrameTree() {
#if defined(DEBUG_dbaron_off)
mCounterManager.Dump();
mContainStyleScopeManager.DumpCounters();
#endif
mIsDestroyingFrameTree = true;
// Prevent frame tree destruction from being O(N^2)
mQuoteList.Clear();
mCounterManager.Clear();
mContainStyleScopeManager.Clear();
nsFrameManager::Destroy();
}
@@ -12069,6 +12082,5 @@ void nsCSSFrameConstructor::AddSizeOfIncludingThis(
// Measurement of the following members may be added later if DMD finds it
// is worthwhile:
// - mFCItemPool
// - mQuoteList
// - mCounterManager
// - mContainStyleScopeManager
}