Truly independent web browser
LibWeb: Hit test SVG images and bounding-box containers
Andreas Klingcacab881ca49
diff --git a/Libraries/LibWeb/Rust/src/painting/hit_test/query.rs b/Libraries/LibWeb/Rust/src/painting/hit_test/query.rsindex 8f64fd5ce0b..e4ec1b3ba99 100644--- a/Libraries/LibWeb/Rust/src/painting/hit_test/query.rs+++ b/Libraries/LibWeb/Rust/src/painting/hit_test/query.rs@@ -116,10 +116,6 @@ impl HitTestList {let Some(path) = item.path.as_ref() else {return false;};- if item.svg_hit_areas & svg_hit_area::BOUNDING_BOX != 0 {- let [box_x, box_y, box_width, box_height] = path.bounding_box();- return x >= box_x && x <= box_x + box_width && y >= box_y && y <= box_y + box_height;- }(item.svg_hit_areas & svg_hit_area::FILL != 0 && path.contains(x, y, item.winding_rule))|| (item.svg_hit_areas & svg_hit_area::STROKE != 0&& itemdiff --git a/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs b/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rsindex 09a84c737c8..8dd65fd2f3c 100644--- a/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/hit_test_items.rs@@ -126,6 +126,25 @@ pub(crate) fn hit_test_facts(arena: &impl PaintRead, paintable: NodeSlotId) -> H} 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.inherited_ui().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.inherited_ui().pointer_events == css_enums::pointer_events::BOUNDING_BOX+ {+ svg_hit_area::BOUNDING_BOX} else {0},@@ -176,6 +195,12 @@ impl<'a, O: Observer> PaintRecorder<'a, O> {.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);}@@ -392,6 +417,10 @@ impl<'a, O: Observer> PaintRecorder<'a, O> {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 {@@ -435,6 +464,34 @@ impl<'a, O: Observer> PaintRecorder<'a, O> {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);diff --git a/Tests/LibWeb/Text/expected/SVG/image-element-hit-test.txt b/Tests/LibWeb/Text/expected/SVG/image-element-hit-test.txtindex cb6e6f86735..515cbc8f893 100644--- a/Tests/LibWeb/Text/expected/SVG/image-element-hit-test.txt+++ b/Tests/LibWeb/Text/expected/SVG/image-element-hit-test.txt@@ -1,3 +1,3 @@-(30, 20): svg+(30, 20): image(130, 20): svg-(230, 20): svg+(230, 20): hidden-alldiff --git a/Tests/LibWeb/Text/expected/wpt-import/svg/interact/scripted/svg-pointer-events-bbox.txt b/Tests/LibWeb/Text/expected/wpt-import/svg/interact/scripted/svg-pointer-events-bbox.txtindex 4366b95cb91..fd3074c90a6 100644--- a/Tests/LibWeb/Text/expected/wpt-import/svg/interact/scripted/svg-pointer-events-bbox.txt+++ b/Tests/LibWeb/Text/expected/wpt-import/svg/interact/scripted/svg-pointer-events-bbox.txt@@ -2,16 +2,15 @@ Harness status: OKFound 31 tests-22 Pass-9 Fail+31 PassPass circle1 contains point at (36, 60)Pass circle1 contains point at (42, 67)Pass circle1 does not contain point at (50, 50)Pass circle1 does not contain point at (50, 55)-Fail group1 contains point at (100, 100)-Fail group1 contains point at (137, 84)-Fail group1 contains point at (51, 156)-Fail group1 contains point at (70, 120)+Pass group1 contains point at (100, 100)+Pass group1 contains point at (137, 84)+Pass group1 contains point at (51, 156)+Pass group1 contains point at (70, 120)Pass circle2 contains point at (400, 150)Pass circle2 contains point at (432, 182)Pass circle2 contains point at (361, 122)@@ -21,17 +20,17 @@ Pass text1 contains point at (134, 16)Pass tspan1 contains point at (579, 46)Pass text1 does not contain point at (395, 73)Pass text1 does not contain point at (74, 5)-Fail bbox of text1 contains point at (435, 32)-Fail bbox of text1 contains point at (115, 46)+Pass bbox of text1 contains point at (435, 32)+Pass bbox of text1 contains point at (115, 46)Pass text2 contains point at (178, 146)Pass tspan2 contains point at (568, 283)Pass text2 does not contain point at (319, 161)Pass text2 does not contain point at (179, 131)-Fail bbox of text2 contains point at (295, 214)+Pass bbox of text2 contains point at (295, 214)Pass bbox of text2 contains point at (444, 222)Pass text3 contains point at (198, 291)Pass text3 contains point at (286, 301)Pass text3 does not contain point at (302, 337)Pass text3 does not contain point at (348, 335)-Fail image1 contains point at (19, 215)-Fail image1 contains point at (45, 225)\ No newline at end of file+Pass image1 contains point at (19, 215)+Pass image1 contains point at (45, 225)\ No newline at end of file