2020-11-22 13:38:18 +01:00
/*
2022-02-19 20:13:47 +01:00
* Copyright ( c ) 2020 - 2022 , Andreas Kling < kling @ serenityos . org >
2020-11-22 13:38:18 +01:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-11-22 13:38:18 +01:00
*/
2020-12-05 20:10:39 +01:00
# include <LibWeb/Dump.h>
2020-11-22 13:38:18 +01:00
# include <LibWeb/Layout/BlockFormattingContext.h>
2020-11-22 15:53:01 +01:00
# include <LibWeb/Layout/Box.h>
2021-01-18 17:33:46 +01:00
# include <LibWeb/Layout/FlexFormattingContext.h>
2020-11-22 13:38:18 +01:00
# include <LibWeb/Layout/FormattingContext.h>
2022-08-23 10:36:27 +02:00
# include <LibWeb/Layout/GridFormattingContext.h>
2020-12-11 22:27:09 +01:00
# include <LibWeb/Layout/ReplacedBox.h>
2021-09-17 23:12:16 +02:00
# include <LibWeb/Layout/SVGFormattingContext.h>
# include <LibWeb/Layout/SVGSVGBox.h>
2021-01-01 18:55:47 +01:00
# include <LibWeb/Layout/TableBox.h>
# include <LibWeb/Layout/TableCellBox.h>
2020-11-22 13:38:18 +01:00
# include <LibWeb/Layout/TableFormattingContext.h>
namespace Web : : Layout {
2022-07-16 23:30:32 +02:00
FormattingContext : : FormattingContext ( Type type , LayoutState & state , Box const & context_box , FormattingContext * parent )
2021-10-15 01:25:12 +02:00
: m_type ( type )
, m_parent ( parent )
2021-10-15 23:15:12 +02:00
, m_context_box ( context_box )
2022-02-19 20:13:47 +01:00
, m_state ( state )
2020-11-22 13:38:18 +01:00
{
}
2022-03-14 13:21:51 -06:00
FormattingContext : : ~ FormattingContext ( ) = default ;
2020-11-22 13:38:18 +01:00
2022-07-20 18:12:12 +02:00
void FormattingContext : : run_intrinsic_sizing ( Box const & box )
2022-07-10 22:06:20 +02:00
{
2022-07-12 02:17:17 +02:00
auto & box_state = m_state . get_mutable ( box ) ;
2022-07-17 18:46:38 +02:00
if ( box_state . has_definite_width ( ) )
2022-07-17 17:59:02 +02:00
box_state . set_content_width ( box . computed_values ( ) . width ( ) . resolved ( box , CSS : : Length : : make_px ( containing_block_width_for ( box ) ) ) . to_px ( box ) ) ;
2022-07-12 02:17:17 +02:00
2022-07-17 18:46:38 +02:00
if ( box_state . has_definite_height ( ) )
2022-07-17 17:59:02 +02:00
box_state . set_content_height ( box . computed_values ( ) . height ( ) . resolved ( box , CSS : : Length : : make_px ( containing_block_height_for ( box ) ) ) . to_px ( box ) ) ;
2022-07-12 02:17:17 +02:00
2022-07-20 18:12:12 +02:00
run ( box , LayoutMode : : IntrinsicSizing ) ;
2022-07-10 22:06:20 +02:00
}
2022-04-01 20:58:27 +03:00
bool FormattingContext : : creates_block_formatting_context ( Box const & box )
2020-12-05 20:10:39 +01:00
{
if ( box . is_root_element ( ) )
return true ;
if ( box . is_floating ( ) )
return true ;
if ( box . is_absolutely_positioned ( ) )
return true ;
if ( box . is_inline_block ( ) )
return true ;
2021-01-01 18:55:47 +01:00
if ( is < TableCellBox > ( box ) )
2020-12-05 20:10:39 +01:00
return true ;
2021-01-18 17:33:46 +01:00
2021-03-30 16:04:32 -04:00
CSS : : Overflow overflow_x = box . computed_values ( ) . overflow_x ( ) ;
if ( ( overflow_x ! = CSS : : Overflow : : Visible ) & & ( overflow_x ! = CSS : : Overflow : : Clip ) )
return true ;
CSS : : Overflow overflow_y = box . computed_values ( ) . overflow_y ( ) ;
if ( ( overflow_y ! = CSS : : Overflow : : Visible ) & & ( overflow_y ! = CSS : : Overflow : : Clip ) )
return true ;
2021-10-06 17:57:44 +02:00
auto display = box . computed_values ( ) . display ( ) ;
if ( display . is_flow_root_inside ( ) )
return true ;
if ( box . parent ( ) ) {
auto parent_display = box . parent ( ) - > computed_values ( ) . display ( ) ;
if ( parent_display . is_flex_inside ( ) ) {
// FIXME: Flex items (direct children of the element with display: flex or inline-flex) if they are neither flex nor grid nor table containers themselves.
if ( ! display . is_flex_inside ( ) )
return true ;
}
2022-08-23 10:36:27 +02:00
if ( parent_display . is_grid_inside ( ) ) {
if ( ! display . is_grid_inside ( ) ) {
return true ;
}
}
2021-01-18 17:33:46 +01:00
}
2020-12-05 20:10:39 +01:00
// FIXME: table-caption
// FIXME: anonymous table cells
// FIXME: Elements with contain: layout, content, or paint.
// FIXME: multicol
// FIXME: column-span: all
return false ;
}
2022-07-16 23:30:32 +02:00
OwnPtr < FormattingContext > FormattingContext : : create_independent_formatting_context_if_needed ( LayoutState & state , Box const & child_box )
2020-11-22 13:38:18 +01:00
{
2022-04-11 00:39:43 +02:00
if ( child_box . is_replaced_box ( ) & & ! child_box . can_have_children ( ) ) {
// NOTE: This is a bit strange.
// Basically, we create a pretend formatting context for replaced elements that does nothing.
// This allows other formatting contexts to treat them like elements that actually need inside layout
// without having separate code to handle replaced elements.
// FIXME: Find a better abstraction for this.
struct ReplacedFormattingContext : public FormattingContext {
2022-07-16 23:30:32 +02:00
ReplacedFormattingContext ( LayoutState & state , Box const & box )
2022-04-11 00:39:43 +02:00
: FormattingContext ( Type : : Block , state , box )
{
}
virtual void run ( Box const & , LayoutMode ) override { }
} ;
return make < ReplacedFormattingContext > ( state , child_box ) ;
}
2021-10-06 21:42:42 +02:00
if ( ! child_box . can_have_children ( ) )
2021-10-17 18:24:07 +02:00
return { } ;
2021-09-17 23:12:16 +02:00
2021-10-06 17:57:44 +02:00
auto child_display = child_box . computed_values ( ) . display ( ) ;
2021-10-17 18:24:07 +02:00
if ( is < SVGSVGBox > ( child_box ) )
2022-02-27 10:10:45 +01:00
return make < SVGFormattingContext > ( state , child_box , this ) ;
2021-09-23 14:24:04 +02:00
2021-10-17 18:24:07 +02:00
if ( child_display . is_flex_inside ( ) )
2022-02-27 10:10:45 +01:00
return make < FlexFormattingContext > ( state , child_box , this ) ;
2021-01-18 17:33:46 +01:00
2021-10-17 18:24:07 +02:00
if ( creates_block_formatting_context ( child_box ) )
2022-02-27 10:10:45 +01:00
return make < BlockFormattingContext > ( state , verify_cast < BlockContainer > ( child_box ) , this ) ;
2021-10-06 17:57:44 +02:00
2021-10-17 18:24:07 +02:00
if ( child_display . is_table_inside ( ) )
2022-02-27 10:10:45 +01:00
return make < TableFormattingContext > ( state , verify_cast < TableBox > ( child_box ) , this ) ;
2021-10-06 17:57:44 +02:00
2022-08-23 10:36:27 +02:00
if ( child_display . is_grid_inside ( ) ) {
return make < GridFormattingContext > ( state , verify_cast < BlockContainer > ( child_box ) , this ) ;
}
2021-10-17 18:24:07 +02:00
VERIFY ( is_block_formatting_context ( ) ) ;
2022-07-08 21:13:32 +02:00
VERIFY ( ! child_box . children_are_inline ( ) ) ;
2021-10-06 17:57:44 +02:00
2021-10-17 18:24:07 +02:00
// The child box is a block container that doesn't create its own BFC.
// It will be formatted by this BFC.
2022-02-21 16:53:38 +01:00
if ( ! child_display . is_flow_inside ( ) ) {
dbgln ( " FIXME: Child box doesn't create BFC, but inside is also not flow! display={} " , child_display . to_string ( ) ) ;
// HACK: Instead of crashing, create a dummy formatting context that does nothing.
// FIXME: Remove this once it's no longer needed. It currently swallows problem with standalone
// table-related boxes that don't get fixed up by CSS anonymous table box generation.
struct DummyFormattingContext : public FormattingContext {
2022-07-16 23:30:32 +02:00
DummyFormattingContext ( LayoutState & state , Box const & box )
2022-02-21 16:53:38 +01:00
: FormattingContext ( Type : : Block , state , box )
{
}
virtual void run ( Box const & , LayoutMode ) override { }
} ;
2022-02-27 10:10:45 +01:00
return make < DummyFormattingContext > ( state , child_box ) ;
2022-02-21 16:53:38 +01:00
}
2021-10-17 18:24:07 +02:00
VERIFY ( child_box . is_block_container ( ) ) ;
2022-02-21 16:53:38 +01:00
VERIFY ( child_display . is_flow_inside ( ) ) ;
2021-10-17 18:24:07 +02:00
return { } ;
}
2022-02-20 15:51:24 +01:00
OwnPtr < FormattingContext > FormattingContext : : layout_inside ( Box const & child_box , LayoutMode layout_mode )
2021-10-17 18:24:07 +02:00
{
2022-07-20 18:13:11 +02:00
{
// OPTIMIZATION: If we're doing intrinsic sizing and `child_box` has definite size in both axes,
// we don't need to layout its insides. The size is resolvable without learning
// the metrics of whatever's inside the box.
auto const & used_values = m_state . get ( child_box ) ;
if ( layout_mode = = LayoutMode : : IntrinsicSizing
& & used_values . width_constraint = = SizeConstraint : : None
& & used_values . height_constraint = = SizeConstraint : : None
& & used_values . has_definite_width ( )
& & used_values . has_definite_height ( ) ) {
return nullptr ;
}
}
2021-10-17 18:24:07 +02:00
if ( ! child_box . can_have_children ( ) )
return { } ;
2022-02-27 10:10:45 +01:00
auto independent_formatting_context = create_independent_formatting_context_if_needed ( m_state , child_box ) ;
2021-10-17 18:24:07 +02:00
if ( independent_formatting_context )
independent_formatting_context - > run ( child_box , layout_mode ) ;
else
run ( child_box , layout_mode ) ;
return independent_formatting_context ;
2020-11-22 13:38:18 +01:00
}
2022-03-18 14:44:36 +01:00
float FormattingContext : : greatest_child_width ( Box const & box )
2020-11-22 13:38:18 +01:00
{
float max_width = 0 ;
if ( box . children_are_inline ( ) ) {
2022-03-18 14:44:36 +01:00
for ( auto & line_box : m_state . get ( verify_cast < BlockContainer > ( box ) ) . line_boxes ) {
max_width = max ( max_width , line_box . width ( ) ) ;
2020-11-22 13:38:18 +01:00
}
} else {
2020-11-22 15:53:01 +01:00
box . for_each_child_of_type < Box > ( [ & ] ( auto & child ) {
2022-03-18 14:44:36 +01:00
max_width = max ( max_width , m_state . get ( child ) . border_box_width ( ) ) ;
2020-11-22 13:38:18 +01:00
} ) ;
}
return max_width ;
}
2022-02-20 15:51:24 +01:00
FormattingContext : : ShrinkToFitResult FormattingContext : : calculate_shrink_to_fit_widths ( Box const & box )
2020-11-22 13:38:18 +01:00
{
2022-03-18 12:44:19 +01:00
return {
2022-07-08 00:40:53 +02:00
. preferred_width = calculate_max_content_width ( box ) ,
. preferred_minimum_width = calculate_min_content_width ( box ) ,
2022-03-18 12:44:19 +01:00
} ;
2020-11-22 13:38:18 +01:00
}
2022-07-16 23:30:32 +02:00
static Gfx : : FloatSize solve_replaced_size_constraint ( LayoutState const & state , float w , float h , ReplacedBox const & box )
2020-12-11 22:27:09 +01:00
{
2020-12-12 00:20:31 +01:00
// 10.4 Minimum and maximum widths: 'min-width' and 'max-width'
2020-12-11 22:27:09 +01:00
2022-02-20 15:51:24 +01:00
auto const & containing_block = * box . containing_block ( ) ;
auto const & containing_block_state = state . get ( containing_block ) ;
2022-07-17 17:59:02 +02:00
auto width_of_containing_block = CSS : : Length : : make_px ( containing_block_state . content_width ( ) ) ;
auto height_of_containing_block = CSS : : Length : : make_px ( containing_block_state . content_height ( ) ) ;
2022-01-19 16:19:43 +00:00
2022-07-07 12:36:02 +02:00
auto specified_min_width = box . computed_values ( ) . min_width ( ) . is_auto ( ) ? 0 : box . computed_values ( ) . min_width ( ) . resolved ( box , width_of_containing_block ) . to_px ( box ) ;
auto specified_max_width = box . computed_values ( ) . max_width ( ) . is_auto ( ) ? w : box . computed_values ( ) . max_width ( ) . resolved ( box , width_of_containing_block ) . to_px ( box ) ;
auto specified_min_height = box . computed_values ( ) . min_height ( ) . is_auto ( ) ? 0 : box . computed_values ( ) . min_height ( ) . resolved ( box , height_of_containing_block ) . to_px ( box ) ;
auto specified_max_height = box . computed_values ( ) . max_height ( ) . is_auto ( ) ? h : box . computed_values ( ) . max_height ( ) . resolved ( box , height_of_containing_block ) . to_px ( box ) ;
2020-12-11 22:27:09 +01:00
2020-12-12 00:20:31 +01:00
auto min_width = min ( specified_min_width , specified_max_width ) ;
auto max_width = max ( specified_min_width , specified_max_width ) ;
auto min_height = min ( specified_min_height , specified_max_height ) ;
auto max_height = max ( specified_min_height , specified_max_height ) ;
2020-12-11 22:27:09 +01:00
2020-12-12 00:20:31 +01:00
if ( w > max_width )
return { w , max ( max_width * h / w , min_height ) } ;
if ( w < min_width )
return { max_width , min ( min_width * h / w , max_height ) } ;
if ( h > max_height )
return { max ( max_height * w / h , min_width ) , max_height } ;
if ( h < min_height )
return { min ( min_height * w / h , max_width ) , min_height } ;
if ( ( w > max_width & & h > max_height ) & & ( max_width / w < max_height / h ) )
return { max_width , max ( min_height , max_width * h / w ) } ;
if ( ( w > max_width & & h > max_height ) & & ( max_width / w > max_height / h ) )
return { max ( min_width , max_height * w / h ) , max_height } ;
if ( ( w < min_width & & h < min_height ) & & ( min_width / w < min_height / h ) )
return { min ( max_width , min_height * w / h ) , min_height } ;
if ( ( w < min_width & & h < min_height ) & & ( min_width / w > min_height / h ) )
return { min_width , min ( max_height , min_width * h / w ) } ;
if ( w < min_width & & h > max_height )
return { min_width , max_height } ;
if ( w > max_width & & h < min_height )
return { max_width , min_height } ;
return { w , h } ;
}
2020-12-11 22:27:09 +01:00
2022-07-16 23:30:32 +02:00
float FormattingContext : : compute_auto_height_for_block_level_element ( LayoutState const & state , Box const & box )
2021-04-17 09:09:57 +03:00
{
2022-03-01 23:21:24 +01:00
if ( creates_block_formatting_context ( box ) )
return compute_auto_height_for_block_formatting_context_root ( state , verify_cast < BlockContainer > ( box ) ) ;
auto const & box_state = state . get ( box ) ;
2022-04-08 18:18:09 +02:00
auto display = box . computed_values ( ) . display ( ) ;
if ( display . is_flex_inside ( ) )
2022-07-17 17:59:02 +02:00
return box_state . content_height ( ) ;
2022-04-08 18:18:09 +02:00
2022-03-01 23:21:24 +01:00
// https://www.w3.org/TR/CSS22/visudet.html#normal-block
// 10.6.3 Block-level non-replaced elements in normal flow when 'overflow' computes to 'visible'
// The element's height is the distance from its top content edge to the first applicable of the following:
// 1. the bottom edge of the last line box, if the box establishes a inline formatting context with one or more lines
2022-03-28 21:02:13 +02:00
if ( box . children_are_inline ( ) & & ! box_state . line_boxes . is_empty ( ) )
return box_state . line_boxes . last ( ) . bottom ( ) ;
2022-03-01 23:21:24 +01:00
// 2. the bottom edge of the bottom (possibly collapsed) margin of its last in-flow child, if the child's bottom margin does not collapse with the element's bottom margin
2022-03-03 10:15:15 +01:00
// FIXME: 3. the bottom border edge of the last in-flow child whose top margin doesn't collapse with the element's bottom margin
2022-03-01 23:21:24 +01:00
if ( ! box . children_are_inline ( ) ) {
2022-03-29 18:46:14 +02:00
for ( auto * child_box = box . last_child_of_type < Box > ( ) ; child_box ; child_box = child_box - > previous_sibling_of_type < Box > ( ) ) {
if ( child_box - > is_absolutely_positioned ( ) | | child_box - > is_floating ( ) )
continue ;
2022-03-03 10:15:15 +01:00
2022-03-29 21:49:41 +02:00
// FIXME: This is hack. If the last child is a list-item marker box, we ignore it for purposes of height calculation.
// Perhaps markers should not be considered in-flow(?) Perhaps they should always be the first child of the list-item
// box instead of the last child.
if ( child_box - > is_list_item_marker_box ( ) )
continue ;
2022-03-29 18:46:14 +02:00
auto const & child_box_state = state . get ( * child_box ) ;
2022-03-29 22:27:53 +02:00
// Ignore anonymous block containers with no lines. These don't count as in-flow block boxes.
if ( child_box - > is_anonymous ( ) & & child_box - > is_block_container ( ) & & child_box_state . line_boxes . is_empty ( ) )
continue ;
// FIXME: Handle margin collapsing.
2022-07-17 17:59:02 +02:00
return max ( 0 , child_box_state . offset . y ( ) + child_box_state . content_height ( ) + child_box_state . margin_box_bottom ( ) ) ;
2022-03-29 18:46:14 +02:00
}
2022-03-01 23:21:24 +01:00
}
// 4. zero, otherwise
return 0 ;
}
// https://www.w3.org/TR/CSS22/visudet.html#root-height
2022-07-16 23:30:32 +02:00
float FormattingContext : : compute_auto_height_for_block_formatting_context_root ( LayoutState const & state , BlockContainer const & root )
2022-03-01 23:21:24 +01:00
{
// 10.6.7 'Auto' heights for block formatting context roots
2021-04-17 09:09:57 +03:00
Optional < float > top ;
Optional < float > bottom ;
2022-03-01 23:21:24 +01:00
if ( root . children_are_inline ( ) ) {
2021-04-17 09:09:57 +03:00
// If it only has inline-level children, the height is the distance between
2021-10-28 19:17:23 +02:00
// the top content edge and the bottom of the bottommost line box.
2022-03-01 23:21:24 +01:00
auto const & line_boxes = state . get ( root ) . line_boxes ;
2021-10-28 19:17:23 +02:00
top = 0 ;
2022-03-28 21:02:13 +02:00
if ( ! line_boxes . is_empty ( ) )
2022-02-27 13:34:34 +01:00
bottom = line_boxes . last ( ) . bottom ( ) ;
2021-04-17 09:09:57 +03:00
} else {
// If it has block-level children, the height is the distance between
// the top margin-edge of the topmost block-level child box
// and the bottom margin-edge of the bottommost block-level child box.
2022-03-01 23:21:24 +01:00
root . for_each_child_of_type < Box > ( [ & ] ( Layout : : Box & child_box ) {
// Absolutely positioned children are ignored,
// and relatively positioned boxes are considered without their offset.
// Note that the child box may be an anonymous block box.
2021-04-17 09:09:57 +03:00
if ( child_box . is_absolutely_positioned ( ) )
return IterationDecision : : Continue ;
2022-03-01 23:21:24 +01:00
// FIXME: This doesn't look right.
if ( ( root . computed_values ( ) . overflow_y ( ) = = CSS : : Overflow : : Visible ) & & child_box . is_floating ( ) )
2021-04-17 09:09:57 +03:00
return IterationDecision : : Continue ;
2022-02-20 15:51:24 +01:00
auto const & child_box_state = state . get ( child_box ) ;
2022-03-26 22:45:51 +01:00
float child_box_top = child_box_state . offset . y ( ) - child_box_state . margin_box_top ( ) ;
2022-07-17 17:59:02 +02:00
float child_box_bottom = child_box_state . offset . y ( ) + child_box_state . content_height ( ) + child_box_state . margin_box_bottom ( ) ;
2021-04-17 09:09:57 +03:00
if ( ! top . has_value ( ) | | child_box_top < top . value ( ) )
top = child_box_top ;
if ( ! bottom . has_value ( ) | | child_box_bottom > bottom . value ( ) )
bottom = child_box_bottom ;
return IterationDecision : : Continue ;
} ) ;
2022-03-26 00:11:37 +01:00
}
2021-04-17 09:09:57 +03:00
2022-03-26 00:11:37 +01:00
// In addition, if the element has any floating descendants
// whose bottom margin edge is below the element's bottom content edge,
// then the height is increased to include those edges.
root . for_each_child_of_type < Box > ( [ & ] ( Layout : : Box & child_box ) {
if ( ! child_box . is_floating ( ) )
return IterationDecision : : Continue ;
2021-04-17 09:09:57 +03:00
2022-03-26 00:11:37 +01:00
auto const & child_box_state = state . get ( child_box ) ;
2022-07-17 17:59:02 +02:00
float child_box_bottom = child_box_state . offset . y ( ) + child_box_state . content_height ( ) + child_box_state . margin_box_bottom ( ) ;
2022-03-26 00:11:37 +01:00
if ( ! bottom . has_value ( ) | | child_box_bottom > bottom . value ( ) )
bottom = child_box_bottom ;
return IterationDecision : : Continue ;
} ) ;
2021-04-17 09:09:57 +03:00
2022-03-26 22:45:51 +01:00
return max ( 0 , bottom . value_or ( 0 ) - top . value_or ( 0 ) ) ;
2021-04-17 09:09:57 +03:00
}
2021-10-14 23:07:53 +02:00
// 10.3.2 Inline, replaced elements, https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-width
2022-07-16 23:30:32 +02:00
float FormattingContext : : tentative_width_for_replaced_element ( LayoutState const & state , ReplacedBox const & box , CSS : : LengthPercentage const & computed_width )
2020-12-12 00:20:31 +01:00
{
2022-02-20 15:51:24 +01:00
auto const & containing_block = * box . containing_block ( ) ;
2022-07-17 17:59:02 +02:00
auto height_of_containing_block = CSS : : Length : : make_px ( state . get ( containing_block ) . content_height ( ) ) ;
2022-07-07 12:36:02 +02:00
auto computed_height = box . computed_values ( ) . height ( ) . resolved ( box , height_of_containing_block ) . resolved ( box ) ;
2020-12-11 22:27:09 +01:00
2022-07-11 12:28:35 +02:00
float used_width = computed_width . resolved ( box , CSS : : Length : : make_px ( containing_block_width_for ( box , state ) ) ) . to_px ( box ) ;
2020-12-11 22:27:09 +01:00
// If 'height' and 'width' both have computed values of 'auto' and the element also has an intrinsic width,
// then that intrinsic width is the used value of 'width'.
2021-10-14 23:07:53 +02:00
if ( computed_height . is_auto ( ) & & computed_width . is_auto ( ) & & box . has_intrinsic_width ( ) )
return box . intrinsic_width ( ) . value ( ) ;
2020-12-11 22:27:09 +01:00
// If 'height' and 'width' both have computed values of 'auto' and the element has no intrinsic width,
// but does have an intrinsic height and intrinsic ratio;
// or if 'width' has a computed value of 'auto',
// 'height' has some other computed value, and the element does have an intrinsic ratio; then the used value of 'width' is:
//
// (used height) * (intrinsic ratio)
2021-10-14 23:07:53 +02:00
if ( ( computed_height . is_auto ( ) & & computed_width . is_auto ( ) & & ! box . has_intrinsic_width ( ) & & box . has_intrinsic_height ( ) & & box . has_intrinsic_aspect_ratio ( ) )
2022-07-11 12:28:35 +02:00
| | ( computed_width . is_auto ( ) & & ! computed_height . is_auto ( ) & & box . has_intrinsic_aspect_ratio ( ) ) ) {
2022-02-20 15:51:24 +01:00
return compute_height_for_replaced_element ( state , box ) * box . intrinsic_aspect_ratio ( ) . value ( ) ;
2020-12-11 22:27:09 +01:00
}
2021-10-14 23:07:53 +02:00
// If 'height' and 'width' both have computed values of 'auto' and the element has an intrinsic ratio but no intrinsic height or width,
// then the used value of 'width' is undefined in CSS 2.2. However, it is suggested that, if the containing block's width does not itself
// depend on the replaced element's width, then the used value of 'width' is calculated from the constraint equation used for block-level,
// non-replaced elements in normal flow.
2020-12-11 22:27:09 +01:00
2021-10-14 23:07:53 +02:00
// Otherwise, if 'width' has a computed value of 'auto', and the element has an intrinsic width, then that intrinsic width is the used value of 'width'.
if ( computed_width . is_auto ( ) & & box . has_intrinsic_width ( ) )
return box . intrinsic_width ( ) . value ( ) ;
// Otherwise, if 'width' has a computed value of 'auto', but none of the conditions above are met, then the used value of 'width' becomes 300px.
// If 300px is too wide to fit the device, UAs should use the width of the largest rectangle that has a 2:1 ratio and fits the device instead.
if ( computed_width . is_auto ( ) )
return 300 ;
2020-12-11 22:27:09 +01:00
return used_width ;
}
2022-02-20 15:51:24 +01:00
void FormattingContext : : compute_width_for_absolutely_positioned_element ( Box const & box )
2021-01-06 18:18:46 +01:00
{
if ( is < ReplacedBox > ( box ) )
2021-06-24 19:53:42 +02:00
compute_width_for_absolutely_positioned_replaced_element ( verify_cast < ReplacedBox > ( box ) ) ;
2021-01-06 18:18:46 +01:00
else
compute_width_for_absolutely_positioned_non_replaced_element ( box ) ;
}
2022-02-20 15:51:24 +01:00
void FormattingContext : : compute_height_for_absolutely_positioned_element ( Box const & box )
2021-01-06 18:18:46 +01:00
{
if ( is < ReplacedBox > ( box ) )
2021-06-24 19:53:42 +02:00
compute_height_for_absolutely_positioned_replaced_element ( verify_cast < ReplacedBox > ( box ) ) ;
2021-01-06 18:18:46 +01:00
else
compute_height_for_absolutely_positioned_non_replaced_element ( box ) ;
}
2022-07-16 23:30:32 +02:00
float FormattingContext : : compute_width_for_replaced_element ( LayoutState const & state , ReplacedBox const & box )
2020-12-11 22:27:09 +01:00
{
2020-12-12 00:20:31 +01:00
// 10.3.4 Block-level, replaced elements in normal flow...
// 10.3.2 Inline, replaced elements
auto zero_value = CSS : : Length : : make_px ( 0 ) ;
2022-07-09 15:17:47 +02:00
auto width_of_containing_block_as_length = CSS : : Length : : make_px ( containing_block_width_for ( box , state ) ) ;
2020-12-11 22:27:09 +01:00
2022-07-09 15:17:47 +02:00
auto margin_left = box . computed_values ( ) . margin ( ) . left . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
auto margin_right = box . computed_values ( ) . margin ( ) . right . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
2020-12-12 00:20:31 +01:00
// A computed value of 'auto' for 'margin-left' or 'margin-right' becomes a used value of '0'.
if ( margin_left . is_auto ( ) )
margin_left = zero_value ;
if ( margin_right . is_auto ( ) )
margin_right = zero_value ;
2022-07-11 12:28:35 +02:00
auto computed_width = box . computed_values ( ) . width ( ) ;
2020-12-12 00:20:31 +01:00
// 1. The tentative used width is calculated (without 'min-width' and 'max-width')
2022-07-11 12:28:35 +02:00
auto used_width = tentative_width_for_replaced_element ( state , box , computed_width ) ;
2020-12-12 00:20:31 +01:00
// 2. The tentative used width is greater than 'max-width', the rules above are applied again,
// but this time using the computed value of 'max-width' as the computed value for 'width'.
2022-07-11 12:28:35 +02:00
auto computed_max_width = box . computed_values ( ) . max_width ( ) ;
if ( ! computed_max_width . is_auto ( ) ) {
if ( used_width > computed_max_width . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ) {
used_width = tentative_width_for_replaced_element ( state , box , computed_max_width ) ;
2020-12-12 00:20:31 +01:00
}
}
// 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
// but this time using the value of 'min-width' as the computed value for 'width'.
2022-07-11 12:28:35 +02:00
auto computed_min_width = box . computed_values ( ) . min_width ( ) ;
if ( ! computed_min_width . is_auto ( ) ) {
if ( used_width < computed_min_width . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ) {
used_width = tentative_width_for_replaced_element ( state , box , computed_min_width ) ;
2020-12-12 00:20:31 +01:00
}
}
return used_width ;
}
2021-10-14 23:15:36 +02:00
// 10.6.2 Inline replaced elements, block-level replaced elements in normal flow, 'inline-block' replaced elements in normal flow and floating replaced elements
// https://www.w3.org/TR/CSS22/visudet.html#inline-replaced-height
2022-07-16 23:30:32 +02:00
float FormattingContext : : tentative_height_for_replaced_element ( LayoutState const & state , ReplacedBox const & box , CSS : : LengthPercentage const & computed_height )
2020-12-12 00:20:31 +01:00
{
2022-07-11 12:28:35 +02:00
auto computed_width = box . computed_values ( ) . width ( ) ;
2020-12-11 22:27:09 +01:00
// If 'height' and 'width' both have computed values of 'auto' and the element also has
// an intrinsic height, then that intrinsic height is the used value of 'height'.
2021-10-14 23:15:36 +02:00
if ( computed_width . is_auto ( ) & & computed_height . is_auto ( ) & & box . has_intrinsic_height ( ) )
return box . intrinsic_height ( ) . value ( ) ;
2020-12-11 22:27:09 +01:00
2021-10-14 23:15:36 +02:00
// Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic ratio then the used value of 'height' is:
//
// (used width) / (intrinsic ratio)
if ( computed_height . is_auto ( ) & & box . has_intrinsic_aspect_ratio ( ) )
2022-02-20 15:51:24 +01:00
return compute_width_for_replaced_element ( state , box ) / box . intrinsic_aspect_ratio ( ) . value ( ) ;
2021-10-14 23:15:36 +02:00
// Otherwise, if 'height' has a computed value of 'auto', and the element has an intrinsic height, then that intrinsic height is the used value of 'height'.
if ( computed_height . is_auto ( ) & & box . has_intrinsic_height ( ) )
return box . intrinsic_height ( ) . value ( ) ;
// Otherwise, if 'height' has a computed value of 'auto', but none of the conditions above are met,
// then the used value of 'height' must be set to the height of the largest rectangle that has a 2:1 ratio, has a height not greater than 150px,
// and has a width not greater than the device width.
if ( computed_height . is_auto ( ) )
return 150 ;
2022-07-11 12:28:35 +02:00
return computed_height . resolved ( box , CSS : : Length : : make_px ( containing_block_height_for ( box , state ) ) ) . to_px ( box ) ;
2020-12-11 22:27:09 +01:00
}
2022-07-16 23:30:32 +02:00
float FormattingContext : : compute_height_for_replaced_element ( LayoutState const & state , ReplacedBox const & box )
2020-12-12 00:20:31 +01:00
{
// 10.6.2 Inline replaced elements, block-level replaced elements in normal flow,
// 'inline-block' replaced elements in normal flow and floating replaced elements
2022-07-09 15:17:47 +02:00
auto width_of_containing_block_as_length = CSS : : Length : : make_px ( containing_block_width_for ( box , state ) ) ;
auto height_of_containing_block_as_length = CSS : : Length : : make_px ( containing_block_height_for ( box , state ) ) ;
2022-07-11 12:28:35 +02:00
auto computed_width = box . computed_values ( ) . width ( ) ;
auto computed_height = box . computed_values ( ) . height ( ) ;
2020-12-12 00:20:31 +01:00
2022-07-11 12:28:35 +02:00
float used_height = tentative_height_for_replaced_element ( state , box , computed_height ) ;
2020-12-12 00:20:31 +01:00
2022-07-11 12:28:35 +02:00
if ( computed_width . is_auto ( ) & & computed_height . is_auto ( ) & & box . has_intrinsic_aspect_ratio ( ) ) {
float w = tentative_width_for_replaced_element ( state , box , computed_width ) ;
2020-12-12 00:20:31 +01:00
float h = used_height ;
2022-02-20 15:51:24 +01:00
used_height = solve_replaced_size_constraint ( state , w , h , box ) . height ( ) ;
2020-12-12 00:20:31 +01:00
}
return used_height ;
}
2022-02-20 15:51:24 +01:00
void FormattingContext : : compute_width_for_absolutely_positioned_non_replaced_element ( Box const & box )
2021-01-06 18:18:46 +01:00
{
2022-07-09 15:17:47 +02:00
auto width_of_containing_block = containing_block_width_for ( box ) ;
auto width_of_containing_block_as_length = CSS : : Length : : make_px ( width_of_containing_block ) ;
2021-01-06 18:18:46 +01:00
auto & computed_values = box . computed_values ( ) ;
auto zero_value = CSS : : Length : : make_px ( 0 ) ;
auto margin_left = CSS : : Length : : make_auto ( ) ;
auto margin_right = CSS : : Length : : make_auto ( ) ;
2022-04-01 20:58:27 +03:00
auto const border_left = computed_values . border_left ( ) . width ;
auto const border_right = computed_values . border_right ( ) . width ;
2022-07-09 15:17:47 +02:00
auto const padding_left = computed_values . padding ( ) . left . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
auto const padding_right = computed_values . padding ( ) . right . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
2021-01-06 18:18:46 +01:00
2022-04-01 20:58:27 +03:00
auto try_compute_width = [ & ] ( auto const & a_width ) {
2022-07-09 15:17:47 +02:00
margin_left = computed_values . margin ( ) . left . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
margin_right = computed_values . margin ( ) . right . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
2021-01-06 18:18:46 +01:00
2022-07-09 15:17:47 +02:00
auto left = computed_values . inset ( ) . left . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
auto right = computed_values . inset ( ) . right . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
2021-01-06 18:18:46 +01:00
auto width = a_width ;
auto solve_for_left = [ & ] {
2022-07-09 15:17:47 +02:00
return CSS : : Length ( width_of_containing_block - margin_left . to_px ( box ) - border_left - padding_left - width . to_px ( box ) - padding_right - border_right - margin_right . to_px ( box ) - right . to_px ( box ) , CSS : : Length : : Type : : Px ) ;
2021-01-06 18:18:46 +01:00
} ;
auto solve_for_width = [ & ] {
2022-07-09 15:17:47 +02:00
return CSS : : Length ( width_of_containing_block - left . to_px ( box ) - margin_left . to_px ( box ) - border_left - padding_left - padding_right - border_right - margin_right . to_px ( box ) - right . to_px ( box ) , CSS : : Length : : Type : : Px ) ;
2021-01-06 18:18:46 +01:00
} ;
auto solve_for_right = [ & ] {
2022-07-09 15:17:47 +02:00
return CSS : : Length ( width_of_containing_block - left . to_px ( box ) - margin_left . to_px ( box ) - border_left - padding_left - width . to_px ( box ) - padding_right - border_right - margin_right . to_px ( box ) , CSS : : Length : : Type : : Px ) ;
2021-01-06 18:18:46 +01:00
} ;
// If all three of 'left', 'width', and 'right' are 'auto':
if ( left . is_auto ( ) & & width . is_auto ( ) & & right . is_auto ( ) ) {
// First set any 'auto' values for 'margin-left' and 'margin-right' to 0.
if ( margin_left . is_auto ( ) )
margin_left = CSS : : Length : : make_px ( 0 ) ;
if ( margin_right . is_auto ( ) )
margin_right = CSS : : Length : : make_px ( 0 ) ;
// Then, if the 'direction' property of the element establishing the static-position containing block
// is 'ltr' set 'left' to the static position and apply rule number three below;
// otherwise, set 'right' to the static position and apply rule number one below.
// FIXME: This is very hackish.
left = CSS : : Length : : make_px ( 0 ) ;
goto Rule3 ;
}
if ( ! left . is_auto ( ) & & ! width . is_auto ( ) & & ! right . is_auto ( ) ) {
// FIXME: This should be solved in a more complicated way.
return width ;
}
if ( margin_left . is_auto ( ) )
margin_left = CSS : : Length : : make_px ( 0 ) ;
if ( margin_right . is_auto ( ) )
margin_right = CSS : : Length : : make_px ( 0 ) ;
// 1. 'left' and 'width' are 'auto' and 'right' is not 'auto',
// then the width is shrink-to-fit. Then solve for 'left'
if ( left . is_auto ( ) & & width . is_auto ( ) & & ! right . is_auto ( ) ) {
auto result = calculate_shrink_to_fit_widths ( box ) ;
solve_for_left ( ) ;
auto available_width = solve_for_width ( ) ;
width = CSS : : Length ( min ( max ( result . preferred_minimum_width , available_width . to_px ( box ) ) , result . preferred_width ) , CSS : : Length : : Type : : Px ) ;
}
// 2. 'left' and 'right' are 'auto' and 'width' is not 'auto',
// then if the 'direction' property of the element establishing
// the static-position containing block is 'ltr' set 'left'
// to the static position, otherwise set 'right' to the static position.
// Then solve for 'left' (if 'direction is 'rtl') or 'right' (if 'direction' is 'ltr').
else if ( left . is_auto ( ) & & right . is_auto ( ) & & ! width . is_auto ( ) ) {
// FIXME: Check direction
// FIXME: Use the static-position containing block
left = zero_value ;
right = solve_for_right ( ) ;
}
// 3. 'width' and 'right' are 'auto' and 'left' is not 'auto',
// then the width is shrink-to-fit. Then solve for 'right'
else if ( width . is_auto ( ) & & right . is_auto ( ) & & ! left . is_auto ( ) ) {
Rule3 :
auto result = calculate_shrink_to_fit_widths ( box ) ;
auto available_width = solve_for_width ( ) ;
width = CSS : : Length ( min ( max ( result . preferred_minimum_width , available_width . to_px ( box ) ) , result . preferred_width ) , CSS : : Length : : Type : : Px ) ;
right = solve_for_right ( ) ;
}
// 4. 'left' is 'auto', 'width' and 'right' are not 'auto', then solve for 'left'
else if ( left . is_auto ( ) & & ! width . is_auto ( ) & & ! right . is_auto ( ) ) {
left = solve_for_left ( ) ;
}
// 5. 'width' is 'auto', 'left' and 'right' are not 'auto', then solve for 'width'
else if ( width . is_auto ( ) & & ! left . is_auto ( ) & & ! right . is_auto ( ) ) {
width = solve_for_width ( ) ;
}
// 6. 'right' is 'auto', 'left' and 'width' are not 'auto', then solve for 'right'
else if ( right . is_auto ( ) & & ! left . is_auto ( ) & & ! width . is_auto ( ) ) {
right = solve_for_right ( ) ;
}
return width ;
} ;
2022-07-09 15:17:47 +02:00
auto specified_width = computed_values . width ( ) . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
2021-01-06 18:18:46 +01:00
// 1. The tentative used width is calculated (without 'min-width' and 'max-width')
auto used_width = try_compute_width ( specified_width ) ;
// 2. The tentative used width is greater than 'max-width', the rules above are applied again,
// but this time using the computed value of 'max-width' as the computed value for 'width'.
2022-07-09 15:17:47 +02:00
auto specified_max_width = computed_values . max_width ( ) . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
2021-01-06 18:18:46 +01:00
if ( ! specified_max_width . is_auto ( ) ) {
if ( used_width . to_px ( box ) > specified_max_width . to_px ( box ) ) {
used_width = try_compute_width ( specified_max_width ) ;
}
}
// 3. If the resulting width is smaller than 'min-width', the rules above are applied again,
// but this time using the value of 'min-width' as the computed value for 'width'.
2022-07-09 15:17:47 +02:00
auto specified_min_width = computed_values . min_width ( ) . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
2021-01-06 18:18:46 +01:00
if ( ! specified_min_width . is_auto ( ) ) {
if ( used_width . to_px ( box ) < specified_min_width . to_px ( box ) ) {
used_width = try_compute_width ( specified_min_width ) ;
}
}
2022-07-09 15:17:47 +02:00
auto & box_state = m_state . get_mutable ( box ) ;
2022-07-17 17:59:02 +02:00
box_state . set_content_width ( used_width . to_px ( box ) ) ;
2021-01-06 18:18:46 +01:00
2022-02-20 15:51:24 +01:00
box_state . margin_left = margin_left . to_px ( box ) ;
box_state . margin_right = margin_right . to_px ( box ) ;
box_state . border_left = border_left ;
box_state . border_right = border_right ;
box_state . padding_left = padding_left ;
box_state . padding_right = padding_right ;
2021-01-06 18:18:46 +01:00
}
2022-02-20 15:51:24 +01:00
void FormattingContext : : compute_width_for_absolutely_positioned_replaced_element ( ReplacedBox const & box )
2021-01-06 18:18:46 +01:00
{
2021-01-06 19:16:40 +01:00
// 10.3.8 Absolutely positioned, replaced elements
// The used value of 'width' is determined as for inline replaced elements.
2022-02-20 15:51:24 +01:00
// FIXME: This const_cast is gross.
const_cast < ReplacedBox & > ( box ) . prepare_for_replaced_layout ( ) ;
2022-07-17 17:59:02 +02:00
m_state . get_mutable ( box ) . set_content_width ( compute_width_for_replaced_element ( m_state , box ) ) ;
2021-01-06 18:18:46 +01:00
}
2022-03-05 16:02:53 +01:00
// https://www.w3.org/TR/CSS22/visudet.html#abs-non-replaced-height
2022-02-20 15:51:24 +01:00
void FormattingContext : : compute_height_for_absolutely_positioned_non_replaced_element ( Box const & box )
2021-01-06 18:18:46 +01:00
{
2022-03-05 16:02:53 +01:00
// 10.6.4 Absolutely positioned, non-replaced elements
// FIXME: The section below is partly on-spec, partly ad-hoc.
2021-01-06 18:18:46 +01:00
auto & computed_values = box . computed_values ( ) ;
2022-07-26 01:30:50 +02:00
2022-07-09 15:17:47 +02:00
auto width_of_containing_block = containing_block_width_for ( box ) ;
auto height_of_containing_block = containing_block_height_for ( box ) ;
auto width_of_containing_block_as_length = CSS : : Length : : make_px ( width_of_containing_block ) ;
auto height_of_containing_block_as_length = CSS : : Length : : make_px ( height_of_containing_block ) ;
2021-01-06 18:18:46 +01:00
2022-07-26 01:30:50 +02:00
auto const & computed_top = computed_values . inset ( ) . top ;
auto const & computed_bottom = computed_values . inset ( ) . bottom ;
auto const & computed_height = computed_values . height ( ) ;
auto const & computed_min_height = computed_values . min_height ( ) ;
auto const & computed_max_height = computed_values . max_height ( ) ;
auto used_top = computed_top . resolved ( box , height_of_containing_block_as_length ) . resolved ( box ) . to_px ( box ) ;
auto used_bottom = computed_bottom . resolved ( box , height_of_containing_block_as_length ) . resolved ( box ) . to_px ( box ) ;
auto tentative_height = CSS : : Length : : make_auto ( ) ;
2021-01-06 18:18:46 +01:00
2022-08-07 17:00:38 +01:00
if ( ! computed_height . is_auto ( ) )
2022-07-26 01:30:50 +02:00
tentative_height = computed_values . height ( ) . resolved ( box , height_of_containing_block_as_length ) . resolved ( box ) ;
2021-01-06 18:18:46 +01:00
2022-07-26 01:30:50 +02:00
auto & box_state = m_state . get_mutable ( box ) ;
2022-07-09 15:17:47 +02:00
box_state . margin_top = computed_values . margin ( ) . top . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
box_state . margin_bottom = computed_values . margin ( ) . bottom . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
2022-02-20 15:51:24 +01:00
box_state . border_top = computed_values . border_top ( ) . width ;
box_state . border_bottom = computed_values . border_bottom ( ) . width ;
2022-07-09 15:17:47 +02:00
box_state . padding_top = computed_values . padding ( ) . top . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
box_state . padding_bottom = computed_values . padding ( ) . bottom . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
2021-01-06 18:18:46 +01:00
2022-07-26 01:30:50 +02:00
if ( computed_height . is_auto ( ) & & computed_top . is_auto ( ) & & computed_bottom . is_auto ( ) ) {
tentative_height = CSS : : Length ( compute_auto_height_for_block_level_element ( m_state , box ) , CSS : : Length : : Type : : Px ) ;
2022-03-05 16:02:53 +01:00
}
2022-07-26 01:30:50 +02:00
else if ( computed_height . is_auto ( ) & & ! computed_top . is_auto ( ) & & computed_bottom . is_auto ( ) ) {
tentative_height = CSS : : Length ( compute_auto_height_for_block_level_element ( m_state , box ) , CSS : : Length : : Type : : Px ) ;
box_state . inset_bottom = height_of_containing_block - tentative_height . to_px ( box ) - used_top - box_state . margin_top - box_state . padding_top - box_state . border_top - box_state . margin_bottom - box_state . padding_bottom - box_state . border_bottom ;
2021-04-17 09:09:57 +03:00
}
2022-07-26 01:30:50 +02:00
else if ( computed_height . is_auto ( ) & & ! computed_top . is_auto ( ) & & ! computed_bottom . is_auto ( ) ) {
tentative_height = CSS : : Length ( height_of_containing_block - used_top - box_state . margin_top - box_state . padding_top - box_state . border_top - used_bottom - box_state . margin_bottom - box_state . padding_bottom - box_state . border_bottom , CSS : : Length : : Type : : Px ) ;
2021-03-26 17:01:44 -04:00
}
2022-07-26 01:30:50 +02:00
float used_height = tentative_height . to_px ( box ) ;
if ( ! computed_max_height . is_auto ( ) )
used_height = min ( used_height , computed_max_height . resolved ( box , height_of_containing_block_as_length ) . resolved ( box ) . to_px ( box ) ) ;
if ( ! computed_min_height . is_auto ( ) )
used_height = max ( used_height , computed_min_height . resolved ( box , height_of_containing_block_as_length ) . resolved ( box ) . to_px ( box ) ) ;
box_state . set_content_height ( used_height ) ;
2021-01-06 18:18:46 +01:00
}
2022-02-20 15:51:24 +01:00
void FormattingContext : : layout_absolutely_positioned_element ( Box const & box )
2021-01-06 18:18:46 +01:00
{
2022-07-17 19:38:06 +02:00
// https://drafts.csswg.org/css-sizing-3/#definite
// Additionally, the size of the containing block of an absolutely positioned element is always definite with respect to that element.
auto & containing_block_state = m_state . get_mutable ( * box . containing_block ( ) ) ;
auto containing_block_had_definite_width = containing_block_state . has_definite_width ( ) ;
containing_block_state . set_has_definite_width ( true ) ;
auto containing_block_definite_width_guard = ScopeGuard ( [ & ] {
containing_block_state . set_has_definite_width ( containing_block_had_definite_width ) ;
} ) ;
2022-07-09 15:17:47 +02:00
auto width_of_containing_block = containing_block_width_for ( box ) ;
auto height_of_containing_block = containing_block_height_for ( box ) ;
auto width_of_containing_block_as_length = CSS : : Length : : make_px ( width_of_containing_block ) ;
auto height_of_containing_block_as_length = CSS : : Length : : make_px ( height_of_containing_block ) ;
2021-01-06 18:18:46 +01:00
2022-07-09 15:17:47 +02:00
auto specified_width = box . computed_values ( ) . width ( ) . resolved ( box , width_of_containing_block_as_length ) . resolved ( box ) ;
2021-01-06 18:18:46 +01:00
compute_width_for_absolutely_positioned_element ( box ) ;
2022-03-19 15:44:02 +01:00
auto independent_formatting_context = layout_inside ( box , LayoutMode : : Normal ) ;
2021-01-06 18:18:46 +01:00
compute_height_for_absolutely_positioned_element ( box ) ;
2022-07-09 15:17:47 +02:00
auto & box_state = m_state . get_mutable ( box ) ;
box_state . margin_left = box . computed_values ( ) . margin ( ) . left . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
box_state . margin_top = box . computed_values ( ) . margin ( ) . top . resolved ( box , height_of_containing_block_as_length ) . to_px ( box ) ;
box_state . margin_right = box . computed_values ( ) . margin ( ) . right . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
box_state . margin_bottom = box . computed_values ( ) . margin ( ) . bottom . resolved ( box , height_of_containing_block_as_length ) . to_px ( box ) ;
2021-01-06 18:18:46 +01:00
2022-02-20 15:51:24 +01:00
box_state . border_left = box . computed_values ( ) . border_left ( ) . width ;
box_state . border_right = box . computed_values ( ) . border_right ( ) . width ;
box_state . border_top = box . computed_values ( ) . border_top ( ) . width ;
box_state . border_bottom = box . computed_values ( ) . border_bottom ( ) . width ;
2021-01-06 18:18:46 +01:00
2022-07-09 15:17:47 +02:00
box_state . inset_left = box . computed_values ( ) . inset ( ) . left . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
box_state . inset_top = box . computed_values ( ) . inset ( ) . top . resolved ( box , height_of_containing_block_as_length ) . to_px ( box ) ;
box_state . inset_right = box . computed_values ( ) . inset ( ) . right . resolved ( box , width_of_containing_block_as_length ) . to_px ( box ) ;
box_state . inset_bottom = box . computed_values ( ) . inset ( ) . bottom . resolved ( box , height_of_containing_block_as_length ) . to_px ( box ) ;
2022-01-19 16:19:43 +00:00
2022-07-25 23:55:20 +02:00
if ( box . computed_values ( ) . inset ( ) . left . is_auto ( ) & & specified_width . is_auto ( ) & & box . computed_values ( ) . inset ( ) . right . is_auto ( ) ) {
if ( box . computed_values ( ) . margin ( ) . left . is_auto ( ) )
2022-02-20 15:51:24 +01:00
box_state . margin_left = 0 ;
2022-07-25 23:55:20 +02:00
if ( box . computed_values ( ) . margin ( ) . right . is_auto ( ) )
2022-02-20 15:51:24 +01:00
box_state . margin_right = 0 ;
2021-01-06 18:18:46 +01:00
}
Gfx : : FloatPoint used_offset ;
2022-08-10 19:21:29 +02:00
auto * relevant_parent = box . first_ancestor_of_type < Layout : : BlockContainer > ( ) ;
while ( relevant_parent ! = nullptr ) {
if ( ! relevant_parent - > is_absolutely_positioned ( ) & & ! relevant_parent - > is_floating ( ) ) {
break ;
} else {
relevant_parent = relevant_parent - > first_ancestor_of_type < Layout : : BlockContainer > ( ) ;
}
}
auto parent_location = absolute_content_rect ( static_cast < Box const & > ( * relevant_parent ) , m_state ) ;
2022-07-25 23:55:20 +02:00
if ( ! box . computed_values ( ) . inset ( ) . left . is_auto ( ) ) {
2022-03-26 13:26:42 +01:00
float x_offset = box_state . inset_left
2022-02-20 15:51:24 +01:00
+ box_state . border_box_left ( ) ;
used_offset . set_x ( x_offset + box_state . margin_left ) ;
2022-07-25 23:55:20 +02:00
} else if ( ! box . computed_values ( ) . inset ( ) . right . is_auto ( ) ) {
2021-01-06 18:18:46 +01:00
float x_offset = 0
2022-03-26 13:26:42 +01:00
- box_state . inset_right
2022-02-20 15:51:24 +01:00
- box_state . border_box_right ( ) ;
2022-07-17 17:59:02 +02:00
used_offset . set_x ( width_of_containing_block + x_offset - box_state . content_width ( ) - box_state . margin_right ) ;
2021-01-06 18:18:46 +01:00
} else {
2022-08-10 19:21:29 +02:00
float x_offset = box_state . margin_box_left ( )
+ ( relevant_parent - > computed_values ( ) . position ( ) = = CSS : : Position : : Relative ? 0 : parent_location . x ( ) ) ;
2021-01-06 18:18:46 +01:00
used_offset . set_x ( x_offset ) ;
}
2022-07-25 23:55:20 +02:00
if ( ! box . computed_values ( ) . inset ( ) . top . is_auto ( ) ) {
2022-03-26 13:26:42 +01:00
float y_offset = box_state . inset_top
2022-02-20 15:51:24 +01:00
+ box_state . border_box_top ( ) ;
used_offset . set_y ( y_offset + box_state . margin_top ) ;
2022-07-25 23:55:20 +02:00
} else if ( ! box . computed_values ( ) . inset ( ) . bottom . is_auto ( ) ) {
2021-01-06 18:18:46 +01:00
float y_offset = 0
2022-03-26 13:26:42 +01:00
- box_state . inset_bottom
2022-02-20 15:51:24 +01:00
- box_state . border_box_bottom ( ) ;
2022-07-17 17:59:02 +02:00
used_offset . set_y ( height_of_containing_block + y_offset - box_state . content_height ( ) - box_state . margin_bottom ) ;
2021-01-06 18:18:46 +01:00
} else {
2022-08-10 19:21:29 +02:00
float y_offset = box_state . margin_box_top ( )
+ compute_box_y_position_with_respect_to_siblings ( box , box_state )
+ ( relevant_parent - > computed_values ( ) . position ( ) = = CSS : : Position : : Relative ? 0 : parent_location . y ( ) ) ;
2021-01-06 18:18:46 +01:00
used_offset . set_y ( y_offset ) ;
}
2022-02-20 15:51:24 +01:00
box_state . offset = used_offset ;
2022-02-12 19:54:09 +01:00
if ( independent_formatting_context )
independent_formatting_context - > parent_context_did_dimension_child_root_box ( ) ;
2021-01-06 18:18:46 +01:00
}
2022-02-20 15:51:24 +01:00
void FormattingContext : : compute_height_for_absolutely_positioned_replaced_element ( ReplacedBox const & box )
2021-01-06 18:18:46 +01:00
{
2021-05-04 10:14:58 -04:00
// 10.6.5 Absolutely positioned, replaced elements
// The used value of 'height' is determined as for inline replaced elements.
2022-07-17 17:59:02 +02:00
m_state . get_mutable ( box ) . set_content_height ( compute_height_for_replaced_element ( m_state , box ) ) ;
2022-02-20 15:51:24 +01:00
}
2022-03-27 16:07:19 +02:00
// https://www.w3.org/TR/css-position-3/#relpos-insets
2022-03-27 15:35:28 +02:00
void FormattingContext : : compute_inset ( Box const & box )
2022-02-20 15:51:24 +01:00
{
if ( box . computed_values ( ) . position ( ) ! = CSS : : Position : : Relative )
return ;
2022-03-27 16:07:19 +02:00
auto resolve_two_opposing_insets = [ & ] ( CSS : : LengthPercentage const & computed_start , CSS : : LengthPercentage const & computed_end , float & used_start , float & used_end , float reference_for_percentage ) {
auto resolved_first = computed_start . resolved ( box , CSS : : Length : : make_px ( reference_for_percentage ) ) . resolved ( box ) ;
auto resolved_second = computed_end . resolved ( box , CSS : : Length : : make_px ( reference_for_percentage ) ) . resolved ( box ) ;
if ( resolved_first . is_auto ( ) & & resolved_second . is_auto ( ) ) {
// If opposing inset properties in an axis both compute to auto (their initial values),
// their used values are zero (i.e., the boxes stay in their original position in that axis).
used_start = 0 ;
used_end = 0 ;
} else if ( resolved_first . is_auto ( ) | | resolved_second . is_auto ( ) ) {
// If only one is auto, its used value becomes the negation of the other, and the box is shifted by the specified amount.
if ( resolved_first . is_auto ( ) ) {
used_end = resolved_second . to_px ( box ) ;
used_start = 0 - used_end ;
} else {
used_start = resolved_first . to_px ( box ) ;
used_end = 0 - used_start ;
}
} else {
// If neither is auto, the position is over-constrained; (with respect to the writing mode of its containing block)
// the computed end side value is ignored, and its used value becomes the negation of the start side.
used_start = resolved_first . to_px ( box ) ;
used_end = 0 - used_start ;
}
} ;
2022-02-21 17:42:09 +01:00
auto & box_state = m_state . get_mutable ( box ) ;
2022-02-20 15:51:24 +01:00
auto const & computed_values = box . computed_values ( ) ;
2022-03-27 16:07:19 +02:00
// FIXME: Respect the containing block's writing-mode.
2022-07-09 15:17:47 +02:00
resolve_two_opposing_insets ( computed_values . inset ( ) . left , computed_values . inset ( ) . right , box_state . inset_left , box_state . inset_right , containing_block_width_for ( box ) ) ;
resolve_two_opposing_insets ( computed_values . inset ( ) . top , computed_values . inset ( ) . bottom , box_state . inset_top , box_state . inset_bottom , containing_block_height_for ( box ) ) ;
2021-01-06 18:18:46 +01:00
}
2022-07-20 17:59:46 +02:00
float FormattingContext : : calculate_fit_content_size ( float min_content_size , float max_content_size , SizeConstraint constraint , Optional < float > available_space ) const
2022-03-12 14:36:59 +01:00
{
// If the available space in a given axis is definite, equal to clamp(min-content size, stretch-fit size, max-content size)
// (i.e. max(min-content size, min(max-content size, stretch-fit size))).
if ( available_space . has_value ( ) ) {
// FIXME: Compute the real stretch-fit size.
auto stretch_fit_size = * available_space ;
auto s = max ( min_content_size , min ( max_content_size , stretch_fit_size ) ) ;
return s ;
}
2022-07-20 17:59:46 +02:00
// When sizing under a min-content constraint, equal to the min-content size.
if ( constraint = = SizeConstraint : : MinContent )
return min_content_size ;
2022-03-12 14:36:59 +01:00
// Otherwise, equal to the max-content size in that axis.
return max_content_size ;
}
2022-07-20 17:59:46 +02:00
float FormattingContext : : calculate_fit_content_width ( Layout : : Box const & box , SizeConstraint constraint , Optional < float > available_space ) const
2022-03-12 14:36:59 +01:00
{
2022-07-20 17:59:46 +02:00
// When sizing under a min-content constraint, equal to the min-content size.
// NOTE: We check this first, to avoid needlessly calculating the max-content size.
if ( constraint = = SizeConstraint : : MinContent )
return calculate_min_content_width ( box ) ;
if ( constraint = = SizeConstraint : : MaxContent )
return calculate_max_content_width ( box ) ;
return calculate_fit_content_size ( calculate_min_content_width ( box ) , calculate_max_content_width ( box ) , constraint , available_space ) ;
2022-03-12 14:36:59 +01:00
}
2022-07-20 17:59:46 +02:00
float FormattingContext : : calculate_fit_content_height ( Layout : : Box const & box , SizeConstraint constraint , Optional < float > available_space ) const
2022-03-12 14:36:59 +01:00
{
2022-07-20 17:59:46 +02:00
// When sizing under a min-content constraint, equal to the min-content size.
// NOTE: We check this first, to avoid needlessly calculating the max-content size.
if ( constraint = = SizeConstraint : : MinContent )
return calculate_min_content_height ( box ) ;
if ( constraint = = SizeConstraint : : MaxContent )
return calculate_max_content_height ( box ) ;
return calculate_fit_content_size ( calculate_min_content_height ( box ) , calculate_max_content_height ( box ) , constraint , available_space ) ;
2022-03-12 14:36:59 +01:00
}
2022-03-30 19:44:18 +02:00
2022-07-16 23:30:32 +02:00
float FormattingContext : : calculate_auto_height ( LayoutState const & state , Box const & box )
2022-03-30 19:44:18 +02:00
{
if ( is < ReplacedBox > ( box ) ) {
return compute_height_for_replaced_element ( state , verify_cast < ReplacedBox > ( box ) ) ;
}
return compute_auto_height_for_block_level_element ( state , box ) ;
}
2022-07-08 00:40:53 +02:00
float FormattingContext : : calculate_min_content_width ( Layout : : Box const & box ) const
{
2022-07-08 12:44:49 +02:00
if ( box . has_intrinsic_width ( ) )
return * box . intrinsic_width ( ) ;
auto & root_state = m_state . m_root ;
2022-07-16 23:30:32 +02:00
auto & cache = * root_state . intrinsic_sizes . ensure ( & box , [ ] { return adopt_own ( * new LayoutState : : IntrinsicSizes ) ; } ) ;
2022-07-08 12:44:49 +02:00
if ( cache . min_content_width . has_value ( ) )
return * cache . min_content_width ;
2022-07-16 23:30:32 +02:00
LayoutState throwaway_state ( & m_state ) ;
2022-07-08 12:44:49 +02:00
auto const & containing_block = * box . containing_block ( ) ;
auto & containing_block_state = throwaway_state . get_mutable ( containing_block ) ;
2022-07-17 17:59:02 +02:00
containing_block_state . set_content_width ( 0 ) ;
2022-07-09 15:17:47 +02:00
2022-07-17 18:46:38 +02:00
if ( ! containing_block_state . has_definite_height ( ) )
2022-07-17 17:59:02 +02:00
containing_block_state . set_content_height ( INFINITY ) ;
2022-07-20 22:07:26 +02:00
else if ( containing_block . computed_values ( ) . height ( ) . is_auto ( ) )
containing_block_state . set_content_height ( containing_block_height_for ( containing_block ) ) ;
2022-07-12 02:17:17 +02:00
2022-07-09 15:17:47 +02:00
auto & box_state = throwaway_state . get_mutable ( box ) ;
box_state . width_constraint = SizeConstraint : : MinContent ;
2022-07-08 12:44:49 +02:00
auto context = const_cast < FormattingContext * > ( this ) - > create_independent_formatting_context_if_needed ( throwaway_state , box ) ;
VERIFY ( context ) ;
2022-07-20 18:12:12 +02:00
context - > run_intrinsic_sizing ( box ) ;
2022-07-08 12:44:49 +02:00
if ( context - > type ( ) = = FormattingContext : : Type : : Flex ) {
2022-07-17 17:59:02 +02:00
cache . min_content_width = box_state . content_width ( ) ;
2022-07-08 12:44:49 +02:00
} else {
cache . min_content_width = context - > greatest_child_width ( box ) ;
}
2022-07-11 23:53:52 +02:00
if ( ! isfinite ( * cache . min_content_width ) ) {
// HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
dbgln ( " FIXME: Calculated non-finite min-content width for {} " , box . debug_description ( ) ) ;
cache . min_content_width = 0 ;
}
2022-07-08 12:44:49 +02:00
return * cache . min_content_width ;
2022-07-08 00:40:53 +02:00
}
float FormattingContext : : calculate_max_content_width ( Layout : : Box const & box ) const
{
2022-07-08 12:44:49 +02:00
if ( box . has_intrinsic_width ( ) )
return * box . intrinsic_width ( ) ;
auto & root_state = m_state . m_root ;
2022-07-16 23:30:32 +02:00
auto & cache = * root_state . intrinsic_sizes . ensure ( & box , [ ] { return adopt_own ( * new LayoutState : : IntrinsicSizes ) ; } ) ;
2022-07-08 12:44:49 +02:00
if ( cache . max_content_width . has_value ( ) )
return * cache . max_content_width ;
2022-07-16 23:30:32 +02:00
LayoutState throwaway_state ( & m_state ) ;
2022-07-08 12:44:49 +02:00
auto const & containing_block = * box . containing_block ( ) ;
auto & containing_block_state = throwaway_state . get_mutable ( containing_block ) ;
2022-07-17 17:59:02 +02:00
containing_block_state . set_content_width ( INFINITY ) ;
2022-07-09 15:17:47 +02:00
2022-07-17 18:46:38 +02:00
if ( ! containing_block_state . has_definite_height ( ) )
2022-07-17 17:59:02 +02:00
containing_block_state . set_content_height ( INFINITY ) ;
2022-07-20 22:07:26 +02:00
else if ( containing_block . computed_values ( ) . height ( ) . is_auto ( ) )
containing_block_state . set_content_height ( containing_block_height_for ( containing_block ) ) ;
2022-07-12 02:17:17 +02:00
2022-07-09 15:17:47 +02:00
auto & box_state = throwaway_state . get_mutable ( box ) ;
box_state . width_constraint = SizeConstraint : : MaxContent ;
2022-07-08 12:44:49 +02:00
auto context = const_cast < FormattingContext * > ( this ) - > create_independent_formatting_context_if_needed ( throwaway_state , box ) ;
VERIFY ( context ) ;
2022-07-20 18:12:12 +02:00
context - > run_intrinsic_sizing ( box ) ;
2022-07-08 12:44:49 +02:00
if ( context - > type ( ) = = FormattingContext : : Type : : Flex ) {
2022-07-17 17:59:02 +02:00
cache . max_content_width = box_state . content_width ( ) ;
2022-07-08 12:44:49 +02:00
} else {
cache . max_content_width = context - > greatest_child_width ( box ) ;
}
2022-07-11 23:53:52 +02:00
if ( ! isfinite ( * cache . max_content_width ) ) {
// HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
dbgln ( " FIXME: Calculated non-finite max-content width for {} " , box . debug_description ( ) ) ;
cache . max_content_width = 0 ;
}
2022-07-08 12:44:49 +02:00
return * cache . max_content_width ;
2022-07-08 00:40:53 +02:00
}
float FormattingContext : : calculate_min_content_height ( Layout : : Box const & box ) const
{
2022-07-08 12:44:49 +02:00
if ( box . has_intrinsic_height ( ) )
return * box . intrinsic_height ( ) ;
auto & root_state = m_state . m_root ;
2022-07-16 23:30:32 +02:00
auto & cache = * root_state . intrinsic_sizes . ensure ( & box , [ ] { return adopt_own ( * new LayoutState : : IntrinsicSizes ) ; } ) ;
2022-07-08 12:44:49 +02:00
if ( cache . min_content_height . has_value ( ) )
return * cache . min_content_height ;
2022-07-16 23:30:32 +02:00
LayoutState throwaway_state ( & m_state ) ;
2022-07-08 12:44:49 +02:00
auto const & containing_block = * box . containing_block ( ) ;
auto & containing_block_state = throwaway_state . get_mutable ( containing_block ) ;
2022-07-17 17:59:02 +02:00
containing_block_state . set_content_height ( 0 ) ;
2022-07-09 15:17:47 +02:00
2022-07-17 18:46:38 +02:00
if ( ! containing_block_state . has_definite_width ( ) )
2022-07-17 17:59:02 +02:00
containing_block_state . set_content_width ( INFINITY ) ;
2022-07-20 22:07:26 +02:00
else if ( containing_block . computed_values ( ) . width ( ) . is_auto ( ) )
containing_block_state . set_content_width ( containing_block_width_for ( containing_block ) ) ;
2022-07-12 02:17:17 +02:00
2022-07-09 15:17:47 +02:00
auto & box_state = throwaway_state . get_mutable ( box ) ;
box_state . height_constraint = SizeConstraint : : MinContent ;
2022-07-08 12:44:49 +02:00
auto context = const_cast < FormattingContext * > ( this ) - > create_independent_formatting_context_if_needed ( throwaway_state , box ) ;
VERIFY ( context ) ;
2022-07-20 18:12:12 +02:00
context - > run_intrinsic_sizing ( box ) ;
2022-07-08 12:44:49 +02:00
if ( context - > type ( ) = = FormattingContext : : Type : : Flex ) {
2022-07-17 17:59:02 +02:00
cache . min_content_height = box_state . content_height ( ) ;
2022-07-08 12:44:49 +02:00
} else {
cache . min_content_height = calculate_auto_height ( throwaway_state , box ) ;
}
2022-07-11 23:53:52 +02:00
if ( ! isfinite ( * cache . min_content_height ) ) {
// HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
dbgln ( " FIXME: Calculated non-finite min-content height for {} " , box . debug_description ( ) ) ;
cache . min_content_height = 0 ;
}
2022-07-08 12:44:49 +02:00
return * cache . min_content_height ;
2022-07-08 00:40:53 +02:00
}
float FormattingContext : : calculate_max_content_height ( Layout : : Box const & box ) const
{
2022-07-08 12:44:49 +02:00
if ( box . has_intrinsic_height ( ) )
return * box . intrinsic_height ( ) ;
auto & root_state = m_state . m_root ;
2022-07-16 23:30:32 +02:00
auto & cache = * root_state . intrinsic_sizes . ensure ( & box , [ ] { return adopt_own ( * new LayoutState : : IntrinsicSizes ) ; } ) ;
2022-07-08 12:44:49 +02:00
if ( cache . max_content_height . has_value ( ) )
return * cache . max_content_height ;
2022-07-16 23:30:32 +02:00
LayoutState throwaway_state ( & m_state ) ;
2022-07-08 12:44:49 +02:00
auto const & containing_block = * box . containing_block ( ) ;
auto & containing_block_state = throwaway_state . get_mutable ( containing_block ) ;
2022-07-17 17:59:02 +02:00
containing_block_state . set_content_height ( INFINITY ) ;
2022-07-09 15:17:47 +02:00
2022-07-17 18:46:38 +02:00
if ( ! containing_block_state . has_definite_width ( ) )
2022-07-17 17:59:02 +02:00
containing_block_state . set_content_width ( INFINITY ) ;
2022-07-20 22:07:26 +02:00
else if ( containing_block . computed_values ( ) . width ( ) . is_auto ( ) )
containing_block_state . set_content_width ( containing_block_width_for ( containing_block ) ) ;
2022-07-12 02:17:17 +02:00
2022-07-09 15:17:47 +02:00
auto & box_state = throwaway_state . get_mutable ( box ) ;
box_state . height_constraint = SizeConstraint : : MaxContent ;
2022-07-08 12:44:49 +02:00
auto context = const_cast < FormattingContext * > ( this ) - > create_independent_formatting_context_if_needed ( throwaway_state , box ) ;
VERIFY ( context ) ;
2022-07-20 18:12:12 +02:00
context - > run_intrinsic_sizing ( box ) ;
2022-07-08 12:44:49 +02:00
if ( context - > type ( ) = = FormattingContext : : Type : : Flex ) {
2022-07-17 17:59:02 +02:00
cache . max_content_height = box_state . content_height ( ) ;
2022-07-08 12:44:49 +02:00
} else {
cache . max_content_height = calculate_auto_height ( throwaway_state , box ) ;
}
2022-07-11 23:53:52 +02:00
if ( ! isfinite ( * cache . max_content_height ) ) {
// HACK: If layout calculates a non-finite result, something went wrong. Force it to zero and log a little whine.
dbgln ( " FIXME: Calculated non-finite max-content height for {} " , box . debug_description ( ) ) ;
cache . max_content_height = 0 ;
}
2022-07-08 12:44:49 +02:00
return * cache . max_content_height ;
2022-07-08 00:40:53 +02:00
}
2022-07-16 23:30:32 +02:00
float FormattingContext : : containing_block_width_for ( Box const & box , LayoutState const & state )
2022-07-09 15:17:47 +02:00
{
auto & containing_block_state = state . get ( * box . containing_block ( ) ) ;
auto & box_state = state . get ( box ) ;
switch ( box_state . width_constraint ) {
case SizeConstraint : : MinContent :
return 0 ;
case SizeConstraint : : MaxContent :
return INFINITY ;
case SizeConstraint : : None :
2022-07-17 17:59:02 +02:00
return containing_block_state . content_width ( ) ;
2022-07-09 15:17:47 +02:00
}
VERIFY_NOT_REACHED ( ) ;
}
2022-07-16 23:30:32 +02:00
float FormattingContext : : containing_block_height_for ( Box const & box , LayoutState const & state )
2022-07-09 15:17:47 +02:00
{
auto & containing_block_state = state . get ( * box . containing_block ( ) ) ;
auto & box_state = state . get ( box ) ;
switch ( box_state . height_constraint ) {
case SizeConstraint : : MinContent :
return 0 ;
case SizeConstraint : : MaxContent :
return INFINITY ;
case SizeConstraint : : None :
2022-07-17 17:59:02 +02:00
return containing_block_state . content_height ( ) ;
2022-07-09 15:17:47 +02:00
}
VERIFY_NOT_REACHED ( ) ;
}
2022-08-10 19:19:44 +02:00
float FormattingContext : : compute_box_y_position_with_respect_to_siblings ( Box const & child_box , LayoutState : : UsedValues const & box_state )
{
float y = box_state . border_box_top ( ) ;
Vector < float > collapsible_margins ;
auto * relevant_sibling = child_box . previous_sibling_of_type < Layout : : BlockContainer > ( ) ;
while ( relevant_sibling ! = nullptr ) {
if ( ! relevant_sibling - > is_absolutely_positioned ( ) & & ! relevant_sibling - > is_floating ( ) ) {
auto const & relevant_sibling_state = m_state . get ( * relevant_sibling ) ;
collapsible_margins . append ( relevant_sibling_state . margin_bottom ) ;
// NOTE: Empty (0-height) preceding siblings have their margins collapsed with *their* preceding sibling, etc.
if ( relevant_sibling_state . border_box_height ( ) > 0 )
break ;
collapsible_margins . append ( relevant_sibling_state . margin_top ) ;
}
relevant_sibling = relevant_sibling - > previous_sibling_of_type < Layout : : BlockContainer > ( ) ;
}
if ( relevant_sibling ) {
// Collapse top margin with the collapsed margin(s) of preceding siblings.
collapsible_margins . append ( box_state . margin_top ) ;
float smallest_margin = 0 ;
float largest_margin = 0 ;
size_t negative_margin_count = 0 ;
for ( auto margin : collapsible_margins ) {
if ( margin < 0 )
+ + negative_margin_count ;
largest_margin = max ( largest_margin , margin ) ;
smallest_margin = min ( smallest_margin , margin ) ;
}
float collapsed_margin = 0 ;
if ( negative_margin_count = = collapsible_margins . size ( ) ) {
// When all margins are negative, the size of the collapsed margin is the smallest (most negative) margin.
collapsed_margin = smallest_margin ;
} else if ( negative_margin_count > 0 ) {
// When negative margins are involved, the size of the collapsed margin is the sum of the largest positive margin and the smallest (most negative) negative margin.
collapsed_margin = largest_margin + smallest_margin ;
} else {
// Otherwise, collapse all the adjacent margins by using only the largest one.
collapsed_margin = largest_margin ;
}
auto const & relevant_sibling_state = m_state . get ( * relevant_sibling ) ;
return y + relevant_sibling_state . offset . y ( )
+ relevant_sibling_state . content_height ( )
+ relevant_sibling_state . border_box_bottom ( )
+ collapsed_margin ;
} else {
return y + box_state . margin_top ;
}
}
2020-11-22 13:38:18 +01:00
}