layout: Make IndependentFormattingContext::contents private (again) (#38350)

This was done in #24871, but after some refactorings it became public.
This makes it private again. As said in
b2b3ea992c:

> Privacy forces the rest of the code to go through methods
> rather than matching on the enum,
> reducing accidental layout-mode-specific behavior.

It also avoids the risk of accidentally calling `layout()` on the inner
layout-mode-specific struct, bypassing caching.

Testing: Not needed (no behavior change)

Signed-off-by: Oriol Brufau <obrufau@igalia.com>
This commit is contained in:
Oriol Brufau 2025-07-30 14:55:32 +02:00 committed by GitHub
parent 8c737a6a79
commit c7c33f5f47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 34 additions and 32 deletions

View file

@ -69,10 +69,10 @@ impl<'dom> ModernContainerJob<'dom> {
BlockContainer::InlineFormattingContext(inline_formatting_context), BlockContainer::InlineFormattingContext(inline_formatting_context),
); );
let info: &NodeAndStyleInfo = anonymous_info; let info: &NodeAndStyleInfo = anonymous_info;
let formatting_context = IndependentFormattingContext { let formatting_context = IndependentFormattingContext::new(
base: LayoutBoxBase::new(info.into(), info.style.clone()), LayoutBoxBase::new(info.into(), info.style.clone()),
contents: IndependentFormattingContextContents::Flow(block_formatting_context), IndependentFormattingContextContents::Flow(block_formatting_context),
}; );
Some(ModernItem { Some(ModernItem {
kind: ModernItemKind::InFlow(formatting_context), kind: ModernItemKind::InFlow(formatting_context),

View file

@ -114,10 +114,7 @@ use webrender_api::FontInstanceKey;
use xi_unicode::linebreak_property; use xi_unicode::linebreak_property;
use super::float::{Clear, PlacementAmongFloats}; use super::float::{Clear, PlacementAmongFloats};
use super::{ use super::{CacheableLayoutResult, IndependentFloatOrAtomicLayoutResult};
CacheableLayoutResult, IndependentFloatOrAtomicLayoutResult,
IndependentFormattingContextContents,
};
use crate::cell::ArcRefCell; use crate::cell::ArcRefCell;
use crate::context::LayoutContext; use crate::context::LayoutContext;
use crate::dom_traversal::NodeAndStyleInfo; use crate::dom_traversal::NodeAndStyleInfo;
@ -2151,10 +2148,8 @@ impl IndependentFormattingContext {
match self.style().clone_baseline_source() { match self.style().clone_baseline_source() {
BaselineSource::First => baselines.first, BaselineSource::First => baselines.first,
BaselineSource::Last => baselines.last, BaselineSource::Last => baselines.last,
BaselineSource::Auto => match &self.contents { BaselineSource::Auto if self.is_block_container() => baselines.last,
IndependentFormattingContextContents::Flow(_) => baselines.last, BaselineSource::Auto => baselines.first,
_ => baselines.first,
},
} }
} }

View file

@ -28,9 +28,7 @@ use crate::flow::float::{
Clear, ContainingBlockPositionInfo, FloatBox, FloatSide, PlacementAmongFloats, Clear, ContainingBlockPositionInfo, FloatBox, FloatSide, PlacementAmongFloats,
SequentialLayoutState, SequentialLayoutState,
}; };
use crate::formatting_contexts::{ use crate::formatting_contexts::{Baselines, IndependentFormattingContext};
Baselines, IndependentFormattingContext, IndependentFormattingContextContents,
};
use crate::fragment_tree::{ use crate::fragment_tree::{
BaseFragmentInfo, BlockLevelLayoutInfo, BoxFragment, CollapsedBlockMargins, CollapsedMargin, BaseFragmentInfo, BlockLevelLayoutInfo, BoxFragment, CollapsedBlockMargins, CollapsedMargin,
Fragment, FragmentFlags, Fragment, FragmentFlags,

View file

@ -34,7 +34,9 @@ use crate::{
#[derive(Debug, MallocSizeOf)] #[derive(Debug, MallocSizeOf)]
pub(crate) struct IndependentFormattingContext { pub(crate) struct IndependentFormattingContext {
pub base: LayoutBoxBase, pub base: LayoutBoxBase,
pub contents: IndependentFormattingContextContents, // Private so that code outside of this module cannot match variants.
// It should go through methods instead.
contents: IndependentFormattingContextContents,
} }
#[derive(Debug, MallocSizeOf)] #[derive(Debug, MallocSizeOf)]
@ -65,6 +67,10 @@ impl Baselines {
} }
impl IndependentFormattingContext { impl IndependentFormattingContext {
pub(crate) fn new(base: LayoutBoxBase, contents: IndependentFormattingContextContents) -> Self {
Self { base, contents }
}
pub fn construct( pub fn construct(
context: &LayoutContext, context: &LayoutContext,
node_and_style_info: &NodeAndStyleInfo, node_and_style_info: &NodeAndStyleInfo,
@ -139,13 +145,6 @@ impl IndependentFormattingContext {
} }
} }
pub fn is_replaced(&self) -> bool {
matches!(
self.contents,
IndependentFormattingContextContents::Replaced(_)
)
}
#[inline] #[inline]
pub fn style(&self) -> &Arc<ComputedValues> { pub fn style(&self) -> &Arc<ComputedValues> {
&self.base.style &self.base.style
@ -238,6 +237,19 @@ impl IndependentFormattingContext {
} }
} }
#[inline]
pub(crate) fn is_block_container(&self) -> bool {
matches!(self.contents, IndependentFormattingContextContents::Flow(_))
}
#[inline]
pub(crate) fn is_replaced(&self) -> bool {
matches!(
self.contents,
IndependentFormattingContextContents::Replaced(_)
)
}
#[inline] #[inline]
pub(crate) fn is_table(&self) -> bool { pub(crate) fn is_table(&self) -> bool {
matches!( matches!(

View file

@ -118,10 +118,10 @@ impl Table {
let mut table = table_builder.finish(); let mut table = table_builder.finish();
table.anonymous = true; table.anonymous = true;
let ifc = IndependentFormattingContext { let ifc = IndependentFormattingContext::new(
base: LayoutBoxBase::new((&table_info).into(), table_style), LayoutBoxBase::new((&table_info).into(), table_style),
contents: IndependentFormattingContextContents::Table(table), IndependentFormattingContextContents::Table(table),
}; );
(table_info, ifc) (table_info, ifc)
} }
@ -881,12 +881,9 @@ impl<'dom> TraversalHandler<'dom> for TableBuilderTraversal<'_, 'dom> {
false, /* is_list_item */ false, /* is_list_item */
), ),
); );
let base = LayoutBoxBase::new(info.into(), info.style.clone());
ArcRefCell::new(TableCaption { ArcRefCell::new(TableCaption {
context: IndependentFormattingContext { context: IndependentFormattingContext::new(base, contents),
base: LayoutBoxBase::new(info.into(), info.style.clone()),
contents,
},
}) })
}); });