Skip to content

Truly independent web browser

LibWeb: Align middle, sub, and super against the parent inline box

sideshowbarker2df415804642
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index bab1de801de..b564fbb691b 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -991,12 +991,13 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
if !style.vertical_align_is_keyword() {
return alphabetic - style.vertical_align_value().to_px(metrics.line_height);
}
- // FIXME: middle, sub and super are defined against the parent inline box's font metrics too, but we use the
- // block container's instead.
match style.vertical_align_keyword() {
vertical_align::BASELINE => alphabetic,
vertical_align::MIDDLE => {
- let x_height = CssPixels::nearest_value_for_f32(self.containing_style().font_x_height());
+ // https://drafts.csswg.org/css2/#valdef-vertical-align-middle
+ // Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of
+ // the parent.
+ let x_height = CssPixels::nearest_value_for_f32(parent_style.font_x_height());
self.current_block_offset
+ line_box_baseline
+ (metrics.effective_box_block_start_offset
@@ -1013,8 +1014,8 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
- metrics.block_size)
/ 2
}
- vertical_align::SUB => alphabetic + self.containing_style().font_size() / 5,
- vertical_align::SUPER => alphabetic - self.containing_style().font_size() / 3,
+ vertical_align::SUB => alphabetic + parent_style.font_size() / 5,
+ vertical_align::SUPER => alphabetic - parent_style.font_size() / 3,
// A top- or bottom-aligned box is the root of an aligned subtree, laid out on the baseline here and
// shifted into place once the final line box size is known. These arms therefore only see boxes that
// form no aligned subtree: the containing block itself, whose alignment does not apply to the line box
diff --git a/Tests/LibWeb/Text/expected/css/vertical-align-against-parent-inline-box.txt b/Tests/LibWeb/Text/expected/css/vertical-align-against-parent-inline-box.txt
new file mode 100644
index 00000000000..a0819ea457f
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/vertical-align-against-parent-inline-box.txt
@@ -0,0 +1,6 @@
+middle: 60 (18, 5)
+middle-small: 30 (0, 7)
+middle-inline-block: 40 (0, 11)
+middle-in-smaller-parent: 60 (26, 5)
+super: 30 (0, 6)
+sub: 32 (0, 22)
diff --git a/Tests/LibWeb/Text/input/css/vertical-align-against-parent-inline-box.html b/Tests/LibWeb/Text/input/css/vertical-align-against-parent-inline-box.html
new file mode 100644
index 00000000000..e37159c7210
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/vertical-align-against-parent-inline-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/20px Ahem; }
+ .parent { font-size: 30px; line-height: 30px; }
+ .small { font-size: 10px; line-height: 10px; }
+ .box { font-size: 50px; line-height: 60px; }
+ .ibs { display: inline-block; width: 10px; height: 10px; }
+ .case { position: relative; }
+</style>
+<div class="case" id="middle"><span class="parent">a<span class="box" style="vertical-align: middle">y</span></span>x</div>
+<div class="case" id="middle-small"><span class="parent">a<span class="small" style="vertical-align: middle">y</span></span>x</div>
+<div class="case" id="middle-inline-block"><span style="font-size: 40px; line-height: 40px">a<span class="ibs" style="vertical-align: middle"></span></span>x</div>
+<div class="case" id="middle-in-smaller-parent"><span class="small">a<span class="box" style="vertical-align: middle">y</span></span>x</div>
+<div class="case" id="super"><span class="parent">a<span class="small" style="vertical-align: super">y</span></span>x</div>
+<div class="case" id="sub"><span class="parent">a<span class="small" style="vertical-align: sub">y</span></span>x</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>