Skip to content

Truly independent web browser

LibWeb: Make room in the line box for super and sub shifted boxes

Andreas Kling4060c360312a
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index 2c5e90cb296..fcb50893881 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -1064,14 +1064,26 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
continue;
}
let alignment_style = self.alignment_style(style_source, style);
- let adjusted_baseline = if alignment_style.vertical_align_is_keyword() {
- fragment_baseline
- } else {
+ // NB: The box reaches as far above the baseline of the line box as it is shifted up from it (see
+ // block_offset_for_alignment).
+ let shifted_by_keyword = alignment_style.vertical_align_is_keyword()
+ && matches!(
+ alignment_style.vertical_align_keyword(),
+ vertical_align::SUPER | vertical_align::SUB
+ );
+ let adjusted_baseline = if !alignment_style.vertical_align_is_keyword() {
fragment_baseline + alignment_style.vertical_align_value().to_px(style.line_height())
+ } else if alignment_style.vertical_align_keyword() == vertical_align::SUPER {
+ fragment_baseline + self.containing_style().font_size() / 3
+ } else if alignment_style.vertical_align_keyword() == vertical_align::SUB {
+ fragment_baseline - self.containing_style().font_size() / 5
+ } else {
+ fragment_baseline
};
- // NB: A vertical-align length or percentage shifts the box relative to the baseline, which leaves the
- // strut on the baseline, whichever box ends up setting the baseline of the line box.
- if !alignment_style.vertical_align_is_keyword() {
+ // NB: A vertical-align length or percentage, like 'sub' and 'super', shifts the box relative to the
+ // baseline, which leaves the strut on the baseline, whichever box ends up setting the baseline of the
+ // line box.
+ if !alignment_style.vertical_align_is_keyword() || shifted_by_keyword {
should_align_strut_to_line_box_baseline = true;
}
if adjusted_baseline > line_box_baseline {
diff --git a/Tests/LibWeb/Layout/expected/float-after-short-inline-block-goes-below-line-box.txt b/Tests/LibWeb/Layout/expected/float-after-short-inline-block-goes-below-line-box.txt
index 103c5d96e9e..312da2698b9 100644
--- a/Tests/LibWeb/Layout/expected/float-after-short-inline-block-goes-below-line-box.txt
+++ b/Tests/LibWeb/Layout/expected/float-after-short-inline-block-goes-below-line-box.txt
@@ -1,6 +1,6 @@
Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline
- BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 386 0+0+0] [BFC] children: not-inline
- BlockContainer <body> at [0,0] [0+0+0 400 0+0+400] [0+0+0 386 0+0+0] children: not-inline
+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 385.328125 0+0+0] [BFC] children: not-inline
+ BlockContainer <body> at [0,0] [0+0+0 400 0+0+400] [0+0+0 385.328125 0+0+0] children: not-inline
BlockContainer <div> at [0,0] [0+0+0 400 0+0+0] [0+0+0 26 0+0+0] [BFC] children: inline
frag 0 from BlockContainer start: 0, length: 0, rect: [20,12 200x1] baseline: 1
BlockContainer <div.short> at [20,12] inline-block [20+0+0 200 0+0+0] [0+0+0 1 0+0+0] [BFC] children: not-inline
@@ -32,46 +32,46 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children
BlockContainer <div.short.tall.narrow.text-top> at [61.671875,166] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
BlockContainer <div.short.tall.narrow.text-bottom> at [131.671875,152] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
BlockContainer <div.wide> at [0,196] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [0,206] [0+0+0 400 0+0+0] [0+0+0 49 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 5, rect: [0,223 41.671875x16] baseline: 13
+ BlockContainer <div> at [0,206] [0+0+0 400 0+0+0] [0+0+0 48.328125 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [0,228 41.671875x16] baseline: 13
"gjpqy"
- frag 1 from BlockContainer start: 0, length: 0, rect: [61.671875,200 50x30] baseline: 30
- frag 2 from BlockContainer start: 0, length: 0, rect: [131.671875,209 50x30] baseline: 30
+ frag 1 from BlockContainer start: 0, length: 0, rect: [61.671875,206 50x30] baseline: 30
+ frag 2 from BlockContainer start: 0, length: 0, rect: [131.671875,214 50x30] baseline: 30
TextNode <#text> (not painted)
- BlockContainer <div.short.tall.narrow.super> at [61.671875,200] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
- BlockContainer <div.short.tall.narrow.sub> at [131.671875,209] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
- BlockContainer <div.wide> at [0,245] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [0,255] [0+0+0 400 0+0+0] [0+0+0 40 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 5, rect: [0,261 41.671875x16] baseline: 13
+ BlockContainer <div.short.tall.narrow.super> at [61.671875,206] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.short.tall.narrow.sub> at [131.671875,214] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.wide> at [0,244.328125] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [0,254.328125] [0+0+0 400 0+0+0] [0+0+0 40 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 5, rect: [0,260.328125 41.671875x16] baseline: 13
"gjpqy"
- frag 1 from BlockContainer start: 0, length: 0, rect: [61.671875,255 50x30] baseline: 0
- frag 2 from BlockContainer start: 0, length: 0, rect: [131.671875,255 50x30] baseline: 19.296875
+ frag 1 from BlockContainer start: 0, length: 0, rect: [61.671875,254.328125 50x30] baseline: 0
+ frag 2 from BlockContainer start: 0, length: 0, rect: [131.671875,254.328125 50x30] baseline: 19.296875
TextNode <#text> (not painted)
- BlockContainer <div.short.tall.narrow.top> at [61.671875,255] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
- BlockContainer <div.short.tall.narrow.middle> at [131.671875,255] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
- BlockContainer <div.wide> at [0,285] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [0,295] [0+0+0 400 0+0+0] [0+0+0 41 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 15, rect: [0,310 125.015625x16] baseline: 13
+ BlockContainer <div.short.tall.narrow.top> at [61.671875,254.328125] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.short.tall.narrow.middle> at [131.671875,254.328125] inline-block [20+0+0 50 0+0+0] [0+0+0 30 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.wide> at [0,284.328125] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [0,294.328125] [0+0+0 400 0+0+0] [0+0+0 41 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 15, rect: [0,309.328125 125.015625x16] baseline: 13
"gjpqygjpqygjpqy"
TextNode <#text> (not painted)
- InlineNode <span.raised> at [125.015625,295] [0+0+0 41.671875 0+0+0] [0+0+0 16 0+0+0]
- frag 0 from TextNode start: 0, length: 5, rect: [125.015625,295 41.671875x16] baseline: 13
+ InlineNode <span.raised> at [125.015625,294.328125] [0+0+0 41.671875 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from TextNode start: 0, length: 5, rect: [125.015625,294.328125 41.671875x16] baseline: 13
"gjpqy"
TextNode <#text> (not painted)
- BlockContainer <div.wide> at [0,326] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
- BlockContainer <div> at [0,336] [0+0+0 400 0+0+0] [0+0+0 50 0+0+0] [BFC] children: inline
- frag 0 from TextNode start: 0, length: 10, rect: [0,355 83.34375x16] baseline: 13
+ BlockContainer <div.wide> at [0,325.328125] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
+ BlockContainer <div> at [0,335.328125] [0+0+0 400 0+0+0] [0+0+0 50 0+0+0] [BFC] children: inline
+ frag 0 from TextNode start: 0, length: 10, rect: [0,354.328125 83.34375x16] baseline: 13
"gjpqygjpqy"
TextNode <#text> (not painted)
- InlineNode <span.big> at [83.34375,336] [0+0+0 28.90625 0+0+0] [0+0+0 40 0+0+0]
- frag 0 from TextNode start: 0, length: 1, rect: [83.34375,336 28.90625x40] baseline: 32
+ InlineNode <span.big> at [83.34375,335.328125] [0+0+0 28.90625 0+0+0] [0+0+0 40 0+0+0]
+ frag 0 from TextNode start: 0, length: 1, rect: [83.34375,335.328125 28.90625x40] baseline: 32
"X"
TextNode <#text> (not painted)
- InlineNode <span.sub> at [112.25,358] [0+0+0 41.671875 0+0+0] [0+0+0 16 0+0+0]
- frag 0 from TextNode start: 0, length: 5, rect: [112.25,358 41.671875x16] baseline: 13
+ InlineNode <span.sub> at [112.25,357.328125] [0+0+0 41.671875 0+0+0] [0+0+0 16 0+0+0]
+ frag 0 from TextNode start: 0, length: 5, rect: [112.25,357.328125 41.671875x16] baseline: 13
"gjpqy"
TextNode <#text> (not painted)
- BlockContainer <div.wide> at [0,376] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
+ BlockContainer <div.wide> at [0,375.328125] floating [0+0+0 300 0+0+0] [0+0+0 10 0+0+0] [BFC] children: not-inline
SC for Viewport<#document> [0,0 800x600] (z-index: auto)
- SC for BlockContainer<HTML> [0,0 800x386] (z-index: auto)
+ SC for BlockContainer<HTML> [0,0 800x385.328125] (z-index: auto)
diff --git a/Tests/LibWeb/Text/expected/vertical-align-super-and-sub-in-line-box.txt b/Tests/LibWeb/Text/expected/vertical-align-super-and-sub-in-line-box.txt
new file mode 100644
index 00000000000..9b7bc9ad7a5
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/vertical-align-super-and-sub-in-line-box.txt
@@ -0,0 +1,9 @@
+1: 0+60 0+50 45+10
+2: 0+60 45+10 0+50
+3: 0+60 0+50 45+10
+4: 0+60 45+10 0+50
+5: 0+65 0+50 50+10
+6: 0+52 0+50 36+10
+7: 0+60 0+50
+8: 0+35 14+10 0+30
+9: 0+35 23+10 0+30
diff --git a/Tests/LibWeb/Text/input/vertical-align-super-and-sub-in-line-box.html b/Tests/LibWeb/Text/input/vertical-align-super-and-sub-in-line-box.html
new file mode 100644
index 00000000000..503025d93f0
--- /dev/null
+++ b/Tests/LibWeb/Text/input/vertical-align-super-and-sub-in-line-box.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<style>
+body { margin: 0; font: 16px/20px SerenitySans; }
+.c { width: 300px; margin-bottom: 10px; }
+.r { display: inline-block; background: gray; }
+</style>
+<div id="cases">
+<div class="c"><img style="width:10px;height:50px;vertical-align:super" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"><img style="width:10px;height:10px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"></div>
+<div class="c"><img style="width:10px;height:10px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"><img style="width:10px;height:50px;vertical-align:super" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"></div>
+<div class="c"><span class="r" style="width:10px;height:50px;vertical-align:super"></span><span class="r" style="width:10px;height:10px"></span></div>
+<div class="c"><span class="r" style="width:10px;height:10px"></span><span class="r" style="width:10px;height:50px;vertical-align:super"></span></div>
+<div class="c"><img style="width:10px;height:50px;vertical-align:10px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"><img style="width:10px;height:10px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"></div>
+<div class="c"><img style="width:10px;height:50px;vertical-align:sub" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"><img style="width:10px;height:10px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"></div>
+<div class="c">text <img style="width:10px;height:50px;vertical-align:super" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"> more</div>
+<div class="c"><img style="width:10px;height:10px;vertical-align:super" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"><img style="width:10px;height:30px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"></div>
+<div class="c"><img style="width:10px;height:10px;vertical-align:sub" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"><img style="width:10px;height:30px" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E"></div>
+</div>
+<script src="include.js"></script>
+<script>
+promiseTest(async () => {
+ await Promise.all([...document.images].map(i => i.decode()));
+ document.querySelectorAll(".c").forEach((c, i) => {
+ const parts = [c, ...c.children].map(e => { const r = e.getBoundingClientRect(); return `${Math.round(r.top - c.getBoundingClientRect().top)}+${Math.round(r.height)}`; });
+ println(`${i + 1}: ${parts.join(" ")}`);
+ });
+ document.getElementById("cases").remove();
+});
+</script>