소스 검색

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.
Timon Kruiper 2 년 전
부모
커밋
8373a14835
1개의 변경된 파일14개의 추가작업 그리고 52개의 파일을 삭제
  1. 14 52
      Userland/Libraries/LibWeb/Layout/FormattingContext.cpp

+ 14 - 52
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);
     };