Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/Layout/FormattingContext.cpp (C++)

Code before:
        box.for_each_in_subtree([&](auto const& node) {
            if (!is<TextNode>(node) || !static_cast<TextNode const&>(node).dom_node().data().is_ascii_whitespace()) {
                contains_only_white_space = false;
                return TraversalDecision::Break;
            }
            return TraversalDecision::Continue;
        });
        if (contains_only_white_space)
            return true;
    }
    return false;
}

CSSPixelRect FormattingContext::absolute_content_rect(Box const& box) const
{
    auto const& box_state = m_state.get(box);
    CSSPixelRect rect { box_state.offset, box_state.content_size() };
    for (auto* block = box_state.containing_block_used_values(); block; block = block->containing_block_used_values())
        rect.translate_by(block->offset);
    return rect;
}

Box const* FormattingContext::box_child_to_derive_baseline_from(Box const& box) const
{
    if (!box.has_children() || box.children_are_inline())
        return nullptr;
    // Find the last in-flow child that has a baseline (either directly via line boxes, or via its descendants).
    for (auto const* child = box.last_child(); child; child = child->previous_sibling()) {
        auto const* child_box = as_if<Box>(*child);
        if (!child_box)
            continue;
        if (child_box->is_out_of_flow(*this))
            continue;
        if (!m_state.get(*child_box).line_boxes.is_empty())
            return child_box;
        if (box_child_to_derive_baseline_from(*child_box))
            return child_box;
    }
    return nullptr;
}

CSSPixels FormattingContext::box_baseline(Box const& box) const
{
    auto const& box_state = m_state.get(box);

    // https://drafts.csswg.org/css2/#propdef-vertical-align
    auto const& vertical_align = box.computed_values().vertical_align();
    if (vertical_align.has<CSS::VerticalAlign>()) {
        switch (vertical_align.get<CSS::VerticalAlign>()) {
        case CSS::VerticalAlign::Top:
            // Top: Align the top of the aligned subtree with the top of the line box.

Review comment (suggestion):
~~I believe this needs a guard to [avoid descending into textarea](https://github.com/LadybirdBrowser/ladybird/pull/7028/changes#diff-68eef508eb5cb4c32b181ed2ad4581ae93eb573ab6ce606fe1632f6ac9b14cbeR1810). Unlike other text nodes, its baseline is bottom edge of the margin box.~~



Code after:
        bool contains_only_white_space = true;
        box.for_each_in_subtree([&](auto const& node) {
            if (!is<TextNode>(node) || !static_cast<TextNode const&>(node).dom_node().data().is_ascii_whitespace()) {
                contains_only_white_space = false;
                return TraversalDecision::Break;
            }
            return TraversalDecision::Continue;
        });
        if (contains_only_white_space)
            return true;
    }
    return false;
}

CSSPixelRect FormattingContext::absolute_content_rect(Box const& box) const
{
    auto const& box_state = m_state.get(box);
    CSSPixelRect rect { box_state.offset, box_state.content_size() };
    for (auto* block = box_state.containing_block_used_values(); block; block = block->containing_block_used_values())
        rect.translate_by(block->offset);
    return rect;
}

Box const* FormattingContext::box_child_to_derive_baseline_from(Box const& box) const
{
    if (!box.has_children() || box.children_are_inline())
        return nullptr;
    // Find the last in-flow child that has a baseline (either directly via line boxes, or via its descendants).
    for (auto const* child = box.last_child(); child; child = child->previous_sibling()) {
        auto const* child_box = as_if<Box>(*child);
        if (!child_box)
            continue;
        if (child_box->is_out_of_flow(*this))
            continue;
        if (!m_state.get(*child_box).line_boxes.is_empty())
            return child_box;
        if (box_child_to_derive_baseline_from(*child_box))
            return child_box;
    }
    return nullptr;
}

CSSPixels FormattingContext::box_baseline(Box const& box) const
{
    auto const& box_state = m_state.get(box);

    // https://drafts.csswg.org/css2/#propdef-vertical-align
    auto const& vertical_align = box.computed_values().vertical_align();
    if (vertical_align.has<CSS::VerticalAlign>()) {
        switch (vertical_align.get<CSS::VerticalAlign>()) {
        case CSS::VerticalAlign::Top: