Skip to content

Commit db5bf6e

Browse files
committed
LibWeb: Rename FormattingState::ensure() -> get_mutable()
This makes it much more obvious what the difference between get() and get_mutable() is.
1 parent 0f15d1f commit db5bf6e

10 files changed

+49
-51
lines changed

Userland/Libraries/LibWeb/DOM/Document.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ void Document::update_layout()
567567
Layout::BlockFormattingContext root_formatting_context(formatting_state, *m_layout_root, nullptr);
568568
m_layout_root->build_stacking_context_tree();
569569

570-
auto& icb_state = formatting_state.ensure(*m_layout_root);
570+
auto& icb_state = formatting_state.get_mutable(*m_layout_root);
571571
icb_state.content_width = viewport_rect.width();
572572
icb_state.content_height = viewport_rect.height();
573573

Userland/Libraries/LibWeb/Layout/BlockFormattingContext.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ void BlockFormattingContext::apply_transformations_to_children(Box const& box)
9090
}
9191
}
9292

93-
auto& child_box_state = m_state.ensure(child_box);
93+
auto& child_box_state = m_state.get_mutable(child_box);
9494
auto untransformed_offset = child_box_state.offset;
9595
child_box_state.offset = Gfx::FloatPoint { untransformed_offset.x(), untransformed_offset.y() + transform_y_offset };
9696
});
@@ -228,7 +228,7 @@ void BlockFormattingContext::compute_width(Box const& box)
228228
}
229229
}
230230

231-
auto& box_state = m_state.ensure(box);
231+
auto& box_state = m_state.get_mutable(box);
232232
box_state.content_width = used_width.to_px(box);
233233
box_state.margin_left = margin_left.to_px(box);
234234
box_state.margin_right = margin_right.to_px(box);
@@ -276,7 +276,7 @@ void BlockFormattingContext::compute_width_for_floating_box(Box const& box)
276276
width = CSS::Length(min(max(result.preferred_minimum_width, available_width), result.preferred_width), CSS::Length::Type::Px);
277277
}
278278

279-
auto& box_state = m_state.ensure(box);
279+
auto& box_state = m_state.get_mutable(box);
280280
box_state.content_width = width.to_px(box);
281281
box_state.margin_left = margin_left.to_px(box);
282282
box_state.margin_right = margin_right.to_px(box);
@@ -288,7 +288,7 @@ void BlockFormattingContext::compute_width_for_floating_box(Box const& box)
288288

289289
void BlockFormattingContext::compute_width_for_block_level_replaced_element_in_normal_flow(ReplacedBox const& box)
290290
{
291-
m_state.ensure(box).content_width = compute_width_for_replaced_element(m_state, box);
291+
m_state.get_mutable(box).content_width = compute_width_for_replaced_element(m_state, box);
292292
}
293293

294294
float BlockFormattingContext::compute_theoretical_height(FormattingState const& state, Box const& box)
@@ -335,7 +335,7 @@ void BlockFormattingContext::compute_height(Box const& box, FormattingState& sta
335335

336336
// First, resolve the top/bottom parts of the surrounding box model.
337337

338-
auto& box_state = state.ensure(box);
338+
auto& box_state = state.get_mutable(box);
339339

340340
// FIXME: While negative values are generally allowed for margins, for now just ignore those for height calculation
341341
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
365365
float content_width = 0;
366366

367367
block_container.for_each_child_of_type<Box>([&](Box& child_box) {
368-
auto& box_state = m_state.ensure(child_box);
368+
auto& box_state = m_state.get_mutable(child_box);
369369

370370
if (child_box.is_absolutely_positioned()) {
371371
m_absolutely_positioned_boxes.append(child_box);
@@ -421,15 +421,15 @@ void BlockFormattingContext::layout_block_level_children(BlockContainer const& b
421421
if (layout_mode != LayoutMode::Default) {
422422
auto& width = block_container.computed_values().width();
423423
if (!width.has_value() || (width->is_length() && width->length().is_auto())) {
424-
auto& block_container_state = m_state.ensure(block_container);
424+
auto& block_container_state = m_state.get_mutable(block_container);
425425
block_container_state.content_width = content_width;
426426
}
427427
}
428428
}
429429

430430
void BlockFormattingContext::compute_vertical_box_model_metrics(Box const& box, BlockContainer const& containing_block)
431431
{
432-
auto& box_state = m_state.ensure(box);
432+
auto& box_state = m_state.get_mutable(box);
433433
auto const& computed_values = box.computed_values();
434434
auto width_of_containing_block = CSS::Length::make_px(m_state.get(containing_block).content_width);
435435

@@ -443,7 +443,7 @@ void BlockFormattingContext::compute_vertical_box_model_metrics(Box const& box,
443443

444444
void BlockFormattingContext::place_block_level_element_in_normal_flow_vertically(Box const& child_box, BlockContainer const& containing_block)
445445
{
446-
auto& box_state = m_state.ensure(child_box);
446+
auto& box_state = m_state.get_mutable(child_box);
447447
auto const& computed_values = child_box.computed_values();
448448

449449
compute_vertical_box_model_metrics(child_box, containing_block);
@@ -510,7 +510,7 @@ void BlockFormattingContext::place_block_level_element_in_normal_flow_vertically
510510

511511
void BlockFormattingContext::place_block_level_element_in_normal_flow_horizontally(Box const& child_box, BlockContainer const& containing_block)
512512
{
513-
auto& box_state = m_state.ensure(child_box);
513+
auto& box_state = m_state.get_mutable(child_box);
514514
auto const& containing_block_state = m_state.get(containing_block);
515515

516516
float x = 0;
@@ -528,7 +528,7 @@ void BlockFormattingContext::layout_initial_containing_block(LayoutMode layout_m
528528
auto viewport_rect = root().browsing_context().viewport_rect();
529529

530530
auto& icb = verify_cast<Layout::InitialContainingBlock>(root());
531-
auto& icb_state = m_state.ensure(icb);
531+
auto& icb_state = m_state.get_mutable(icb);
532532

533533
VERIFY(!icb.children_are_inline());
534534
layout_block_level_children(root(), layout_mode);
@@ -558,7 +558,7 @@ void BlockFormattingContext::layout_floating_child(Box const& box, BlockContaine
558558
{
559559
VERIFY(box.is_floating());
560560

561-
auto& box_state = m_state.ensure(box);
561+
auto& box_state = m_state.get_mutable(box);
562562
auto containing_block_content_width = m_state.get(containing_block).content_width;
563563

564564
compute_width(box);
@@ -667,8 +667,8 @@ void BlockFormattingContext::layout_list_item_marker(ListItemBox const& list_ite
667667
return;
668668

669669
auto& marker = *list_item_box.marker();
670-
auto& marker_state = m_state.ensure(marker);
671-
auto& list_item_state = m_state.ensure(list_item_box);
670+
auto& marker_state = m_state.get_mutable(marker);
671+
auto& list_item_state = m_state.get_mutable(list_item_box);
672672

673673
int image_width = 0;
674674
int image_height = 0;

Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ static bool is_undefined_or_auto(Optional<CSS::LengthPercentage> const& length_p
3434

3535
FlexFormattingContext::FlexFormattingContext(FormattingState& state, Box const& flex_container, FormattingContext* parent)
3636
: FormattingContext(Type::Flex, state, flex_container, parent)
37-
, m_flex_container_state(m_state.ensure(flex_container))
37+
, m_flex_container_state(m_state.get_mutable(flex_container))
3838
, m_flex_direction(flex_container.computed_values().flex_direction())
3939
{
4040
}
@@ -335,41 +335,41 @@ bool FlexFormattingContext::is_main_axis_margin_second_auto(Box const& box) cons
335335
void FlexFormattingContext::set_main_size(Box const& box, float size)
336336
{
337337
if (is_row_layout())
338-
m_state.ensure(box).content_width = size;
338+
m_state.get_mutable(box).content_width = size;
339339
else
340-
m_state.ensure(box).content_height = size;
340+
m_state.get_mutable(box).content_height = size;
341341
}
342342

343343
void FlexFormattingContext::set_cross_size(Box const& box, float size)
344344
{
345345
if (is_row_layout())
346-
m_state.ensure(box).content_height = size;
346+
m_state.get_mutable(box).content_height = size;
347347
else
348-
m_state.ensure(box).content_width = size;
348+
m_state.get_mutable(box).content_width = size;
349349
}
350350

351351
void FlexFormattingContext::set_offset(Box const& box, float main_offset, float cross_offset)
352352
{
353353
if (is_row_layout())
354-
m_state.ensure(box).offset = Gfx::FloatPoint { main_offset, cross_offset };
354+
m_state.get_mutable(box).offset = Gfx::FloatPoint { main_offset, cross_offset };
355355
else
356-
m_state.ensure(box).offset = Gfx::FloatPoint { cross_offset, main_offset };
356+
m_state.get_mutable(box).offset = Gfx::FloatPoint { cross_offset, main_offset };
357357
}
358358

359359
void FlexFormattingContext::set_main_axis_first_margin(Box const& box, float margin)
360360
{
361361
if (is_row_layout())
362-
m_state.ensure(box).margin_left = margin;
362+
m_state.get_mutable(box).margin_left = margin;
363363
else
364-
m_state.ensure(box).margin_top = margin;
364+
m_state.get_mutable(box).margin_top = margin;
365365
}
366366

367367
void FlexFormattingContext::set_main_axis_second_margin(Box const& box, float margin)
368368
{
369369
if (is_row_layout())
370-
m_state.ensure(box).margin_right = margin;
370+
m_state.get_mutable(box).margin_right = margin;
371371
else
372-
m_state.ensure(box).margin_bottom = margin;
372+
m_state.get_mutable(box).margin_bottom = margin;
373373
}
374374

375375
float FlexFormattingContext::sum_of_margin_padding_border_in_main_axis(Box const& box) const

Userland/Libraries/LibWeb/Layout/FormattingContext.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ float FormattingContext::compute_height_for_replaced_element(FormattingState con
428428
void FormattingContext::compute_width_for_absolutely_positioned_non_replaced_element(Box const& box)
429429
{
430430
auto& containing_block_state = m_state.get(*box.containing_block());
431-
auto& box_state = m_state.ensure(box);
431+
auto& box_state = m_state.get_mutable(box);
432432

433433
auto width_of_containing_block = CSS::Length::make_px(containing_block_state.content_width);
434434
auto& computed_values = box.computed_values();
@@ -574,15 +574,15 @@ void FormattingContext::compute_width_for_absolutely_positioned_replaced_element
574574
// The used value of 'width' is determined as for inline replaced elements.
575575
// FIXME: This const_cast is gross.
576576
const_cast<ReplacedBox&>(box).prepare_for_replaced_layout();
577-
m_state.ensure(box).content_width = compute_width_for_replaced_element(m_state, box);
577+
m_state.get_mutable(box).content_width = compute_width_for_replaced_element(m_state, box);
578578
}
579579

580580
void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_element(Box const& box)
581581
{
582582
auto& computed_values = box.computed_values();
583583
auto const& containing_block = *box.containing_block();
584584
auto const& containing_block_state = m_state.get(containing_block);
585-
auto& box_state = m_state.ensure(box);
585+
auto& box_state = m_state.get_mutable(box);
586586
auto width_of_containing_block = CSS::Length::make_px(containing_block_state.content_width);
587587
auto height_of_containing_block = CSS::Length::make_px(containing_block_state.content_height);
588588

@@ -631,7 +631,7 @@ void FormattingContext::layout_absolutely_positioned_element(Box const& box)
631631
auto const& containing_block_state = m_state.get(*box.containing_block());
632632
auto width_of_containing_block = CSS::Length::make_px(containing_block_state.content_width);
633633
auto height_of_containing_block = CSS::Length::make_px(containing_block_state.content_height);
634-
auto& box_state = m_state.ensure(box);
634+
auto& box_state = m_state.get_mutable(box);
635635

636636
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();
637637

@@ -705,7 +705,7 @@ void FormattingContext::compute_height_for_absolutely_positioned_replaced_elemen
705705
{
706706
// 10.6.5 Absolutely positioned, replaced elements
707707
// The used value of 'height' is determined as for inline replaced elements.
708-
m_state.ensure(box).content_height = compute_height_for_replaced_element(m_state, box);
708+
m_state.get_mutable(box).content_height = compute_height_for_replaced_element(m_state, box);
709709
}
710710

711711
void FormattingContext::compute_position(Box const& box)
@@ -716,7 +716,7 @@ void FormattingContext::compute_position(Box const& box)
716716
if (box.computed_values().position() != CSS::Position::Relative)
717717
return;
718718

719-
auto& box_state = m_state.ensure(box);
719+
auto& box_state = m_state.get_mutable(box);
720720
auto const& computed_values = box.computed_values();
721721
float width_of_containing_block = m_state.get(*box.containing_block()).content_width;
722722
auto width_of_containing_block_as_length = CSS::Length::make_px(width_of_containing_block);

Userland/Libraries/LibWeb/Layout/FormattingState.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,14 @@ struct FormattingState {
6666

6767
void commit();
6868

69-
NodeState& ensure(NodeWithStyleAndBoxModelMetrics const& box)
69+
NodeState& get_mutable(NodeWithStyleAndBoxModelMetrics const& box)
7070
{
7171
return *nodes.ensure(&box, [] { return make<NodeState>(); });
7272
}
7373

7474
NodeState const& get(NodeWithStyleAndBoxModelMetrics const& box) const
7575
{
76-
if (!nodes.contains(&box))
77-
return const_cast<FormattingState&>(*this).ensure(box);
78-
return *nodes.get(&box).value();
76+
return const_cast<FormattingState&>(*this).get_mutable(box);
7977
}
8078

8179
HashMap<NodeWithStyleAndBoxModelMetrics const*, NonnullOwnPtr<NodeState>> nodes;

Userland/Libraries/LibWeb/Layout/InlineFormattingContext.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ void InlineFormattingContext::run(Box const&, LayoutMode layout_mode)
9696
content_height += max_height;
9797
}
9898

99-
auto& containing_block_state = m_state.ensure(containing_block());
99+
auto& containing_block_state = m_state.get_mutable(containing_block());
100100

101101
if (layout_mode != LayoutMode::Default) {
102102
containing_block_state.content_width = max_line_width;
@@ -108,7 +108,7 @@ void InlineFormattingContext::run(Box const&, LayoutMode layout_mode)
108108
void InlineFormattingContext::dimension_box_on_line(Box const& box, LayoutMode layout_mode)
109109
{
110110
auto width_of_containing_block = CSS::Length::make_px(m_state.get(containing_block()).content_width);
111-
auto& box_state = m_state.ensure(box);
111+
auto& box_state = m_state.get_mutable(box);
112112
auto const& computed_values = box.computed_values();
113113

114114
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
170170

171171
void InlineFormattingContext::generate_line_boxes(LayoutMode layout_mode)
172172
{
173-
auto& containing_block_state = m_state.ensure(containing_block());
173+
auto& containing_block_state = m_state.get_mutable(containing_block());
174174
auto& line_boxes = containing_block_state.line_boxes;
175175
line_boxes.clear_with_capacity();
176176

Userland/Libraries/LibWeb/Layout/InlineLevelIterator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void InlineLevelIterator::enter_node_with_box_model_metrics(Layout::NodeWithStyl
3030

3131
// FIXME: It's really weird that *this* is where we assign box model metrics for these layout nodes..
3232

33-
auto& node_state = m_formatting_state.ensure(node);
33+
auto& node_state = m_formatting_state.get_mutable(node);
3434
auto const& container_state = m_formatting_state.get(m_container);
3535
auto const& computed_values = node.computed_values();
3636

@@ -51,7 +51,7 @@ void InlineLevelIterator::exit_node_with_box_model_metrics()
5151
m_extra_trailing_metrics = ExtraBoxMetrics {};
5252

5353
auto& node = m_box_model_node_stack.last();
54-
auto& node_state = m_formatting_state.ensure(node);
54+
auto& node_state = m_formatting_state.get_mutable(node);
5555
auto const& container_state = m_formatting_state.get(m_container);
5656
auto const& computed_values = node.computed_values();
5757

Userland/Libraries/LibWeb/Layout/LineBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Web::Layout {
1212
LineBuilder::LineBuilder(InlineFormattingContext& context, FormattingState& formatting_state)
1313
: m_context(context)
1414
, m_formatting_state(formatting_state)
15-
, m_containing_block_state(formatting_state.ensure(context.containing_block()))
15+
, m_containing_block_state(formatting_state.get_mutable(context.containing_block()))
1616
{
1717
begin_new_line(false);
1818
}

Userland/Libraries/LibWeb/Layout/SVGFormattingContext.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void SVGFormattingContext::run(Box const& box, LayoutMode)
3232
auto stroke_width = geometry_box.dom_node().stroke_width().value_or(0);
3333
bounding_box.inflate(stroke_width, stroke_width);
3434

35-
auto& geometry_box_state = m_state.ensure(geometry_box);
35+
auto& geometry_box_state = m_state.get_mutable(geometry_box);
3636
geometry_box_state.offset = bounding_box.top_left();
3737
geometry_box_state.content_width = bounding_box.width();
3838
geometry_box_state.content_height = bounding_box.height();

Userland/Libraries/LibWeb/Layout/TableFormattingContext.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ TableFormattingContext::~TableFormattingContext()
2727

2828
void TableFormattingContext::run(Box const& box, LayoutMode)
2929
{
30-
auto& box_state = m_state.ensure(box);
30+
auto& box_state = m_state.get_mutable(box);
3131

3232
compute_width(box);
3333

3434
float total_content_width = 0;
3535
float total_content_height = 0;
3636

3737
box.for_each_child_of_type<TableRowGroupBox>([&](auto& row_group_box) {
38-
auto& row_group_box_state = m_state.ensure(row_group_box);
38+
auto& row_group_box_state = m_state.get_mutable(row_group_box);
3939

4040
compute_width(row_group_box);
4141
auto column_count = row_group_box.column_count();
@@ -50,7 +50,7 @@ void TableFormattingContext::run(Box const& box, LayoutMode)
5050
float content_height = 0;
5151

5252
row_group_box.template for_each_child_of_type<TableRowBox>([&](auto& row) {
53-
auto& row_state = m_state.ensure(row);
53+
auto& row_state = m_state.get_mutable(row);
5454
row_state.offset = { 0, content_height };
5555
layout_row(row, column_widths);
5656
content_width = max(content_width, row_state.content_width);
@@ -75,12 +75,12 @@ void TableFormattingContext::run(Box const& box, LayoutMode)
7575

7676
void TableFormattingContext::calculate_column_widths(Box const& row, Vector<float>& column_widths)
7777
{
78-
m_state.ensure(row);
78+
m_state.get_mutable(row);
7979
size_t column_index = 0;
8080
auto* table = row.first_ancestor_of_type<TableBox>();
8181
bool use_auto_layout = !table || (!table->computed_values().width().has_value() || (table->computed_values().width()->is_length() && table->computed_values().width()->length().is_auto()));
8282
row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
83-
auto& cell_state = m_state.ensure(cell);
83+
auto& cell_state = m_state.get_mutable(cell);
8484
compute_width(cell);
8585
if (use_auto_layout) {
8686
(void)layout_inside(cell, LayoutMode::OnlyRequiredLineBreaks);
@@ -94,15 +94,15 @@ void TableFormattingContext::calculate_column_widths(Box const& row, Vector<floa
9494

9595
void TableFormattingContext::layout_row(Box const& row, Vector<float>& column_widths)
9696
{
97-
auto& row_state = m_state.ensure(row);
97+
auto& row_state = m_state.get_mutable(row);
9898
size_t column_index = 0;
9999
float tallest_cell_height = 0;
100100
float content_width = 0;
101101
auto* table = row.first_ancestor_of_type<TableBox>();
102102
bool use_auto_layout = !table || (!table->computed_values().width().has_value() || (table->computed_values().width()->is_length() && table->computed_values().width()->length().is_auto()));
103103

104104
row.for_each_child_of_type<TableCellBox>([&](auto& cell) {
105-
auto& cell_state = m_state.ensure(cell);
105+
auto& cell_state = m_state.get_mutable(cell);
106106
cell_state.offset = row_state.offset.translated(content_width, 0);
107107

108108
// 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<float>& column_wi
121121
if (use_auto_layout) {
122122
row_state.content_width = content_width;
123123
} else {
124-
auto& table_state = m_state.ensure(*table);
124+
auto& table_state = m_state.get_mutable(*table);
125125
row_state.content_width = table_state.content_width;
126126
}
127127

0 commit comments

Comments
 (0)