Skip to content

Truly independent web browser

LibWeb: Report the blocks inside an inline among its client rects

sideshowbarker4652e25c17bf
diff --git a/Libraries/LibWeb/Rust/src/painting/client_rects.rs b/Libraries/LibWeb/Rust/src/painting/client_rects.rs
index d5294355643..959decae088 100644
--- a/Libraries/LibWeb/Rust/src/painting/client_rects.rs
+++ b/Libraries/LibWeb/Rust/src/painting/client_rects.rs
@@ -5,6 +5,7 @@
*/
use crate::css::css_pixels::{CssPixelRect, CssPixels};
+use crate::layout::inline_content::InlineBoxPieceRecord;
use crate::layout::node_data::{NodeFlag, NodeKind, NodeSlotId};
use crate::layout::node_facts::{self, NodeShape};
use crate::painting::node_painting;
@@ -58,7 +59,9 @@ pub(crate) fn can_compute_client_rects_without_visual_context_update(
true
}
-fn for_each_inline_piece_border_box_rect(
+/// The border boxes of the fragments of an inline box, in content order: its pieces on the lines, and the in-flow
+/// block-level boxes inside it, which break it around themselves.
+fn for_each_inline_fragment_border_box_rect(
arena: &impl PaintRead,
inline_paintable: NodeSlotId,
mut push_rect: impl FnMut(CssPixelRect),
@@ -68,14 +71,155 @@ fn for_each_inline_piece_border_box_rect(
};
let root_positions = paintable_geometry::LineRootPositions::new(arena, root);
let root_side = arena.committed_side_data(root);
+ let mut pushed_a_rect = false;
+ let mut empty_piece_after_last_block = None;
with_inline_pieces(arena, inline_paintable, |piece| {
if piece.is_geometry_only_placeholder {
+ // A placeholder piece on a line that a block-level box takes up stands for that box, if the box is inside
+ // the inline box. (Empty inline boxes get placeholder pieces too.)
+ if let Some(block) = interrupting_block(arena, root, inline_paintable, piece) {
+ // https://drafts.csswg.org/css2/#anonymous-block-level
+ // When an inline box contains an in-flow block-level box, the inline box (and its inline ancestors
+ // within the same line box) is broken around the block-level box (and any block-level siblings
+ // that are consecutive or separated only by collapsible whitespace and/or out-of-flow elements),
+ // splitting the inline box into two boxes (even if either side is empty), one on each side
+ // of the block-level box(es).
+ // NB: So the empty part of the inline box before the first block gets a rect when nothing of
+ // it precedes that block, and the empty part after the last block gets one when nothing
+ // of it follows that block.
+ if !pushed_a_rect {
+ push_rect(empty_piece_rect(&block, false));
+ }
+ push_rect(block.border_box_rect);
+ pushed_a_rect = true;
+ empty_piece_after_last_block = Some(empty_piece_rect(&block, true));
+ }
return true;
}
let root_position = root_positions.of_line(root_side.lines(), piece.line_index as usize);
push_rect(CssPixelRect::from(piece.border_box_rect).translated_by(root_position));
+ pushed_a_rect = true;
+ empty_piece_after_last_block = None;
true
});
+ if let Some(rect) = empty_piece_after_last_block {
+ push_rect(rect);
+ }
+}
+
+struct InterruptingBlock {
+ border_box_rect: CssPixelRect,
+ /// The margin box the block would have without its relative offset.
+ normal_flow_margin_box_rect: CssPixelRect,
+ /// The content box of the block's line root, on the block's line.
+ line_root_content_rect: CssPixelRect,
+ writing_mode: u8,
+ direction: u8,
+}
+
+/// The block-level box that takes up the line that the placeholder `piece` of `inline_paintable` is on, if that box is
+/// inside `inline_paintable`.
+fn interrupting_block(
+ arena: &impl PaintRead,
+ line_root: NodeSlotId,
+ inline_paintable: NodeSlotId,
+ piece: &InlineBoxPieceRecord,
+) -> Option<InterruptingBlock> {
+ let root_side = arena.committed_side_data(line_root);
+ let line = root_side.lines().get(piece.line_index as usize)?;
+ if !line.has_block_level_box {
+ return None;
+ }
+ // NOTE: A line taken up by a block-level box holds that box's fragment and nothing else.
+ let first = piece.first_fragment_index as usize;
+ let fragment = root_side
+ .fragments()
+ .get(first..first + piece.fragment_count as usize)?
+ .iter()
+ .find(|fragment| arena.paintable_row_is_populated(fragment.layout_node))?;
+ let mut ancestor = arena.node(fragment.layout_node)?.parent();
+ while ancestor != inline_paintable {
+ if ancestor == line_root {
+ return None;
+ }
+ ancestor = arena.node(ancestor)?.parent();
+ }
+ let border_box_rect = paintable_geometry::absolute_border_box_rect(arena, fragment.layout_node);
+ // NOTE: For an in-flow box, the insets are its relative offset, and zero unless it's relatively positioned.
+ let relative_offset = paintable_geometry::committed_inset(arena, fragment.layout_node);
+ let margin = paintable_geometry::committed_margin(arena, fragment.layout_node);
+ let line_root_content_size = paintable_geometry::committed_content_size(arena, line_root);
+ Some(InterruptingBlock {
+ border_box_rect,
+ normal_flow_margin_box_rect: CssPixelRect {
+ x: border_box_rect.x - relative_offset.left - margin.left,
+ y: border_box_rect.y - relative_offset.top - margin.top,
+ width: border_box_rect.width + margin.left + margin.right,
+ height: border_box_rect.height + margin.top + margin.bottom,
+ },
+ line_root_content_rect: CssPixelRect::from_location_and_size(
+ paintable_geometry::absolute_line_root_position(arena, line_root, piece.line_index as usize),
+ line_root_content_size.into(),
+ ),
+ writing_mode: line.writing_mode,
+ direction: line.direction,
+ })
+}
+
+/// The zero-size rect of an empty part of an inline box that a block-level box breaks. The parts are on the anonymous
+/// lines around the block, so they're at the line's start (the inline-start edge of the block's margin box), and at the
+/// block-start edge of the block's margin box for the part before the block (but not before the start of the line
+/// root's content, which a margin that collapses through the line root can reach past), or its block-end edge for the
+/// part after it. The parts are in normal flow, so a relative offset of the block doesn't move them.
+fn empty_piece_rect(block: &InterruptingBlock, after_the_block: bool) -> CssPixelRect {
+ use crate::css::css_enums::{direction, writing_mode};
+ let inline_axis_is_reverse = match block.writing_mode {
+ writing_mode::SIDEWAYS_LR => block.direction == direction::LTR,
+ _ => block.direction == direction::RTL,
+ };
+ let block_axis_is_reverse = matches!(
+ block.writing_mode,
+ writing_mode::VERTICAL_RL | writing_mode::SIDEWAYS_RL
+ );
+ let at_block_axis_high_side = after_the_block != block_axis_is_reverse;
+ let rect = block.normal_flow_margin_box_rect;
+ let content = block.line_root_content_rect;
+ // The block-axis position of the part on the block-axis low or high side, as above.
+ let block_axis_position = |low: CssPixels, size: CssPixels, content_low: CssPixels, content_size: CssPixels| match (
+ at_block_axis_high_side,
+ after_the_block,
+ ) {
+ (true, true) => low + size,
+ (false, true) => low,
+ (true, false) => (low + size).min(content_low + content_size),
+ (false, false) => low.max(content_low),
+ };
+ let (x, y) = if block.writing_mode == writing_mode::HORIZONTAL_TB {
+ (
+ if inline_axis_is_reverse {
+ rect.x + rect.width
+ } else {
+ rect.x
+ },
+ block_axis_position(rect.y, rect.height, content.y, content.height),
+ )
+ } else {
+ (
+ block_axis_position(rect.x, rect.width, content.x, content.width),
+ if inline_axis_is_reverse {
+ rect.y + rect.height
+ } else {
+ rect.y
+ },
+ )
+ };
+ let zero = CssPixels::from_integer(0);
+ CssPixelRect {
+ x,
+ y,
+ width: zero,
+ height: zero,
+ }
}
// https://drafts.csswg.org/cssom-view/#dom-element-getclientrects
@@ -118,11 +262,13 @@ pub(crate) fn for_each_client_rect(
// - Apply the transforms that apply to the element and its ancestors.
// FIXME: - If the element on which the method was invoked has a computed value for the display property of table
// or inline-table include both the table box and the caption box, if any, but not the anonymous container box.
- // FIXME: - Replace each anonymous block box with its child box(es) and repeat this until no anonymous block boxes
- // are left in the final list.
+ // - Replace each anonymous block box with its child box(es) and repeat this until no anonymous block boxes are left
+ // in the final list.
+ // NB: The anonymous block boxes that wrap a block-level box inside an inline box are replaced by that block-level
+ // box, which is one of the fragments of the inline box it breaks.
if row_is_populated && node_painting::is_fragmented_inline(arena, layout_node) {
let mut pushed_a_piece_rect = false;
- for_each_inline_piece_border_box_rect(arena, layout_node, |piece_border_box_rect| {
+ for_each_inline_fragment_border_box_rect(arena, layout_node, |piece_border_box_rect| {
pushed_a_piece_rect = true;
push_rect_in_viewport_space(piece_border_box_rect);
});
diff --git a/Tests/LibWeb/Text/expected/geometry/getClientRects-inline-basics.txt b/Tests/LibWeb/Text/expected/geometry/getClientRects-inline-basics.txt
index d4b7f77ed8f..e24a3d5d0a1 100644
--- a/Tests/LibWeb/Text/expected/geometry/getClientRects-inline-basics.txt
+++ b/Tests/LibWeb/Text/expected/geometry/getClientRects-inline-basics.txt
@@ -2,13 +2,16 @@ wrap: 3
wrap[0]: 10,15 40x10
wrap[1]: 10,35 40x10
wrap[2]: 10,55 40x10
-interrupted: 2
+interrupted: 3
interrupted[0]: 10,85 30x10
-interrupted[1]: 10,135 40x10
-interrupted bounding: 10,85 40x60
-only-block: 1
+interrupted[1]: 10,100 80x30
+interrupted[2]: 10,135 40x10
+interrupted bounding: 10,85 80x60
+only-block: 3
only-block[0]: 10,160 0x0
-only-block bounding: 10,160 0x0
+only-block[1]: 10,160 80x30
+only-block[2]: 10,190 0x0
+only-block bounding: 10,160 80x30
empty: 1
empty[0]: 10,200 0x20
none: 0
diff --git a/Tests/LibWeb/Text/expected/getClientRects-inline-with-block-children.txt b/Tests/LibWeb/Text/expected/getClientRects-inline-with-block-children.txt
new file mode 100644
index 00000000000..b18aa5ef0a7
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/getClientRects-inline-with-block-children.txt
@@ -0,0 +1,17 @@
+reporter: 3 rects: 0,50 0x0 | 0,50 800x200 | 0,250 0x0 || bounding 0,50 800x200
+text-around: 3 rects: 0,2 19x16 | 0,20 800x30 | 0,52 19x16 || bounding 0,2 800x66
+two-blocks: 4 rects: 0,0 0x0 | 0,0 800x30 | 0,30 800x40 | 0,70 0x0 || bounding 0,0 800x70
+outer: 3 rects: 0,0 0x0 | 0,0 800x30 | 0,30 0x0 || bounding 0,0 800x30
+inner: 3 rects: 0,0 0x0 | 0,0 800x30 | 0,30 0x0 || bounding 0,0 800x30
+abspos: 1 rects: 0,2 19x16 || bounding 0,2 19x16
+no-block: 1 rects: 0,2 73x16 || bounding 0,2 73x16
+text-before: 3 rects: 0,2 19x16 | 0,20 800x30 | 0,50 0x0 || bounding 0,2 800x48
+relpos: 3 rects: 0,0 0x0 | 0,100 800x30 | 0,30 0x0 || bounding 0,100 800x30
+box-model: 3 rects: 0,0 0x0 | 7,0 786x30 | 0,35 0x0 || bounding 7,0 786x30
+margins-in-padding: 3 rects: 0,1 0x0 | 7,6 786x20 | 0,31 0x0 || bounding 7,6 786x20
+rtl-margins: 3 rects: 800,1 0x0 | 7,6 786x20 | 800,31 0x0 || bounding 7,6 786x20
+run-in: 3 rects: 0,0 0x0 | 0,0 80x30 | 0,30 0x0 || bounding 0,0 80x30
+rtl: 3 rects: 100,0 0x0 | 0,0 100x30 | 100,30 0x0 || bounding 0,0 100x30
+vertical-rl: 3 rects: 100,0 0x0 | 70,0 30x80 | 70,0 0x0 || bounding 70,0 30x80
+vertical-lr: 3 rects: 0,0 0x0 | 0,0 30x80 | 30,0 0x0 || bounding 0,0 30x80
+scrollTop after scrollIntoView: 950
diff --git a/Tests/LibWeb/Text/expected/getClientRects-inline-with-block-in-shadow-root.txt b/Tests/LibWeb/Text/expected/getClientRects-inline-with-block-in-shadow-root.txt
new file mode 100644
index 00000000000..fdb385199cb
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/getClientRects-inline-with-block-in-shadow-root.txt
@@ -0,0 +1,2 @@
+light DOM block: bounding 50,50 200x80; client rects: 50,50 0x0, 50,50 200x80, 50,130 0x0
+shadow DOM block: bounding 50,200 200x80; client rects: 50,200 0x0, 50,200 200x80, 50,280 0x0
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/cssom-view/getClientRects-inline-with-block-child.txt b/Tests/LibWeb/Text/expected/wpt-import/css/cssom-view/getClientRects-inline-with-block-child.txt
new file mode 100644
index 00000000000..82939a423fc
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/cssom-view/getClientRects-inline-with-block-child.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass getClientRects-inline-with-block-child
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/getClientRects-inline-with-block-children.html b/Tests/LibWeb/Text/input/getClientRects-inline-with-block-children.html
new file mode 100644
index 00000000000..d90d04abd4b
--- /dev/null
+++ b/Tests/LibWeb/Text/input/getClientRects-inline-with-block-children.html
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<!-- An inline box that contains in-flow block-level boxes is broken around them, so its client rects are its pieces
+ before and after each block (even if empty) and the blocks themselves, in content order. -->
+<style>
+ body { margin: 0; font: 16px/20px SerenitySans; }
+ fieldset { margin: 0; padding: 0; border: 0; height: 200px; }
+ .case { position: relative; }
+ #scroller { height: 300px; overflow: auto; }
+</style>
+<div style="height: 50px">before</div>
+<x-wrap id="reporter"><fieldset>block inside an inline custom element</fieldset></x-wrap>
+<div style="height: 50px">after</div>
+<div class="case"><span id="text-around">aa<div style="height: 30px">block</div>bb</span></div>
+<div class="case"><span id="two-blocks"><div style="height: 30px"></div><div style="height: 40px"></div></span></div>
+<div class="case"><span id="outer"><b id="inner"><div style="height: 30px">x</div></b></span></div>
+<div class="case"><span id="abspos">a<div style="position: absolute; height: 30px">x</div>b</span></div>
+<div class="case"><span id="no-block">just text</span></div>
+<div class="case"><span id="text-before">aa<div style="height: 30px">block</div></span></div>
+<div class="case"><span id="relpos"><div style="position: relative; top: 100px; height: 30px"></div></span></div>
+<div class="case"><span id="box-model"><div style="margin: 5px 7px; border: 3px solid; padding: 2px; height: 20px"></div></span></div>
+<div class="case" style="padding: 1px 0"><span id="margins-in-padding"><div style="margin: 5px 7px; height: 20px"></div></span></div>
+<div class="case" style="padding: 1px 0; direction: rtl"><span id="rtl-margins"><div style="margin: 5px 7px; height: 20px"></div></span></div>
+<div class="case"><span id="run-in"><div style="display: run-in; width: 80px; height: 30px"></div></span></div>
+<div class="case" style="width: 100px; direction: rtl"><span id="rtl"><div style="height: 30px"></div></span></div>
+<div class="case" style="width: 100px; height: 80px; writing-mode: vertical-rl"><span id="vertical-rl"><div style="width: 30px"></div></span></div>
+<div class="case" style="width: 100px; height: 80px; writing-mode: vertical-lr"><span id="vertical-lr"><div style="width: 30px"></div></span></div>
+<div id="scroller">
+ <div style="height: 1000px"></div>
+ <x-wrap id="b"><fieldset>section</fieldset></x-wrap>
+ <div style="height: 1000px"></div>
+</div>
+<script>
+ test(() => {
+ // Each rect is x,y widthxheight, relative to the case's container.
+ const ids = ["reporter", "text-around", "two-blocks", "outer", "inner", "abspos", "no-block", "text-before", "relpos",
+ "box-model", "margins-in-padding", "rtl-margins", "run-in", "rtl", "vertical-rl", "vertical-lr"];
+ for (const id of ids) {
+ const element = document.getElementById(id);
+ const container = id === "reporter" ? { x: 0, y: 0 } : element.closest(".case").getBoundingClientRect();
+ const format = r => `${Math.round(r.x - container.x)},${Math.round(r.y - container.y)} ${Math.round(r.width)}x${Math.round(r.height)}`;
+ const rects = [...element.getClientRects()].map(format).join(" | ");
+ println(`${id}: ${element.getClientRects().length} rects: ${rects} || bounding ${format(element.getBoundingClientRect())}`);
+ }
+ document.getElementById("b").scrollIntoView({ block: "center" });
+ println(`scrollTop after scrollIntoView: ${document.getElementById("scroller").scrollTop}`);
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/getClientRects-inline-with-block-in-shadow-root.html b/Tests/LibWeb/Text/input/getClientRects-inline-with-block-in-shadow-root.html
new file mode 100644
index 00000000000..e4d93ed3bcf
--- /dev/null
+++ b/Tests/LibWeb/Text/input/getClientRects-inline-with-block-in-shadow-root.html
@@ -0,0 +1,38 @@
+<!DOCTYPE html>
+<script src="include.js"></script>
+<style>
+ body {
+ margin: 0;
+ }
+ #light, #shadow {
+ position: absolute;
+ left: 50px;
+ }
+ #light {
+ top: 50px;
+ }
+ #shadow {
+ top: 200px;
+ }
+</style>
+<!-- An inline element's client rects include the block it contains, whether that block is its light-DOM child or in
+ its shadow tree. -->
+<div id="light"><x-light><div style="width: 200px; height: 80px">light DOM block</div></x-light></div>
+<div id="shadow"><x-tile></x-tile></div>
+<script>
+ customElements.define("x-tile", class extends HTMLElement {
+ constructor() {
+ super();
+ this.attachShadow({ mode: "open" }).innerHTML = `<div style="width: 200px; height: 80px">shadow DOM block</div>`;
+ }
+ });
+ test(() => {
+ const describe = (name, element) => {
+ const bounds = element.getBoundingClientRect();
+ const rects = Array.from(element.getClientRects()).map(r => `${r.x},${r.y} ${r.width}x${r.height}`);
+ println(`${name}: bounding ${bounds.x},${bounds.y} ${bounds.width}x${bounds.height}; client rects: ${rects.join(", ")}`);
+ };
+ describe("light DOM block", document.querySelector("x-light"));
+ describe("shadow DOM block", document.querySelector("x-tile"));
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/cssom-view/getClientRects-inline-with-block-child.html b/Tests/LibWeb/Text/input/wpt-import/css/cssom-view/getClientRects-inline-with-block-child.html
new file mode 100644
index 00000000000..3d8239fd14e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/cssom-view/getClientRects-inline-with-block-child.html
@@ -0,0 +1,37 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<link rel="help" href="https://drafts.csswg.org/cssom-view-1/#dom-element-getclientrects">
+<link rel="author" title="Oriol Brufau" href="mailto:[email protected]">
+
+<section>
+ <span id="target">
+ <div style="height: 50px"></div>
+ </span>
+</section>
+
+<script src="../../resources/testharness.js"></script>
+<script src="../../resources/testharnessreport.js"></script>
+<script>
+function assert_rect_equals(actual, expected, description) {
+ assert_equals(actual.x, expected.x, description + " - x");
+ assert_equals(actual.y, expected.y, description + " - y");
+ assert_equals(actual.width, expected.width, description + " - width");
+ assert_equals(actual.height, expected.height, description + " - height");
+}
+test(() => {
+ const target = document.getElementById("target");
+ const rects = target.getClientRects();
+ // TODO: check that it's exactly 3 fragments.
+ assert_greater_than_equal(rects.length, 2, "At least 2 fragments");
+ assert_rect_equals(
+ rects[0],
+ {x: 8, y: 8, width: 0, height: 0},
+ "First rect",
+ );
+ assert_rect_equals(
+ rects[rects.length - 1],
+ {x: 8, y: 58, width: 0, height: 0},
+ "Last rect",
+ );
+});
+</script>