From 8373a14835b70548e0af6d93b9fa8813e29588be Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Wed, 12 Oct 2022 10:55:52 +0200 Subject: [PATCH] LibWeb: Remove code duplication in computing height for abs-pos elements This refactors the solve_for_{top, bottom, height, etc} lambdas to use a common solve_for lambda that takes the length to be solved as an argument. This way some code duplication is removed. --- .../LibWeb/Layout/FormattingContext.cpp | 66 ++++--------------- 1 file changed, 14 insertions(+), 52 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp index f7d51eb4fc..6176488297 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp @@ -700,9 +700,10 @@ void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_el auto height_of_containing_block = available_space.height.to_px(); auto height_of_containing_block_as_length = CSS::Length::make_px(height_of_containing_block); - auto solve_for_top = [&] { - top = CSS::Length::make_px( + auto solve_for = [&](CSS::Length length) { + return CSS::Length::make_px( height_of_containing_block + - top.resolved(box, height_of_containing_block_as_length).to_px(box) - margin_top.length().to_px(box) - box.computed_values().border_top().width - box.computed_values().padding().top().length().to_px(box) @@ -710,71 +711,32 @@ void FormattingContext::compute_height_for_absolutely_positioned_non_replaced_el - box.computed_values().padding().bottom().length().to_px(box) - box.computed_values().border_bottom().width - margin_bottom.length().to_px(box) - - bottom.resolved(box, height_of_containing_block_as_length).to_px(box)); + - bottom.resolved(box, height_of_containing_block_as_length).to_px(box) + + length.to_px(box)); + }; + + auto solve_for_top = [&] { + top = solve_for(top.resolved(box, height_of_containing_block_as_length)); }; auto solve_for_bottom = [&] { - bottom = CSS::Length::make_px( - height_of_containing_block - - top.resolved(box, height_of_containing_block_as_length).to_px(box) - - margin_top.length().to_px(box) - - box.computed_values().border_top().width - - box.computed_values().padding().top().length().to_px(box) - - height.resolved(box, height_of_containing_block_as_length).to_px(box) - - box.computed_values().padding().bottom().length().to_px(box) - - box.computed_values().border_bottom().width - - margin_bottom.length().to_px(box)); + bottom = solve_for(bottom.resolved(box, height_of_containing_block_as_length)); }; auto solve_for_height = [&] { - height = CSS::Size::make_px( - height_of_containing_block - - top.resolved(box, height_of_containing_block_as_length).to_px(box) - - margin_top.length().to_px(box) - - box.computed_values().border_top().width - - box.computed_values().padding().top().length().to_px(box) - - box.computed_values().padding().bottom().length().to_px(box) - - box.computed_values().border_bottom().width - - margin_bottom.length().to_px(box) - - bottom.resolved(box, height_of_containing_block_as_length).to_px(box)); + height = CSS::Size::make_length(solve_for(height.resolved(box, height_of_containing_block_as_length))); }; auto solve_for_margin_top = [&] { - margin_top = CSS::Length::make_px( - height_of_containing_block - - top.resolved(box, height_of_containing_block_as_length).to_px(box) - - box.computed_values().border_top().width - - box.computed_values().padding().top().length().to_px(box) - - height.resolved(box, height_of_containing_block_as_length).to_px(box) - - box.computed_values().padding().bottom().length().to_px(box) - - box.computed_values().border_bottom().width - - margin_bottom.length().to_px(box) - - bottom.resolved(box, height_of_containing_block_as_length).to_px(box)); + margin_top = solve_for(margin_top.length()); }; auto solve_for_margin_bottom = [&] { - margin_bottom = CSS::Length::make_px( - height_of_containing_block - - top.resolved(box, height_of_containing_block_as_length).to_px(box) - - margin_top.length().to_px(box) - - box.computed_values().border_top().width - - box.computed_values().padding().top().length().to_px(box) - - height.resolved(box, height_of_containing_block_as_length).to_px(box) - - box.computed_values().padding().bottom().length().to_px(box) - - box.computed_values().border_bottom().width - - bottom.resolved(box, height_of_containing_block_as_length).to_px(box)); + margin_bottom = solve_for(margin_bottom.length()); }; auto solve_for_margin_top_and_margin_bottom = [&] { - auto remainder = height_of_containing_block - - top.resolved(box, height_of_containing_block_as_length).to_px(box) - - box.computed_values().border_top().width - - box.computed_values().padding().top().length().to_px(box) - - height.resolved(box, height_of_containing_block_as_length).to_px(box) - - box.computed_values().padding().bottom().length().to_px(box) - - box.computed_values().border_bottom().width - - bottom.resolved(box, height_of_containing_block_as_length).to_px(box); - + auto remainder = solve_for(CSS::Length::make_px(margin_top.length().to_px(box) + margin_bottom.length().to_px(box))).to_px(box); margin_top = CSS::Length::make_px(remainder / 2); margin_bottom = CSS::Length::make_px(remainder / 2); };