Skip to content

Truly independent web browser

LibWeb: Count the inline boxes around a top- or bottom-aligned box

sideshowbarker8fbac924cb9b
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index 9b83bf5f578..d0baab845a4 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -1217,7 +1217,25 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
)
};
self.line_mut(line_index).fragments[fragment_index].baseline = fragment_baseline;
- if has_line_relative_aligned_subtree && self.line_relative_aligned_subtree_root(style_source).is_some() {
+ if has_line_relative_aligned_subtree
+ && let Some((root, _)) = self.line_relative_aligned_subtree_root(style_source)
+ {
+ // NB: The inline boxes around the root of an aligned subtree are aligned like any other, and hold
+ // their own layout bounds around their baselines. Those inside an aligned subtree of their own
+ // don't take part in choosing the baseline of the line box.
+ let enclosing_shifts = self.vertical_shifts_of_enclosing_inline_boxes(root);
+ let mut ancestor_shift = enclosing_shifts
+ .iter()
+ .fold(CssPixels::default(), |total, (_, shift)| total + *shift);
+ for (ancestor, shift) in enclosing_shifts {
+ if self.line_relative_aligned_subtree_root(ancestor).is_none()
+ && self.inline_box_has_strut_on_line(text, ancestor)
+ {
+ let ascent = TextLayoutBounds::new(self.style(ancestor), None).ascent - ancestor_shift;
+ line_box_baseline = line_box_baseline.max(ascent);
+ }
+ ancestor_shift -= shift;
+ }
continue;
}
let alignment_style = self.alignment_style(style_source, style);
@@ -1998,6 +2016,47 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
});
ancestor = self.context().parent_node(ancestor);
}
+ // NB: The inline boxes around the root of an aligned subtree are aligned like any other, and hold their own
+ // layout bounds around their baselines. The walk above stops at that root, or skips them altogether
+ // for a root that isn't atomic, and a box it already recorded isn't recorded again.
+ if let Some((root, _)) = aligned_subtree {
+ let enclosing_shifts = self.vertical_shifts_of_enclosing_inline_boxes(root);
+ let mut ancestor_shift = enclosing_shifts
+ .iter()
+ .fold(CssPixels::default(), |total, (_, shift)| total + *shift);
+ for (ancestor, shift) in enclosing_shifts {
+ let ancestor_baseline = current_block_offset + line_box_baseline + ancestor_shift;
+ let inserted =
+ self.line_mut(line_index)
+ .set_inline_box_baseline(ancestor, ancestor_baseline, ancestor_shift);
+ if inserted && self.inline_box_has_strut_on_line(&text, ancestor) {
+ let bounds = TextLayoutBounds::new(self.style(ancestor), None);
+ let (box_start, box_end) =
+ (ancestor_baseline - bounds.ascent, ancestor_baseline + bounds.descent);
+ // NB: An inline box inside an aligned subtree of its own extends that subtree.
+ if let Some((outer_root, alignment)) = self.line_relative_aligned_subtree_root(ancestor) {
+ if let Some(subtree) =
+ aligned_subtrees.iter_mut().find(|subtree| subtree.root == outer_root)
+ {
+ subtree.block_start = subtree.block_start.min(box_start);
+ subtree.block_end = subtree.block_end.max(box_end);
+ } else {
+ aligned_subtrees.push(LineRelativeAlignedSubtree {
+ root: outer_root,
+ alignment,
+ block_start: box_start,
+ block_end: box_end,
+ shift: CssPixels::default(),
+ });
+ }
+ } else {
+ earliest = earliest.min(box_start);
+ latest = latest.max(box_end);
+ }
+ }
+ ancestor_shift -= shift;
+ }
+ }
let accumulated_vertical_shift = new_block_offset - baseline_aligned_block_offset;
{
let fragment = &mut self.line_mut(line_index).fragments[fragment_index];
diff --git a/Tests/LibWeb/Text/expected/css/inline-box-around-line-relative-aligned-box.txt b/Tests/LibWeb/Text/expected/css/inline-box-around-line-relative-aligned-box.txt
new file mode 100644
index 00000000000..327dee109cf
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/inline-box-around-line-relative-aligned-box.txt
@@ -0,0 +1,8 @@
+top-child-alone: 80 (30, -5)
+top-child-after-text: 80 (30, -5)
+bottom-child-alone: 80 (30, 65)
+top-child-in-shifted-box: 80 (30, -5)
+two-boxes-around-top-child: 80 (30, 30, -5)
+raised-box-inside-top-box: 125 (110, 10, 95)
+raised-box-around-bottom-box: 125 (10, 95)
+inline-block-in-top-box: 80 (30, -5, 1)
diff --git a/Tests/LibWeb/Text/input/css/inline-box-around-line-relative-aligned-box.html b/Tests/LibWeb/Text/input/css/inline-box-around-line-relative-aligned-box.html
new file mode 100644
index 00000000000..e1d54ad24ab
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/inline-box-around-line-relative-aligned-box.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<style>
+ @font-face { font-family: Ahem; src: url("../wpt-import/fonts/Ahem.ttf"); }
+ body { margin: 0; font: 20px/10px Ahem; }
+ .outer { line-height: 80px; }
+ .inner { line-height: 10px; }
+ .case { position: relative; }
+</style>
+<div class="case" id="top-child-alone"><span class="outer"><span class="inner" style="vertical-align: top">X</span></span></div>
+<div class="case" id="top-child-after-text">a<span class="outer"><span class="inner" style="vertical-align: top">X</span></span></div>
+<div class="case" id="bottom-child-alone"><span class="outer"><span class="inner" style="vertical-align: bottom">X</span></span></div>
+<div class="case" id="top-child-in-shifted-box"><span class="outer" style="vertical-align: 10px"><span class="inner" style="vertical-align: top">X</span></span></div>
+<div class="case" id="two-boxes-around-top-child"><span class="outer"><span style="line-height: 40px"><span class="inner" style="vertical-align: top">X</span></span></span></div>
+<div class="case" id="raised-box-inside-top-box"><span style="vertical-align: top">A<span style="vertical-align: 100px; line-height: 40px"><span style="vertical-align: bottom">B</span></span></span></div>
+<div class="case" id="raised-box-around-bottom-box">x<span style="vertical-align: 100px; line-height: 40px"><span style="vertical-align: bottom">B</span></span></div>
+<div class="case" id="inline-block-in-top-box"><span class="outer"><span class="inner" style="vertical-align: top"><span style="display: inline-block; width: 10px; height: 10px"></span></span></span></div>
+<script src="../include.js"></script>
+<script>
+ promiseTest(async () => {
+ await document.fonts.ready;
+ for (const element of document.querySelectorAll(".case")) {
+ const rect = element.getBoundingClientRect();
+ const offsets = [...element.querySelectorAll("span")].map(span => span.getBoundingClientRect().top - rect.top);
+ println(`${element.id}: ${rect.height} (${offsets.join(", ")})`);
+ }
+ });
+</script>