use std::borrow::Cow;
use super::*;
#[derive(Debug, Clone)]
pub(in crate::layout) struct FormattingContextChild<'a> {
pub(in crate::layout) kind: FormattingContextChildKind<'a>,
pub(in crate::layout) style: ComputedStyle,
}
#[derive(Debug, Clone)]
pub(in crate::layout) enum FormattingContextChildKind<'a> {
Element {
element: &'a Element,
signature: Box<ElementSignature>,
generated_pseudo: Option<box_tree::GeneratedPseudoKind>,
children: Option<Cow<'a, [box_tree::FormattingBox<'a>]>>,
table_fragment: Option<Box<box_tree::TableFragment<'a>>>,
},
AnonymousContent {
children: Vec<box_tree::FormattingBox<'a>>,
},
}
impl<'a> FormattingContextChild<'a> {
pub(in crate::layout) fn element_parts(
&self,
) -> Option<(
&'a Element,
&ElementSignature,
Option<&[box_tree::FormattingBox<'a>]>,
)> {
match &self.kind {
FormattingContextChildKind::Element {
element,
signature,
children,
..
} => Some((*element, signature.as_ref(), children.as_deref())),
FormattingContextChildKind::AnonymousContent { .. } => None,
}
}
pub(in crate::layout) fn anonymous_content(&self) -> Option<&[box_tree::FormattingBox<'a>]> {
match &self.kind {
FormattingContextChildKind::AnonymousContent { children } => Some(children),
FormattingContextChildKind::Element { .. } => None,
}
}
pub(in crate::layout) fn generated_pseudo_kind(&self) -> Option<box_tree::GeneratedPseudoKind> {
match &self.kind {
FormattingContextChildKind::Element {
generated_pseudo, ..
} => *generated_pseudo,
FormattingContextChildKind::AnonymousContent { .. } => None,
}
}
pub(in crate::layout) fn table_fragment(&self) -> Option<&box_tree::TableFragment<'a>> {
match &self.kind {
FormattingContextChildKind::Element { table_fragment, .. } => table_fragment.as_deref(),
FormattingContextChildKind::AnonymousContent { .. } => None,
}
}
pub(in crate::layout) fn is_replaced_element(&self) -> bool {
self.element_parts()
.is_some_and(|(element, _, _)| is_replaced_element(element))
}
}
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct ItemizationOptions {
pub(in crate::layout) anonymous_item_tag: &'static str,
pub(in crate::layout) strip_blockified_inline_text_paint: bool,
pub(in crate::layout) establish_independent_formatting_context: bool,
}
pub(in crate::layout) fn itemize_blockified_children<'a>(
child_boxes: &'a [box_tree::FormattingBox<'a>],
options: ItemizationOptions,
) -> (
Vec<FormattingContextChild<'a>>,
Vec<FormattingContextChild<'a>>,
) {
let mut in_flow = Vec::new();
let mut positioned = Vec::new();
let mut anonymous_run = Vec::new();
for box_ in child_boxes {
if matches!(box_, box_tree::FormattingBox::Text(_))
|| formatting_box_is_contents_generated_pseudo(box_)
{
anonymous_run.push(box_.clone());
continue;
}
flush_anonymous_item_run(&mut in_flow, &mut anonymous_run, options);
let Some((element, signature, style, children)) = box_.element_parts() else {
continue;
};
let generated_pseudo = box_.element_core().and_then(|core| match &core.source {
box_tree::BoxSource::Principal => None,
box_tree::BoxSource::GeneratedPseudo(pseudo) => Some(pseudo.kind),
});
if style.display.is_none() {
continue;
}
let source_display = style.display;
let mut style = style.clone();
style.display = style.display.blockified();
let children = blockified_item_children(
source_display,
&style,
children,
options.strip_blockified_inline_text_paint,
);
if options.establish_independent_formatting_context {
style = independent_formatting_context_item_style(style);
}
let table_fragment = match box_ {
box_tree::FormattingBox::Table(table) => Some(Box::new(table.fragment.clone())),
_ => None,
};
let child = FormattingContextChild {
kind: FormattingContextChildKind::Element {
element,
signature: Box::new(signature.clone()),
generated_pseudo,
children: Some(children),
table_fragment,
},
style,
};
if matches!(child.style.position, Position::Absolute | Position::Fixed) {
positioned.push(child);
} else {
in_flow.push(child);
}
}
flush_anonymous_item_run(&mut in_flow, &mut anonymous_run, options);
in_flow.sort_by_key(|child| child.style.order);
(in_flow, positioned)
}
fn formatting_box_is_contents_generated_pseudo(box_: &box_tree::FormattingBox<'_>) -> bool {
box_.element_core().is_some_and(|core| {
core.style.display.is_contents()
&& matches!(core.source, box_tree::BoxSource::GeneratedPseudo(_))
})
}
pub(in crate::layout) fn independent_formatting_context_item_style(
mut style: ComputedStyle,
) -> ComputedStyle {
if style.display.is_inline_level() {
style.display = style.display.blockified();
}
if style.display.is_flow() {
style.display = style.display.with_inner(DisplayInner::FlowRoot);
}
style
}
pub(in crate::layout) fn blockified_item_children<'a>(
source_display: Display,
item_style: &ComputedStyle,
children: &'a [box_tree::FormattingBox<'a>],
strip_inline_text_paint: bool,
) -> Cow<'a, [box_tree::FormattingBox<'a>]> {
if source_display.is_inline_or_run_in_level()
&& source_display.is_flow()
&& item_style.display.is_block_level()
&& item_style.display.is_flow()
{
let mut children = box_tree::clone_frozen_child_boxes_as_mutable(children);
if strip_inline_text_paint {
strip_blockified_inline_text_paint(&mut children);
}
return Cow::Owned(box_tree::freeze_child_boxes(
box_tree::normalize_block_container_children(children, item_style),
));
}
Cow::Borrowed(children)
}
fn flush_anonymous_item_run<'a>(
children: &mut Vec<FormattingContextChild<'a>>,
anonymous_run: &mut Vec<box_tree::FormattingBox<'a>>,
options: ItemizationOptions,
) {
if anonymous_run.is_empty() {
return;
}
if anonymous_run
.iter()
.all(box_tree::formatting_box_is_document_whitespace)
{
anonymous_run.clear();
return;
}
let mut style = anonymous_item_style(options.anonymous_item_tag, &anonymous_run[0]);
if options.establish_independent_formatting_context {
style = independent_formatting_context_item_style(style);
}
children.push(FormattingContextChild {
kind: FormattingContextChildKind::AnonymousContent {
children: std::mem::take(anonymous_run),
},
style,
});
}
fn anonymous_item_style(tag: &'static str, source: &box_tree::FormattingBox<'_>) -> ComputedStyle {
let mut style = css::style_for_element_with_signature(
ElementSignature::new(tag, HashMap::new()),
None,
&css::Stylesheets::document_only(&[]),
Some(source.style()),
&[],
);
style.display = Display::BLOCK;
style
}
fn strip_blockified_inline_text_paint(children: &mut [box_tree::MutableFormattingBox<'_>]) {
for child in children {
if matches!(child, box_tree::MutableFormattingBox::Text(_)) {
strip_text_fragment_paint(child.style_mut());
}
}
}
fn strip_text_fragment_paint(style: &mut ComputedStyle) {
style.background.background_color = css::BackgroundColor::TRANSPARENT;
style.background.background_image = css::ComputedImage::None;
style.background.background_layers.clear();
style.border_width = 0.0;
style.border_widths = css::Edges::ZERO;
style.border_styles = css::BorderStyles::NONE;
style.border_image = css::BorderImage::initial();
}