Skip to content

Truly independent web browser

LibWeb: Generate hit-test items from any PaintRead

Aliaksandr Kalenik8523725faa90
diff --git a/Libraries/LibWeb/Rust/src/painting/border_radii.rs b/Libraries/LibWeb/Rust/src/painting/border_radii.rs
index 391c59691e1..0de30d51217 100644
--- a/Libraries/LibWeb/Rust/src/painting/border_radii.rs
+++ b/Libraries/LibWeb/Rust/src/painting/border_radii.rs
@@ -7,7 +7,10 @@
use crate::css::computed_value_views::LengthPercentageRef;
use crate::css::css_pixels::{CssPixelFraction, CssPixels};
use crate::css::css_pixels::{CssPixelPoint, CssPixelRect};
+use crate::layout::node_data::NodeSlotId;
use crate::painting::display_list::device_pixels::DevicePixelConverter;
+use crate::painting::paint_read::PaintRead;
+use crate::painting::paintable_geometry;
use libgfx_rust::{CornerRadii, CornerRadius};
pub(crate) fn normalize_border_radii_data(
@@ -177,3 +180,60 @@ impl BorderRadii {
true
}
}
+
+pub(crate) fn border_radii(arena: &impl PaintRead, paintable: NodeSlotId) -> BorderRadii {
+ let Some(style) = arena.node_style_if_live(paintable) else {
+ return BorderRadii::default();
+ };
+ crate::painting::visual_context::node_values::border_radii_data(style, arena, paintable)
+}
+
+pub(crate) fn piece_border_radii(
+ arena: &impl PaintRead,
+ paintable: NodeSlotId,
+ piece: &crate::painting::paintable_data::InlineBoxPieceRecord,
+) -> BorderRadii {
+ let Some(style) = arena.node_style_if_live(paintable) else {
+ return BorderRadii::default();
+ };
+ crate::painting::visual_context::node_values::piece_border_radii_data(
+ style,
+ piece.border_box_rect.width,
+ piece.border_box_rect.height,
+ piece.present_edges,
+ )
+}
+
+pub(crate) fn fragment_border_box_and_radii(
+ arena: &impl PaintRead,
+ paintable: NodeSlotId,
+ index: usize,
+) -> (CssPixelRect, BorderRadii) {
+ let rect = paintable_geometry::absolute_fragment_border_box_rect(arena, paintable, index);
+ let Some(style) = arena.node_style_if_live(paintable) else {
+ return (rect, BorderRadii::default());
+ };
+ let radii = crate::painting::visual_context::node_values::piece_border_radii_data(
+ style,
+ rect.width,
+ rect.height,
+ paintable_geometry::committed_fragment_present_edges(arena, paintable, index),
+ );
+ (rect, radii)
+}
+
+pub(crate) fn border_boxes_and_radii_of_fragments(
+ arena: &impl PaintRead,
+ paintable: NodeSlotId,
+) -> smallvec::SmallVec<[(CssPixelRect, BorderRadii); 1]> {
+ let fragment_count = paintable_geometry::fragment_count(arena, paintable);
+ if fragment_count > 1 && !crate::painting::node_painting::is_fragmented_inline(arena, paintable) {
+ return (0..fragment_count)
+ .map(|index| fragment_border_box_and_radii(arena, paintable, index))
+ .collect();
+ }
+ smallvec::smallvec![(
+ paintable_geometry::absolute_border_box_rect(arena, paintable),
+ border_radii(arena, paintable)
+ )]
+}
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
new file mode 100644
index 00000000000..74bc8fc2df1
--- /dev/null
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/items.rs
@@ -0,0 +1,644 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+//! The hit-test items of one box in one paint phase, in the order they stack, read from rows.
+
+use super::*;
+use crate::css::computed_value_types::SVG_PAINT_NONE;
+use crate::css::css_enums;
+use crate::css::css_pixels::{CssPixelRect, CssPixels};
+use crate::layout::node_data::{DomPaintFact, NodeKind, NodeSlotId};
+use crate::layout::node_facts::{self, NodeShape};
+use crate::painting::display_list::commands::ContextRef;
+use crate::painting::fragment_ownership;
+use crate::painting::node_painting;
+use crate::painting::paint_read::{PaintRead, PaintRow};
+use crate::painting::paintable_data::CommittedSideData;
+use crate::painting::paintable_data::*;
+use crate::painting::paintable_geometry;
+use crate::painting::record::PaintPhase;
+use crate::painting::style_queries;
+use crate::painting::text_fragment;
+use libgfx_rust::WindingRule;
+
+/// A fragment of a row's inline content, read through whatever holds the row's committed side
+/// data.
+struct CommittedFragment<SideData> {
+ side_data: SideData,
+ index: usize,
+}
+
+impl<SideData: std::ops::Deref<Target = CommittedSideData>> std::ops::Deref for CommittedFragment<SideData> {
+ type Target = FragmentRecord;
+
+ fn deref(&self) -> &FragmentRecord {
+ &self.side_data.fragments()[self.index]
+ }
+}
+
+#[derive(Clone, Copy, Debug, Default)]
+pub(crate) struct HitTestFacts {
+ pub(crate) visible_for_hit_testing: bool,
+ pub(crate) has_resizer: bool,
+ pub(crate) could_be_scrolled_horizontally: bool,
+ pub(crate) could_be_scrolled_vertically: bool,
+ pub(crate) svg_hit_areas: u8,
+ pub(crate) svg_path_winding_rule: WindingRule,
+}
+
+// https://svgwg.org/svg2-draft/interact.html#PointerEventsProperty
+fn svg_hit_areas(pointer_events: u8, visibility_is_visible: bool, fill_is_none: bool, stroke_is_none: bool) -> u8 {
+ use css_enums::pointer_events;
+ let fill_and_stroke = svg_hit_area::FILL | svg_hit_area::STROKE;
+ // The pointer is over a painted area if it is over the interior (i.e., fill) of the element and the fill property
+ // has an actual value other than none or it is over the perimeter (i.e., stroke) of the element and the stroke
+ // property has an actual value other than none.
+ let painted_areas =
+ (if fill_is_none { 0 } else { svg_hit_area::FILL }) | (if stroke_is_none { 0 } else { svg_hit_area::STROKE });
+ let if_visible = |areas: u8| if visibility_is_visible { areas } else { 0 };
+ match pointer_events {
+ // auto: The default behavior, which is the same as for visiblePainted.
+ // visiblePainted: The given element can be the target element for pointer events when the visibility property
+ // is set to visible and when the pointer is over a "painted" area.
+ pointer_events::AUTO | pointer_events::VISIBLEPAINTED => if_visible(painted_areas),
+ // bounding-box: The given element can be a target element for pointer events when the pointer is over the
+ // bounding box of the element.
+ pointer_events::BOUNDING_BOX => svg_hit_area::BOUNDING_BOX,
+ // visibleFill: The given element can be the target element for pointer events when the visibility property is
+ // set to visible and when the pointer is over the interior (i.e., fill) of the element.
+ pointer_events::VISIBLEFILL => if_visible(svg_hit_area::FILL),
+ // visibleStroke: The given element can be the target element for pointer events when the visibility property
+ // is set to visible and when the pointer is over the perimeter (i.e., stroke) of the element.
+ pointer_events::VISIBLESTROKE => if_visible(svg_hit_area::STROKE),
+ // visible: The given element can be the target element for pointer events when the visibility property is set
+ // to visible and the pointer is over either the interior (i.e., fill) or the perimeter (i.e.,
+ // stroke) of the element.
+ pointer_events::VISIBLE => if_visible(fill_and_stroke),
+ // painted: The given element can be the target element for pointer events when the pointer is over a "painted"
+ // area.
+ pointer_events::PAINTED => painted_areas,
+ // fill: The given element can be the target element for pointer events when the pointer is over the interior
+ // (i.e., fill) of the element.
+ pointer_events::FILL => svg_hit_area::FILL,
+ // stroke: The given element can be the target element for pointer events when the pointer is over the
+ // perimeter (i.e., stroke) of the element.
+ pointer_events::STROKE => svg_hit_area::STROKE,
+ // all: The given element can be the target element for pointer events whenever the pointer is over either the
+ // interior (i.e., fill) or the perimeter (i.e., stroke) of the element.
+ pointer_events::ALL => fill_and_stroke,
+ // none: The given element does not receive pointer events.
+ _ => 0,
+ }
+}
+
+pub(crate) fn hit_test_facts(arena: &impl PaintRead, paintable: NodeSlotId) -> HitTestFacts {
+ let Some(row) = arena.node(paintable) else {
+ return HitTestFacts::default();
+ };
+ let Some(style) = row.style() else {
+ return HitTestFacts::default();
+ };
+ let wheel_axes = crate::painting::chrome_geometry::wheel_scrollable_axes(arena, paintable);
+ let svg_path = node_painting::is_svg_path(row.kind());
+ let svg = style.inherited_svg();
+ HitTestFacts {
+ visible_for_hit_testing: arena.paintable_row_is_populated(paintable)
+ && row.dom_paint_facts() & DomPaintFact::Inert as u8 == 0
+ && style.pointer_events() != css_enums::pointer_events::NONE,
+ has_resizer: crate::painting::chrome_geometry::has_resizer(arena, paintable),
+ could_be_scrolled_horizontally: wheel_axes.horizontal,
+ could_be_scrolled_vertically: wheel_axes.vertical,
+ svg_hit_areas: if svg_path {
+ let areas = svg_hit_areas(
+ style.pointer_events(),
+ style.visibility() == css_enums::visibility::VISIBLE,
+ svg.fill.kind == SVG_PAINT_NONE,
+ svg.stroke.kind == SVG_PAINT_NONE,
+ );
+ // For text elements, hit-testing is performed on a character cell basis, so the text receives events
+ // anywhere within a character cell whenever its fill or its stroke would.
+ if matches!(row.kind(), NodeKind::SVGTextBox | NodeKind::SVGTextPathBox)
+ && areas & (svg_hit_area::FILL | svg_hit_area::STROKE) != 0
+ {
+ svg_hit_area::FILL
+ } else {
+ areas
+ }
+ } else if row.kind() == NodeKind::SVGImageBox {
+ // For raster images, hit-testing is performed on a whole-image basis (i.e., the rectangular area for the
+ // image is the determinant for whether the image receives the event).
+ // NB: The rectangular area for the image is also its bounding box.
+ if svg_hit_areas(
+ style.pointer_events(),
+ style.visibility() == css_enums::visibility::VISIBLE,
+ false,
+ false,
+ ) != 0
+ {
+ svg_hit_area::BOUNDING_BOX
+ } else {
+ 0
+ }
+ } else if row.kind() == NodeKind::SVGGraphicsBox
+ && style.pointer_events() == css_enums::pointer_events::BOUNDING_BOX
+ {
+ svg_hit_area::BOUNDING_BOX
+ } else {
+ 0
+ },
+ svg_path_winding_rule: if svg_path && svg.fill_rule == css_enums::fill_rule::EVENODD {
+ WindingRule::EvenOdd
+ } else {
+ WindingRule::Nonzero
+ },
+ }
+}
+
+/// Where the items of a box go, with what generating them asks of whoever wants them.
+pub(crate) trait HitTestItemSink {
+ /// The facts of a row, which a sink that visits a row in several phases may keep.
+ fn hit_test_facts(&mut self, paintable: NodeSlotId) -> HitTestFacts;
+
+ /// The scale from the space of the paintable's own box to the screen, which a stroke that does not scale is
+ /// measured against.
+ fn own_accumulated_2d_scale(&self, paintable: NodeSlotId) -> libgfx_rust::FloatSize;
+
+ /// Whether an item of this kind over this rect is worth building. The item of a text fragment resolves a font
+ /// and measures the fragment, which a sink that only wants what lies under a point can save.
+ fn wants(&mut self, _kind: HitTestItemKind, _rect: CssPixelRect, _context: ContextRef) -> bool {
+ true
+ }
+
+ fn append(&mut self, item: HitTestItem);
+}
+
+pub(crate) struct HitTestItems<'a, R: PaintRead, S: HitTestItemSink> {
+ rows: &'a R,
+ sink: &'a mut S,
+}
+
+/// Hands `sink` the items `paintable` has in `phase`.
+pub(crate) fn hit_test_items_of_phase<R: PaintRead, S: HitTestItemSink>(
+ rows: &R,
+ sink: &mut S,
+ paintable: NodeSlotId,
+ phase: PaintPhase,
+) {
+ HitTestItems { rows, sink }.of_phase(paintable, phase);
+}
+
+impl<'a, R: PaintRead, S: HitTestItemSink> HitTestItems<'a, R, S> {
+ fn visibility_is_visible(&self, paintable: NodeSlotId) -> bool {
+ self.rows
+ .node_style_if_live(paintable)
+ .is_none_or(|style| style.visibility() == css_enums::visibility::VISIBLE)
+ }
+
+ fn is_anonymous(&self, paintable: NodeSlotId) -> bool {
+ style_queries::is_anonymous(self.rows, paintable)
+ }
+
+ fn is_generated_for_pseudo_element(&self, paintable: NodeSlotId) -> bool {
+ self.rows.node_is_generated_for_pseudo_element(paintable)
+ }
+
+ fn of_phase(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
+ if node_painting::is_fragmented_inline(self.rows, paintable) {
+ self.inline_items(paintable, phase);
+ } else if node_painting::has_lines(self.rows, paintable) {
+ self.base_items(paintable, phase);
+ self.lines_items(paintable, phase);
+ } else if self
+ .rows
+ .node_kind_if_live(paintable)
+ .is_some_and(node_painting::is_svg_path)
+ {
+ self.svg_path_items(paintable, phase);
+ } else if self
+ .rows
+ .node_kind_if_live(paintable)
+ .is_some_and(|kind| matches!(kind, NodeKind::SVGImageBox | NodeKind::SVGGraphicsBox))
+ {
+ self.svg_box_items(paintable, phase);
+ } else {
+ self.base_items(paintable, phase);
+ }
+ }
+
+ fn base_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
+ if phase != PaintPhase::Background && phase != PaintPhase::Overlay {
+ return;
+ }
+ if !self.visibility_is_visible(paintable) || !self.sink.hit_test_facts(paintable).visible_for_hit_testing {
+ return;
+ }
+ if phase == PaintPhase::Background {
+ if self.is_anonymous(paintable) && !self.is_generated_for_pseudo_element(paintable) {
+ return;
+ }
+ // Column and column group boxes only exist to paint their backgrounds under the cells of their columns;
+ // they are not hit-tested, as in other engines.
+ let display = style_queries::display(self.rows, paintable);
+ if display.is_table_column() || display.is_table_column_group() {
+ return;
+ }
+ let context = self.rows.paintable_data(paintable).accumulated_visual_context;
+ for (rect, radii) in
+ crate::painting::border_radii::border_boxes_and_radii_of_fragments(self.rows, paintable)
+ {
+ self.append_box(paintable, rect, context, radii);
+ }
+ return;
+ }
+ let facts = self.sink.hit_test_facts(paintable);
+ let context = self.rows.paintable_data(paintable).accumulated_visual_context;
+ if facts.has_resizer {
+ self.append_chrome_widget(paintable, CHROME_WIDGET_RESIZE_HANDLE, context);
+ }
+ if facts.could_be_scrolled_horizontally {
+ self.append_chrome_widget(paintable, CHROME_WIDGET_HORIZONTAL_SCROLLBAR, context);
+ }
+ if facts.could_be_scrolled_vertically {
+ self.append_chrome_widget(paintable, CHROME_WIDGET_VERTICAL_SCROLLBAR, context);
+ }
+ }
+
+ fn lines_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
+ if phase != PaintPhase::Foreground {
+ return;
+ }
+ let fragment_count = self.rows.committed_side_data(paintable).fragments().len();
+ if fragment_count == 0 && crate::painting::paint_order::first_paint_child(self.rows, paintable).is_none() {
+ if style_queries::is_visible(self.rows, paintable)
+ && self.sink.hit_test_facts(paintable).visible_for_hit_testing
+ {
+ self.empty_editable_item(paintable);
+ }
+ return;
+ }
+ let context = self
+ .rows
+ .paintable_data(paintable)
+ .accumulated_visual_context_for_descendants;
+ // Fragments inside self-painting inline boxes are recorded during that box's paint.
+ let filter = fragment_ownership::effective_filter(self.rows, paintable);
+ filter.for_each_owned_fragment_index(fragment_count, |index| {
+ if self.fragment_is_block_level_box(paintable, index) {
+ return;
+ }
+ self.append_text_fragment(paintable, index as u32, context);
+ });
+ }
+
+ fn inline_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
+ if self.is_anonymous(paintable) && !self.is_generated_for_pseudo_element(paintable) {
+ return;
+ }
+ // Only this box's own items are gated on its visibility; the fragments it owns may belong
+ // to descendants that override visibility or pointer-events, so they are filtered per
+ // fragment instead.
+ let box_itself_is_hit_testable = style_queries::is_visible(self.rows, paintable)
+ && self.sink.hit_test_facts(paintable).visible_for_hit_testing;
+
+ if phase == PaintPhase::Background {
+ if box_itself_is_hit_testable {
+ self.append_piece_boxes(paintable);
+ }
+ return;
+ }
+ if phase != PaintPhase::Foreground {
+ return;
+ }
+
+ if fragment_ownership::is_self_painting_inline(self.rows, paintable) {
+ let Some(root) = self.inline_root(paintable) else {
+ return;
+ };
+ let context = self
+ .rows
+ .paintable_data(paintable)
+ .accumulated_visual_context_for_descendants;
+ let fragment_count = self.rows.committed_side_data(root).fragments().len();
+ let filter = fragment_ownership::effective_filter(self.rows, paintable);
+ // Hit-test precedence follows paint order: this box's own text loses to the box itself
+ // (re-recorded so its z-order matches this box's paint order), while nested content
+ // (e.g. a link inside this box) wins over it.
+ let own_layout_node = paintable;
+ let mut nested_content_indices = Vec::new();
+ filter.for_each_owned_fragment_index(fragment_count, |index| {
+ if self.fragment_is_block_level_box(root, index) {
+ return;
+ }
+ let fragment_node = self.rows.committed_side_data(root).fragments()[index].layout_node;
+ if fragment_ownership::nearest_fragmented_inline_ancestor(self.rows, fragment_node)
+ == Some(own_layout_node)
+ {
+ self.append_text_fragment(root, index as u32, context);
+ } else {
+ nested_content_indices.push(index);
+ }
+ });
+ if box_itself_is_hit_testable && self.rows.paintable_data(paintable).establishes_stacking_context {
+ self.append_piece_boxes(paintable);
+ }
+ for index in nested_content_indices {
+ self.append_text_fragment(root, index as u32, context);
+ }
+ }
+
+ // Clicks must be able to place the caret in an editable element with no content, so its
+ // target is recorded in foreground order, winning over earlier-painted overlapping content.
+ if box_itself_is_hit_testable && !self.inline_has_content(paintable) {
+ self.empty_editable_item(paintable);
+ }
+ }
+
+ fn inline_root(&self, paintable: NodeSlotId) -> Option<NodeSlotId> {
+ let block = self.rows.paintable_data(paintable).containing_block;
+ if block.is_invalid() || !self.rows.paintable_row_is_populated(block) {
+ return None;
+ }
+ node_painting::has_lines(self.rows, block).then_some(block)
+ }
+
+ fn piece_of(&self, root: NodeSlotId, piece_index: u32) -> InlineBoxPieceRecord {
+ self.rows.committed_side_data(root).inline_box_pieces()[piece_index as usize]
+ }
+
+ fn inline_has_content(&self, paintable: NodeSlotId) -> bool {
+ let has_content_pieces = self.inline_root(paintable).is_some_and(|root| {
+ self.rows
+ .committed_side_data(root)
+ .inline_box_piece_indices(paintable)
+ .iter()
+ .any(|piece_index| !self.piece_of(root, *piece_index).is_geometry_only_placeholder)
+ });
+ has_content_pieces || crate::painting::paint_order::first_paint_child(self.rows, paintable).is_some()
+ }
+
+ fn append_piece_boxes(&mut self, paintable: NodeSlotId) {
+ let Some(root) = self.inline_root(paintable) else {
+ return;
+ };
+ let root_positions = paintable_geometry::LineRootPositions::new(self.rows, root);
+ let layout_arena = self.rows;
+ let context = self.rows.paintable_data(paintable).accumulated_visual_context;
+ let side = layout_arena.committed_side_data(root);
+ for piece_index in side.inline_box_piece_indices(paintable) {
+ let piece = &side.inline_box_pieces()[*piece_index as usize];
+ if piece.is_geometry_only_placeholder {
+ continue;
+ }
+ let root_position = root_positions.of_line(side.lines(), piece.line_index as usize);
+ let rect = CssPixelRect::from(piece.border_box_rect).translated_by(root_position);
+ let radii = crate::painting::border_radii::piece_border_radii(self.rows, paintable, piece);
+ self.append_box(paintable, rect, context, radii);
+ }
+ }
+
+ fn svg_path_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
+ if phase != PaintPhase::Foreground {
+ return;
+ }
+ let Some(path) = paintable_geometry::committed_svg_path(self.rows, paintable) else {
+ return;
+ };
+ if !self.sink.hit_test_facts(paintable).visible_for_hit_testing {
+ return;
+ }
+ let facts = self.sink.hit_test_facts(paintable);
+ if facts.svg_hit_areas == 0 {
+ return;
+ }
+ if facts.svg_hit_areas & svg_hit_area::BOUNDING_BOX != 0 {
+ self.append_svg_bounding_box(paintable);
+ return;
+ }
+ let stroke = if facts.svg_hit_areas & svg_hit_area::STROKE != 0 {
+ self.svg_stroke_hit_region(paintable)
+ } else {
+ None
+ };
+ if facts.svg_hit_areas & !svg_hit_area::STROKE == 0 && stroke.is_none() {
+ return;
+ }
+ let [mut x, mut y, mut width, mut height] = path.bounding_box();
+ if let Some(stroke) = &stroke {
+ let outset = stroke.outset();
+ x -= outset;
+ y -= outset;
+ width += outset * 2.0;
+ height += outset * 2.0;
+ }
+ // The item rect rounds outward, past the far edges, so fixed-point quantization can never reject a valid hit
+ // and points on those edges stay inside it.
+ let x1 = x.floor() as i32;
+ let y1 = y.floor() as i32;
+ let x2 = (x + width).floor() as i32 + 1;
+ let y2 = (y + height).floor() as i32 + 1;
+ let bounding_box = CssPixelRect::new(
+ CssPixels::from_integer(i64::from(x1)),
+ CssPixels::from_integer(i64::from(y1)),
+ CssPixels::from_integer(i64::from(x2 - x1)),
+ CssPixels::from_integer(i64::from(y2 - y1)),
+ );
+ if bounding_box.is_empty() {
+ return;
+ }
+ let context = self.rows.paintable_data(paintable).accumulated_visual_context;
+ let item = HitTestItem {
+ rect: bounding_box,
+ path: Some(path),
+ winding_rule: facts.svg_path_winding_rule,
+ svg_hit_areas: facts.svg_hit_areas,
+ svg_stroke: stroke,
+ ..self.base_hit_test_item(HitTestItemKind::SvgPath, paintable, context)
+ };
+ self.sink.append(item);
+ }
+
+ fn svg_box_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
+ if phase != PaintPhase::Foreground || !self.sink.hit_test_facts(paintable).visible_for_hit_testing {
+ return;
+ }
+ if self.sink.hit_test_facts(paintable).svg_hit_areas & svg_hit_area::BOUNDING_BOX != 0 {
+ self.append_svg_bounding_box(paintable);
+ }
+ }
+
+ // NB: The rect of an SVG box is its bounding box, which takes in the boxes of its descendants, like the tspans of a
+ // text element. A point on the far edges of the bounding box is over it too, so the item rect reaches one
+ // fixed-point unit past them.
+ fn append_svg_bounding_box(&mut self, paintable: NodeSlotId) {
+ let one_unit = CssPixels::from_raw(1);
+ let rect = paintable_geometry::absolute_rect(self.rows, paintable).inflated(
+ CssPixels::default(),
+ one_unit,
+ one_unit,
+ CssPixels::default(),
+ );
+ let context = self.rows.paintable_data(paintable).accumulated_visual_context;
+ let item = HitTestItem {
+ rect,
+ ..self.base_hit_test_item(HitTestItemKind::Box, paintable, context)
+ };
+ self.sink.append(item);
+ }
+
+ fn svg_stroke_hit_region(&self, paintable: NodeSlotId) -> Option<std::sync::Arc<SvgStrokeHitRegion>> {
+ let style = self.rows.node_style_if_live(paintable)?;
+ let stroke = crate::painting::record::paint::svg::svg_stroke_style(self.rows, paintable, &style);
+ let mut thickness = stroke.width;
+ let mut dash_array = stroke.dash_array;
+ let mut dash_offset = stroke.dash_offset;
+ // https://svgwg.org/svg2-draft/painting.html#PaintingVectorEffects
+ // With the non-scaling-stroke vector effect, stroke outline shall be calculated in the "host" coordinate
+ // space instead of user coordinate system.
+ if stroke.non_scaling_stroke {
+ let accumulated_scale = self.sink.own_accumulated_2d_scale(paintable);
+ let accumulated_scale_area = accumulated_scale.width * accumulated_scale.height;
+ if accumulated_scale_area > 0.0 {
+ let scale = accumulated_scale_area.sqrt();
+ thickness /= scale;
+ dash_array.iter_mut().for_each(|value| *value /= scale);
+ dash_offset /= scale;
+ }
+ }
+ if thickness.is_nan() || thickness <= 0.0 {
+ return None;
+ }
+ Some(std::sync::Arc::new(SvgStrokeHitRegion::new(
+ libgfx_rust::path::StrokeStyle {
+ thickness,
+ cap_style: stroke.cap_style,
+ join_style: stroke.join_style,
+ miter_limit: stroke.miter_limit,
+ dash_array,
+ dash_offset,
+ },
+ )))
+ }
+
+ fn empty_editable_item(&mut self, paintable: NodeSlotId) {
+ if self.is_anonymous(paintable)
+ || !self
+ .rows
+ .node_has_dom_paint_fact(paintable, DomPaintFact::EditableOrEditingHost)
+ {
+ return;
+ }
+ let context = self.rows.paintable_data(paintable).accumulated_visual_context;
+ for rect in paintable_geometry::absolute_border_box_rects_of_fragments(self.rows, paintable) {
+ self.append_empty_editable(paintable, rect, context);
+ }
+ }
+
+ fn fragment(
+ &self,
+ owner: NodeSlotId,
+ index: usize,
+ ) -> CommittedFragment<impl std::ops::Deref<Target = CommittedSideData> + 'a> {
+ CommittedFragment {
+ side_data: self.rows.committed_side_data(owner),
+ index,
+ }
+ }
+
+ fn fragment_is_block_level_box(&self, owner: NodeSlotId, index: usize) -> bool {
+ text_fragment::is_block_level_box(self.rows, &self.fragment(owner, index))
+ }
+
+ // A text node has no style of its own, so its parent both decides whether the fragment can be hit and is what an
+ // event dispatched from it resolves against. Returns that node, or None when the fragment is not hit-testable.
+ fn hit_node_for_text_fragment(&mut self, fragment: &FragmentRecord) -> Option<NodeSlotId> {
+ let node = fragment.layout_node;
+ let row = self.rows.node(node)?;
+ if fragment.is_block_ellipsis || !node_facts::kind_is_text(row.kind()) {
+ return None;
+ }
+ let parent = row.parent();
+ let parent_style = self.rows.node_style_if_live(parent)?;
+ if parent_style.visibility() != css_enums::visibility::VISIBLE
+ || parent_style.effects().opacity == 0.0
+ || parent_style.pointer_events() == css_enums::pointer_events::NONE
+ || row.dom_paint_facts() & DomPaintFact::Inert as u8 != 0
+ {
+ return None;
+ }
+ // Resolving the hit needs a committed paintable row; without one there is nothing to resolve against.
+ if !self.rows.paintable_row_is_populated(parent) {
+ return None;
+ }
+ Some(parent)
+ }
+
+ fn append_box(
+ &mut self,
+ paintable_box: NodeSlotId,
+ rect: CssPixelRect,
+ context: ContextRef,
+ border_radii: BorderRadii,
+ ) {
+ let item = HitTestItem {
+ rect,
+ border_radii,
+ ..self.base_hit_test_item(HitTestItemKind::Box, paintable_box, context)
+ };
+ self.sink.append(item);
+ }
+
+ fn base_hit_test_item(&self, kind: HitTestItemKind, target: NodeSlotId, context: ContextRef) -> HitTestItem {
+ HitTestItem {
+ kind,
+ paintable: target,
+ hit_node: target,
+ chrome_widget_kind: CHROME_WIDGET_NONE,
+ text_fragment_index: None,
+ rect: CssPixelRect::default(),
+ context,
+ border_radii: BorderRadii::default(),
+ path: None,
+ winding_rule: WindingRule::Nonzero,
+ svg_hit_areas: 0,
+ svg_stroke: None,
+ }
+ }
+
+ fn append_text_fragment(&mut self, owner: NodeSlotId, fragment_index: u32, context: ContextRef) {
+ let fragment = self.fragment(owner, fragment_index as usize);
+ let layout_arena = self.rows;
+ let rect = text_fragment::absolute_rect(layout_arena, &fragment);
+ if !self.sink.wants(HitTestItemKind::TextFragment, rect, context) {
+ return;
+ }
+ let Some(hit_node) = self.hit_node_for_text_fragment(&fragment) else {
+ return;
+ };
+ let item = HitTestItem {
+ text_fragment_index: Some(fragment_index),
+ rect,
+ hit_node,
+ ..self.base_hit_test_item(HitTestItemKind::TextFragment, owner, context)
+ };
+ self.sink.append(item);
+ }
+
+ fn append_empty_editable(&mut self, paintable: NodeSlotId, rect: CssPixelRect, context: ContextRef) {
+ let item = HitTestItem {
+ rect,
+ ..self.base_hit_test_item(HitTestItemKind::EmptyEditable, paintable, context)
+ };
+ self.sink.append(item);
+ }
+
+ fn append_chrome_widget(&mut self, paintable: NodeSlotId, chrome_widget_kind: u8, context: ContextRef) {
+ let item = HitTestItem {
+ chrome_widget_kind,
+ ..self.base_hit_test_item(HitTestItemKind::ChromeWidget, paintable, context)
+ };
+ self.sink.append(item);
+ }
+}
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
index 3525f6c8bc8..adec176bbef 100644
--- a/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/mod.rs
@@ -5,6 +5,7 @@
*/
pub mod caret;
+pub(crate) mod items;
pub mod query;
pub mod resolve;
diff --git a/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs b/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs
index 263a07ef625..5458a728091 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs
@@ -4,585 +4,30 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
-use crate::painting::paint_read::{GeometryRead, PaintRead, PaintRow};
-use crate::painting::record::trace::Observer;
-
use super::{PaintPhase, PaintRecorder};
-use crate::css::computed_value_types::SVG_PAINT_NONE;
-use crate::css::css_enums;
-use crate::css::css_pixels::{CssPixelRect, CssPixels};
-use crate::layout::node_data::{DomPaintFact, NodeKind, NodeSlotId};
-use crate::layout::node_facts::{self, NodeShape};
-use crate::painting::display_list::commands::ContextRef;
-use crate::painting::fragment_ownership;
-use crate::painting::hit_test::*;
-use crate::painting::node_painting;
-use crate::painting::paintable_data::CommittedSideData;
-use crate::painting::paintable_data::*;
-use crate::painting::paintable_geometry;
-use crate::painting::text_fragment;
-use libgfx_rust::WindingRule;
-
-/// A fragment of a row's inline content, read through whatever holds the row's committed side
-/// data.
-struct CommittedFragment<SideData> {
- side_data: SideData,
- index: usize,
-}
-
-impl<SideData: std::ops::Deref<Target = CommittedSideData>> std::ops::Deref for CommittedFragment<SideData> {
- type Target = FragmentRecord;
+use crate::layout::node_data::NodeSlotId;
+use crate::painting::hit_test::HitTestItem;
+pub(crate) use crate::painting::hit_test::items::{HitTestFacts, hit_test_facts};
+use crate::painting::hit_test::items::{HitTestItemSink, hit_test_items_of_phase};
+use crate::painting::record::trace::Observer;
- fn deref(&self) -> &FragmentRecord {
- &self.side_data.fragments()[self.index]
+impl<O: Observer> HitTestItemSink for PaintRecorder<'_, O> {
+ fn hit_test_facts(&mut self, paintable: NodeSlotId) -> HitTestFacts {
+ self.paintable_facts(paintable)
}
-}
-
-#[derive(Clone, Copy, Debug, Default)]
-pub(crate) struct HitTestFacts {
- pub(crate) visible_for_hit_testing: bool,
- pub(crate) has_resizer: bool,
- pub(crate) could_be_scrolled_horizontally: bool,
- pub(crate) could_be_scrolled_vertically: bool,
- pub(crate) svg_hit_areas: u8,
- pub(crate) svg_path_winding_rule: WindingRule,
-}
-// https://svgwg.org/svg2-draft/interact.html#PointerEventsProperty
-fn svg_hit_areas(pointer_events: u8, visibility_is_visible: bool, fill_is_none: bool, stroke_is_none: bool) -> u8 {
- use css_enums::pointer_events;
- let fill_and_stroke = svg_hit_area::FILL | svg_hit_area::STROKE;
- // The pointer is over a painted area if it is over the interior (i.e., fill) of the element and the fill property
- // has an actual value other than none or it is over the perimeter (i.e., stroke) of the element and the stroke
- // property has an actual value other than none.
- let painted_areas =
- (if fill_is_none { 0 } else { svg_hit_area::FILL }) | (if stroke_is_none { 0 } else { svg_hit_area::STROKE });
- let if_visible = |areas: u8| if visibility_is_visible { areas } else { 0 };
- match pointer_events {
- // auto: The default behavior, which is the same as for visiblePainted.
- // visiblePainted: The given element can be the target element for pointer events when the visibility property
- // is set to visible and when the pointer is over a "painted" area.
- pointer_events::AUTO | pointer_events::VISIBLEPAINTED => if_visible(painted_areas),
- // bounding-box: The given element can be a target element for pointer events when the pointer is over the
- // bounding box of the element.
- pointer_events::BOUNDING_BOX => svg_hit_area::BOUNDING_BOX,
- // visibleFill: The given element can be the target element for pointer events when the visibility property is
- // set to visible and when the pointer is over the interior (i.e., fill) of the element.
- pointer_events::VISIBLEFILL => if_visible(svg_hit_area::FILL),
- // visibleStroke: The given element can be the target element for pointer events when the visibility property
- // is set to visible and when the pointer is over the perimeter (i.e., stroke) of the element.
- pointer_events::VISIBLESTROKE => if_visible(svg_hit_area::STROKE),
- // visible: The given element can be the target element for pointer events when the visibility property is set
- // to visible and the pointer is over either the interior (i.e., fill) or the perimeter (i.e.,
- // stroke) of the element.
- pointer_events::VISIBLE => if_visible(fill_and_stroke),
- // painted: The given element can be the target element for pointer events when the pointer is over a "painted"
- // area.
- pointer_events::PAINTED => painted_areas,
- // fill: The given element can be the target element for pointer events when the pointer is over the interior
- // (i.e., fill) of the element.
- pointer_events::FILL => svg_hit_area::FILL,
- // stroke: The given element can be the target element for pointer events when the pointer is over the
- // perimeter (i.e., stroke) of the element.
- pointer_events::STROKE => svg_hit_area::STROKE,
- // all: The given element can be the target element for pointer events whenever the pointer is over either the
- // interior (i.e., fill) or the perimeter (i.e., stroke) of the element.
- pointer_events::ALL => fill_and_stroke,
- // none: The given element does not receive pointer events.
- _ => 0,
+ fn own_accumulated_2d_scale(&self, paintable: NodeSlotId) -> libgfx_rust::FloatSize {
+ PaintRecorder::own_accumulated_2d_scale(self, paintable)
}
-}
-pub(crate) fn hit_test_facts(arena: &impl PaintRead, paintable: NodeSlotId) -> HitTestFacts {
- let Some(row) = arena.node(paintable) else {
- return HitTestFacts::default();
- };
- let Some(style) = row.style() else {
- return HitTestFacts::default();
- };
- let wheel_axes = crate::painting::chrome_geometry::wheel_scrollable_axes(arena, paintable);
- let svg_path = node_painting::is_svg_path(row.kind());
- let svg = style.inherited_svg();
- HitTestFacts {
- visible_for_hit_testing: arena.paintable_row_is_populated(paintable)
- && row.dom_paint_facts() & DomPaintFact::Inert as u8 == 0
- && style.pointer_events() != css_enums::pointer_events::NONE,
- has_resizer: crate::painting::chrome_geometry::has_resizer(arena, paintable),
- could_be_scrolled_horizontally: wheel_axes.horizontal,
- could_be_scrolled_vertically: wheel_axes.vertical,
- svg_hit_areas: if svg_path {
- let areas = svg_hit_areas(
- style.pointer_events(),
- style.visibility() == css_enums::visibility::VISIBLE,
- svg.fill.kind == SVG_PAINT_NONE,
- svg.stroke.kind == SVG_PAINT_NONE,
- );
- // For text elements, hit-testing is performed on a character cell basis, so the text receives events
- // anywhere within a character cell whenever its fill or its stroke would.
- if matches!(row.kind(), NodeKind::SVGTextBox | NodeKind::SVGTextPathBox)
- && areas & (svg_hit_area::FILL | svg_hit_area::STROKE) != 0
- {
- svg_hit_area::FILL
- } else {
- areas
- }
- } else if row.kind() == NodeKind::SVGImageBox {
- // For raster images, hit-testing is performed on a whole-image basis (i.e., the rectangular area for the
- // image is the determinant for whether the image receives the event).
- // NB: The rectangular area for the image is also its bounding box.
- if svg_hit_areas(
- style.pointer_events(),
- style.visibility() == css_enums::visibility::VISIBLE,
- false,
- false,
- ) != 0
- {
- svg_hit_area::BOUNDING_BOX
- } else {
- 0
- }
- } else if row.kind() == NodeKind::SVGGraphicsBox
- && style.pointer_events() == css_enums::pointer_events::BOUNDING_BOX
- {
- svg_hit_area::BOUNDING_BOX
- } else {
- 0
- },
- svg_path_winding_rule: if svg_path && svg.fill_rule == css_enums::fill_rule::EVENODD {
- WindingRule::EvenOdd
- } else {
- WindingRule::Nonzero
- },
+ fn append(&mut self, item: HitTestItem) {
+ self.list.append(item);
}
}
-impl<'a, O: Observer> PaintRecorder<'a, O> {
- fn is_anonymous(&self, paintable: NodeSlotId) -> bool {
- crate::painting::style_queries::is_anonymous(self.source, paintable)
- }
-
- fn is_generated_for_pseudo_element(&self, paintable: NodeSlotId) -> bool {
- self.source.node_is_generated_for_pseudo_element(paintable)
- }
-
+impl<O: Observer> PaintRecorder<'_, O> {
pub(crate) fn record_hit_test_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
- if node_painting::is_fragmented_inline(self.source, paintable) {
- self.record_inline_hit_test_items(paintable, phase);
- } else if node_painting::has_lines(self.source, paintable) {
- self.record_base_hit_test_items(paintable, phase);
- self.record_lines_hit_test_items(paintable, phase);
- } else if self
- .source
- .node_kind_if_live(paintable)
- .is_some_and(node_painting::is_svg_path)
- {
- self.record_svg_path_hit_test_items(paintable, phase);
- } else if self
- .source
- .node_kind_if_live(paintable)
- .is_some_and(|kind| matches!(kind, NodeKind::SVGImageBox | NodeKind::SVGGraphicsBox))
- {
- self.record_svg_box_hit_test_items(paintable, phase);
- } else {
- self.record_base_hit_test_items(paintable, phase);
- }
- }
-
- fn record_base_hit_test_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
- if phase != PaintPhase::Background && phase != PaintPhase::Overlay {
- return;
- }
- if !self.visibility_is_visible(paintable) || !self.visible_for_hit_testing(paintable) {
- return;
- }
- if phase == PaintPhase::Background {
- if self.is_anonymous(paintable) && !self.is_generated_for_pseudo_element(paintable) {
- return;
- }
- // Column and column group boxes only exist to paint their backgrounds under the cells of their columns;
- // they are not hit-tested, as in other engines.
- let display = self.display(paintable);
- if display.is_table_column() || display.is_table_column_group() {
- return;
- }
- let context = self.data(paintable).accumulated_visual_context;
- for (rect, radii) in self.border_boxes_of_fragments(paintable) {
- self.append_box(paintable, rect, context, radii);
- }
- return;
- }
- let facts = self.paintable_facts(paintable);
- let context = self.data(paintable).accumulated_visual_context;
- if facts.has_resizer {
- self.append_chrome_widget(paintable, CHROME_WIDGET_RESIZE_HANDLE, context);
- }
- if facts.could_be_scrolled_horizontally {
- self.append_chrome_widget(paintable, CHROME_WIDGET_HORIZONTAL_SCROLLBAR, context);
- }
- if facts.could_be_scrolled_vertically {
- self.append_chrome_widget(paintable, CHROME_WIDGET_VERTICAL_SCROLLBAR, context);
- }
- }
-
- fn record_lines_hit_test_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
- if phase != PaintPhase::Foreground {
- return;
- }
- let fragment_count = self.source.committed_side_data(paintable).fragments().len();
- if fragment_count == 0 && crate::painting::paint_order::first_paint_child(self.source, paintable).is_none() {
- if self.is_visible(paintable) && self.visible_for_hit_testing(paintable) {
- self.record_empty_editable_hit_test_item(paintable);
- }
- return;
- }
- let context = self.data(paintable).accumulated_visual_context_for_descendants;
- // Fragments inside self-painting inline boxes are recorded during that box's paint.
- let filter = fragment_ownership::effective_filter(self.source, paintable);
- filter.for_each_owned_fragment_index(fragment_count, |index| {
- if self.fragment_is_block_level_box(paintable, index) {
- return;
- }
- self.append_text_fragment(paintable, index as u32, context);
- });
- }
-
- fn record_inline_hit_test_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
- if self.is_anonymous(paintable) && !self.is_generated_for_pseudo_element(paintable) {
- return;
- }
- // Only this box's own items are gated on its visibility; the fragments it owns may belong
- // to descendants that override visibility or pointer-events, so they are filtered per
- // fragment instead.
- let box_itself_is_hit_testable = self.is_visible(paintable) && self.visible_for_hit_testing(paintable);
-
- if phase == PaintPhase::Background {
- if box_itself_is_hit_testable {
- self.append_piece_boxes(paintable);
- }
- return;
- }
- if phase != PaintPhase::Foreground {
- return;
- }
-
- if fragment_ownership::is_self_painting_inline(self.source, paintable) {
- let Some(root) = self.inline_root(paintable) else {
- return;
- };
- let context = self.data(paintable).accumulated_visual_context_for_descendants;
- let fragment_count = self.source.committed_side_data(root).fragments().len();
- let filter = fragment_ownership::effective_filter(self.source, paintable);
- // Hit-test precedence follows paint order: this box's own text loses to the box itself
- // (re-recorded so its z-order matches this box's paint order), while nested content
- // (e.g. a link inside this box) wins over it.
- let own_layout_node = paintable;
- let mut nested_content_indices = Vec::new();
- filter.for_each_owned_fragment_index(fragment_count, |index| {
- if self.fragment_is_block_level_box(root, index) {
- return;
- }
- let fragment_node = self.source.committed_side_data(root).fragments()[index].layout_node;
- if fragment_ownership::nearest_fragmented_inline_ancestor(self.source, fragment_node)
- == Some(own_layout_node)
- {
- self.append_text_fragment(root, index as u32, context);
- } else {
- nested_content_indices.push(index);
- }
- });
- if box_itself_is_hit_testable && self.has_stacking_context(paintable) {
- self.append_piece_boxes(paintable);
- }
- for index in nested_content_indices {
- self.append_text_fragment(root, index as u32, context);
- }
- }
-
- // Clicks must be able to place the caret in an editable element with no content, so its
- // target is recorded in foreground order, winning over earlier-painted overlapping content.
- if box_itself_is_hit_testable && !self.inline_has_content(paintable) {
- self.record_empty_editable_hit_test_item(paintable);
- }
- }
-
- fn inline_root(&self, paintable: NodeSlotId) -> Option<NodeSlotId> {
- let block = self.data(paintable).containing_block;
- if block.is_invalid() || !self.source.paintable_row_is_populated(block) {
- return None;
- }
- node_painting::has_lines(self.source, block).then_some(block)
- }
-
- fn piece_of(&self, root: NodeSlotId, piece_index: u32) -> InlineBoxPieceRecord {
- self.source.committed_side_data(root).inline_box_pieces()[piece_index as usize]
- }
-
- fn inline_has_content(&self, paintable: NodeSlotId) -> bool {
- let has_content_pieces = self.inline_root(paintable).is_some_and(|root| {
- self.source
- .committed_side_data(root)
- .inline_box_piece_indices(paintable)
- .iter()
- .any(|piece_index| !self.piece_of(root, *piece_index).is_geometry_only_placeholder)
- });
- has_content_pieces || crate::painting::paint_order::first_paint_child(self.source, paintable).is_some()
- }
-
- fn append_piece_boxes(&mut self, paintable: NodeSlotId) {
- let Some(root) = self.inline_root(paintable) else {
- return;
- };
- let root_positions = self.line_root_positions(root);
- let layout_arena = self.source;
- let context = self.data(paintable).accumulated_visual_context;
- let side = layout_arena.committed_side_data(root);
- for piece_index in side.inline_box_piece_indices(paintable) {
- let piece = &side.inline_box_pieces()[*piece_index as usize];
- if piece.is_geometry_only_placeholder {
- continue;
- }
- let root_position = root_positions.of_line(side.lines(), piece.line_index as usize);
- let rect = CssPixelRect::from(piece.border_box_rect).translated_by(root_position);
- let radii = self.piece_border_radii(paintable, piece);
- self.append_box(paintable, rect, context, radii);
- }
- }
-
- fn record_svg_path_hit_test_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
- if phase != PaintPhase::Foreground {
- return;
- }
- let Some(path) = paintable_geometry::committed_svg_path(self.source, paintable) else {
- return;
- };
- if !self.visible_for_hit_testing(paintable) {
- return;
- }
- let facts = self.paintable_facts(paintable);
- if facts.svg_hit_areas == 0 {
- return;
- }
- if facts.svg_hit_areas & svg_hit_area::BOUNDING_BOX != 0 {
- self.append_svg_bounding_box(paintable);
- return;
- }
- let stroke = if facts.svg_hit_areas & svg_hit_area::STROKE != 0 {
- self.svg_stroke_hit_region(paintable)
- } else {
- None
- };
- if facts.svg_hit_areas & !svg_hit_area::STROKE == 0 && stroke.is_none() {
- return;
- }
- let [mut x, mut y, mut width, mut height] = path.bounding_box();
- if let Some(stroke) = &stroke {
- let outset = stroke.outset();
- x -= outset;
- y -= outset;
- width += outset * 2.0;
- height += outset * 2.0;
- }
- // The item rect rounds outward, past the far edges, so fixed-point quantization can never reject a valid hit
- // and points on those edges stay inside it.
- let x1 = x.floor() as i32;
- let y1 = y.floor() as i32;
- let x2 = (x + width).floor() as i32 + 1;
- let y2 = (y + height).floor() as i32 + 1;
- let bounding_box = CssPixelRect::new(
- CssPixels::from_integer(i64::from(x1)),
- CssPixels::from_integer(i64::from(y1)),
- CssPixels::from_integer(i64::from(x2 - x1)),
- CssPixels::from_integer(i64::from(y2 - y1)),
- );
- if bounding_box.is_empty() {
- return;
- }
- let context = self.data(paintable).accumulated_visual_context;
- let item = HitTestItem {
- rect: bounding_box,
- path: Some(path),
- winding_rule: facts.svg_path_winding_rule,
- svg_hit_areas: facts.svg_hit_areas,
- svg_stroke: stroke,
- ..self.base_hit_test_item(HitTestItemKind::SvgPath, paintable, context)
- };
- self.list.append(item);
- }
-
- fn record_svg_box_hit_test_items(&mut self, paintable: NodeSlotId, phase: PaintPhase) {
- if phase != PaintPhase::Foreground || !self.visible_for_hit_testing(paintable) {
- return;
- }
- if self.paintable_facts(paintable).svg_hit_areas & svg_hit_area::BOUNDING_BOX != 0 {
- self.append_svg_bounding_box(paintable);
- }
- }
-
- // NB: The rect of an SVG box is its bounding box, which takes in the boxes of its descendants, like the tspans of a
- // text element. A point on the far edges of the bounding box is over it too, so the item rect reaches one
- // fixed-point unit past them.
- fn append_svg_bounding_box(&mut self, paintable: NodeSlotId) {
- let one_unit = CssPixels::from_raw(1);
- let rect = paintable_geometry::absolute_rect(self.source, paintable).inflated(
- CssPixels::default(),
- one_unit,
- one_unit,
- CssPixels::default(),
- );
- let context = self.data(paintable).accumulated_visual_context;
- let item = HitTestItem {
- rect,
- ..self.base_hit_test_item(HitTestItemKind::Box, paintable, context)
- };
- self.list.append(item);
- }
-
- fn svg_stroke_hit_region(&self, paintable: NodeSlotId) -> Option<std::sync::Arc<SvgStrokeHitRegion>> {
- let style = self.source.node_style_if_live(paintable)?;
- let stroke = crate::painting::record::paint::svg::svg_stroke_style(self.source, paintable, &style);
- let mut thickness = stroke.width;
- let mut dash_array = stroke.dash_array;
- let mut dash_offset = stroke.dash_offset;
- // https://svgwg.org/svg2-draft/painting.html#PaintingVectorEffects
- // With the non-scaling-stroke vector effect, stroke outline shall be calculated in the "host" coordinate
- // space instead of user coordinate system.
- if stroke.non_scaling_stroke {
- let accumulated_scale = self.own_accumulated_2d_scale(paintable);
- let accumulated_scale_area = accumulated_scale.width * accumulated_scale.height;
- if accumulated_scale_area > 0.0 {
- let scale = accumulated_scale_area.sqrt();
- thickness /= scale;
- dash_array.iter_mut().for_each(|value| *value /= scale);
- dash_offset /= scale;
- }
- }
- if thickness.is_nan() || thickness <= 0.0 {
- return None;
- }
- Some(std::sync::Arc::new(SvgStrokeHitRegion::new(
- libgfx_rust::path::StrokeStyle {
- thickness,
- cap_style: stroke.cap_style,
- join_style: stroke.join_style,
- miter_limit: stroke.miter_limit,
- dash_array,
- dash_offset,
- },
- )))
- }
-
- fn record_empty_editable_hit_test_item(&mut self, paintable: NodeSlotId) {
- if self.is_anonymous(paintable)
- || !self
- .source
- .node_has_dom_paint_fact(paintable, DomPaintFact::EditableOrEditingHost)
- {
- return;
- }
- let context = self.data(paintable).accumulated_visual_context;
- for rect in paintable_geometry::absolute_border_box_rects_of_fragments(self.source, paintable) {
- self.append_empty_editable(paintable, rect, context);
- }
- }
-
- fn fragment(
- &self,
- owner: NodeSlotId,
- index: usize,
- ) -> CommittedFragment<impl std::ops::Deref<Target = CommittedSideData> + 'a> {
- CommittedFragment {
- side_data: self.source.committed_side_data(owner),
- index,
- }
- }
-
- fn fragment_is_block_level_box(&self, owner: NodeSlotId, index: usize) -> bool {
- text_fragment::is_block_level_box(self.source, &self.fragment(owner, index))
- }
-
- // A text node has no style of its own, so its parent both decides whether the fragment can be hit and is what an
- // event dispatched from it resolves against. Returns that node, or None when the fragment is not hit-testable.
- fn hit_node_for_text_fragment(&mut self, fragment: &FragmentRecord) -> Option<NodeSlotId> {
- let node = fragment.layout_node;
- let row = self.source.node(node)?;
- if fragment.is_block_ellipsis || !node_facts::kind_is_text(row.kind()) {
- return None;
- }
- let parent = row.parent();
- let parent_style = self.source.node_style_if_live(parent)?;
- if parent_style.visibility() != css_enums::visibility::VISIBLE
- || parent_style.effects().opacity == 0.0
- || parent_style.pointer_events() == css_enums::pointer_events::NONE
- || row.dom_paint_facts() & DomPaintFact::Inert as u8 != 0
- {
- return None;
- }
- // Resolving the hit needs a committed paintable row; without one there is nothing to resolve against.
- if !self.source.paintable_row_is_populated(parent) {
- return None;
- }
- Some(parent)
- }
-
- fn append_box(
- &mut self,
- paintable_box: NodeSlotId,
- rect: CssPixelRect,
- context: ContextRef,
- border_radii: BorderRadii,
- ) {
- let item = HitTestItem {
- rect,
- border_radii,
- ..self.base_hit_test_item(HitTestItemKind::Box, paintable_box, context)
- };
- self.list.append(item);
- }
-
- fn base_hit_test_item(&self, kind: HitTestItemKind, target: NodeSlotId, context: ContextRef) -> HitTestItem {
- HitTestItem {
- kind,
- paintable: target,
- hit_node: target,
- chrome_widget_kind: CHROME_WIDGET_NONE,
- text_fragment_index: None,
- rect: CssPixelRect::default(),
- context,
- border_radii: BorderRadii::default(),
- path: None,
- winding_rule: WindingRule::Nonzero,
- svg_hit_areas: 0,
- svg_stroke: None,
- }
- }
-
- fn append_text_fragment(&mut self, owner: NodeSlotId, fragment_index: u32, context: ContextRef) {
- let fragment = self.fragment(owner, fragment_index as usize);
- let Some(hit_node) = self.hit_node_for_text_fragment(&fragment) else {
- return;
- };
- let item = HitTestItem {
- text_fragment_index: Some(fragment_index),
- rect: text_fragment::absolute_rect(self.source, &fragment),
- hit_node,
- ..self.base_hit_test_item(HitTestItemKind::TextFragment, owner, context)
- };
- self.list.append(item);
- }
-
- fn append_empty_editable(&mut self, paintable: NodeSlotId, rect: CssPixelRect, context: ContextRef) {
- let item = HitTestItem {
- rect,
- ..self.base_hit_test_item(HitTestItemKind::EmptyEditable, paintable, context)
- };
- self.list.append(item);
- }
-
- fn append_chrome_widget(&mut self, paintable: NodeSlotId, chrome_widget_kind: u8, context: ContextRef) {
- let item = HitTestItem {
- chrome_widget_kind,
- ..self.base_hit_test_item(HitTestItemKind::ChromeWidget, paintable, context)
- };
- self.list.append(item);
+ let rows = self.source;
+ hit_test_items_of_phase(rows, self, paintable, phase);
}
}
diff --git a/Libraries/LibWeb/Rust/src/painting/record/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
index 022ec6524e0..53e994c1b24 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
@@ -589,22 +589,11 @@ impl<O: Observer> PaintRecorder<'_, O> {
}
pub(crate) fn border_radii(&mut self, paintable: NodeSlotId) -> BorderRadii {
- let Some(style) = self.source.node_style_if_live(paintable) else {
- return BorderRadii::default();
- };
- crate::painting::visual_context::node_values::border_radii_data(style, self.source, paintable)
+ crate::painting::border_radii::border_radii(self.source, paintable)
}
pub(crate) fn piece_border_radii(&mut self, paintable: NodeSlotId, piece: &InlineBoxPieceRecord) -> BorderRadii {
- let Some(style) = self.source.node_style_if_live(paintable) else {
- return BorderRadii::default();
- };
- crate::painting::visual_context::node_values::piece_border_radii_data(
- style,
- piece.border_box_rect.width,
- piece.border_box_rect.height,
- piece.present_edges,
- )
+ crate::painting::border_radii::piece_border_radii(self.source, paintable, piece)
}
pub(crate) fn fragment_border_box(
@@ -612,34 +601,14 @@ impl<O: Observer> PaintRecorder<'_, O> {
paintable: NodeSlotId,
index: usize,
) -> (crate::css::css_pixels::CssPixelRect, BorderRadii) {
- let rect =
- crate::painting::paintable_geometry::absolute_fragment_border_box_rect(self.source, paintable, index);
- let Some(style) = self.source.node_style_if_live(paintable) else {
- return (rect, BorderRadii::default());
- };
- let radii = crate::painting::visual_context::node_values::piece_border_radii_data(
- style,
- rect.width,
- rect.height,
- crate::painting::paintable_geometry::committed_fragment_present_edges(self.source, paintable, index),
- );
- (rect, radii)
+ crate::painting::border_radii::fragment_border_box_and_radii(self.source, paintable, index)
}
pub(crate) fn border_boxes_of_fragments(
&mut self,
paintable: NodeSlotId,
) -> smallvec::SmallVec<[(CssPixelRect, BorderRadii); 1]> {
- let fragment_count = crate::painting::paintable_geometry::fragment_count(self.source, paintable);
- if fragment_count > 1 && !crate::painting::node_painting::is_fragmented_inline(self.source, paintable) {
- return (0..fragment_count)
- .map(|index| self.fragment_border_box(paintable, index))
- .collect();
- }
- smallvec::smallvec![(
- crate::painting::paintable_geometry::absolute_border_box_rect(self.source, paintable),
- self.border_radii(paintable),
- )]
+ crate::painting::border_radii::border_boxes_and_radii_of_fragments(self.source, paintable)
}
pub(crate) fn base_paint_facts(&mut self, paintable: NodeSlotId) -> BasePaintFacts {
@@ -652,10 +621,7 @@ impl<O: Observer> PaintRecorder<'_, O> {
return facts;
};
let effects = style.effects();
- let retains_animated_content =
- self.source.node_flags_if_live(paintable) & NodeFlag::HasAnimatedOpacityOrTransform as u32 != 0;
- let is_visible = style.visibility() == crate::css::css_enums::visibility::VISIBLE
- && (effects.opacity != 0.0 || retains_animated_content);
+ let is_visible = crate::painting::style_queries::is_visible(self.source, paintable);
let empty_cells_property_applies = self.display(paintable).is_internal_table()
&& style.empty_cells() == crate::css::css_enums::empty_cells::HIDE
&& crate::painting::paint_order::first_paint_child(self.source, paintable).is_none();
diff --git a/Libraries/LibWeb/Rust/src/painting/style_queries.rs b/Libraries/LibWeb/Rust/src/painting/style_queries.rs
index cece955bc11..4250ee3b933 100644
--- a/Libraries/LibWeb/Rust/src/painting/style_queries.rs
+++ b/Libraries/LibWeb/Rust/src/painting/style_queries.rs
@@ -195,6 +195,16 @@ pub(crate) fn has_layout_containment(arena: &impl PaintRead, node: NodeSlotId, s
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)