Skip to content

Truly independent web browser

LibWeb: Locate empty inline containing blocks on their lines

sideshowbarker06aa754fa7b2
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index c825c5510ac..eeba8a53991 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -627,6 +627,25 @@ pub(crate) fn compute(
.filter(|node| context.facts(*node).has_dom_node() && !node_to_index.contains_key(node))
.collect();
+ let mut empty_inline_positions = HashMap::<Node, (usize, CssPixels, CssPixels)>::default();
+ if !without_fragments.is_empty() {
+ for (line_index, line) in context.line_data().line_boxes.iter().enumerate() {
+ for marker in &line.static_position_markers {
+ let mut ancestor = context.nearest_fragmented_inline_ancestor(marker.box_);
+ while !ancestor.is_invalid() {
+ if !node_to_index.contains_key(&ancestor) {
+ empty_inline_positions.entry(ancestor).or_insert((
+ line_index,
+ marker.inline_offset,
+ marker.block_offset,
+ ));
+ }
+ ancestor = context.nearest_fragmented_inline_ancestor(ancestor);
+ }
+ }
+ }
+ }
+
let mut staged = Vec::<StagedPiece>::new();
let mut deepest_first: Vec<_> = (0..per_nodes.len()).collect();
deepest_first.sort_by(|left, right| {
@@ -822,11 +841,28 @@ pub(crate) fn compute(
context.entered_inline_box(node).is_some(),
"a fragmented inline without fragments is an inline box the context entered"
);
- let line_height = context.style(node).line_height();
- let placeholder_rect = FfiCssPixelRect {
- height: line_height,
+ let style = context.style(node);
+ let mut placeholder_rect = FfiCssPixelRect {
+ height: style.line_height(),
..Default::default()
};
+ let mut placeholder_line_index = 0;
+ if let Some(&(line_index, inline_offset, block_offset)) = empty_inline_positions.get(&node) {
+ // An inline with only out-of-flow content still occupies its position on the line. Its child's marker
+ // retains that position even though the inline has no in-flow fragment to supply its containing block.
+ let data = context.line_data();
+ let line = &data.line_boxes[line_index];
+ placeholder_line_index = line_index as u32;
+ placeholder_rect.x = inline_offset;
+ if line.is_empty() {
+ placeholder_rect.y = block_offset;
+ placeholder_rect.height = CssPixels::default();
+ } else {
+ placeholder_rect.height = line_builder::normal_line_height(style);
+ placeholder_rect.y = line.block_start + line.baseline
+ - line_builder::LineBuilder::baseline_for_style(style, placeholder_rect.height);
+ }
+ }
if collect_inline_containing_block_rects
&& context
.run
@@ -848,7 +884,7 @@ pub(crate) fn compute(
node,
first_fragment_index: 0,
fragment_count: 0,
- line_index: 0,
+ line_index: placeholder_line_index,
border_box_rect: placeholder_rect,
relpos_delta: FfiCssPixelPoint::default(),
baseline: CssPixels::default(),
@@ -2902,6 +2938,9 @@ impl<'context> InlineFormattingContext<'context> {
None,
);
}
+ if self.run.fragments.is_some() {
+ self.compute_inline_box_pieces();
+ }
line_builder.remove_last_line_if_empty();
self.parent.append_floating_boxes(floating_boxes_after_clamp_point);
}
@@ -2917,7 +2956,6 @@ impl<'context> InlineFormattingContext<'context> {
};
self.generate_line_boxes(iterator);
if self.run.fragments.is_some() {
- self.compute_inline_box_pieces();
self.fold_inline_ancestor_relative_insets_into_line_data();
self.register_inline_anchor_candidates();
}
@@ -3018,7 +3056,7 @@ impl<'context> InlineFormattingContext<'context> {
(!rects.is_empty()).then(|| rects.into_boxed_slice())
}
- fn compute_inline_box_pieces(&mut self) {
+ fn compute_inline_box_pieces(&self) {
let (pieces, inline_containing_block_rect_candidates) = compute(self);
self.line_data_mut().inline_box_pieces = pieces;
for candidate in inline_containing_block_rect_candidates {
diff --git a/Tests/LibWeb/Text/expected/css/abspos-pseudo-empty-inline-container.txt b/Tests/LibWeb/Text/expected/css/abspos-pseudo-empty-inline-container.txt
new file mode 100644
index 00000000000..51b10aff364
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/abspos-pseudo-empty-inline-container.txt
@@ -0,0 +1,7 @@
+before empty: x=true; y=true; positioned=true
+before space: x=true; y=true; positioned=true
+after empty: x=true; y=true; positioned=true
+after space: x=true; y=true; positioned=true
+element empty: x=true; y=true; positioned=true
+element space: x=true; y=true; positioned=true
+empty line: block height=0; x=true; y=0; host height=0
diff --git a/Tests/LibWeb/Text/input/css/abspos-pseudo-empty-inline-container.html b/Tests/LibWeb/Text/input/css/abspos-pseudo-empty-inline-container.html
new file mode 100644
index 00000000000..267da9418a6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/abspos-pseudo-empty-inline-container.html
@@ -0,0 +1,34 @@
+<!doctype html>
+<style>
+ body { margin: 40px; font: 16px/32px monospace; }
+ .host { position: relative; }
+ .before::before, .after::after, .absolute { content: ""; position: absolute; top: 0; left: 0; width: 4px; height: 4px; background: red; }
+</style>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ for (const pseudo of ["before", "after", "element"]) {
+ for (const content of ["", " "]) {
+ const block = document.createElement("div");
+ block.innerHTML = `<span class="lead">a</span><span class="host ${pseudo}">${content}</span>b`;
+ document.body.append(block);
+ const host = block.querySelector(".host");
+ if (pseudo === "element") {
+ const child = document.createElement("i");
+ child.className = "absolute";
+ host.append(child);
+ }
+ const lead = block.querySelector(".lead").getBoundingClientRect();
+ const rect = host.getBoundingClientRect();
+ const hit = document.elementFromPoint(lead.right + 1, lead.top + 1);
+ println(`${pseudo} ${content === "" ? "empty" : "space"}: x=${rect.left === lead.right}; y=${rect.top === lead.top}; positioned=${hit === host || host.contains(hit)}`);
+ }
+ }
+ const block = document.createElement("div");
+ block.innerHTML = '<span class="host before"></span>';
+ document.body.append(block);
+ const host = block.firstChild;
+ const b = block.getBoundingClientRect(), h = host.getBoundingClientRect();
+ println(`empty line: block height=${b.height}; x=${h.left === b.left}; y=${h.top - b.top}; host height=${h.height}`);
+ });
+</script>