Repository: servo/servo
File: components/layout/query.rs (Rust)

Code before:
use crate::dom::NodeExt;
use crate::flow::inline::construct::{TextTransformation, WhitespaceCollapse, capitalize_string};
use crate::fragment_tree::{
    BoxFragment, Fragment, FragmentFlags, FragmentTree, SpecificLayoutInfo,
};
use crate::style_ext::ComputedValuesExt;
use crate::taffy::SpecificTaffyGridInfo;

/// Get a scroll node that would represents this [`ServoLayoutNode`]'s transform and
/// calculate its cumlative transform from its root scroll node to the scroll node.
fn root_transform_for_layout_node(
    scroll_tree: &ScrollTree,
    node: ServoThreadSafeLayoutNode<'_>,
) -> Option<FastLayoutTransform> {
    let fragments = node.fragments_for_pseudo(None);
    let box_fragment = fragments
        .first()
        .and_then(Fragment::retrieve_box_fragment)?
        .borrow();
    let scroll_tree_node_id = box_fragment.spatial_tree_node()?;
    Some(scroll_tree.cumulative_node_to_root_transform(scroll_tree_node_id))
}

pub(crate) fn process_padding_top_and_left_request(
    node: ServoThreadSafeLayoutNode<'_>,
) -> Option<(Au, Au)> {
    let fragments = node.fragments_for_pseudo(None);
    let fragment = fragments.first()?;
    match fragment {
        Fragment::Box(box_fragment) | Fragment::Float(box_fragment) => {
            let padding = box_fragment.borrow().padding;
            Some((padding.top, padding.left))
        },
        _ => Some((Au::zero(), Au::zero())),
    }
}

pub(crate) fn process_box_area_request(
    stacking_context_tree: &StackingContextTree,
    node: ServoThreadSafeLayoutNode<'_>,
    area: BoxAreaType,
) -> Option<Rect<Au>> {
    let rects: Vec<_> = node
        .fragments_for_pseudo(None)
        .iter()
        .filter_map(|node| node.cumulative_box_area_rect(area))
        .collect();
    if rects.is_empty() {
        return None;
    }
    let rect_union = rects.iter().fold(Rect::zero(), |unioned_rect, rect| {

Review comment (suggestion):
Nit, I think it's a bit friendlier just to return the entire padding everywhere and name this a `padding_query`.

Code after:
use crate::flow::inline::construct::{TextTransformation, WhitespaceCollapse, capitalize_string};
use crate::fragment_tree::{
    BoxFragment, Fragment, FragmentFlags, FragmentTree, SpecificLayoutInfo,
};
use crate::style_ext::ComputedValuesExt;
use crate::taffy::SpecificTaffyGridInfo;

/// Get a scroll node that would represents this [`ServoLayoutNode`]'s transform and
/// calculate its cumlative transform from its root scroll node to the scroll node.
fn root_transform_for_layout_node(
    scroll_tree: &ScrollTree,
    node: ServoThreadSafeLayoutNode<'_>,
) -> Option<FastLayoutTransform> {
    let fragments = node.fragments_for_pseudo(None);
    let box_fragment = fragments
        .first()
        .and_then(Fragment::retrieve_box_fragment)?
        .borrow();
    let scroll_tree_node_id = box_fragment.spatial_tree_node()?;
    Some(scroll_tree.cumulative_node_to_root_transform(scroll_tree_node_id))
}

pub(crate) fn process_padding_request(
    node: ServoThreadSafeLayoutNode<'_>,
) -> Option<PhysicalSides> {
    let fragments = node.fragments_for_pseudo(None);
    let fragment = fragments.first()?;
    Some(match fragment {
        Fragment::Box(box_fragment) | Fragment::Float(box_fragment) => {
            let padding = box_fragment.borrow().padding;
            PhysicalSides {
                top: padding.top,
                left: padding.left,
                bottom: padding.bottom,
                right: padding.right,
            }
        },
        _ => Default::default(),
    })
}

pub(crate) fn process_box_area_request(
    stacking_context_tree: &StackingContextTree,
    node: ServoThreadSafeLayoutNode<'_>,
    area: BoxAreaType,
) -> Option<Rect<Au>> {
    let rects: Vec<_> = node
        .fragments_for_pseudo(None)
        .iter()
        .filter_map(|node| node.cumulative_box_area_rect(area))
        .collect();