Truly independent web browser
LibWeb: Clear floats from where the margins before a box put it
sideshowbarkereee664d088bd
diff --git a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rsindex d60f0f9f988..a78a5504db0 100644--- a/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/block_formatting_context.rs@@ -108,6 +108,15 @@ pub(crate) struct InterruptedInlineContent {offset_from_relatively_positioned_inline_ancestors: FfiCssPixelPoint,}+/// What the place of a block-level box of a flow depends on before the box is laid out.+#[derive(Clone, Copy)]+pub(crate) struct BlockLevelBoxInFlow<'input> {+ pub(crate) block_start_margin: CssPixels,+ pub(crate) containing_block_inline_size: CssPixels,+ /// What the box is laid out with.+ pub(crate) input: &'input LayoutInput,+}+/// A block container whose children a block formatting context lays out, with the cells the container is laid out with.#[derive(Clone, Copy)]pub(crate) struct BlockContainer {@@ -434,6 +443,63 @@ struct FloatAvoidanceProbe {content_inline_size: Option<CssPixels>,}+/// The sides whose floats the `clear` of a box names, or on which a scan of boxes found floats.+// FIXME: Honor writing-mode, direction and text-orientation.+#[derive(Clone, Copy, Default)]+struct FloatSides {+ left: bool,+ right: bool,+}++impl FloatSides {+ fn cleared_by(style: &StyleValues<'_>) -> Self {+ Self {+ left: matches!(style.clear(), clear::LEFT | clear::BOTH | clear::INLINE_START),+ right: matches!(style.clear(), clear::RIGHT | clear::BOTH | clear::INLINE_END),+ }+ }++ fn of_float(style: &StyleValues<'_>) -> Self {+ let is_left = matches!(style.float_(), float::LEFT | float::INLINE_START);+ Self {+ left: is_left,+ right: !is_left,+ }+ }++ fn any_of(self, sides: Self) -> bool {+ (self.left && sides.left) || (self.right && sides.right)+ }++ fn with(self, sides: Self) -> Self {+ Self {+ left: self.left || sides.left,+ right: self.right || sides.right,+ }+ }+}++/// What a scan of the contents of a box has found of the margins that collapse with the block-start margin of the box.+struct MarginsOfContents<'scan> {+ /// The margins that collapse with the block-start margin of the box, as far as the scan has found them.+ margins: CollapsedMargins,+ /// The sides of the floats among the boxes the scan went through.+ passed_floats: FloatSides,+ /// Answers whether one of the contents that clears the floats on the given sides would get clearance, where the+ /// given margins are those the scan found down to that one's own.+ gets_clearance: &'scan dyn Fn(FloatSides, CollapsedMargins) -> bool,+}++/// Where the block-start border edge of a box would go if the box cleared no float.+#[derive(Clone, Copy)]+struct PositionWithoutClearance {+ /// The position among the flow positions of the box's block container, as that container stands now.+ flow_position: CssPixels,+ /// The block container whose contents the block-start margin of the box moves along with the box, and how far,+ /// where it collapses into the block-start margin of that container and moves it.+ moves_contents: Option<(Node, CssPixels)>,+}+fn round_css_pixels(value: CssPixels) -> CssPixels {let half = CssPixels::from_raw(crate::layout::FIXED_POINT_DENOMINATOR >> 1);if value > CssPixels::default() {@@ -2714,15 +2780,31 @@ impl<'pass> BlockFormattingContext<'pass> {}pub(crate) fn block_offset_adjustment_from_pending_ancestor_block_start_margins(&self, node: Node) -> CssPixels {+ self.block_offset_adjustment_from_pending_ancestor_block_start_margins_where_innermost_group_holds(node, None)+ }++ /// As [`Self::block_offset_adjustment_from_pending_ancestor_block_start_margins`], with `margins_of_innermost_group`+ /// as what the open top margin group of the innermost margin state holds, where it has one and they are given.+ fn block_offset_adjustment_from_pending_ancestor_block_start_margins_where_innermost_group_holds(+ &self,+ node: Node,+ margins_of_innermost_group: Option<CollapsedMargins>,+ ) -> CssPixels {let states = self.margin_states.borrow();let mut adjustment = CssPixels::default();// NB: Contents that join the top margin group of their enclosing margin state hold their margins until they are// laid out, so the group they join collapses theirs too.let mut margins_joining_enclosing_group = CollapsedMargins::default();for (index, state) in states.iter().enumerate().rev() {- let Some(group) = state.top_margin_group else {+ let Some(mut group) = state.top_margin_group else {continue;};+ if index + 1 == states.len()+ && group.open+ && let Some(margins) = margins_of_innermost_group+ {+ group.margins = margins;+ }if group.joins_enclosing_group {margins_joining_enclosing_group = margins_joining_enclosing_group.collapsed_with(group.margins);continue;@@ -2808,29 +2890,36 @@ impl<'pass> BlockFormattingContext<'pass> {self.rebuild_float_bands();}+ /// Moves the flow past the floats the `clear` of `node` names, if they reach below where the box would go, and+ /// returns whether it did. `block_level_box` holds what the place of a block-level box of this flow depends on;+ /// what a line clears has none of that.pub(crate) fn clear_floating_boxes(&self,node: Node,inline_context: Option<&inline_formatting_context::InlineFormattingContext>,+ block_level_box: Option<BlockLevelBoxInFlow<'_>>,containing_block_position_in_root: FfiCssPixelPoint,) -> bool {if self.style(node).clear() == clear::NONE {return false;}+ let flow_position =+ inline_context.map_or_else(|| self.flow_position(), |context| context.block_axis_float_clearance());let lowest_flow_position = match inline_context {Some(_) => CssPixels::default(),- None => self.flow_position().min(CssPixels::default()),+ None => flow_position.min(CssPixels::default()),};self.note_float_read_from(containing_block_position_in_root.y+ self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node)+ lowest_flow_position,);- let flow_position =- inline_context.map_or_else(|| self.flow_position(), |context| context.block_axis_float_clearance());- let Some(clearance) =- self.flow_position_past_floats_cleared_by(node, flow_position, containing_block_position_in_root)- else {+ let Some(clearance) = self.flow_position_past_floats_cleared_by(+ node,+ flow_position,+ block_level_box,+ containing_block_position_in_root,+ ) else {return false;};match inline_context {@@ -2840,17 +2929,345 @@ impl<'pass> BlockFormattingContext<'pass> {true}+ /// Where the block-start border edge of `node`, a block-level box of this flow with the block-start margin+ /// `block_start_margin`, would go from `flow_position` if the box cleared no float: past that margin, collapsed+ /// with the margins before the box.+ fn position_of_box_without_clearance(+ &self,+ node: Node,+ flow_position: CssPixels,+ block_level_box: BlockLevelBoxInFlow<'_>,+ containing_block_position_in_root: FfiCssPixelPoint,+ ) -> PositionWithoutClearance {+ // NB: The block-start border edge of one of the contents whose margin collapses with that of the box is where+ // that of the box is, so it gets clearance where the box would with its `clear`, at the margins down to it.+ let gets_clearance = |cleared_sides: FloatSides, margins: CollapsedMargins| {+ let position = self.position_past_margins(node, flow_position, margins);+ self.flow_position_past_floats_on(node, cleared_sides, &position, containing_block_position_in_root)+ .is_some()+ };+ let margins_before_contents = self+ .margin_state()+ .margins_since_reset+ .with_margin(block_level_box.block_start_margin);+ let mut contents = MarginsOfContents {+ margins: margins_before_contents,+ passed_floats: FloatSides::default(),+ gets_clearance: &gets_clearance,+ };+ self.add_margins_of_contents_collapsing_with_block_start_margin(+ node,+ block_level_box.containing_block_inline_size,+ block_level_box.input,+ &mut contents,+ 0,+ );+ self.position_past_margins(node, flow_position, contents.margins)+ }++ /// Where the block-start border edge of `node` would go from `flow_position` past `margins`: its block-start+ /// margin, collapsed with those of its contents and with the margins before the box.+ fn position_past_margins(+ &self,+ node: Node,+ flow_position: CssPixels,+ margins: CollapsedMargins,+ ) -> PositionWithoutClearance {+ if !self.margin_state().has_open_top_margin_group() {+ return PositionWithoutClearance {+ flow_position: flow_position + margins.collapsed_margin(),+ moves_contents: None,+ };+ }+ // NB: The margin collapses into the block-start margin of a block container the box is in, so it moves that+ // container, and everything in it, rather than the box in its own block container.+ let offset =+ self.block_offset_adjustment_from_pending_ancestor_block_start_margins_where_innermost_group_holds(+ node,+ Some(margins),+ ) - self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node);+ let moved_block_container = self+ .margin_states+ .borrow()+ .iter()+ .rev()+ .find(|state| state.top_margin_group.is_some_and(|group| !group.joins_enclosing_group))+ .map(|state| state.block_container);+ PositionWithoutClearance {+ flow_position: flow_position + offset,+ moves_contents: moved_block_container+ .filter(|_| offset != CssPixels::default())+ .map(|block_container| (block_container, offset)),+ }+ }++ /// Resolves the block size of `sized_child`, a box without a formatting context of its own, as far as it is known+ /// before the contents of the box are laid out.+ fn resolve_block_size_before_contents(+ &self,+ sized_child: ChildSizedHere<'_>,+ available_space: AvailableSpace,+ constraints: ContainingBlockConstraints,+ ) {+ let node = sized_child.node;+ self.resolve_used_block_size_if_not_treated_as_auto(sized_child, available_space, constraints);+ // NOTE: Flex containers with an automatic block size are treated as max-content, so resolve it early.+ if self.facts(node).has_auto_content_box_size() || self.style(node).display().is_flex_inside() {+ self.resolve_used_block_size_if_treated_as_auto(sized_child, available_space, constraints, None, None);+ }+ }++ /// Resolves the block size of `sized_child`, a box without a formatting context of its own, from `children`: the+ /// sum of the contents this flow laid out in the box, or `None` for contents that take up no block size.+ fn resolve_block_size_after_contents(+ &self,+ sized_child: ChildSizedHere<'_>,+ available_space: AvailableSpace,+ constraints: ContainingBlockConstraints,+ children: Option<&ChildrenSummary>,+ ) {+ let node = sized_child.node;+ // NB: Tables set their block size during their run in every case.+ if !self.style(node).display().is_table_inside() {+ self.resolve_used_block_size_if_treated_as_auto(sized_child, available_space, constraints, None, children);+ }+ if self.sizing().block_size_is_ratio_dependent(+ sizing_context::SizedBox::new(node, &self.metrics_of(sized_child)),+ available_space,+ constraints,+ ) {+ let content_block_size = self.compute_automatic_block_size_for_block_level_element(+ sized_child,+ self.cells_laid_out_here(sized_child)+ .available_inner_space_or_constraints_from(available_space),+ constraints,+ None,+ children,+ );+ self.sizing().apply_automatic_minimum_block_size_from_aspect_ratio(+ self.sized_cells_of(sized_child),+ available_space,+ constraints,+ Some(content_block_size),+ );+ }+ }++ /// The cells of `node`, a box without a formatting context of its own that this flow lays out with `input` in a+ /// containing block `containing_block_inline_size` wide, sized the way its layout sizes them around contents that+ /// take up no block size.+ fn cells_of_box_sized_around_no_contents(+ &self,+ node: Node,+ containing_block_inline_size: CssPixels,+ input: &LayoutInput,+ ) -> used_values::RootInputCells {+ let constraints = input.containing_block_constraints;+ let cells = Cell::new(used_values::cells_before_sizing(&self.run.pass, node, constraints));+ let sized_box = ChildSizedHere::laid_out_in(node, &cells);+ self.resolve_vertical_box_model_metrics(sized_box, containing_block_inline_size, constraints);+ self.resolve_block_size_known_ahead_of_contents(sized_box, input);+ if let Some(content_inline_size) =+ self.resolve_root_inline_metrics_and_content_size(sized_box, input.available_space, constraints, None)+ {+ self.update_cells_of(sized_box, |cells| cells.set_content_inline_size(content_inline_size));+ }+ let available_space =+ self.sizing()+ .available_space_for_block_size_resolution(node, input.available_space, constraints);+ self.resolve_block_size_before_contents(sized_box, available_space, constraints);+ self.resolve_block_size_after_contents(sized_box, available_space, constraints, None);+ cells.get()+ }++ /// Adds to `contents` the margins of the contents of `node` that collapse with its block-start margin, as far as+ /// the box tree and the sizes of the boxes tell before the contents are laid out: the block-start margins of its+ /// first in-flow children, and both margins of the boxes before them that margins collapse through. Returns+ /// whether margins collapse through `node` altogether. `node` is laid out with `input`, in a containing block+ /// `containing_block_inline_size` wide.+ // https://drafts.csswg.org/css2/#collapsing-margins+ fn add_margins_of_contents_collapsing_with_block_start_margin(+ &self,+ node: Node,+ containing_block_inline_size: CssPixels,+ input: &LayoutInput,+ contents: &mut MarginsOfContents<'_>,+ depth: usize,+ ) -> bool {+ // NB: A chain of first children nested deeper than this is left out, so the scan never runs far down the stack.+ const DEEPEST_FIRST_CHILD: usize = 32;+ let facts = self.facts(node);+ if depth > DEEPEST_FIRST_CHILD+ || !facts.is_block_container()+ || formatting_context::formatting_context_type_created_by_box(facts).is_some()+ {+ return false;+ }+ let cells = self.cells_of_box_sized_around_no_contents(node, containing_block_inline_size, input);+ if cells.border_top() > CssPixels::default() || cells.padding_top() > CssPixels::default() {+ return false;+ }+ if facts.children_are_inline() {+ // NB: A line box keeps the margins after it apart from the box's block-start margin, and what a line+ // holds is known only once it is laid out.+ if self+ .children(node)+ .any(|child| !self.facts(child).is_floating_or_absolutely_positioned())+ || self.run.pass.children(node).any(|child| !self.facts(child).is_box())+ {+ return false;+ }+ for child in self.children(node) {+ if self.facts(child).is_floating() {+ contents.passed_floats = contents.passed_floats.with(FloatSides::of_float(&self.style(child)));+ }+ }+ } else {+ let space_available_for_contents = if facts.is_anonymous() {+ input.available_space+ } else {+ cells.available_inner_space_or_constraints_from(input.available_space)+ };+ // NB: The input of the contents doesn't depend on what their block-axis percentage insets resolve against.+ let input_of_contents = self.child_layout_input(+ BlockContainer {+ node,+ cells,+ percentage_block_insets_have_definite_basis: false,+ },+ input.clone(),+ space_available_for_contents,+ );+ let content_inline_size = cells.content_inline_size();+ for child in self.children(node) {+ let child_facts = self.facts(child);+ let child_style = self.style(child);+ if child_facts.is_floating() {+ contents.passed_floats = contents.passed_floats.with(FloatSides::of_float(&child_style));+ }+ if child_facts.is_floating_or_absolutely_positioned() || child_facts.is_list_item_marker_box() {+ continue;+ }+ let sides_child_clears = FloatSides::cleared_by(&child_style);+ // NB: A float the scan went through goes wherever these boxes go, so no margin takes the child past+ // it, and the clearance that gets the child keeps its margins apart from these.+ if sides_child_clears.any_of(contents.passed_floats) {+ return false;+ }+ let margins_before_child = contents.margins;+ let mut child_sides = used_values::RootInputCells::default();+ child_sides.resolve_vertical_box_model(&child_style, content_inline_size);+ contents.margins = contents.margins.with_margin(child_sides.margin_top());+ let margins_collapse_through_child = self.add_margins_of_contents_collapsing_with_block_start_margin(+ child,+ content_inline_size,+ &input_of_contents,+ contents,+ depth + 1,+ );+ // NB: Clearance would keep the margins of the child apart from these.+ if (sides_child_clears.left || sides_child_clears.right)+ && (contents.gets_clearance)(sides_child_clears, contents.margins)+ {+ contents.margins = margins_before_child;+ return false;+ }+ if !margins_collapse_through_child {+ return false;+ }+ contents.margins = contents.margins.with_margin(child_sides.margin_bottom());+ }+ }+ margins_can_collapse_through_box_itself(facts, false, || cells.border_box_block_size(false))+ }++ /// The lowest margin edges, in the root's coordinate space, of the left and of the right floats inside+ /// `block_container`, and of those outside it.+ fn lowest_margin_edges_of_floats_inside_and_outside(&self, block_container: Node) -> [[Option<CssPixels>; 2]; 2] {+ let mut edges = [[None; 2]; 2];+ for floating_box in self.floats.borrow().iter() {+ let edge = floating_box.margin_box_rect_in_root_coordinate_space.bottom()+ + self.block_offset_adjustment_from_pending_ancestor_block_start_margins(floating_box.box_);+ let is_outside = !self.is_inclusive_ancestor_of(block_container, floating_box.box_);+ let lowest = &mut edges[usize::from(is_outside)][usize::from(floating_box.side != FloatSide::Left)];+ *lowest = (*lowest).max(Some(edge));+ }+ edges+ }+/// Where the flow goes from `flow_position` to place `node` past the floats its `clear` names, if they reach below- /// that position.+ /// where the box would go without clearance. For a block-level box of this flow, that depends on+ /// `block_level_box`. What a line clears has none of that, and goes at the flow position.fn flow_position_past_floats_cleared_by(&self,node: Node,flow_position: CssPixels,+ block_level_box: Option<BlockLevelBoxInFlow<'_>>,containing_block_position_in_root: FfiCssPixelPoint,) -> Option<CssPixels> {- let style = self.style(node);- let mut position_past_floats = None;- let mut clear_to = |lowest_margin_edge_in_root: Option<CssPixels>| {+ // https://drafts.csswg.org/css2/#flow-control+ // Computing the clearance of an element on which clear is set is done by first determining the hypothetical+ // position of the element's top border edge. This position is where the actual top border edge would have been+ // if the element's clear property had been none.+ // If this hypothetical position of the element's top border edge is not past the relevant floats, then+ // clearance is introduced, and margins collapse according to the rules in 8.3.1.+ let cleared_sides = FloatSides::cleared_by(&self.style(node));+ let deferred_float_sides = if self.run.fragmentainer.is_some() {+ self.deferred_float_sides.get()+ } else {+ 0+ };+ let has_float_to_clear = (cleared_sides.left+ && (self.lowest_left_margin_edge.get().is_some() || deferred_float_sides & 1 != 0))+ || (cleared_sides.right+ && (self.lowest_right_margin_edge.get().is_some() || deferred_float_sides & 2 != 0));+ if !has_float_to_clear {+ return None;+ }+ let without_clearance = match block_level_box {+ Some(block_level_box) => self.position_of_box_without_clearance(+ node,+ flow_position,+ block_level_box,+ containing_block_position_in_root,+ ),+ None => PositionWithoutClearance {+ flow_position,+ moves_contents: None,+ },+ };+ // NB: A margin can take the box up beside floats that the flow is already past, which it then reads.+ if without_clearance.flow_position < flow_position {+ self.note_float_read_from(+ containing_block_position_in_root.y+ + self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node)+ + without_clearance.flow_position,+ );+ }+ self.flow_position_past_floats_on(+ node,+ cleared_sides,+ &without_clearance,+ containing_block_position_in_root,+ )+ }++ /// Where the flow goes to place `node` past the floats on `cleared_sides`, if they reach below `without_clearance`,+ /// where the box would go if it cleared none.+ fn flow_position_past_floats_on(+ &self,+ node: Node,+ cleared_sides: FloatSides,+ without_clearance: &PositionWithoutClearance,+ containing_block_position_in_root: FfiCssPixelPoint,+ ) -> Option<CssPixels> {+ let FloatSides {+ left: clears_left,+ right: clears_right,+ } = cleared_sides;+ let mut introduces_clearance = false;+ let mut position_past_floats: Option<CssPixels> = None;+ let mut clear_past = |lowest_margin_edge_in_root: Option<CssPixels>, flow_position_without_clearance| {let Some(clearance_block_offset_in_root) = lowest_margin_edge_in_root else {return;};@@ -2862,9 +3279,8 @@ impl<'pass> BlockFormattingContext<'pass> {let clearance = clearance_block_offset_in_root- containing_block_position_in_root.y- self.block_offset_adjustment_from_pending_ancestor_block_start_margins(node);- if clearance > position_past_floats.unwrap_or(flow_position) {- position_past_floats = Some(clearance);- }+ introduces_clearance |= clearance > flow_position_without_clearance;+ position_past_floats = position_past_floats.max(Some(clearance));};if let Some(block_size) = self@@ -2873,21 +3289,34 @@ impl<'pass> BlockFormattingContext<'pass> {.and_then(|fragmentainer| fragmentainer.block_size){let sides = self.deferred_float_sides.get();- if (sides & 1 != 0 && matches!(style.clear(), clear::LEFT | clear::BOTH | clear::INLINE_START))- || (sides & 2 != 0 && matches!(style.clear(), clear::RIGHT | clear::BOTH | clear::INLINE_END))- {- clear_to(Some(block_size));+ if (sides & 1 != 0 && clears_left) || (sides & 2 != 0 && clears_right) {+ clear_past(Some(block_size), without_clearance.flow_position);}}// FIXME: Honor writing-mode, direction and text-orientation.- if matches!(style.clear(), clear::LEFT | clear::BOTH | clear::INLINE_START) {- clear_to(self.lowest_left_margin_edge.get());- }- if matches!(style.clear(), clear::RIGHT | clear::BOTH | clear::INLINE_END) {- clear_to(self.lowest_right_margin_edge.get());+ match without_clearance.moves_contents {+ None => {+ if clears_left {+ clear_past(self.lowest_left_margin_edge.get(), without_clearance.flow_position);+ }+ if clears_right {+ clear_past(self.lowest_right_margin_edge.get(), without_clearance.flow_position);+ }+ }+ // NB: The floats in a block container that the box's margin moves go wherever the box does, so no margin+ // takes the box past them. They are as far below it as they are below the flow position now.+ Some((moved_block_container, offset)) => {+ let [inside, outside] = self.lowest_margin_edges_of_floats_inside_and_outside(moved_block_container);+ for (side, is_cleared) in [clears_left, clears_right].into_iter().enumerate() {+ if is_cleared {+ clear_past(inside[side], without_clearance.flow_position - offset);+ clear_past(outside[side], without_clearance.flow_position);+ }+ }+ }}- position_past_floats+ position_past_floats.filter(|_| introduces_clearance)}fn compute_normal_flow_inline_offset(@@ -3476,7 +3905,21 @@ impl<'pass> BlockFormattingContext<'pass> {self.margin_state_mut().add_margin(self.metrics_of(sized_child).margin_top());}- let introduced_clearance = self.clear_floating_boxes(node, None, containing_block_position_in_root);+ let block_start_margin = if discards_block_start_margin {+ CssPixels::default()+ } else {+ self.metrics_of(sized_child).margin_top()+ };+ let introduced_clearance = self.clear_floating_boxes(+ node,+ None,+ Some(BlockLevelBoxInFlow {+ block_start_margin,+ containing_block_inline_size: block_container_inline_size,+ input: &input,+ }),+ containing_block_position_in_root,+ );if introduced_clearance {self.margin_state_mut().reset();}@@ -3686,21 +4129,11 @@ impl<'pass> BlockFormattingContext<'pass> {// independent run only commits in its prelude — so independent// children resolve these pre-body block sizes there instead.if !has_independent_formatting_context {- self.resolve_used_block_size_if_not_treated_as_auto(+ self.resolve_block_size_before_contents(sized_child,available_space_for_block_size_resolution,input.containing_block_constraints,);- // NOTE: Flex containers with an automatic block size are treated as max-content, so resolve it early.- if facts.has_auto_content_box_size() || style.display().is_flex_inside() {- self.resolve_used_block_size_if_treated_as_auto(- sized_child,- available_space_for_block_size_resolution,- input.containing_block_constraints,- None,- None,- );- }}if continuation.is_some() && self.cells_laid_out_here(sized_child).has_definite_block_size() {@@ -3920,37 +4353,14 @@ impl<'pass> BlockFormattingContext<'pass> {}// An independent run resolved its automatic block size in its own epilogue; same-flow children still resolve- // here. Tables set their block size during their run in every case.- if !has_independent_formatting_context && !style.display().is_table_inside() {- self.resolve_used_block_size_if_treated_as_auto(+ // here.+ if !has_independent_formatting_context {+ self.resolve_block_size_after_contents(sized_child,available_space_for_block_size_resolution,input.containing_block_constraints,- None,- summary_of_children_laid_out_in_this_flow.as_ref(),- );- }- if !has_independent_formatting_context- && self.sizing().block_size_is_ratio_dependent(- sizing_context::SizedBox::new(node, &self.metrics_of(sized_child)),- available_space_for_block_size_resolution,- input.containing_block_constraints,- )- {- let content_block_size = self.compute_automatic_block_size_for_block_level_element(- sized_child,- self.cells_laid_out_here(sized_child)- .available_inner_space_or_constraints_from(available_space_for_block_size_resolution),- input.containing_block_constraints,- None,summary_of_children_laid_out_in_this_flow.as_ref(),);- self.sizing().apply_automatic_minimum_block_size_from_aspect_ratio(- self.sized_cells_of(sized_child),- available_space_for_block_size_resolution,- input.containing_block_constraints,- Some(content_block_size),- );}if let Some(continuation) = &continuation {@@ -4109,6 +4519,8 @@ impl<'pass> BlockFormattingContext<'pass> {}/// Places `unplaced`, the layout of `sized_child`, and moves the flow past it.+ // NB: This is out of line to keep what it holds off the stack of every level of nested boxes.+ #[inline(never)]fn place_laid_out_block_level_box(&self,sized_child: ChildSizedHere<'_>,@@ -4534,8 +4946,16 @@ impl<'pass> BlockFormattingContext<'pass> {}self.rebuild_float_bands();let flow_position = self.flow_position();- let position_past_floats =- self.flow_position_past_floats_cleared_by(node, flow_position, containing_block_position_in_root);+ let position_past_floats = self.flow_position_past_floats_cleared_by(+ node,+ flow_position,+ Some(BlockLevelBoxInFlow {+ block_start_margin: entry.cells.margin_top(),+ containing_block_inline_size: entry.key.containing_block_inline_size,+ input: &entry.key.input,+ }),+ containing_block_position_in_root,+ );let block_start_margin = {let mut margin_state = self.margin_state().clone();let mut margins_left_by_contents = entry.margins_left_by_contents.clone();@@ -4579,8 +4999,16 @@ impl<'pass> BlockFormattingContext<'pass> {}self.rebuild_float_bands();let flow_position = self.flow_position();- let position_past_floats =- self.flow_position_past_floats_cleared_by(node, flow_position, containing_block_position_in_root);+ let position_past_floats = self.flow_position_past_floats_cleared_by(+ node,+ flow_position,+ Some(BlockLevelBoxInFlow {+ block_start_margin: entry.cells.margin_top(),+ containing_block_inline_size: entry.key.containing_block_inline_size,+ input: &entry.key.input,+ }),+ containing_block_position_in_root,+ );if floats_read.read_before_top_margins_settled&& (floats_read.stored_in_open_top_margin_group|| floats_read.margins_before_box != self.margin_state().margins_since_reset@@ -4705,7 +5133,16 @@ impl<'pass> BlockFormattingContext<'pass> {self.run.pass.layout_trace().reused_in_flow_subtree(node);self.cells_laid_out_here(sized_child).set(entry.cells);let metrics = self.metrics_of(sized_child);- let introduces_clearance = self.clear_floating_boxes(node, None, containing_block_position_in_root);+ let introduces_clearance = self.clear_floating_boxes(+ node,+ None,+ Some(BlockLevelBoxInFlow {+ block_start_margin: metrics.margin_top(),+ containing_block_inline_size: block_container.cells.content_inline_size,+ input: &entry.key.input,+ }),+ containing_block_position_in_root,+ );let mut margins_left_by_contents = entry.margins_left_by_contents.clone();let block_start_margin = collapse_block_start_margin_of_reused_child(&mut self.margin_state_mut(),@@ -5151,8 +5588,16 @@ impl<'pass> BlockFormattingContext<'pass> {}self.rebuild_float_bands();let flow_position = self.flow_position();- let position_past_floats =- self.flow_position_past_floats_cleared_by(node, flow_position, containing_block_position_in_root);+ let position_past_floats = self.flow_position_past_floats_cleared_by(+ node,+ flow_position,+ Some(BlockLevelBoxInFlow {+ block_start_margin: entry.cells.margin_top(),+ containing_block_inline_size: entry.key.containing_block_inline_size,+ input: &entry.key.input,+ }),+ containing_block_position_in_root,+ );let block_start_margin = {let mut margin_state = self.margin_state().clone();let mut margins_left_by_contents = entry.margins_left_by_contents.clone();diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 69628da139d..ed9738131af 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -1642,7 +1642,7 @@ impl<'context> InlineFormattingContext<'context> {fn clear_floating_boxes(&self, node: Node) -> bool {self.parent- .clear_floating_boxes(node, Some(self), self.containing_block_position_in_bfc_root)+ .clear_floating_boxes(node, Some(self), None, self.containing_block_position_in_bfc_root)}fn reset_parent_margin_state(&self) {diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-001-ref.xht b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-001-ref.xhtnew file mode 100644index 00000000000..c6087ea7525--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-001-ref.xht@@ -0,0 +1,36 @@+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">++<html xmlns="http://www.w3.org/1999/xhtml">++ <head>++ <title>CSS Reftest Reference</title>++ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />++ <style type="text/css"><![CDATA[+ div+ {+ background-color: green;+ height: 1.25em;+ margin: 1em 0em 1.25em;+ width: 6.25em;+ }++ div + div {margin-bottom: 2.5em;}+ ]]></style>++ </head>++ <body>++ <p>Test passes if there is no red.</p>++ <div></div>++ <div></div>++ <div></div>++ </body>+</html>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-003-ref.xht b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-003-ref.xhtnew file mode 100644index 00000000000..f2c3f436186--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-003-ref.xht@@ -0,0 +1,36 @@+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">++<html xmlns="http://www.w3.org/1999/xhtml">++ <head>++ <title>CSS Reftest Reference</title>++ <link rel="author" title="Gérard Talbot" href="http://www.gtalbot.org/BrowserBugsSection/css21testsuite/" />++ <style type="text/css"><![CDATA[+ div+ {+ background-color: green;+ height: 1.25em;+ margin: 1em 0em 1.25em;+ width: 6.25em;+ }++ div + div {margin-bottom: 3.75em;}+ ]]></style>++ </head>++ <body>++ <p>Test passes if there is no red.</p>++ <div></div>++ <div></div>++ <div></div>++ </body>+</html>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-001.xht b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-001.xhtnew file mode 100644index 00000000000..8809d485fcf--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-001.xht@@ -0,0 +1,54 @@+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">+<html xmlns="http://www.w3.org/1999/xhtml">+ <head>+ <title>CSS Test: Clearance calculations - clear box non-collapsing margins with larger margings than clearance</title>+ <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />+ <link rel="help" href="http://www.w3.org/TR/CSS21/visuren.html#flow-control" />+ <link rel="match" href="../../../../../expected/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-001-ref.xht" />++ <meta name="flags" content="ahem image" />+ <meta name="assert" content="When a cleared element has more top margin than the height of the floated element the margin is collpased with previous in-flow children. The element is then placed at the point of the resulting collapsed margin." />+ <!-- C1 = H-M2 -->+ <!-- C2 = Max(M1, M2) - M1 - M2 -->+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />+ <style type="text/css">+ div+ {+ font: 20px/1em Ahem;+ width: 5em;+ }+ #parent+ {+ background: url('support/clear-clearance-calculation-001.png');+ min-height: 6em;+ }+ div div+ {+ background: green;+ height: 1em;+ }+ #div1+ {+ margin-bottom: 1em;+ }+ #div2+ {+ float: left;+ margin: 0;+ }+ #div3+ {+ clear: left;+ margin-top: 4em;+ }+ </style>+ </head>+ <body>+ <p>Test passes if there is no red.</p>+ <div id="parent">+ <div id="div1"></div>+ <div id="div2"></div>+ <div id="div3"></div>+ </div>+ </body>+</html>\ No newline at end of filediff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-003.xht b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-003.xhtnew file mode 100644index 00000000000..4ecf7fa5a89--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-003.xht@@ -0,0 +1,57 @@+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">+<html xmlns="http://www.w3.org/1999/xhtml">+ <head>+ <title>CSS Test: Clearance calculations - clear box collapsing margins</title>+ <link rel="author" title="Microsoft" href="http://www.microsoft.com/" />+ <link rel="help" href="http://www.w3.org/TR/CSS21/visuren.html#flow-control" />+ <link rel="match" href="../../../../../expected/wpt-import/css/CSS2/floats-clear/clear-clearance-calculation-003-ref.xht" />++ <meta name="assert" content="When a cleared element has more top margin than the height of the floated element the margin is collpased with previous in-flow children. The element is then placed at the point of the resulting collapsed margin." />+ <!-- C1 = H-MT2 -->+ <!-- C2 = Max(M1, MT2, MB2) - M1 - MT2 -->+ <link rel="stylesheet" type="text/css" href="../../../fonts/ahem.css" />+ <style type="text/css">+ div+ {+ font: 20px/1em Ahem;+ width: 5em;+ }+ #parent+ {+ background: url('support/clear-clearance-calculation-003.png');+ min-height: 7em;+ position: relative;+ }+ #div1+ {+ margin-bottom: 1em;+ }+ #div2+ {+ float: left;+ margin: 0;+ }+ #div3+ {+ background: red;+ clear: left;+ margin-bottom: 5em;+ margin-top: 4em;+ }+ #div1, #div2, #div4+ {+ background: green;+ height: 1em;+ }+ </style>+ </head>+ <body>+ <p>Test passes if there is no red.</p>+ <div id="parent">+ <div id="div1"></div>+ <div id="div2"></div>+ <div id="div3"></div>+ <div id="div4"></div>+ </div>+ </body>+</html>\ No newline at end of filediff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-on-parent-with-margins-no-clearance.html b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-on-parent-with-margins-no-clearance.htmlnew file mode 100644index 00000000000..97064c21409--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/floats-clear/clear-on-parent-with-margins-no-clearance.html@@ -0,0 +1,12 @@+<!DOCTYPE html>+<title>Child of block with clear</title>+<link rel="author" title="Morten Stenshorne" href="[email protected]">+<link rel="help" href="https://www.w3.org/TR/CSS22/visuren.html#flow-control" title="9.5.2 Controlling flow next to floats: the 'clear' property">+<link rel="match" href="../../../../../expected/wpt-import/css/CSS2/floats-clear/../../reference/ref-filled-green-100px-square-only.html">+<p>Test passes if there is a filled green square.</p>+<div style="border-top:1px solid white; position:relative; z-index:-1; top:-51px; width:100px; background:green;">+ <div style="float:left; width:100px; height:50px; background:white;"></div>+ <div style="clear:left; margin-top:25px;">+ <div style="height:50px; margin-top:150px; background:white;"></div>+ </div>+</div>diff --git a/Tests/LibWeb/Text/expected/clearance-from-hypothetical-position.txt b/Tests/LibWeb/Text/expected/clearance-from-hypothetical-position.txtnew file mode 100644index 00000000000..b11860864a3--- /dev/null+++ b/Tests/LibWeb/Text/expected/clearance-from-hypothetical-position.txt@@ -0,0 +1,58 @@+after-clearfix: 0,80 0,110 200,110 400,110+after-block-without-clearfix: 0,80+after-block-as-tall-as-float: 0,80+margin-reaches-past-float: 0,100+margin-ends-at-float-end: 0,80+margin-ends-beside-float: 0,80+sibling-margin-reaches-past-float: 0,110+pulled-up-beside-float-after-short-block: 0,80+pulled-up-but-still-below-float: 0,90+pulled-up-by-one-pixel: 0,80+no-margin: 0,80+collapses-with-sibling-positive-margin: 0,80+collapses-with-sibling-negative-margin: 0,80+after-empty-block-with-negative-margin: 0,80+negative-margin-on-parent-of-clearing-box: 0,50 0,80+clearing-first-child: 0,80 0,80+clearing-first-grandchild: 0,80 0,80 0,80+first-child-margin-pulls-box-up: 0,80 0,80+first-child-margin-reaches-past-float: 0,100 0,100+first-child-margin-ends-beside-float: 0,80 0,80+margin-after-empty-first-child: 0,100 0,100+first-child-margin-behind-border: 0,80 0,181+float-in-box-the-margin-moves: 0,10 0,10 0,90+float-in-box-the-margin-moves-pulled-up: 0,0 0,0 0,80+float-in-sibling-in-box-the-margin-moves: 0,0 0,0 0,80+float-outside-box-the-margin-moves: 0,0 0,200 0,200+float-in-outer-of-two-boxes-the-margin-moves: 0,0 0,0 0,0 0,80+floats-inside-and-outside-box-the-margin-moves: 0,0 0,0 500,0 0,120+clears-only-float-outside-box-the-margin-moves: 0,0 0,200 500,200 0,200+clears-only-float-inside-box-the-margin-moves: 0,0 0,0 500,0 0,80+block-between-float-and-box-in-box: 0,0 0,0 0,110+float-after-pending-margin: 0,110+in-nested-block: 0,80+in-inline-box: 0,80+clear-left-beside-right-float: 0,50+clear-right-beside-right-float: 0,120+empty: 0,80 0,80+empty-with-bottom-margin: 0,80 0,100+flow-root: 0,80+border-top: 0,80+float-after-block: 0,100+clearing-float: 0,50+two-in-a-row: 0,80 0,80+vertical: 80,0+first-child-percentage-margin-in-narrow-box: 0,80+first-child-percentage-margin-in-wide-box: 0,100+first-child-percentage-margin-in-padded-box: 0,80+first-grandchild-percentage-margin: 0,80+zero-height-block-before-first-child: 0,100+zero-min-height-block-before-first-child: 0,100+zero-height-block-with-contents-before-first-child: 0,80+min-height-block-before-first-child: 0,80+aspect-ratio-block-before-first-child: 0,80+first-child-clears-side-without-float: 0,100+first-child-margin-reaches-past-float-it-clears: 0,100+first-child-margin-ends-beside-float-it-clears: 0,80+first-child-pulls-box-up-and-clears-side-without-float: 0,80+first-grandchild-margin-ends-past-float-it-clears: 0,90diff --git a/Tests/LibWeb/Text/expected/layout-block-children-in-parallel.txt b/Tests/LibWeb/Text/expected/layout-block-children-in-parallel.txtindex 756e59b18b1..c329173d10c 100644--- a/Tests/LibWeb/Text/expected/layout-block-children-in-parallel.txt+++ b/Tests/LibWeb/Text/expected/layout-block-children-in-parallel.txt@@ -1,9 +1,9 @@243 boxesfirst layout: 500x2628, digest 341720136-narrower root: 420x2868, digest 851212892-a float reaches further: 420x3028, digest 165634362-a float is removed: 420x3024, digest 242798716-one paragraph grows: 420x3054, digest 404230740-a margin before everything grows: 420x3086, digest 959660885-a new float comes in: 420x3086, digest 145006701-clearance goes: 420x2952, digest 462459129+narrower root: 420x2916, digest 390305384+a float reaches further: 420x3060, digest 523700326+a float is removed: 420x3040, digest 914502595+one paragraph grows: 420x3070, digest 397139334+a margin before everything grows: 420x3102, digest 952569479+a new float comes in: 420x3102, digest 137915295+clearance goes: 420x2968, digest 477532684diff --git a/Tests/LibWeb/Text/expected/multicol-box-reuse-geometry.txt b/Tests/LibWeb/Text/expected/multicol-box-reuse-geometry.txtindex 9e0bb4766da..226a68b0ce9 100644--- a/Tests/LibWeb/Text/expected/multicol-box-reuse-geometry.txt+++ b/Tests/LibWeb/Text/expected/multicol-box-reuse-geometry.txt@@ -10,9 +10,9 @@ avoid box with overflowing lines after an empty formatting context, lead 30: heiavoid box with overflowing lines after an empty formatting context, lead 45: height 63: 0,0 200x45 | 0,45 200x17.5 + 210,0 200x22.5 | 210,22.5 200x0 | 210,22.5 200x10 | 210,32.5 200x30avoid box with overflowing lines after an empty formatting context, lead 60: height 70: 0,0 200x60 | 0,60 200x10 + 210,0 200x30 | 210,30 200x0 | 210,30 200x10 | 210,40 200x30avoid box with overflowing lines after an empty formatting context, lead 75: height 78: 0,0 200x75 | 0,75 200x2.5 + 210,0 200x37.5 | 210,37.5 200x0 | 210,37.5 200x10 | 210,47.5 200x30-clear box with a float and a negative margin, lead 0: height 90: 0,0 30x30 | 0,0 200x0 | 0,0 200x40 | 0,-10 200x100 + 210,0 200x0 | 210,0 30x90 | 0,-10 200x10 | 210,0 200x30-clear box with a float and a negative margin, lead 15: height 90: 0,0 30x30 | 0,0 200x15 | 0,15 200x40 | 0,5 200x85 + 210,0 200x0 | 210,0 30x90 | 0,5 200x10 | 210,0 200x30-clear box with a float and a negative margin, lead 30: height 90: 0,0 30x30 | 0,0 200x30 | 0,30 200x40 | 0,20 200x70 + 210,0 200x0 | 210,0 30x90 | 0,20 200x10 | 210,0 200x30+clear box with a float and a negative margin, lead 0: height 90: 0,0 30x30 | 0,0 200x0 | 0,0 200x40 | 0,30 200x60 + 210,0 200x0 | 210,0 30x90 | 0,30 200x10 | 210,0 200x30+clear box with a float and a negative margin, lead 15: height 90: 0,0 30x30 | 0,0 200x15 | 0,15 200x40 | 0,30 200x60 + 210,0 200x0 | 210,0 30x90 | 0,30 200x10 | 210,0 200x30+clear box with a float and a negative margin, lead 30: height 90: 0,0 30x30 | 0,0 200x30 | 0,30 200x40 | 0,30 200x60 + 210,0 200x0 | 210,0 30x90 | 0,30 200x10 | 210,0 200x30clear box with a float and a negative margin, lead 45: height 90: 0,0 30x30 | 0,0 200x45 | 0,45 200x40 | 0,35 200x55 + 210,0 200x0 | 210,0 30x90 | 0,35 200x10 | 210,0 200x30clear box with a float and a negative margin, lead 60: height 100: 0,0 30x30 | 0,0 200x60 | 0,60 200x40 | 0,50 200x50 + 210,0 200x0 | 210,0 30x90 | 0,50 200x10 | 210,0 200x30clear box with a float and a negative margin, lead 75: height 115: 0,0 30x30 | 0,0 200x75 | 0,75 200x40 | 0,65 200x50 + 210,0 200x0 | 210,0 30x90 | 0,65 200x10 | 210,0 200x30diff --git a/Tests/LibWeb/Text/input/clearance-from-hypothetical-position.html b/Tests/LibWeb/Text/input/clearance-from-hypothetical-position.htmlnew file mode 100644index 00000000000..2138a535ca4--- /dev/null+++ b/Tests/LibWeb/Text/input/clearance-from-hypothetical-position.html@@ -0,0 +1,89 @@+<!DOCTYPE html>+<script src="include.js"></script>+<style>+ body { margin: 0; width: 600px; }+ .root { position: relative; display: flow-root; }+ .clearfix::after { content: ""; display: table; clear: both; }+ .float { float: left; width: 300px; height: 80px; }+ .right { float: right; width: 100px; height: 120px; }+ .column { float: left; width: 200px; height: 60px; margin-top: 30px; }+ .tall { height: 80px; }+ .short { height: 10px; }+</style>+<div class="root" id="after-clearfix"><div class="clearfix"><div class="float"></div></div><div class="clearfix x" style="clear: both; margin-top: -30px"><div class="column x"></div><div class="column x"></div><div class="column x"></div></div></div>+<div class="root" id="after-block-without-clearfix"><div><div class="float"></div></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="after-block-as-tall-as-float"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="margin-reaches-past-float"><div class="float"></div><div class="x" style="clear: both; margin-top: 100px"><div class="short"></div></div></div>+<div class="root" id="margin-ends-at-float-end"><div class="float"></div><div class="x" style="clear: both; margin-top: 80px"><div class="short"></div></div></div>+<div class="root" id="margin-ends-beside-float"><div class="float"></div><div class="x" style="clear: both; margin-top: 20px"><div class="short"></div></div></div>+<div class="root" id="sibling-margin-reaches-past-float"><div class="float"></div><div class="short" style="margin-bottom: 100px"></div><div class="x" style="clear: both; margin-top: 20px"><div class="short"></div></div></div>+<div class="root" id="pulled-up-beside-float-after-short-block"><div class="float"></div><div class="short"></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="pulled-up-but-still-below-float"><div class="float"></div><div class="tall" style="height: 120px"></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="pulled-up-by-one-pixel"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both; margin-top: -1px"><div class="short"></div></div></div>+<div class="root" id="no-margin"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both"><div class="short"></div></div></div>+<div class="root" id="collapses-with-sibling-positive-margin"><div class="float"></div><div class="tall" style="margin-bottom: 20px"></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="collapses-with-sibling-negative-margin"><div class="float"></div><div class="tall" style="margin-bottom: -40px"></div><div class="x" style="clear: both; margin-top: 20px"><div class="short"></div></div></div>+<div class="root" id="after-empty-block-with-negative-margin"><div class="float"></div><div class="tall"></div><div style="margin-bottom: -30px"></div><div class="x" style="clear: both"><div class="short"></div></div></div>+<div class="root" id="negative-margin-on-parent-of-clearing-box"><div class="float"></div><div class="tall"></div><div class="x" style="margin-top: -30px"><div class="short x" style="clear: both"></div></div></div>+<div class="root" id="clearing-first-child"><div class="float"></div><div class="tall"></div><div class="x"><div class="short x" style="clear: both; margin-top: -30px"></div></div></div>+<div class="root" id="clearing-first-grandchild"><div class="float"></div><div class="tall"></div><div class="x"><div class="x"><div class="short x" style="clear: both; margin-top: -30px"></div></div></div></div>+<div class="root" id="first-child-margin-pulls-box-up"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both"><div class="short x" style="margin-top: -30px"></div></div></div>+<div class="root" id="first-child-margin-reaches-past-float"><div class="float"></div><div class="x" style="clear: both"><div class="short x" style="margin-top: 100px"></div></div></div>+<div class="root" id="first-child-margin-ends-beside-float"><div class="float"></div><div class="x" style="clear: both"><div class="short x" style="margin-top: 20px"></div></div></div>+<div class="root" id="margin-after-empty-first-child"><div class="float"></div><div class="x" style="clear: both"><div style="margin: 30px 0 100px"></div><div class="short x"></div></div></div>+<div class="root" id="first-child-margin-behind-border"><div class="float"></div><div class="x" style="clear: both; border-top: 1px solid"><div class="short x" style="margin-top: 100px"></div></div></div>+<div class="root" id="float-in-box-the-margin-moves"><div class="short"></div><div class="x"><div class="float x"></div><div class="x" style="clear: both; margin-top: 200px"><div class="short"></div></div></div></div>+<div class="root" id="float-in-box-the-margin-moves-pulled-up"><div class="x"><div class="float x"></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div></div>+<div class="root" id="float-in-sibling-in-box-the-margin-moves"><div class="x"><div><div class="float x"></div></div><div class="x" style="clear: both; margin-top: 400px"><div class="short"></div></div></div></div>+<div class="root" id="float-outside-box-the-margin-moves"><div class="float x"></div><div class="x"><div class="x" style="clear: both; margin-top: 200px"><div class="short"></div></div></div></div>+<div class="root" id="float-in-outer-of-two-boxes-the-margin-moves"><div class="x"><div class="float x"></div><div class="x"><div class="x" style="clear: both; margin-top: 200px"><div class="short"></div></div></div></div></div>+<div class="root" id="floats-inside-and-outside-box-the-margin-moves"><div class="float x" style="height: 120px"></div><div class="x"><div class="right x" style="height: 80px"></div><div class="x" style="clear: both; margin-top: 200px"><div class="short"></div></div></div></div>+<div class="root" id="clears-only-float-outside-box-the-margin-moves"><div class="float x" style="height: 120px"></div><div class="x"><div class="right x" style="height: 80px"></div><div class="x" style="clear: left; margin-top: 200px"><div class="short"></div></div></div></div>+<div class="root" id="clears-only-float-inside-box-the-margin-moves"><div class="float x" style="height: 120px"></div><div class="x"><div class="right x" style="height: 80px"></div><div class="x" style="clear: right; margin-top: 200px"><div class="short"></div></div></div></div>+<div class="root" id="block-between-float-and-box-in-box"><div class="x"><div class="float x"></div><div class="short"></div><div class="x" style="clear: both; margin-top: 100px"><div class="short"></div></div></div></div>+<div class="root" id="float-after-pending-margin"><div class="short" style="margin-bottom: 10px"></div><div class="float"></div><div class="x" style="clear: both; margin-top: 100px"><div class="short"></div></div></div>+<div class="root" id="in-nested-block"><div class="float"></div><div><div class="tall"></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div></div>+<div class="root" id="in-inline-box"><div class="float"></div><div class="tall"></div><span><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></span></div>+<div class="root" id="clear-left-beside-right-float"><div class="right"></div><div class="tall"></div><div class="x" style="clear: left; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="clear-right-beside-right-float"><div class="right"></div><div class="tall"></div><div class="x" style="clear: right; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="empty"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both; margin-top: -30px"></div><div class="short x"></div></div>+<div class="root" id="empty-with-bottom-margin"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both; margin: -30px 0 20px"></div><div class="short x"></div></div>+<div class="root" id="flow-root"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both; margin-top: -30px; overflow: hidden"><div class="short"></div></div></div>+<div class="root" id="border-top"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both; margin-top: -30px; border-top: 2px solid"><div class="short"></div></div></div>+<div class="root" id="float-after-block"><div class="tall"></div><div class="float" style="height: 20px"></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="clearing-float"><div class="float"></div><div class="tall"></div><div class="x" style="float: left; clear: both; margin-top: -30px; width: 50px; height: 10px"></div></div>+<div class="root" id="two-in-a-row"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div><div class="x" style="clear: both; margin-top: -30px"><div class="short"></div></div></div>+<div class="root" id="vertical" style="writing-mode: vertical-lr; height: 300px"><div class="float" style="width: 80px; height: 100px"></div><div style="width: 80px"></div><div class="x" style="clear: both; margin-left: -30px; width: 10px"></div></div>+<div class="root" id="first-child-percentage-margin-in-narrow-box"><div class="float"></div><div class="x" style="clear: both; width: 200px"><div class="short" style="margin-top: 20%"></div></div></div>+<div class="root" id="first-child-percentage-margin-in-wide-box"><div class="float"></div><div class="x" style="clear: both; width: 500px"><div class="short" style="margin-top: 20%"></div></div></div>+<div class="root" id="first-child-percentage-margin-in-padded-box"><div class="float"></div><div class="x" style="clear: both; padding: 0 200px"><div class="short" style="margin-top: 30%"></div></div></div>+<div class="root" id="first-grandchild-percentage-margin"><div class="float"></div><div class="x" style="clear: both; width: 400px"><div style="width: 50%"><div class="short" style="margin-top: 30%"></div></div></div></div>+<div class="root" id="zero-height-block-before-first-child"><div class="float"></div><div class="x" style="clear: both"><div style="height: 0; margin-bottom: 100px"></div><div class="short"></div></div></div>+<div class="root" id="zero-min-height-block-before-first-child"><div class="float"></div><div class="x" style="clear: both"><div style="min-height: 0; margin-bottom: 100px"></div><div class="short"></div></div></div>+<div class="root" id="zero-height-block-with-contents-before-first-child"><div class="float"></div><div class="x" style="clear: both"><div style="height: 0; margin-bottom: 100px"><div class="short"></div></div><div class="short"></div></div></div>+<div class="root" id="min-height-block-before-first-child"><div class="float"></div><div class="x" style="clear: both"><div style="min-height: 1px; margin-bottom: 100px"></div><div class="short"></div></div></div>+<div class="root" id="aspect-ratio-block-before-first-child"><div class="float"></div><div class="x" style="clear: both"><div style="aspect-ratio: 1; width: 30px; margin-bottom: 100px"></div><div class="short"></div></div></div>+<div class="root" id="first-child-clears-side-without-float"><div class="float"></div><div class="x" style="clear: both"><div class="short" style="clear: right; margin-top: 100px"></div></div></div>+<div class="root" id="first-child-margin-reaches-past-float-it-clears"><div class="float"></div><div class="x" style="clear: both"><div class="short" style="clear: left; margin-top: 100px"></div></div></div>+<div class="root" id="first-child-margin-ends-beside-float-it-clears"><div class="float"></div><div class="x" style="clear: both"><div class="short" style="clear: left; margin-top: 20px"></div></div></div>+<div class="root" id="first-child-pulls-box-up-and-clears-side-without-float"><div class="float"></div><div class="tall"></div><div class="x" style="clear: both"><div class="short" style="clear: right; margin-top: -30px"></div></div></div>+<div class="root" id="first-grandchild-margin-ends-past-float-it-clears"><div class="float"></div><div class="x" style="clear: both"><div style="margin-top: 90px"><div class="short" style="clear: left; margin-top: 20px"></div></div></div></div>+<script>+ test(() => {+ // A box with "clear" gets clearance if its top border edge wouldn't be past the floats without it. That edge is+ // where the margins before it put it, so a negative margin can pull the box up beside a float that the flow is+ // already past, and a positive one can carry it past a float on its own. The margins of its first children+ // collapse with its own, and count the same way: at the sizes their percentages resolve to in the boxes they+ // are in, through the boxes before them that have no height, and for as long as none of them gets clearance+ // itself. But a float inside a box that the margin would move goes wherever the clearing box goes, so no+ // margin carries the box past it.+ for (const root of document.querySelectorAll(".root")) {+ const origin = root.getBoundingClientRect();+ const positions = [...root.querySelectorAll(".x")].map(box => {+ const rect = box.getBoundingClientRect();+ return `${rect.left - origin.left},${rect.top - origin.top}`;+ });+ println(`${root.id}: ${positions.join(" ")}`);+ }+ });+</script>