Truly independent web browser
/*
* Copyright (c) 2026-present, the Ladybird developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
use super::tree_shape::ShapeCell;
use crate::layout::CssPixels;
use std::ffi::c_void;
pub const GENERATED_FOR_AFTER: u8 = 1;
pub const GENERATED_FOR_BACKDROP: u8 = 2;
pub const GENERATED_FOR_BEFORE: u8 = 3;
pub const GENERATED_FOR_FIRST_LETTER: u8 = 4;
pub const GENERATED_FOR_MARKER: u8 = 6;
/// The last pseudo-element an element holds a box for in its own right; the ones from
/// `GENERATED_FOR_AFTER` up to it are an element's synthetic pseudo-elements.
pub const GENERATED_FOR_LAST_SYNTHETIC: u8 = 11;
/// `CSS::PseudoElement::Selection`, as the style engine numbers an element's pseudo-element
/// records. It generates no box, so no row names it.
pub const SELECTION_PSEUDO_KIND: u8 = 8;
/// `CSS::PseudoElement::SearchText`, as the style engine numbers an element's pseudo-element
/// records. It generates no box either.
pub const SEARCH_TEXT_PSEUDO_KIND: u8 = 6;
/// The current find-in-page match's `::search-text`, as the style engine numbers an element's
/// pseudo-element records. It generates no box either.
pub const SEARCH_TEXT_CURRENT_PSEUDO_KIND: u8 = 7;
/// `CSS::PseudoElement::SpellingError`, as the style engine numbers an element's pseudo-element
/// records. It generates no box either.
pub const SPELLING_ERROR_PSEUDO_KIND: u8 = 9;
/// The pseudo-element a row generated for `generated_for` stands for, as the style engine numbers
/// an element's pseudo-element records. `Layout::Node::encode_generated_for` is its inverse.
pub(crate) const fn pseudo_kind_of(generated_for: u8) -> u8 {
generated_for - 1
}
/// The style a row is laid out by: the groups at the front of the style record the row pins, of an
/// anonymous box's style or of an animation overlay. A copy counts no reference: whoever made the
/// row's style keeps it alive while the row holds it.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
#[repr(transparent)]
pub(crate) struct LayoutStyleRef(std::ptr::NonNull<crate::css::computed_value_views::StyleRecordGroups>);
const _: () = assert!(size_of::<LayoutStyleRef>() == 8 && size_of::<Option<LayoutStyleRef>>() == 8);
// SAFETY: A style's groups are immutable while a row holds them, and the handle touches no
// reference count, so sharing one shares only reads.
unsafe impl Send for LayoutStyleRef {}
unsafe impl Sync for LayoutStyleRef {}
impl LayoutStyleRef {
pub(crate) fn of(groups: &crate::css::computed_value_views::StyleRecordGroups) -> Self {
Self(std::ptr::NonNull::from(groups))
}
pub(crate) fn of_groups(groups: &crate::css::computed_values::StyleGroups) -> Self {
Self::of(crate::css::computed_value_views::StyleRecordGroups::of(groups))
}
/// The style `pointer` names, as the host passes one, or `None` for null.
///
/// # Safety
/// A non-null `pointer` must point at the groups of a style that outlives every row holding it.
pub(crate) unsafe fn from_raw(pointer: *const c_void) -> Option<Self> {
std::ptr::NonNull::new(pointer.cast_mut()).map(|pointer| Self(pointer.cast()))
}
pub(crate) fn as_ptr(self) -> *const c_void {
self.0.as_ptr().cast_const().cast()
}
/// A style no read reaches, for a row a test lays out without reading its style.
#[cfg(test)]
pub(crate) fn dangling() -> Self {
Self(std::ptr::NonNull::dangling())
}
/// The style's computed values.
///
/// # Safety
/// The style must stay alive and unchanged for `'a`.
#[inline]
pub(crate) unsafe fn view<'a>(self) -> crate::css::computed_value_views::ComputedValuesView<'a> {
// SAFETY: As the caller guarantees.
crate::css::computed_value_views::ComputedValuesView::of_record_groups(unsafe { self.0.as_ref() })
}
}
impl From<crate::painting::host::FfiNaturalSize> for crate::css::style::NaturalSize {
fn from(size: crate::painting::host::FfiNaturalSize) -> Self {
Self {
width: size.width.has_value.then_some(size.width.value.raw_value()),
height: size.height.has_value.then_some(size.height.value.raw_value()),
aspect_ratio: size.has_aspect_ratio.then_some((
size.aspect_ratio_numerator.raw_value(),
size.aspect_ratio_denominator.raw_value(),
)),
}
}
}
impl From<crate::css::style::NaturalSize> for crate::painting::host::FfiNaturalSize {
fn from(size: crate::css::style::NaturalSize) -> Self {
let (numerator, denominator) = size.aspect_ratio.unwrap_or_default();
Self {
width: size.width.map(CssPixels::from_raw).into(),
height: size.height.map(CssPixels::from_raw).into(),
has_aspect_ratio: size.aspect_ratio.is_some(),
aspect_ratio_numerator: CssPixels::from_raw(numerator),
aspect_ratio_denominator: CssPixels::from_raw(denominator),
}
}
}
pub use super::node_slot_id::{MAX_NODE_SLOT_COUNT, NodeSlotId};
/// A DOM node, named the way the host names one without pointing at it: the document, by itself,
/// or any other node by its StyleNodeID. No node at all is a StyleNodeID of 0.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(C)]
pub struct FfiNodeIdentity {
pub style_node: u32,
pub is_document: bool,
}
impl FfiNodeIdentity {
pub(crate) fn is_none(self) -> bool {
self.style_node == 0 && !self.is_document
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum NodeKind {
Unset = 0,
AudioBox = 1,
BlockContainer = 2,
Box = 3,
BreakNode = 4,
CanvasBox = 5,
CheckBox = 6,
FieldSetBox = 7,
GeneratedTextNode = 8,
ImageBox = 9,
InlineNode = 10,
LegendBox = 11,
ListItemBox = 12,
ListItemMarkerBox = 13,
NavigableContainerViewport = 14,
Node = 15,
RadioButton = 18,
RangeInputBox = 19,
SVGClipBox = 22,
SVGForeignObjectBox = 23,
SVGGeometryBox = 24,
SVGGraphicsBox = 25,
SVGImageBox = 26,
SVGMaskBox = 27,
SVGPatternBox = 28,
SVGSVGBox = 29,
SVGTextBox = 30,
SVGTextPathBox = 31,
TableWrapper = 32,
TextAreaBox = 33,
TextInputBox = 34,
TextNode = 35,
VideoBox = 37,
Viewport = 38,
FrameSetBox = 39,
/// A `<wbr>`, which gives the line a soft wrap opportunity.
WordBreakOpportunityNode = 40,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u32)]
pub enum NodeFlag {
Anonymous = 1 << 0,
HasStyle = 1 << 1,
ChildrenAreInline = 1 << 2,
IsFlexItem = 1 << 3,
IsGridItem = 1 << 4,
/// The box is an element's or a pseudo-element's own box, and that element or pseudo-element
/// stores a scroll offset other than zero. The overflow update measures such a box eagerly
/// after a full commit so the offset can be clamped.
HasScrollOffset = 1 << 5,
IsBody = 1 << 6,
NeedsLayoutUpdate = 1 << 7,
/// A style change altered geometry-determining properties of this node itself, so a partial relayout must re-resolve
/// its own size and position instead of reusing them.
NeedsOwnGeometryUpdate = 1 << 8,
AbsposDescendantEscapes = 1 << 9,
CompensatesForHorizontalScroll = 1 << 10,
CompensatesForVerticalScroll = 1 << 11,
IsReplacedElement = 1 << 12,
IsHtmlInputElement = 1 << 13,
IsHtmlHtmlElement = 1 << 14,
IsInUserAgentShadowTree = 1 << 15,
UsesButtonLayout = 1 << 16,
IsEditingHost = 1 << 17,
ReplacedBoxCanHaveChildren = 1 << 18,
IsPseudoElementPrincipalBox = 1 << 19,
FollowsPrincipalStyle = 1 << 20,
EstablishesAbsolutePositionContainingBlock = 1 << 21,
ProducesLineBoxFragmentWhenEmpty = 1 << 22,
ListMarkerIsInside = 1 << 23,
HasAnchorNames = 1 << 24,
InsetsUseAnchorFunctions = 1 << 25,
HasCommittedFragmentLink = 1 << 26,
HasPreserve3dTransformStyle = 1 << 27,
IsMissingTableCell = 1 << 28,
HasAnimatedOpacityOrTransform = 1 << 29,
IsDocumentElement = 1 << 30,
EstablishesFixedPositionContainingBlock = 0x8000_0000,
}
impl NodeFlag {
/// The flags that say what node a row stands for. A row is built with them, and installing a style changes none.
pub(crate) const IDENTITY: u32 = Self::Anonymous as u32 | Self::IsBody as u32 | Self::IsDocumentElement as u32;
/// The flags a box is built with from what its element is, which no style change changes: see
/// `node_facts::construction_flags`.
pub(crate) const CONSTRUCTION: u32 = Self::Anonymous as u32
| Self::HasStyle as u32
| Self::IsReplacedElement as u32
| Self::IsHtmlInputElement as u32
| Self::IsHtmlHtmlElement as u32
| Self::IsDocumentElement as u32
| Self::IsInUserAgentShadowTree as u32
| Self::UsesButtonLayout as u32
| Self::IsEditingHost as u32
| Self::IsBody as u32
| Self::ReplacedBoxCanHaveChildren as u32;
}
/// Facts a node takes from its ancestors. They are derived when the node is attached or its
/// ancestors' styles change, so laying out a subtree never reads above it to learn them.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub(crate) enum AncestorFact {
/// The parent's inner display type is flex or grid.
ParentIsFlexOrGridContainer = 1 << 0,
/// The parent is unstyled, or is not floating and has a flow or flow-root inner display type.
ParentIsUnfloatedFlowContainer = 1 << 1,
/// An anonymous box whose parent uses button layout, like the wrapper around a button's content.
IsAnonymousButtonContentWrapper = 1 << 2,
/// An anonymous box whose parent is an anonymous button content wrapper.
IsAnonymousButtonContentBox = 1 << 3,
/// An anonymous box whose nearest non-anonymous ancestor puts an ellipsis on overflowing lines.
InheritsTextOverflowEllipsis = 1 << 4,
ParentLaysOutInColumns = 1 << 5,
/// The parent lays out its children with a MathML layout algorithm.
ParentUsesMathLayout = 1 << 6,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
pub enum CompositorAnimationFrameKind {
Opacity = 1 << 0,
BackgroundColor = 1 << 1,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(u8)]
#[cfg_attr(not(test), expect(dead_code, reason = "C++ constructs the variants"))]
pub enum FfiNodeLink {
Parent,
FirstChild,
LastChild,
PreviousSibling,
NextSibling,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum DomPaintFact {
Inert = 1 << 0,
EditableOrEditingHost = 1 << 1,
InsideBlockingWheelEventHandler = 1 << 2,
NestedNavigableContainer = 1 << 3,
/// The node is an element HTML classifies as a devolvable widget.
/// https://drafts.csswg.org/css-ui/#devolvable
DevolvableWidget = 1 << 4,
}
#[cfg(test)]
#[derive(Clone, Copy)]
pub(crate) struct NodeConstructionFacts {
pub kind: NodeKind,
pub is_anonymous: bool,
pub is_html_input_element: bool,
pub is_html_html_element: bool,
pub is_document_element: bool,
pub is_in_user_agent_shadow_tree: bool,
pub uses_button_layout: bool,
pub is_editing_host: bool,
pub is_body: bool,
pub dom_paint_facts: u8,
/// The StyleNodeID of the element the row is bound to, or 0.
pub style_node: u32,
}
#[repr(C)]
#[derive(Clone)]
pub(crate) struct NodeData {
pub parent: ShapeCell<NodeSlotId>,
pub first_child: ShapeCell<NodeSlotId>,
pub last_child: ShapeCell<NodeSlotId>,
pub previous_sibling: ShapeCell<NodeSlotId>,
pub next_sibling: ShapeCell<NodeSlotId>,
pub kind: ShapeCell<NodeKind>,
pub generated_for: ShapeCell<u8>,
pub flags: ShapeCell<u32>,
/// Advanced on every layout invalidation that reaches this node or its
/// subtree, with no propagation boundary: unlike intrinsic size resets,
/// changes inside absolutely positioned and SVG descendants must reach
/// every ancestor, because their fragments live in ancestor run trees.
/// Wide enough that wrapping between a cache store and the next probe
/// is unreachable.
pub fragment_cache_epoch: u32,
pub slot_generation: ShapeCell<u8>,
pub compositor_animation_frame_kinds: ShapeCell<u8>,
pub table_column_span: u16,
pub table_row_span: u16,
pub dom_paint_facts: ShapeCell<u8>,
pub ancestor_facts: u8,
pub style: ShapeCell<Option<LayoutStyleRef>>,
}
impl Default for NodeData {
fn default() -> Self {
Self {
parent: ShapeCell::new(NodeSlotId::INVALID),
first_child: ShapeCell::new(NodeSlotId::INVALID),
last_child: ShapeCell::new(NodeSlotId::INVALID),
previous_sibling: ShapeCell::new(NodeSlotId::INVALID),
next_sibling: ShapeCell::new(NodeSlotId::INVALID),
kind: ShapeCell::new(NodeKind::Unset),
generated_for: ShapeCell::new(0),
flags: ShapeCell::new(0),
slot_generation: ShapeCell::new(0),
compositor_animation_frame_kinds: ShapeCell::new(0),
table_column_span: 1,
table_row_span: 1,
dom_paint_facts: ShapeCell::new(0),
ancestor_facts: 0,
fragment_cache_epoch: 0,
style: ShapeCell::new(None),
}
}
}
/// The facts of a node that no publication carries, for writing: writing them marks nothing for the paint side,
/// unlike a write to the node's shape.
pub(crate) struct UnpublishedNodeFactsMut<'a> {
pub(crate) fragment_cache_epoch: &'a mut u32,
pub(crate) table_column_span: &'a mut u16,
pub(crate) table_row_span: &'a mut u16,
pub(crate) ancestor_facts: &'a mut u8,
}
impl NodeData {
pub(super) fn unpublished_facts_mut(&mut self) -> UnpublishedNodeFactsMut<'_> {
UnpublishedNodeFactsMut {
fragment_cache_epoch: &mut self.fragment_cache_epoch,
table_column_span: &mut self.table_column_span,
table_row_span: &mut self.table_row_span,
ancestor_facts: &mut self.ancestor_facts,
}
}
}
/// The part of a node the paint side reads, as a row of the arena's published column.
#[derive(Clone, PartialEq)]
pub(crate) struct PaintNode {
pub(crate) generation: u8,
pub(crate) kind: NodeKind,
pub(crate) generated_for: u8,
pub(crate) dom_paint_facts: u8,
pub(crate) compositor_animation_frame_kinds: u8,
pub(crate) flags: u32,
pub(crate) parent: NodeSlotId,
pub(crate) first_child: NodeSlotId,
pub(crate) last_child: NodeSlotId,
pub(crate) previous_sibling: NodeSlotId,
pub(crate) next_sibling: NodeSlotId,
pub(crate) style: Option<LayoutStyleRef>,
/// The style record the row is built from, or 0.
pub(crate) style_record: u64,
/// The node whose style the row carries.
pub(crate) style_node: Option<crate::css::style::tree::StyleNodeID>,
/// Whether the arena derived the row's style record: an anonymous box's, or a style layout adjusted.
pub(crate) style_is_derived: bool,
}
impl Default for PaintNode {
fn default() -> Self {
Self {
generation: 0,
kind: NodeKind::Unset,
generated_for: 0,
dom_paint_facts: 0,
compositor_animation_frame_kinds: 0,
flags: 0,
parent: NodeSlotId::INVALID,
first_child: NodeSlotId::INVALID,
last_child: NodeSlotId::INVALID,
previous_sibling: NodeSlotId::INVALID,
next_sibling: NodeSlotId::INVALID,
style: None,
style_record: 0,
style_node: None,
style_is_derived: false,
}
}
}
impl PaintNode {
/// The node's row as `data` holds it, with the style record and node the arena keeps beside it, and whether it
/// derived the record.
pub(crate) fn of(
data: &NodeData,
style_record: u64,
style_node: Option<crate::css::style::tree::StyleNodeID>,
style_is_derived: bool,
) -> Self {
Self {
generation: data.slot_generation.get(),
kind: data.kind.get(),
generated_for: data.generated_for.get(),
dom_paint_facts: data.dom_paint_facts.get(),
compositor_animation_frame_kinds: data.compositor_animation_frame_kinds.get(),
flags: data.flags.get(),
parent: data.parent.get(),
first_child: data.first_child.get(),
last_child: data.last_child.get(),
previous_sibling: data.previous_sibling.get(),
next_sibling: data.next_sibling.get(),
style: data.style.get(),
style_record,
style_node,
style_is_derived,
}
}
/// The node's computed style, or `None` for a node without one.
pub(crate) fn style(&self) -> Option<crate::css::computed_value_views::ComputedValuesView<'_>> {
// SAFETY: No style record is released while a recording reads the rows published for it.
self.style.map(|style| unsafe { style.view() })
}
}
#[cfg(test)]
mod tests {
use crate::layout::node_data::{MAX_NODE_SLOT_COUNT, NodeData, NodeFlag, NodeKind, NodeSlotId};
#[test]
fn node_kind_has_a_stable_default_and_byte_width() {
assert_eq!(std::mem::size_of::<NodeKind>(), 1);
assert_eq!(NodeData::default().kind.get(), NodeKind::Unset);
}
#[test]
fn node_data_keeps_its_size_and_field_offsets() {
assert_eq!(std::mem::size_of::<NodeData>(), 48);
assert_eq!(std::mem::offset_of!(NodeData, flags), 24);
assert_eq!(std::mem::offset_of!(NodeData, fragment_cache_epoch), 28);
assert_eq!(std::mem::offset_of!(NodeData, slot_generation), 32);
assert_eq!(std::mem::offset_of!(NodeData, compositor_animation_frame_kinds), 33);
assert_eq!(std::mem::offset_of!(NodeData, table_column_span), 34);
assert_eq!(std::mem::offset_of!(NodeData, table_row_span), 36);
assert_eq!(std::mem::offset_of!(NodeData, dom_paint_facts), 38);
assert_eq!(std::mem::offset_of!(NodeData, ancestor_facts), 39);
assert_eq!(std::mem::offset_of!(NodeData, style), 40);
}
#[test]
fn node_slot_id_packs_a_24_bit_index_and_an_8_bit_generation() {
let id = NodeSlotId::new(MAX_NODE_SLOT_COUNT - 1, u8::MAX);
assert_eq!(id.slot_index(), MAX_NODE_SLOT_COUNT - 1);
assert_eq!(id.generation(), u8::MAX);
assert_ne!(id, NodeSlotId::INVALID);
}
#[test]
fn list_marker_position_uses_expected_flag_bit() {
assert_eq!(NodeFlag::ListMarkerIsInside as u32, 1 << 23);
}
#[test]
fn committed_fragment_link_flag_uses_a_previously_unassigned_bit() {
assert_eq!(NodeFlag::HasCommittedFragmentLink as u32, 1 << 26);
assert_eq!(NodeFlag::HasPreserve3dTransformStyle as u32, 1 << 27);
}
#[test]
fn stamped_fact_flags_use_previously_unassigned_bits() {
assert_eq!(NodeFlag::IsHtmlInputElement as u32, 1 << 13);
assert_eq!(NodeFlag::IsHtmlHtmlElement as u32, 1 << 14);
assert_eq!(NodeFlag::IsInUserAgentShadowTree as u32, 1 << 15);
assert_eq!(NodeFlag::UsesButtonLayout as u32, 1 << 16);
assert_eq!(NodeFlag::IsEditingHost as u32, 1 << 17);
assert_eq!(NodeFlag::ReplacedBoxCanHaveChildren as u32, 1 << 18);
assert_eq!(NodeFlag::ProducesLineBoxFragmentWhenEmpty as u32, 1 << 22);
assert_eq!(NodeFlag::IsDocumentElement as u32, 1 << 30);
}
}