From 0bbf7a1b541c01a39f493206000c77b9474e2ceb Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 28 Dec 2022 10:11:54 +0100 Subject: [PATCH] LibWeb: Refactor should_skip_anonymous_text_runs This same function was being copied in the {Flex,Grid}FormattingContext, so unify them in the parent FormattingContext. --- .../LibWeb/Layout/FlexFormattingContext.cpp | 15 ++------------- .../LibWeb/Layout/FormattingContext.cpp | 17 +++++++++++++++++ .../Libraries/LibWeb/Layout/FormattingContext.h | 1 + .../LibWeb/Layout/GridFormattingContext.cpp | 17 +---------------- 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp index 86c212fc10..c730e67b69 100644 --- a/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FlexFormattingContext.cpp @@ -305,19 +305,8 @@ void FlexFormattingContext::generate_anonymous_flex_items() HashMap> order_item_bucket; flex_container().for_each_child_of_type([&](Box& child_box) { - // Skip anonymous text runs that are only whitespace. - if (child_box.is_anonymous() && !child_box.is_generated() && !child_box.first_child_of_type()) { - bool contains_only_white_space = true; - child_box.for_each_in_subtree([&](auto const& node) { - if (!is(node) || !static_cast(node).dom_node().data().is_whitespace()) { - contains_only_white_space = false; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - if (contains_only_white_space) - return IterationDecision::Continue; - } + if (can_skip_is_anonymous_text_run(child_box)) + return IterationDecision::Continue; // Skip any "out-of-flow" children if (child_box.is_out_of_flow(*this)) diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp index 5434372461..55774d67a4 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.cpp @@ -1473,4 +1473,21 @@ bool FormattingContext::should_treat_height_as_auto(Box const& box, AvailableSpa || (box.computed_values().height().contains_percentage() && !available_space.height.is_definite()); } +bool FormattingContext::can_skip_is_anonymous_text_run(Box& box) +{ + if (box.is_anonymous() && !box.is_generated() && !box.first_child_of_type()) { + bool contains_only_white_space = true; + box.for_each_in_subtree([&](auto const& node) { + if (!is(node) || !static_cast(node).dom_node().data().is_whitespace()) { + contains_only_white_space = false; + return IterationDecision::Break; + } + return IterationDecision::Continue; + }); + if (contains_only_white_space) + return true; + } + return false; +} + } diff --git a/Userland/Libraries/LibWeb/Layout/FormattingContext.h b/Userland/Libraries/LibWeb/Layout/FormattingContext.h index b18f6e46b9..7026021149 100644 --- a/Userland/Libraries/LibWeb/Layout/FormattingContext.h +++ b/Userland/Libraries/LibWeb/Layout/FormattingContext.h @@ -81,6 +81,7 @@ public: virtual void determine_height_of_child(Box const&, AvailableSpace const&) { } virtual Gfx::FloatPoint calculate_static_position(Box const&) const; + bool can_skip_is_anonymous_text_run(Box&); protected: FormattingContext(Type, LayoutState&, Box const&, FormattingContext* parent = nullptr); diff --git a/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp b/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp index 79430a081b..e103f66d2c 100644 --- a/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp +++ b/Userland/Libraries/LibWeb/Layout/GridFormattingContext.cpp @@ -22,21 +22,6 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const auto& box_state = m_state.get_mutable(box); auto grid_template_columns = box.computed_values().grid_template_columns(); auto grid_template_rows = box.computed_values().grid_template_rows(); - auto should_skip_is_anonymous_text_run = [&](Box& child_box) -> bool { - if (child_box.is_anonymous() && !child_box.first_child_of_type()) { - bool contains_only_white_space = true; - child_box.for_each_in_subtree([&](auto const& node) { - if (!is(node) || !static_cast(node).dom_node().data().is_whitespace()) { - contains_only_white_space = false; - return IterationDecision::Break; - } - return IterationDecision::Continue; - }); - if (contains_only_white_space) - return true; - } - return false; - }; auto resolve_definite_track_size = [&](CSS::GridSize const& grid_size) -> float { VERIFY(grid_size.is_definite()); @@ -110,7 +95,7 @@ void GridFormattingContext::run(Box const& box, LayoutMode, AvailableSpace const Vector positioned_boxes; Vector boxes_to_place; box.for_each_child_of_type([&](Box& child_box) { - if (should_skip_is_anonymous_text_run(child_box)) + if (can_skip_is_anonymous_text_run(child_box)) return IterationDecision::Continue; boxes_to_place.append(child_box); return IterationDecision::Continue;