From db5bf6e64c0371edd4118024cce37b8b99924e03 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 21 Feb 2022 17:42:09 +0100 Subject: [PATCH] LibWeb: Rename FormattingState::ensure() -> get_mutable() This makes it much more obvious what the difference between get() and get_mutable() is. --- Userland/Libraries/LibWeb/DOM/Document.cpp | 2 +- .../LibWeb/Layout/BlockFormattingContext.cpp | 28 +++++++++---------- .../LibWeb/Layout/FlexFormattingContext.cpp | 22 +++++++-------- .../LibWeb/Layout/FormattingContext.cpp | 12 ++++---- .../Libraries/LibWeb/Layout/FormattingState.h | 6 ++-- .../LibWeb/Layout/InlineFormattingContext.cpp | 6 ++-- .../LibWeb/Layout/InlineLevelIterator.cpp | 4 +-- .../Libraries/LibWeb/Layout/LineBuilder.cpp | 2 +- .../LibWeb/Layout/SVGFormattingContext.cpp | 2 +- .../LibWeb/Layout/TableFormattingContext.cpp | 16 +++++------ 10 files changed, 49 insertions(+), 51 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 59617f0199..19c36132f5 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -567,7 +567,7 @@ void Document::update_layout() Layout::BlockFormattingContext root_formatting_context(formatting_state, *m_layout_root, nullptr); m_layout_root->build_stacking_context_tree(); - auto& icb_state = formatting_state.ensure(*m_layout_root); + auto& icb_state = formatting_state.get_mutable(*m_layout_root); icb_state.content_width = viewport_rect.width(); icb_state.content_height = viewport_rect.height(); diff --git a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp index 895b79339d..31a500d5ec 100644 --- a/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp @@ -90,7 +90,7 @@ void BlockFormattingContext::apply_transformations_to_children(Box const& box) } } - auto& child_box_state = m_state.ensure(child_box); + auto& child_box_state = m_state.get_mutable(child_box); auto untransformed_offset = child_box_state.offset; child_box_state.offset = Gfx::FloatPoint { untransformed_offset.x(), untransformed_offset.y() + transform_y_offset }; }); @@ -228,7 +228,7 @@ void BlockFormattingContext::compute_width(Box const& box) } } - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); box_state.content_width = used_width.to_px(box); box_state.margin_left = margin_left.to_px(box); box_state.margin_right = margin_right.to_px(box); @@ -276,7 +276,7 @@ void BlockFormattingContext::compute_width_for_floating_box(Box const& box) width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px); } - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); box_state.content_width = width.to_px(box); box_state.margin_left = margin_left.to_px(box); box_state.margin_right = margin_right.to_px(box); @@ -288,7 +288,7 @@ void BlockFormattingContext::compute_width_for_floating_box(Box const& box) void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox const& box) { - m_state.ensure(box).content_width = compute_width_for_replaced_element(m_state, box); + m_state.get_mutable(box).content_width = compute_width_for_replaced_element(m_state, box); } float BlockFormattingContext::compute_theoretical_height(FormattingState const& state, Box const& box) @@ -335,7 +335,7 @@ void BlockFormattingContext::compute_height(Box const& box, FormattingState& sta // First, resolve the top/bottom parts of the surrounding box model. - auto& box_state = state.ensure(box); + auto& box_state = state.get_mutable(box); // FIXME: While negative values are generally allowed for margins, for now just ignore those for height calculation box_state.margin_top = max(computed_values.margin().top.resolved(box, width_of_containing_block_as_length).to_px(box), 0); @@ -365,7 +365,7 @@ void BlockFormattingContext::layout_block_level_children(BlockContainer const& b float content_width = 0; block_container.for_each_child_of_type([&](Box& child_box) { - auto& box_state = m_state.ensure(child_box); + auto& box_state = m_state.get_mutable(child_box); if (child_box.is_absolutely_positioned()) { m_absolutely_positioned_boxes.append(child_box); @@ -421,7 +421,7 @@ void BlockFormattingContext::layout_block_level_children(BlockContainer const& b if (layout_mode != LayoutMode::Default) { auto& width = block_container.computed_values().width(); if (!width.has_value() || (width->is_length() && width->length().is_auto())) { - auto& block_container_state = m_state.ensure(block_container); + auto& block_container_state = m_state.get_mutable(block_container); block_container_state.content_width = content_width; } } @@ -429,7 +429,7 @@ void BlockFormattingContext::layout_block_level_children(BlockContainer const& b void BlockFormattingContext::compute_vertical_box_model_metrics(Box const& box, BlockContainer const& containing_block) { - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); auto const& computed_values = box.computed_values(); auto width_of_containing_block = CSS::Length::make_px(m_state.get(containing_block).content_width); @@ -443,7 +443,7 @@ void BlockFormattingContext::compute_vertical_box_model_metrics(Box const& box, void BlockFormattingContext::place_block_level_element_in_normal_flow_vertically(Box const& child_box, BlockContainer const& containing_block) { - auto& box_state = m_state.ensure(child_box); + auto& box_state = m_state.get_mutable(child_box); auto const& computed_values = child_box.computed_values(); compute_vertical_box_model_metrics(child_box, containing_block); @@ -510,7 +510,7 @@ void BlockFormattingContext::place_block_level_element_in_normal_flow_vertically void BlockFormattingContext::place_block_level_element_in_normal_flow_horizontally(Box const& child_box, BlockContainer const& containing_block) { - auto& box_state = m_state.ensure(child_box); + auto& box_state = m_state.get_mutable(child_box); auto const& containing_block_state = m_state.get(containing_block); float x = 0; @@ -528,7 +528,7 @@ void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_m auto viewport_rect = root().browsing_context().viewport_rect(); auto& icb = verify_cast(root()); - auto& icb_state = m_state.ensure(icb); + auto& icb_state = m_state.get_mutable(icb); VERIFY(!icb.children_are_inline()); layout_block_level_children(root(), layout_mode); @@ -558,7 +558,7 @@ void BlockFormattingContext::layout_floating_child(Box const& box, BlockContaine { VERIFY(box.is_floating()); - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); auto containing_block_content_width = m_state.get(containing_block).content_width; compute_width(box); @@ -667,8 +667,8 @@ void BlockFormattingContext::layout_list_item_marker(ListItemBox const& list_ite return; auto& marker = *list_item_box.marker(); - auto& marker_state = m_state.ensure(marker); - auto& list_item_state = m_state.ensure(list_item_box); + auto& marker_state = m_state.get_mutable(marker); + auto& list_item_state = m_state.get_mutable(list_item_box); int image_width = 0; int image_height = 0; diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp index 9b5316ab44..4f77355e14 100644 --- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp @@ -34,7 +34,7 @@ static bool is_undefined_or_auto(Optional const& length_p FlexFormattingContext::FlexFormattingContext(FormattingState& state, Box const& flex_container, FormattingContext* parent) : FormattingContext(Type::Flex, state, flex_container, parent) - , m_flex_container_state(m_state.ensure(flex_container)) + , m_flex_container_state(m_state.get_mutable(flex_container)) , m_flex_direction(flex_container.computed_values().flex_direction()) { } @@ -335,41 +335,41 @@ bool FlexFormattingContext::is_main_axis_margin_second_auto(Box const& box) cons void FlexFormattingContext::set_main_size(Box const& box, float size) { if (is_row_layout()) - m_state.ensure(box).content_width = size; + m_state.get_mutable(box).content_width = size; else - m_state.ensure(box).content_height = size; + m_state.get_mutable(box).content_height = size; } void FlexFormattingContext::set_cross_size(Box const& box, float size) { if (is_row_layout()) - m_state.ensure(box).content_height = size; + m_state.get_mutable(box).content_height = size; else - m_state.ensure(box).content_width = size; + m_state.get_mutable(box).content_width = size; } void FlexFormattingContext::set_offset(Box const& box, float main_offset, float cross_offset) { if (is_row_layout()) - m_state.ensure(box).offset = Gfx::FloatPoint { main_offset, cross_offset }; + m_state.get_mutable(box).offset = Gfx::FloatPoint { main_offset, cross_offset }; else - m_state.ensure(box).offset = Gfx::FloatPoint { cross_offset, main_offset }; + m_state.get_mutable(box).offset = Gfx::FloatPoint { cross_offset, main_offset }; } void FlexFormattingContext::set_main_axis_first_margin(Box const& box, float margin) { if (is_row_layout()) - m_state.ensure(box).margin_left = margin; + m_state.get_mutable(box).margin_left = margin; else - m_state.ensure(box).margin_top = margin; + m_state.get_mutable(box).margin_top = margin; } void FlexFormattingContext::set_main_axis_second_margin(Box const& box, float margin) { if (is_row_layout()) - m_state.ensure(box).margin_right = margin; + m_state.get_mutable(box).margin_right = margin; else - m_state.ensure(box).margin_bottom = margin; + m_state.get_mutable(box).margin_bottom = margin; } float FlexFormattingContext::sum_of_margin_padding_border_in_main_axis(Box const& box) const diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp index 47124e8936..493c7496a1 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp @@ -428,7 +428,7 @@ float FormattingContext::compute_height_for_replaced_element(FormattingState con void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_element(Box const& box) { auto& containing_block_state = m_state.get(*box.containing_block()); - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); auto width_of_containing_block = CSS::Length::make_px(containing_block_state.content_width); auto& computed_values = box.computed_values(); @@ -574,7 +574,7 @@ void FormattingContext::compute_width_for_absolutely_positioned_replaced_element // The used value of 'width' is determined as for inline replaced elements. // FIXME: This const_cast is gross. const_cast(box).prepare_for_replaced_layout(); - m_state.ensure(box).content_width = compute_width_for_replaced_element(m_state, box); + m_state.get_mutable(box).content_width = compute_width_for_replaced_element(m_state, box); } void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_element(Box const& box) @@ -582,7 +582,7 @@ void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_el auto& computed_values = box.computed_values(); auto const& containing_block = *box.containing_block(); auto const& containing_block_state = m_state.get(containing_block); - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); 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); @@ -631,7 +631,7 @@ void FormattingContext::layout_absolutely_positioned_element(Box const& box) auto const& containing_block_state = m_state.get(*box.containing_block()); 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); - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); auto specified_width = box.computed_values().width().has_value() ? box.computed_values().width()->resolved(box, width_of_containing_block).resolved(box) : CSS::Length::make_auto(); @@ -705,7 +705,7 @@ void FormattingContext::compute_height_for_absolutely_positioned_replaced_elemen { // 10.6.5 Absolutely positioned, replaced elements // The used value of 'height' is determined as for inline replaced elements. - m_state.ensure(box).content_height = compute_height_for_replaced_element(m_state, box); + m_state.get_mutable(box).content_height = compute_height_for_replaced_element(m_state, box); } void FormattingContext::compute_position(Box const& box) @@ -716,7 +716,7 @@ void FormattingContext::compute_position(Box const& box) if (box.computed_values().position() != CSS::Position::Relative) return; - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); auto const& computed_values = box.computed_values(); float width_of_containing_block = m_state.get(*box.containing_block()).content_width; auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block); diff --git a/Userland/Libraries/LibWeb/Layout/FormattingState.h b/Userland/Libraries/LibWeb/Layout/FormattingState.h index b3168c0999..7573bef49a 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingState.h +++ b/Userland/Libraries/LibWeb/Layout/FormattingState.h @@ -66,16 +66,14 @@ struct FormattingState { void commit(); - NodeState& ensure(NodeWithStyleAndBoxModelMetrics const& box) + NodeState& get_mutable(NodeWithStyleAndBoxModelMetrics const& box) { return *nodes.ensure(&box, [] { return make(); }); } NodeState const& get(NodeWithStyleAndBoxModelMetrics const& box) const { - if (!nodes.contains(&box)) - return const_cast(*this).ensure(box); - return *nodes.get(&box).value(); + return const_cast(*this).get_mutable(box); } HashMap> nodes; diff --git a/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp index 5cc766239e..9ff6e190dd 100644 --- a/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp @@ -96,7 +96,7 @@ void InlineFormattingContext::run(Box const&, LayoutMode layout_mode) content_height += max_height; } - auto& containing_block_state = m_state.ensure(containing_block()); + auto& containing_block_state = m_state.get_mutable(containing_block()); if (layout_mode != LayoutMode::Default) { containing_block_state.content_width = max_line_width; @@ -108,7 +108,7 @@ void InlineFormattingContext::run(Box const&, LayoutMode layout_mode) void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode layout_mode) { auto width_of_containing_block = CSS::Length::make_px(m_state.get(containing_block()).content_width); - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); auto const& computed_values = box.computed_values(); box_state.margin_left = computed_values.margin().left.resolved(box, width_of_containing_block).to_px(box); @@ -170,7 +170,7 @@ void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode l void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode) { - auto& containing_block_state = m_state.ensure(containing_block()); + auto& containing_block_state = m_state.get_mutable(containing_block()); auto& line_boxes = containing_block_state.line_boxes; line_boxes.clear_with_capacity(); diff --git a/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp b/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp index 5c63e7ba52..69d2763f0d 100644 --- a/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp +++ b/Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp @@ -30,7 +30,7 @@ void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyl // FIXME: It's really weird that *this* is where we assign box model metrics for these layout nodes.. - auto& node_state = m_formatting_state.ensure(node); + auto& node_state = m_formatting_state.get_mutable(node); auto const& container_state = m_formatting_state.get(m_container); auto const& computed_values = node.computed_values(); @@ -51,7 +51,7 @@ void InlineLevelIterator::exit_node_with_box_model_metrics() m_extra_trailing_metrics = ExtraBoxMetrics {}; auto& node = m_box_model_node_stack.last(); - auto& node_state = m_formatting_state.ensure(node); + auto& node_state = m_formatting_state.get_mutable(node); auto const& container_state = m_formatting_state.get(m_container); auto const& computed_values = node.computed_values(); diff --git a/Userland/Libraries/LibWeb/Layout/LineBuilder.cpp b/Userland/Libraries/LibWeb/Layout/LineBuilder.cpp index bf25487309..ada9288e7d 100644 --- a/Userland/Libraries/LibWeb/Layout/LineBuilder.cpp +++ b/Userland/Libraries/LibWeb/Layout/LineBuilder.cpp @@ -12,7 +12,7 @@ namespace Web::Layout { LineBuilder::LineBuilder(InlineFormattingContext& context, FormattingState& formatting_state) : m_context(context) , m_formatting_state(formatting_state) - , m_containing_block_state(formatting_state.ensure(context.containing_block())) + , m_containing_block_state(formatting_state.get_mutable(context.containing_block())) { begin_new_line(false); } diff --git a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp index 2c4624c6f3..6bcdfee60e 100644 --- a/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp @@ -32,7 +32,7 @@ void SVGFormattingContext::run(Box const& box, LayoutMode) auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0); bounding_box.inflate(stroke_width, stroke_width); - auto& geometry_box_state = m_state.ensure(geometry_box); + auto& geometry_box_state = m_state.get_mutable(geometry_box); geometry_box_state.offset = bounding_box.top_left(); geometry_box_state.content_width = bounding_box.width(); geometry_box_state.content_height = bounding_box.height(); diff --git a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp index d7aad2777a..4fc369adc1 100644 --- a/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp @@ -27,7 +27,7 @@ TableFormattingContext::~TableFormattingContext() void TableFormattingContext::run(Box const& box, LayoutMode) { - auto& box_state = m_state.ensure(box); + auto& box_state = m_state.get_mutable(box); compute_width(box); @@ -35,7 +35,7 @@ void TableFormattingContext::run(Box const& box, LayoutMode) float total_content_height = 0; box.for_each_child_of_type([&](auto& row_group_box) { - auto& row_group_box_state = m_state.ensure(row_group_box); + auto& row_group_box_state = m_state.get_mutable(row_group_box); compute_width(row_group_box); auto column_count = row_group_box.column_count(); @@ -50,7 +50,7 @@ void TableFormattingContext::run(Box const& box, LayoutMode) float content_height = 0; row_group_box.template for_each_child_of_type([&](auto& row) { - auto& row_state = m_state.ensure(row); + auto& row_state = m_state.get_mutable(row); row_state.offset = { 0, content_height }; layout_row(row, column_widths); content_width = max(content_width, row_state.content_width); @@ -75,12 +75,12 @@ void TableFormattingContext::run(Box const& box, LayoutMode) void TableFormattingContext::calculate_column_widths(Box const& row, Vector& column_widths) { - m_state.ensure(row); + m_state.get_mutable(row); size_t column_index = 0; auto* table = row.first_ancestor_of_type(); bool use_auto_layout = !table || (!table->computed_values().width().has_value() || (table->computed_values().width()->is_length() && table->computed_values().width()->length().is_auto())); row.for_each_child_of_type([&](auto& cell) { - auto& cell_state = m_state.ensure(cell); + auto& cell_state = m_state.get_mutable(cell); compute_width(cell); if (use_auto_layout) { (void)layout_inside(cell, LayoutMode::OnlyRequiredLineBreaks); @@ -94,7 +94,7 @@ void TableFormattingContext::calculate_column_widths(Box const& row, Vector& column_widths) { - auto& row_state = m_state.ensure(row); + auto& row_state = m_state.get_mutable(row); size_t column_index = 0; float tallest_cell_height = 0; float content_width = 0; @@ -102,7 +102,7 @@ void TableFormattingContext::layout_row(Box const& row, Vector& column_wi bool use_auto_layout = !table || (!table->computed_values().width().has_value() || (table->computed_values().width()->is_length() && table->computed_values().width()->length().is_auto())); row.for_each_child_of_type([&](auto& cell) { - auto& cell_state = m_state.ensure(cell); + auto& cell_state = m_state.get_mutable(cell); cell_state.offset = row_state.offset.translated(content_width, 0); // Layout the cell contents a second time, now that we know its final width. @@ -121,7 +121,7 @@ void TableFormattingContext::layout_row(Box const& row, Vector& column_wi if (use_auto_layout) { row_state.content_width = content_width; } else { - auto& table_state = m_state.ensure(*table); + auto& table_state = m_state.get_mutable(*table); row_state.content_width = table_state.content_width; }