Skip to content

Truly independent web browser

29.5 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

use crate::css::computed_value_types::{ComputedFilter, ComputedStyleValueHandle, MaskValues};
use crate::css::computed_value_views::ComputedValuesView;
use crate::css::css_enums::{
    backface_visibility, content_visibility, isolation, line_style, mix_blend_mode, outline_style, overflow,
    positioning, transform_style,
};
use crate::css::css_pixels::CssPixels;
use crate::css::css_string::CssString;
use crate::css::property_metadata::property_name;
use crate::css::serialize::{StringUnits, with_fly_string_units};
use crate::css::style_value::StyleValueData;
use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};
use crate::layout::node_facts::{self, NodeShape};
use crate::painting::paint_read::{PaintRead, PaintRow};

const SEPARATOR_COMMA: u8 = 1;

fn has_flag(arena: &impl PaintRead, node: NodeSlotId, flag: NodeFlag) -> bool {
    arena.node_flags_if_live(node) & flag as u32 != 0
}

pub(crate) fn handle_value(handle: &ComputedStyleValueHandle) -> Option<&StyleValueData> {
    // SAFETY: A non-null handle points at the retained style value owned by the
    // node's style group payload, which outlives every reader of the pass.
    unsafe { handle.pointer.cast::<StyleValueData>().as_ref() }
}

fn for_each_comma_item(value: &StyleValueData, mut f: impl FnMut(&StyleValueData) -> bool) -> bool {
    if let StyleValueData::ValueList { values, separator, .. } = value
        && *separator == SEPARATOR_COMMA
    {
        return values.as_slice().iter().any(|item| f(item.data()));
    }
    f(value)
}

pub(crate) fn comma_items(value: &StyleValueData) -> Vec<&StyleValueData> {
    if let StyleValueData::ValueList { values, separator, .. } = value
        && *separator == SEPARATOR_COMMA
    {
        return values.as_slice().iter().map(|item| item.data()).collect();
    }
    vec![value]
}

pub(crate) fn is_abstract_image(value: &StyleValueData) -> bool {
    matches!(
        value,
        StyleValueData::Image { .. }
            | StyleValueData::ImageSet { .. }
            | StyleValueData::LinearGradient { .. }
            | StyleValueData::ConicGradient { .. }
            | StyleValueData::RadialGradient { .. }
    )
}

pub(crate) fn has_css_borders(style: ComputedValuesView<'_>) -> bool {
    let zero = CssPixels::from_raw(0);
    style.border_top_width() != zero
        || style.border_right_width() != zero
        || style.border_bottom_width() != zero
        || style.border_left_width() != zero
}

pub(crate) fn background_layers_have_image(style: ComputedValuesView<'_>) -> bool {
    let Some(value) = handle_value(&style.background().background_image) else {
        return false;
    };
    for_each_comma_item(value, is_abstract_image)
}

fn fly_string_equals_ascii(string: &CssString, expected: &[u8]) -> bool {
    with_fly_string_units(string, |units| match units {
        StringUnits::Ascii(bytes) => bytes == expected,
        StringUnits::Utf16(units) => units.iter().copied().eq(expected.iter().copied().map(u16::from)),
    })
}

fn will_change_has_any_property(style: ComputedValuesView<'_>, names: &[&[u8]]) -> bool {
    let hints = style.misc_interaction().implicit_will_change.as_slice();
    if hints
        .iter()
        .any(|&property| names.contains(&property_name(property).as_bytes()))
    {
        return true;
    }
    let Some(value) = handle_value(&style.misc_interaction().will_change) else {
        return false;
    };
    let StyleValueData::ValueList { values, .. } = value else {
        return false;
    };
    values.as_slice().iter().any(|item| match item.data() {
        StyleValueData::CustomIdent { custom_ident } => {
            names.iter().any(|name| fly_string_equals_ascii(custom_ident, name))
        }
        _ => false,
    })
}

fn will_change_has_property(style: ComputedValuesView<'_>, name: &[u8]) -> bool {
    will_change_has_any_property(style, &[name])
}

// A box whose will-change names a transform or effect property receives the matching visual
// context node before the property has a value. The value's arrival then patches the node's
// payload instead of reshaping the tree and re-recording the box's paint subtree.
pub(crate) fn will_change_promotes_transform_node(style: ComputedValuesView<'_>) -> bool {
    will_change_has_any_property(
        style,
        &[
            b"transform".as_slice(),
            b"translate".as_slice(),
            b"rotate".as_slice(),
            b"scale".as_slice(),
        ],
    )
}

pub(crate) fn will_change_promotes_effects_node(style: ComputedValuesView<'_>) -> bool {
    will_change_has_any_property(style, &[b"opacity".as_slice(), b"filter".as_slice()])
}

fn filter_has_filters(filter: &ComputedFilter) -> bool {
    filter.operations.length != 0
}

fn mask_references_url(mask: &MaskValues) -> bool {
    let Some(value) = handle_value(&mask.mask_image) else {
        return false;
    };
    let mut first = None;
    for_each_comma_item(value, |item| {
        first.get_or_insert(matches!(item, StyleValueData::Url { .. }));
        true
    });
    first.unwrap_or(false)
}

fn mask_image_is_image(mask: &MaskValues) -> bool {
    let Some(value) = handle_value(&mask.mask_image) else {
        return false;
    };
    let mut first = None;
    for_each_comma_item(value, |item| {
        first.get_or_insert(is_abstract_image(item));
        true
    });
    first.unwrap_or(false)
}

pub(crate) fn mask_layers_have_image(mask: &MaskValues) -> bool {
    let Some(value) = handle_value(&mask.mask_image) else {
        return false;
    };
    for_each_comma_item(value, is_abstract_image)
}

pub(crate) fn clip_path_has_value(mask: &MaskValues) -> bool {
    // NB: Besides none, clip-path is a <clip-source>, or a <basic-shape>, a <geometry-box>, or both.
    handle_value(&mask.clip_path).is_some_and(|value| match value {
        StyleValueData::Url { .. } | StyleValueData::BasicShape { .. } | StyleValueData::ValueList { .. } => true,
        StyleValueData::Keyword { keyword } => *keyword != crate::css::css_enums::keyword::NONE,
        _ => false,
    })
}

fn view_transition_name_has_value(style: ComputedValuesView<'_>) -> bool {
    handle_value(&style.misc_interaction().view_transition_name)
        .is_some_and(|value| matches!(value, StyleValueData::CustomIdent { .. }))
}

fn containment_applies_to_display(arena: &impl PaintRead, node: NodeSlotId, style: ComputedValuesView<'_>) -> bool {
    let display = style.display();
    if display.is_internal_table() && !display.is_table_cell() {
        return false;
    }
    let is_replaced_box = arena
        .node_kind_if_live(node)
        .is_some_and(node_facts::kind_is_replaced_box);
    if display.is_inline_outside() && display.is_flow_inside() && !is_replaced_box {
        return false;
    }
    true
}

pub(crate) fn has_layout_containment(arena: &impl PaintRead, node: NodeSlotId, style: ComputedValuesView<'_>) -> bool {
    let contained = style.box_values().layout_containment || style.content_visibility() == content_visibility::AUTO;
    contained && containment_applies_to_display(arena, node, style)
}

/// Whether the box itself shows: it is visible, and not fully transparent unless an animation of its opacity or
/// transform keeps its content for the compositor to reveal.
pub(crate) fn is_visible(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    arena.node_style_if_live(node).is_some_and(|style| {
        style.visibility() == crate::css::css_enums::visibility::VISIBLE
            && (style.effects().opacity != 0.0
                || arena.node_flags_if_live(node) & NodeFlag::HasAnimatedOpacityOrTransform as u32 != 0)
    })
}

pub(crate) fn is_scroll_container(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    arena
        .node(node)
        .is_some_and(|row| node_facts::kind_and_style_make_scroll_container(row.kind(), row.style()))
}

pub(crate) fn has_style_containment(style: ComputedValuesView<'_>) -> bool {
    let box_values = style.box_values();
    box_values.style_containment
        || box_values.is_size_container
        || box_values.is_inline_size_container
        || style.content_visibility() == content_visibility::AUTO
}

pub(crate) fn has_paint_containment(arena: &impl PaintRead, node: NodeSlotId, style: ComputedValuesView<'_>) -> bool {
    let contained = style.box_values().paint_containment || style.content_visibility() == content_visibility::AUTO;
    contained && containment_applies_to_display(arena, node, style)
}

pub(crate) fn kind_is_svg_element_box(kind: NodeKind) -> bool {
    node_facts::kind_is_svg_box(kind) || matches!(kind, NodeKind::SVGSVGBox | NodeKind::SVGForeignObjectBox)
}

/// The document element's box among `parent`'s children, looking through anonymous wrappers.
pub(crate) fn document_element_box_under(arena: &impl PaintRead, parent: NodeSlotId) -> Option<NodeSlotId> {
    arena.children(parent).find_map(|child| {
        let flags = arena.node_flags_if_live(child);
        if flags & NodeFlag::IsDocumentElement as u32 != 0 {
            return Some(child);
        }
        if flags & NodeFlag::Anonymous as u32 != 0 {
            return document_element_box_under(arena, child);
        }
        None
    })
}

pub(crate) fn node_is_root_element(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    let flags = arena.node_flags_if_live(node);
    // NB: Only the document element is the root: An html element anywhere else, e.g. in a shadow tree, is an
    //     ordinary element.
    flags & NodeFlag::Anonymous as u32 == 0
        && flags & NodeFlag::IsHtmlHtmlElement as u32 != 0
        && flags & NodeFlag::IsDocumentElement as u32 != 0
}

fn style_establishes_fixed_positioning_containing_block(
    arena: &impl PaintRead,
    node: NodeSlotId,
    style: ComputedValuesView<'_>,
) -> bool {
    use crate::css::css_enums::{backface_visibility, content_visibility};
    let will_change = |name: &[u8]| will_change_has_property(style, name);
    let transformable = || is_transformable(arena, node);
    let transform = style.transform();

    if (!transform.transformations.pointer.is_null() || will_change(b"transform")) && transformable() {
        return true;
    }
    if (!transform.translate.pointer.is_null() || will_change(b"translate")) && transformable() {
        return true;
    }
    if (!transform.rotate.pointer.is_null() || will_change(b"rotate")) && transformable() {
        return true;
    }
    if (!transform.scale.pointer.is_null() || will_change(b"scale")) && transformable() {
        return true;
    }
    // https://drafts.csswg.org/motion-1/#offset-path-property
    // All the usual effects of having a transform apply (such as creating a stacking context, etc.)
    if (transform.has_offset_path || will_change(b"offset-path")) && transformable() {
        return true;
    }

    if (transform.has_perspective || will_change(b"perspective")) && transformable() {
        return true;
    }

    let effects = style.effects();
    let is_root_element = || node_is_root_element(arena, node);
    if (filter_has_filters(&effects.filter) || will_change(b"filter")) && !is_root_element() {
        return true;
    }

    if (filter_has_filters(&effects.backdrop_filter) || will_change(b"backdrop-filter")) && !is_root_element() {
        return true;
    }

    if will_change(b"contain") {
        return true;
    }
    let content_visibility_adds_containment = style.content_visibility() == content_visibility::AUTO;
    let box_values = style.box_values();
    if (box_values.layout_containment || content_visibility_adds_containment)
        && has_layout_containment(arena, node, style)
    {
        return true;
    }
    if (box_values.paint_containment || content_visibility_adds_containment)
        && has_paint_containment(arena, node, style)
    {
        return true;
    }

    if (transform.transform_style == transform_style::PRESERVE_3D || will_change(b"transform-style")) && transformable()
    {
        return true;
    }

    if (transform.backface_visibility == backface_visibility::HIDDEN || will_change(b"backface-visibility"))
        && transformable()
        && participates_in_a_3d_rendering_context(arena, node)
    {
        return true;
    }

    false
}

fn inline_establishes_absolute_position_containing_block(style: ComputedValuesView<'_>) -> bool {
    let effects = style.effects();
    style.position() != positioning::STATIC
        || filter_has_filters(&effects.filter)
        || filter_has_filters(&effects.backdrop_filter)
        || will_change_has_any_property(style, &[b"position", b"filter", b"backdrop-filter"])
}

/// Whether a box with `style` may establish a containing block for absolutely or fixed positioned boxes, which
/// [`establishes_positioning_containing_blocks`] answers for the box. A box this answers no for establishes neither,
/// unless it is the viewport or a `<foreignObject>`'s box, which establish them whatever their style.
pub(crate) fn style_may_establish_positioning_containing_blocks(style: ComputedValuesView<'_>) -> bool {
    use crate::css::css_enums::{backface_visibility, content_visibility};
    let transform = style.transform();
    let effects = style.effects();
    let box_values = style.box_values();
    let misc = style.misc_interaction();
    style.position() != positioning::STATIC
        || !misc.implicit_will_change.as_slice().is_empty()
        || matches!(handle_value(&misc.will_change), Some(StyleValueData::ValueList { .. }))
        || !transform.transformations.pointer.is_null()
        || !transform.translate.pointer.is_null()
        || !transform.rotate.pointer.is_null()
        || !transform.scale.pointer.is_null()
        || transform.has_offset_path
        || transform.has_perspective
        || filter_has_filters(&effects.filter)
        || filter_has_filters(&effects.backdrop_filter)
        || box_values.layout_containment
        || box_values.paint_containment
        || style.content_visibility() == content_visibility::AUTO
        || transform.transform_style == transform_style::PRESERVE_3D
        || transform.backface_visibility == backface_visibility::HIDDEN
}

pub(crate) fn establishes_positioning_containing_blocks(arena: &impl PaintRead, node: NodeSlotId) -> (bool, bool) {
    let Some(row) = arena.node(node) else {
        return (false, false);
    };
    let kind = row.kind();
    if kind == NodeKind::InlineNode {
        let absolute = !node_facts::has_flag(&row, NodeFlag::Anonymous)
            && row
                .style()
                .is_some_and(inline_establishes_absolute_position_containing_block);
        return (absolute, false);
    }
    if !node_facts::kind_is_box(kind) {
        return (false, false);
    }

    if kind == NodeKind::SVGForeignObjectBox {
        return (true, true);
    }

    let Some(style) = row.style() else {
        return (false, false);
    };
    if style_establishes_fixed_positioning_containing_block(arena, node, style) {
        return (true, true);
    }

    if style.position() != positioning::STATIC || will_change_has_property(style, b"position") {
        return (true, false);
    }

    if kind == NodeKind::Viewport {
        return (true, false);
    }

    (false, false)
}

pub(crate) fn any_ancestor_establishes_a_fixed_position_containing_block(
    arena: &impl PaintRead,
    node: NodeSlotId,
) -> bool {
    // https://www.w3.org/TR/css-position-3/#fixed-positioning-containing-block
    // The containing block is established by the nearest ancestor box that establishes an fixed positioning containing
    // block, with the bounds of the containing block determined identically to the absolute positioning containing block.
    let mut ancestor = arena.node_containing_block_if_live(node);
    while let Some(ancestor_slot) = ancestor {
        if establishes_positioning_containing_blocks(arena, ancestor_slot).1 {
            return true;
        }
        ancestor = arena.node_containing_block_if_live(ancestor_slot);
    }
    false
}

pub(crate) fn has_css_transform(arena: &impl PaintRead, node: NodeSlotId, style: ComputedValuesView<'_>) -> bool {
    let transform = style.transform();
    let has_transform = !transform.transformations.pointer.is_null()
        || !transform.rotate.pointer.is_null()
        || !transform.translate.pointer.is_null()
        || !transform.scale.pointer.is_null()
        || transform.has_offset_path;
    has_transform && is_transformable(arena, node)
}

pub(crate) fn is_transformable(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    let Some(row) = arena.node(node) else {
        return false;
    };
    let kind = row.kind();
    if kind_is_svg_element_box(kind) {
        if matches!(kind, NodeKind::SVGClipBox | NodeKind::SVGPatternBox) {
            return true;
        }
        let is_renderable = (crate::layout::svg_formatting_context::kind_is_svg_graphics_box(kind)
            && kind != NodeKind::SVGMaskBox)
            || matches!(kind, NodeKind::SVGSVGBox | NodeKind::SVGForeignObjectBox);
        if !is_renderable {
            return false;
        }
        let mut ancestor = arena.node_parent_if_live(node);
        while let Some(current) = ancestor {
            if matches!(
                arena.node_kind_if_live(current),
                Some(NodeKind::SVGTextBox | NodeKind::SVGTextPathBox)
            ) {
                return false;
            }
            ancestor = arena.node_parent_if_live(current);
        }
        return true;
    }

    let is_dom_element = !node_facts::has_flag(&row, NodeFlag::Anonymous)
        && !node_facts::kind_is_text(kind)
        && kind != NodeKind::Viewport;
    let is_element_or_pseudo_element = is_dom_element || row.generated_for() != 0;
    // NB: A table's wrapper box takes the transform of the table (see LayoutStyle::copy_table_properties).
    if kind == NodeKind::TableWrapper {
        return true;
    }
    if is_element_or_pseudo_element && node_facts::kind_is_box(kind) {
        let Some(style) = row.style() else {
            return false;
        };
        let display = style.display();
        if display.is_table_column() || display.is_table_column_group() {
            return false;
        }
        if display.is_inline_outside() && !row.is_atomic_inline() {
            return false;
        }
        return true;
    }

    false
}

fn used_transform_style_is_preserve_3d(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    let Some(style) = arena.node_style_if_live(node) else {
        return false;
    };
    if style.transform().transform_style != transform_style::PRESERVE_3D {
        return false;
    }
    let effects = style.effects();
    let mask_values = style.mask();
    let grouping_overflow = |value: u8| value != overflow::VISIBLE && value != overflow::CLIP;
    let has_transform_style_grouping_property = grouping_overflow(style.overflow_x())
        || grouping_overflow(style.overflow_y())
        || effects.opacity < 1.0
        || filter_has_filters(&effects.filter)
        || effects.clip_is_rect
        || clip_path_has_value(mask_values)
        || effects.isolation == isolation::ISOLATE
        || mask_references_url(mask_values)
        || mask_layers_have_image(mask_values)
        || effects.mix_blend_mode != mix_blend_mode::NORMAL
        || filter_has_filters(&effects.backdrop_filter);
    if has_transform_style_grouping_property {
        return false;
    }
    if has_paint_containment(arena, node, style) {
        return false;
    }
    true
}

pub(crate) fn establishes_or_extends_a_3d_rendering_context(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    if !has_flag(arena, node, NodeFlag::HasPreserve3dTransformStyle) {
        return false;
    }
    used_transform_style_is_preserve_3d(arena, node) && is_transformable(arena, node)
}

fn participates_in_a_3d_rendering_context(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    let mut ancestor = arena.node_parent_if_live(node);
    while let Some(current) = ancestor {
        if has_flag(arena, current, NodeFlag::Anonymous) {
            ancestor = arena.node_parent_if_live(current);
            continue;
        }
        return establishes_or_extends_a_3d_rendering_context(arena, current);
    }
    false
}

#[derive(Clone, Copy)]
pub(crate) struct OutlineData {
    pub color: u32,
    pub geometry: OutlineGeometry,
    pub is_auto: bool,
}

#[derive(Clone, Copy)]
pub(crate) struct OutlineGeometry {
    pub line_style: u8,
    pub width: CssPixels,
}

fn outline_style_to_line_style(style: u8) -> u8 {
    match style {
        outline_style::AUTO => line_style::SOLID,
        outline_style::NONE => line_style::NONE,
        outline_style::DOTTED => line_style::DOTTED,
        outline_style::DASHED => line_style::DASHED,
        outline_style::SOLID => line_style::SOLID,
        outline_style::DOUBLE => line_style::DOUBLE,
        outline_style::GROOVE => line_style::GROOVE,
        outline_style::RIDGE => line_style::RIDGE,
        outline_style::INSET => line_style::INSET,
        outline_style::OUTSET => line_style::OUTSET,
        _ => unreachable!("computed outline-style holds an unknown value"),
    }
}

// `auto` lets us do whatever we want for the outline. 2px of the accent color seems reasonable.
pub(crate) fn auto_outline_width() -> CssPixels {
    CssPixels::from_integer(2)
}

pub(crate) fn outline_geometry(style: ComputedValuesView<'_>) -> Option<OutlineGeometry> {
    let misc = style.misc_layout();
    let (line_style, width) = if misc.outline_style == outline_style::AUTO {
        (line_style::SOLID, auto_outline_width())
    } else {
        (outline_style_to_line_style(misc.outline_style), misc.outline_width)
    };
    if line_style == line_style::NONE || width.raw_value() == 0 {
        return None;
    }
    Some(OutlineGeometry { line_style, width })
}

pub(crate) fn auto_outline_data(auto_outline_color: u32) -> OutlineData {
    OutlineData {
        color: auto_outline_color,
        geometry: OutlineGeometry {
            line_style: line_style::SOLID,
            width: CssPixels::from_integer(2),
        },
        is_auto: true,
    }
}

pub(crate) fn outline_data(
    arena: &impl PaintRead,
    node: NodeSlotId,
    window_is_focused: impl FnOnce() -> bool,
    auto_outline_color: u32,
) -> Option<OutlineData> {
    let style = arena.node_style_if_live(node)?;
    let is_auto = style.misc_layout().outline_style == outline_style::AUTO;
    if is_auto && !window_is_focused() {
        return None;
    }
    let geometry = outline_geometry(style)?;
    let color = if is_auto {
        auto_outline_color
    } else {
        style.outline_color()
    };
    if color >> 24 == 0 {
        return None;
    }
    Some(OutlineData {
        color,
        geometry,
        is_auto,
    })
}

pub(crate) fn outline_offset(arena: &impl PaintRead, node: NodeSlotId) -> CssPixels {
    arena
        .node_style_if_live(node)
        .map_or(CssPixels::from_raw(0), |style| style.misc_layout().outline_offset)
}

pub(crate) fn is_text_decoration_propagation_boundary(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    let Some(row) = arena.node(node) else {
        return false;
    };
    // NB: Anonymous wrappers must stay transparent to propagation so an element's own decorations still reach
    //     its text. The principal box of a pseudo-element is not a wrapper and must be checked like any other
    //     element, and a table wrapper carries the float and position of the table it wraps, so it is the only
    //     box where an out-of-flow table is observable.
    if node_facts::has_flag(&row, NodeFlag::Anonymous)
        && row.generated_for() == 0
        && row.kind() != NodeKind::TableWrapper
    {
        return false;
    }

    // https://drafts.csswg.org/css-text-decor-4/#decorating-box
    // NOTE: Note that text decorations are not propagated to any out-of-flow descendants, nor to the contents
    //       of atomic inline-level descendants such as inline blocks and inline tables.
    row.is_out_of_flow() || row.is_atomic_inline()
}

pub(crate) fn z_index(arena: &impl PaintRead, node: NodeSlotId) -> Option<i32> {
    let style = arena.node_style_if_live(node)?;
    let box_values = style.box_values();
    box_values.has_z_index.then_some(box_values.z_index)
}

pub(crate) fn effective_z_index(arena: &impl PaintRead, paintable: NodeSlotId) -> Option<i32> {
    if !is_positioned(arena, paintable) {
        return None;
    }
    z_index(arena, paintable)
}

// Facts a paintable shares with its layout node, read live from the arena so
// style changes that do not relayout (z-index, for one) are never stale here.

pub(crate) fn is_positioned(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    arena.node(node).is_some_and(PaintRow::is_positioned)
}

pub(crate) fn position(arena: &impl PaintRead, node: NodeSlotId) -> u8 {
    node_facts::node_position(arena.node_style_if_live(node))
}

pub(crate) fn is_fixed_position(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    position(arena, node) == positioning::FIXED
}

pub(crate) fn is_sticky_position(style: ComputedValuesView<'_>) -> bool {
    style.position() == positioning::STICKY
}

pub(crate) fn is_floating(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    arena.node(node).is_some_and(PaintRow::is_floating)
}

pub(crate) fn is_inline(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    node_facts::node_is_inline_outside(arena.node_style_if_live(node))
}

pub(crate) fn display(arena: &impl PaintRead, node: NodeSlotId) -> crate::css::display::FfiDisplay {
    node_facts::node_display(arena.node_style_if_live(node))
}

pub(crate) fn is_flex_or_grid_item(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    has_flag(arena, node, NodeFlag::IsFlexItem) || has_flag(arena, node, NodeFlag::IsGridItem)
}

pub(crate) fn is_anonymous(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    has_flag(arena, node, NodeFlag::Anonymous)
}

pub(crate) fn is_replaced_box(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    arena
        .node_kind_if_live(node)
        .is_some_and(node_facts::kind_is_replaced_box)
}

pub(crate) fn establishes_stacking_context(arena: &impl PaintRead, node: NodeSlotId) -> bool {
    let Some(row) = arena.node(node) else {
        return false;
    };
    let kind = row.kind();

    if node_facts::kind_is_svg_box(kind) {
        return false;
    }

    if kind == NodeKind::Viewport {
        return true;
    }

    let flags = row.flags();
    if flags & NodeFlag::Anonymous as u32 == 0 && flags & NodeFlag::IsHtmlHtmlElement as u32 != 0 {
        return true;
    }

    let Some(style) = row.style() else {
        return false;
    };
    let box_values = style.box_values();
    let will_change = |name: &[u8]| will_change_has_property(style, name);

    let position = style.position();
    let has_z_index = box_values.has_z_index || will_change(b"z-index");

    if (position == positioning::ABSOLUTE || position == positioning::RELATIVE) && has_z_index {
        return true;
    }

    if position == positioning::FIXED || position == positioning::STICKY || will_change(b"position") {
        return true;
    }

    let transformable = is_transformable(arena, node);
    let transform = style.transform();
    if transformable {
        if !transform.transformations.pointer.is_null() || will_change(b"transform") {
            return true;
        }
        if !transform.translate.pointer.is_null() || will_change(b"translate") {
            return true;
        }
        if !transform.rotate.pointer.is_null() || will_change(b"rotate") {
            return true;
        }
        if !transform.scale.pointer.is_null() || will_change(b"scale") {
            return true;
        }
        // https://drafts.csswg.org/motion-1/#offset-path-property
        // All the usual effects of having a transform apply (such as creating a stacking context, etc.)
        if transform.has_offset_path || will_change(b"offset-path") {
            return true;
        }
    }

    if has_z_index
        && let Some(parent) = arena.node_parent_if_live(node)
        && let Some(parent_style) = arena.node_style_if_live(parent)
    {
        let parent_display = parent_style.display();
        if parent_display.is_flex_inside() || parent_display.is_grid_inside() {
            return true;
        }
    }

    let effects = style.effects();
    if filter_has_filters(&effects.backdrop_filter)
        || filter_has_filters(&effects.filter)
        || will_change(b"backdrop-filter")
        || will_change(b"filter")
    {
        return true;
    }

    let mask_values = style.mask();
    if mask_references_url(mask_values)
        || clip_path_has_value(mask_values)
        || mask_image_is_image(mask_values)
        || will_change(b"mask")
        || will_change(b"clip-path")
        || will_change(b"mask-image")
    {
        return true;
    }

    if kind == NodeKind::SVGForeignObjectBox {
        return true;
    }

    if effects.isolation == isolation::ISOLATE || will_change(b"isolation") {
        return true;
    }

    if has_layout_containment(arena, node, style)
        || has_paint_containment(arena, node, style)
        || will_change(b"contain")
    {
        return true;
    }

    if effects.mix_blend_mode != mix_blend_mode::NORMAL || will_change(b"mix-blend-mode") {
        return true;
    }

    if view_transition_name_has_value(style) || will_change(b"view-transition-name") {
        return true;
    }

    if transformable && (transform.has_perspective || will_change(b"perspective")) {
        return true;
    }

    if transformable && (transform.transform_style == transform_style::PRESERVE_3D || will_change(b"transform-style")) {
        return true;
    }

    if (transform.backface_visibility == backface_visibility::HIDDEN || will_change(b"backface-visibility"))
        && transformable
        && participates_in_a_3d_rendering_context(arena, node)
    {
        return true;
    }

    effects.opacity < 1.0 || will_change(b"opacity")
}