use super::*;
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::layout) enum PageBoundaryValue {
Inapplicable,
Inherited,
Auto,
Named(String),
}
impl PageBoundaryValue {
pub(in crate::layout) fn from_style(style: &ComputedStyle) -> Self {
if !style.page.is_specified() {
return Self::Inherited;
}
style
.page
.specified_name()
.map(|name| name.as_str().to_string())
.map(Self::Named)
.unwrap_or(Self::Auto)
}
pub(in crate::layout) fn overrides_parent_summary(&self) -> bool {
matches!(self, Self::Auto | Self::Named(_))
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::layout) struct PageBoundaryValues {
pub(in crate::layout) start: PageBoundaryValue,
pub(in crate::layout) end: PageBoundaryValue,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub(in crate::layout) struct ResolvedPageBoundaryValues {
pub(in crate::layout) start: Option<String>,
pub(in crate::layout) end: Option<String>,
}
impl ResolvedPageBoundaryValues {
pub(in crate::layout) fn uniform(page_name: Option<String>) -> Self {
Self {
start: page_name.clone(),
end: page_name,
}
}
pub(in crate::layout) fn inapplicable() -> Self {
Self::uniform(None)
}
}
impl PageBoundaryValues {
pub(in crate::layout) fn inapplicable() -> Self {
Self {
start: PageBoundaryValue::Inapplicable,
end: PageBoundaryValue::Inapplicable,
}
}
pub(in crate::layout) fn from_style(style: &ComputedStyle) -> Self {
let own = PageBoundaryValue::from_style(style);
Self {
start: own.clone(),
end: own,
}
}
}
pub(in crate::layout) fn page_value_sources_from_style_and_children(
style: &ComputedStyle,
child_boxes: &[box_tree::FormattingBox<'_>],
) -> PageBoundaryValues {
let PageBoundaryValues { mut start, mut end } = PageBoundaryValues::from_style(style);
if style.display.is_flex() {
return PageBoundaryValues { start, end };
}
let mut normal_flow_children = child_boxes
.iter()
.filter(|child| formatting_box_is_page_value_participant(child));
let Some(first) = normal_flow_children.next() else {
return PageBoundaryValues { start, end };
};
let first_sources = formatting_box_page_value_sources(first);
if first_sources.start.overrides_parent_summary() {
start = first_sources.start.clone();
}
let last_sources = normal_flow_children
.next_back()
.map(formatting_box_page_value_sources)
.unwrap_or(first_sources);
if last_sources.end.overrides_parent_summary() {
end = last_sources.end;
}
PageBoundaryValues { start, end }
}
pub(in crate::layout) fn page_value_sources_from_element_style_and_children(
element: &Element,
style: &ComputedStyle,
child_boxes: &[box_tree::FormattingBox<'_>],
) -> PageBoundaryValues {
let mut sources = page_value_sources_from_style_and_children(style, child_boxes);
if !style.page.is_specified() && element_has_leading_direct_inline_content(element, style) {
sources.start = PageBoundaryValue::Inherited;
}
sources
}
fn element_has_leading_direct_inline_content(element: &Element, style: &ComputedStyle) -> bool {
if element_suppresses_direct_text_children(element) {
return false;
}
let mut text = String::new();
for child in &element.children {
match &child.kind {
NodeKind::Text(value) => text.push_str(value),
NodeKind::Element(child) if is_line_break_element(child) => text.push(INLINE_BREAK),
NodeKind::Element(_) => break,
}
}
!normalized_text_for_style(&text, style).is_empty()
}
pub(in crate::layout) fn formatting_box_page_value_sources(
box_: &box_tree::FormattingBox<'_>,
) -> PageBoundaryValues {
match box_ {
box_tree::FormattingBox::Block(box_) => page_value_sources_from_element_style_and_children(
box_.core.element,
&box_.core.style,
&box_.core.children,
),
box_tree::FormattingBox::Inline(box_) => {
let mut descendant_style = box_.core.style.as_ref().clone();
descendant_style.page = css::PageAssignment::Unspecified;
page_value_sources_from_element_style_and_children(
box_.core.element,
&descendant_style,
&box_.core.children,
)
}
box_tree::FormattingBox::InlineSplitBlockContext(box_) => {
let mut descendant_style = box_.core.style.as_ref().clone();
descendant_style.page = css::PageAssignment::Unspecified;
page_value_sources_from_element_style_and_children(
box_.core.element,
&descendant_style,
&box_.core.children,
)
}
box_tree::FormattingBox::AtomicInline(_) => PageBoundaryValues::inapplicable(),
box_tree::FormattingBox::Flex(box_) => PageBoundaryValues::from_style(&box_.core.style),
box_tree::FormattingBox::Table(box_) => {
table::table_page_boundary_summary(&box_.fragment, &box_.core.style, None).sources
}
box_tree::FormattingBox::Replaced(box_) => PageBoundaryValues::from_style(&box_.core.style),
box_tree::FormattingBox::AnonymousBlock(box_) => {
page_value_sources_from_style_and_children(&box_.style, &box_.children)
}
box_tree::FormattingBox::Text(_) => PageBoundaryValues::inapplicable(),
}
}
fn resolved_page_name_for_style(
style: &ComputedStyle,
inherited_page_name: Option<&str>,
) -> Option<String> {
style
.page
.effective_name(inherited_page_name.map(str::to_string))
}
pub(in crate::layout) fn resolved_page_boundary_values_from_style_and_children(
style: &ComputedStyle,
child_boxes: &[box_tree::FormattingBox<'_>],
inherited_page_name: Option<&str>,
) -> ResolvedPageBoundaryValues {
let own_page_name = resolved_page_name_for_style(style, inherited_page_name);
let mut values = ResolvedPageBoundaryValues::uniform(own_page_name.clone());
if style.display.is_flex() {
return values;
}
let mut normal_flow_children = child_boxes
.iter()
.filter(|child| formatting_box_is_page_value_participant(child));
let Some(first) = normal_flow_children.next() else {
return values;
};
let first_sources = formatting_box_page_value_sources(first);
let first_values =
resolved_formatting_box_page_boundary_values(first, own_page_name.as_deref());
if first_sources.start.overrides_parent_summary() {
values.start = first_values.start;
}
let last = normal_flow_children.next_back().unwrap_or(first);
let last_sources = formatting_box_page_value_sources(last);
if last_sources.end.overrides_parent_summary() {
values.end =
resolved_formatting_box_page_boundary_values(last, own_page_name.as_deref()).end;
}
values
}
pub(in crate::layout) fn resolved_formatting_box_page_boundary_values(
box_: &box_tree::FormattingBox<'_>,
inherited_page_name: Option<&str>,
) -> ResolvedPageBoundaryValues {
match box_ {
box_tree::FormattingBox::Block(box_) => {
let mut values = resolved_page_boundary_values_from_style_and_children(
&box_.core.style,
&box_.core.children,
inherited_page_name,
);
if !box_.core.style.page.is_specified()
&& element_has_leading_direct_inline_content(box_.core.element, &box_.core.style)
{
values.start = resolved_page_name_for_style(&box_.core.style, inherited_page_name);
}
values
}
box_tree::FormattingBox::Inline(box_) => {
let mut style = box_.core.style.as_ref().clone();
style.page = css::PageAssignment::Unspecified;
let mut values = resolved_page_boundary_values_from_style_and_children(
&style,
&box_.core.children,
inherited_page_name,
);
if element_has_leading_direct_inline_content(box_.core.element, &style) {
values.start = resolved_page_name_for_style(&style, inherited_page_name);
}
values
}
box_tree::FormattingBox::InlineSplitBlockContext(box_) => {
let mut style = box_.core.style.as_ref().clone();
style.page = css::PageAssignment::Unspecified;
let mut values = resolved_page_boundary_values_from_style_and_children(
&style,
&box_.core.children,
inherited_page_name,
);
if element_has_leading_direct_inline_content(box_.core.element, &style) {
values.start = resolved_page_name_for_style(&style, inherited_page_name);
}
values
}
box_tree::FormattingBox::AtomicInline(_) => ResolvedPageBoundaryValues::inapplicable(),
box_tree::FormattingBox::Flex(box_) => ResolvedPageBoundaryValues::uniform(
resolved_page_name_for_style(&box_.core.style, inherited_page_name),
),
box_tree::FormattingBox::Table(box_) => {
table::table_page_boundary_summary(
&box_.fragment,
&box_.core.style,
inherited_page_name,
)
.resolved
}
box_tree::FormattingBox::Replaced(box_) => ResolvedPageBoundaryValues::uniform(
resolved_page_name_for_style(&box_.core.style, inherited_page_name),
),
box_tree::FormattingBox::AnonymousBlock(box_) => {
resolved_page_boundary_values_from_style_and_children(
&box_.style,
&box_.children,
inherited_page_name,
)
}
box_tree::FormattingBox::Text(_) => ResolvedPageBoundaryValues::inapplicable(),
}
}
pub(in crate::layout) fn formatting_box_is_page_value_participant(
box_: &box_tree::FormattingBox<'_>,
) -> bool {
if let box_tree::FormattingBox::Text(box_) = box_ {
return !(box_.text.is_empty()
|| (box_.style.white_space.collapses_spaces()
&& box_.text.chars().all(is_css_collapsible_whitespace)));
}
if !formatting_box_is_in_normal_flow(box_) {
return false;
}
match box_ {
box_tree::FormattingBox::AnonymousBlock(box_) => box_
.children
.iter()
.any(formatting_box_is_page_value_participant),
box_tree::FormattingBox::InlineSplitBlockContext(box_) => box_
.core
.children
.iter()
.any(formatting_box_is_page_value_participant),
_ => !formatting_box_can_only_create_phantom_line_boxes(box_),
}
}
#[cfg(test)]
pub(in crate::layout) fn formatting_box_page_values(
box_: &box_tree::FormattingBox<'_>,
) -> (Option<String>, Option<String>) {
let sources = formatting_box_page_value_sources(box_);
let name = |source: PageBoundaryValue| match source {
PageBoundaryValue::Named(name) => Some(name),
PageBoundaryValue::Inapplicable
| PageBoundaryValue::Inherited
| PageBoundaryValue::Auto => None,
};
(name(sources.start), name(sources.end))
}
pub(in crate::layout) fn formatting_box_is_in_normal_flow(
box_: &box_tree::FormattingBox<'_>,
) -> bool {
!matches!(box_, box_tree::FormattingBox::Text(_)) && style_is_in_normal_flow(box_.style())
}
pub(in crate::layout) fn formatting_box_is_zero_height_page_boundary(
box_: &box_tree::FormattingBox<'_>,
) -> bool {
let Some((_, _, style, _)) = box_.element_parts() else {
return false;
};
formatting_box_is_in_normal_flow(box_)
&& style.page.is_specified()
&& style
.box_values
.height
.length_if_no_percent()
.is_some_and(|height| height.abs() < 0.01)
}
pub(in crate::layout) fn coalesced_zero_height_page_start(
child_boxes: &[box_tree::FormattingBox<'_>],
current_index: usize,
inherited_page_name: Option<&str>,
) -> Option<String> {
child_boxes
.iter()
.skip(current_index + 1)
.filter(|child| formatting_box_is_in_normal_flow(child))
.find(|child| !formatting_box_is_zero_height_page_boundary(child))
.map(|child| resolved_formatting_box_page_boundary_values(child, inherited_page_name).start)
.unwrap_or_else(|| {
resolved_formatting_box_page_boundary_values(
&child_boxes[current_index],
inherited_page_name,
)
.start
})
}
#[cfg(test)]
mod tests {
use super::*;
fn test_parent_style() -> ComputedStyle {
ComputedStyle {
font_size: 12.0,
line_height: 14.4,
color: CssColor::BLACK,
..ComputedStyle::initial()
}
}
#[test]
fn atomic_inline_page_values_do_not_escape_its_atomic_formatting_context() {
let root = dom::parse(
"<html><body>\
<div style=\"page:c; display:inline-block\">\
<div style=\"page:a\">A</div>\
<div style=\"page:b\">B</div>\
</div>\
<div style=\"page:c\">C</div>\
</body></html>",
);
let stylesheets = Stylesheets::for_document(css::html5_user_agent_stylesheet(), None, &[]);
let page = box_tree::freeze_page_box(box_tree::build_page_box(
&root,
&stylesheets,
&test_parent_style(),
));
let body = &page.children[0].children()[0];
let anonymous_inline_run = &body.children()[0];
assert_eq!(
formatting_box_page_values(anonymous_inline_run),
(None, None)
);
assert_eq!(
formatting_box_page_values(&body.children()[1]),
(Some("c".to_string()), Some("c".to_string()))
);
}
#[test]
fn deeply_nested_inline_page_values_do_not_repeat_single_child_traversal() {
std::thread::Builder::new()
.name("deep-page-value-regression".to_string())
.spawn(|| {
let spans = "<span>".repeat(40);
let closing_spans = "</span>".repeat(40);
let root = dom::parse(&format!(
"<html><style>span {{ zoom: .1%; }}</style><body>{spans}text{closing_spans}</body></html>"
));
let stylesheets =
Stylesheets::for_document(css::html5_user_agent_stylesheet(), None, &[]);
let page = box_tree::freeze_page_box(box_tree::build_page_box(
&root,
&stylesheets,
&test_parent_style(),
));
let body = &page.children[0].children()[0];
assert_eq!(
formatting_box_page_value_sources(body),
PageBoundaryValues {
start: PageBoundaryValue::Inherited,
end: PageBoundaryValue::Inherited,
}
);
})
.expect("deep page-value regression thread should start")
.join()
.expect("deep page-value regression thread should complete");
}
}