Truly independent web browser
LibWeb: Count the line-height of inline boxes with no text of their own
sideshowbarker2d2a0886e65a
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rsindex 19e19d7bf16..676bffe8bc3 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -54,7 +54,7 @@ struct LineAlignment {earliest: CssPixels,latest: CssPixels,line_box_baseline: CssPixels,- should_align_strut_to_line_box_baseline: bool,+ should_advance_to_line_box_block_end: bool,has_line_relative_aligned_subtree: bool,aligned_subtrees: Vec<LineRelativeAlignedSubtree>,first_unshifted_text_baseline: Option<CssPixels>,@@ -1020,6 +1020,31 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {}line_box_baseline = adjusted_baseline;}+ // https://drafts.csswg.org/css2/#line-height+ // The inline-level boxes are aligned vertically according to their 'vertical-align' property.+ // NB: An inline box around the fragment that sits on the baseline holds its own layout bounds above it, which+ // a box whose content is only other inline boxes has no text fragment of its own to bring in.+ let containing_block = self.context().containing_block;+ let mut ancestor = if style_source == containing_block {+ NodeSlotId::INVALID+ } else {+ self.context().parent_node(style_source)+ };+ while !ancestor.is_invalid() && ancestor != containing_block && self.facts(ancestor).is_fragmented_inline()+ {+ let ancestor_style = self.style(ancestor);+ if !ancestor_style.vertical_align_is_keyword()+ || ancestor_style.vertical_align_keyword() != vertical_align::BASELINE+ {+ break;+ }+ let ascent = TextLayoutBounds::new(ancestor_style, None).ascent;+ if ascent > line_box_baseline {+ should_align_strut_to_line_box_baseline = true;+ line_box_baseline = ascent;+ }+ ancestor = self.context().parent_node(ancestor);+ }}if let Some(marker_line) = self.outside_marker_line(line_index)&& marker_line.ascent > line_box_baseline@@ -1123,7 +1148,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {earliest,latest,line_box_baseline,- should_align_strut_to_line_box_baseline,+ should_advance_to_line_box_block_end,has_line_relative_aligned_subtree,mut aligned_subtrees,first_unshifted_text_baseline,@@ -1202,7 +1227,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {line.block_end = current_block_offset + line.block_length;// The painted extent can differ from the advance used by begin_new_line.// Retain that exact advance so a reused prefix resumes at the same position.- line.next_line_block_offset = if should_align_strut_to_line_box_baseline {+ line.next_line_block_offset = if should_advance_to_line_box_block_end {line.block_end} else {current_block_offset + self.max_block_size_on_current_line.max(containing_style.line_height())@@ -1212,7 +1237,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {line.baseline =first_unshifted_text_baseline.unwrap_or(line_box_baseline + block_offset) + normal_subtree_shift;}- self.should_advance_to_last_line_box_block_end = should_align_strut_to_line_box_baseline;+ self.should_advance_to_last_line_box_block_end = should_advance_to_line_box_block_end;}/// Aligns the fragments of the line at `line_index` within its line box, and finds the extent of that line box.@@ -1231,6 +1256,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {.any(|fragment| self.line_relative_aligned_subtree_root(fragment.style_source).is_some());let (line_box_baseline, should_align_strut_to_line_box_baseline) =self.line_box_baseline(line_index, has_line_relative_aligned_subtree);+ let mut should_advance_to_line_box_block_end = should_align_strut_to_line_box_baseline;let strut_start = self.current_block_offset;let strut_end = if should_align_strut_to_line_box_baseline {@@ -1312,6 +1338,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {line_box_baseline,)};+ let own_vertical_shift = new_block_offset - baseline_aligned_block_offset;if snapshot.style_source != containing_block && self.facts(snapshot.style_source).is_fragmented_inline() {inline_box_alignments.push(InlineBoxAlignment {box_: snapshot.style_source,@@ -1330,6 +1357,10 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {{let ancestor_style = self.style(ancestor);if line_relative_alignment(ancestor_style).is_some() {+ inline_box_alignments.push(InlineBoxAlignment {+ box_: ancestor,+ vertical_shift: CssPixels::default(),+ });break;}if ancestor_style.vertical_align_is_keyword()@@ -1430,24 +1461,58 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {if first_unshifted_text_baseline.is_none() && fragment_is_unshifted {first_unshifted_text_baseline = Some(fragment_baseline - current_block_offset);}+ }+ let inline_box_layout_baseline = text_fragment_baseline.or_else(|| {+ snapshot.is_atomic_inline.then_some(+ current_block_offset + line_box_baseline + accumulated_vertical_shift - own_vertical_shift,+ )+ });+ if let Some(mut inline_box_baseline) = inline_box_layout_baseline {// https://drafts.csswg.org/css-text-decor-4/#text-line-constancy// UAs must adjust line positions to match the shifted metrics of decorating boxes shifted with// vertical-align values other than baseline [CSS2] or subscripted/superscripted via// font-variant-position [CSS-FONTS-3], but must not adjust the line position or thickness in// response to descendants of a decorating box that are so styled.- let mut inline_box_baseline = fragment_baseline;let mut remaining_vertical_shift = accumulated_vertical_shift;for alignment in &inline_box_alignments {- let inserted = self.line_mut(line_index).set_inline_box_baseline(- alignment.box_,- inline_box_baseline,- remaining_vertical_shift,+ if is_text_node {+ let inserted = self.line_mut(line_index).set_inline_box_baseline(+ alignment.box_,+ inline_box_baseline,+ remaining_vertical_shift,+ );+ // An already recorded box implies its ancestors are recorded as well, since the+ // walk that recorded it continued through them.+ if !inserted {+ break;+ }+ }+ // https://drafts.csswg.org/css2/#line-height+ // The height of each inline-level box in the line box is calculated. For replaced elements,+ // inline-block elements, and inline-table elements, this is the height of their margin box; for+ // inline boxes, this is their 'line-height'.+ // NB: An inline box without direct text contributes its layout bounds around its own baseline,+ // independently of the size and vertical alignment of its descendants.+ let bounds = TextLayoutBounds::new(self.style(alignment.box_), None);+ let (box_start, box_end) = (+ inline_box_baseline - bounds.ascent,+ inline_box_baseline + bounds.descent,);- // An already recorded box implies its ancestors are recorded as well, since the- // walk that recorded it continued through them.- if !inserted {- break;+ if let Some((root, _)) = aligned_subtree {+ if let Some(subtree) = aligned_subtrees.iter_mut().find(|subtree| subtree.root == root) {+ should_advance_to_line_box_block_end |=+ box_start < subtree.block_start || box_end > subtree.block_end;+ subtree.block_start = subtree.block_start.min(box_start);+ subtree.block_end = subtree.block_end.max(box_end);+ }+ } else {+ should_advance_to_line_box_block_end |= box_start < current_block_offset+ || box_end+ > current_block_offset+ + self.max_block_size_on_current_line.max(containing_style.line_height());+ earliest = earliest.min(box_start);+ latest = latest.max(box_end);}inline_box_baseline -= alignment.vertical_shift;remaining_vertical_shift -= alignment.vertical_shift;@@ -1471,7 +1536,7 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {earliest,latest,line_box_baseline,- should_align_strut_to_line_box_baseline,+ should_advance_to_line_box_block_end,has_line_relative_aligned_subtree,aligned_subtrees,first_unshifted_text_baseline,diff --git a/Tests/LibWeb/Layout/expected/content-image-simple.txt b/Tests/LibWeb/Layout/expected/content-image-simple.txtindex 39321ea9d89..974b3c9fa50 100644--- a/Tests/LibWeb/Layout/expected/content-image-simple.txt+++ b/Tests/LibWeb/Layout/expected/content-image-simple.txt@@ -1,24 +1,24 @@-Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline overflow: [0,0 800x1216]- BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 1216 0+0+0] [BFC] children: not-inline- BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 1200 0+0+8] children: not-inline- BlockContainer <div> at [8,8] [0+0+0 784 0+0+0] [0+0+0 1200 0+0+0] children: inline- frag 0 from ImageBox start: 0, length: 0, rect: [8,808 400x400] baseline: 400- InlineNode <(anonymous)> at [8,395] [0+0+0 454.796875 0+0+0] [0+0+0 413 0+0+0]+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline overflow: [0,0 800x1222]+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 1222 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 1206 0+0+8] children: not-inline+ BlockContainer <div> at [8,8] [0+0+0 784 0+0+0] [0+0+0 1206 0+0+0] children: inline+ frag 0 from ImageBox start: 0, length: 0, rect: [8,814 400x400] baseline: 400+ InlineNode <(anonymous)> at [8,395] [0+0+0 454.796875 0+0+0] [0+0+0 416 0+0+0]frag 0 from GeneratedTextNode start: 0, length: 3, rect: [8,395 27.15625x16] baseline: 13"foo"frag 1 from ImageBox start: 0, length: 0, rect: [35.15625,8 400x400] baseline: 400frag 2 from GeneratedTextNode start: 0, length: 3, rect: [435.15625,395 27.640625x16] baseline: 13"bar"- frag 0 from ImageBox start: 0, length: 0, rect: [8,408 400x400] baseline: 400- frag 1 from GeneratedTextNode start: 0, length: 3, rect: [408,795 27.203125x16] baseline: 13+ frag 0 from ImageBox start: 0, length: 0, rect: [8,411 400x400] baseline: 400+ frag 1 from GeneratedTextNode start: 0, length: 3, rect: [408,798 27.203125x16] baseline: 13"baz"GeneratedTextNode <(anonymous)> (not painted)ImageBox <(anonymous)> at [35.15625,8] [0+0+0 400 0+0+0] [0+0+0 400 0+0+0] children: not-inlineGeneratedTextNode <(anonymous)> (not painted)- ImageBox <(anonymous)> at [8,408] [0+0+0 400 0+0+0] [0+0+0 400 0+0+0] children: not-inline+ ImageBox <(anonymous)> at [8,411] [0+0+0 400 0+0+0] [0+0+0 400 0+0+0] children: not-inlineGeneratedTextNode <(anonymous)> (not painted)- ImageBox <img> at [8,808] [0+0+0 400 0+0+0] [0+0+0 400 0+0+0] children: not-inline+ ImageBox <img> at [8,814] [0+0+0 400 0+0+0] [0+0+0 400 0+0+0] children: not-inlineTextNode <#text> (not painted)SC for Viewport<#document> [0,0 800x600] (z-index: auto)- SC for BlockContainer<HTML> [0,0 800x1216] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x1222] (z-index: auto)diff --git a/Tests/LibWeb/Layout/expected/display-table-inline-children.txt b/Tests/LibWeb/Layout/expected/display-table-inline-children.txtindex e82ef36ee8f..01411ef23c6 100644--- a/Tests/LibWeb/Layout/expected/display-table-inline-children.txt+++ b/Tests/LibWeb/Layout/expected/display-table-inline-children.txt@@ -1,10 +1,10 @@-Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline overflow: [0,0 1208x616]- BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 616 0+0+0] [BFC] children: not-inline- BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 600 0+0+8] children: not-inline- TableWrapper <(anonymous)> at [8,8] [0+0+0 1200 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline- Box <div.aligncenter.block-image> at [8,8] table-box [0+0+0 1200 0+0+0] [0+0+0 600 0+0+0] [TFC] children: not-inline- Box <(anonymous)> at [8,8] table-row [0+0+0 1200 0+0+0] [0+0+0 600 0+0+0] children: not-inline- BlockContainer <(anonymous)> at [8,8] table-cell [0+0+0 1200 0+0+0] [0+0+0 600 0+0+0] [BFC] children: inline+Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] children: not-inline overflow: [0,0 1208x619]+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 619 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 603 0+0+8] children: not-inline+ TableWrapper <(anonymous)> at [8,8] [0+0+0 1200 0+0+0] [0+0+0 603 0+0+0] [BFC] children: not-inline+ Box <div.aligncenter.block-image> at [8,8] table-box [0+0+0 1200 0+0+0] [0+0+0 603 0+0+0] [TFC] children: not-inline+ Box <(anonymous)> at [8,8] table-row [0+0+0 1200 0+0+0] [0+0+0 603 0+0+0] children: not-inline+ BlockContainer <(anonymous)> at [8,8] table-cell [0+0+0 1200 0+0+0] [0+0+0 603 0+0+0] [BFC] children: inlineInlineNode <a> at [8,8] [0+0+0 1200 0+0+0] [0+0+0 600 0+0+0]frag 0 from ImageBox start: 0, length: 0, rect: [8,8 1200x600] baseline: 600TextNode <#text> (not painted)@@ -13,4 +13,4 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenTextNode <#text> (not painted)SC for Viewport<#document> [0,0 800x600] (z-index: auto)- SC for BlockContainer<HTML> [0,0 800x616] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x619] (z-index: auto)diff --git a/Tests/LibWeb/Layout/expected/images-replaced-after-first-layout.txt b/Tests/LibWeb/Layout/expected/images-replaced-after-first-layout.txtindex a3d3fab4fec..bc2bf14aed6 100644--- a/Tests/LibWeb/Layout/expected/images-replaced-after-first-layout.txt+++ b/Tests/LibWeb/Layout/expected/images-replaced-after-first-layout.txt@@ -1,11 +1,11 @@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 536 0+0+0] [BFC] children: not-inline- BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 520 0+0+8] children: not-inline+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 539 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 523 0+0+8] children: not-inlineBlockContainer <(anonymous)> at [8,8] [0+0+0 784 0+0+0] [0+0+0 120 0+0+0] children: inlinefrag 0 from ImageBox start: 0, length: 0, rect: [8,8 120x120] baseline: 120ImageBox <img#image> at [8,8] [0+0+0 120 0+0+0] [0+0+0 120 0+0+0] children: not-inlineTextNode <#text> (not painted)- BlockContainer <div#generated.swapped> at [8,128] [0+0+0 784 0+0+0] [0+0+0 400 0+0+0] children: inline+ BlockContainer <div#generated.swapped> at [8,128] [0+0+0 784 0+0+0] [0+0+0 403 0+0+0] children: inlineInlineNode <(anonymous)> at [8,408] [0+0+0 520 0+0+0] [0+0+0 400 0+0+0]frag 0 from ImageBox start: 0, length: 0, rect: [8,408 120x120] baseline: 120frag 1 from ImageBox start: 0, length: 0, rect: [128,128 400x400] baseline: 400@@ -13,4 +13,4 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenImageBox <(anonymous)> at [128,128] [0+0+0 400 0+0+0] [0+0+0 400 0+0+0] children: not-inlineSC for Viewport<#document> [0,0 800x600] (z-index: auto)- SC for BlockContainer<HTML> [0,0 800x536] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x539] (z-index: auto)diff --git a/Tests/LibWeb/Layout/expected/picture-source-media-query.txt b/Tests/LibWeb/Layout/expected/picture-source-media-query.txtindex 5ff38b0cdd5..db1c84353ed 100644--- a/Tests/LibWeb/Layout/expected/picture-source-media-query.txt+++ b/Tests/LibWeb/Layout/expected/picture-source-media-query.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 416 0+0+0] [BFC] children: not-inline- BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 400 0+0+8] children: inline+ BlockContainer <html> at [0,0] [0+0+0 800 0+0+0] [0+0+0 419 0+0+0] [BFC] children: not-inline+ BlockContainer <body> at [8,8] [8+0+0 784 0+0+8] [8+0+0 403 0+0+8] children: inlineInlineNode <picture> at [8,8] [0+0+0 400 0+0+0] [0+0+0 400 0+0+0]frag 0 from ImageBox start: 0, length: 0, rect: [8,8 400x400] baseline: 400TextNode <#text> (not painted)@@ -11,4 +11,4 @@ Viewport <#document> at [0,0] [0+0+0 800 0+0+0] [0+0+0 600 0+0+0] [BFC] childrenTextNode <#text> (not painted)SC for Viewport<#document> [0,0 800x600] (z-index: auto)- SC for BlockContainer<HTML> [0,0 800x416] (z-index: auto)+ SC for BlockContainer<HTML> [0,0 800x419] (z-index: auto)diff --git a/Tests/LibWeb/Text/expected/css/inline-box-line-height-preserves-strut.txt b/Tests/LibWeb/Text/expected/css/inline-box-line-height-preserves-strut.txtnew file mode 100644index 00000000000..55c14d7ed1f--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/inline-box-line-height-preserves-strut.txt@@ -0,0 +1,2 @@+single line: 130+two lines: 230diff --git a/Tests/LibWeb/Text/expected/css/inline-box-line-height-top-bottom.txt b/Tests/LibWeb/Text/expected/css/inline-box-line-height-top-bottom.txtnew file mode 100644index 00000000000..6ff48493c4d--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/inline-box-line-height-top-bottom.txt@@ -0,0 +1,8 @@+top direct: height=124, next=100+top nested: height=124, next=100+top atomic: height=124, next=100+top shifted: height=146, next=122+bottom direct: height=124, next=100+bottom nested: height=124, next=100+bottom atomic: height=124, next=100+bottom shifted: height=146, next=122diff --git a/Tests/LibWeb/Text/expected/css/inline-box-line-height-with-only-inline-content.txt b/Tests/LibWeb/Text/expected/css/inline-box-line-height-with-only-inline-content.txtnew file mode 100644index 00000000000..e757dc23690--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/inline-box-line-height-with-only-inline-content.txt@@ -0,0 +1,20 @@+pseudo: 50+nested: 50+own-text: 50+nested text sits where own text does: true+own text is below the line's top: true+nested-break: 74+break: second lines align: true+nested-wrap: 74+wrap: second lines align: true+atomic: 50+atomic-break: 74+atomic: second lines align: true+later text change matches fresh layout: true+four lines after text change: 122+atomic baseline: height matches with text: true+atomic baseline: position matches with text: true+atomic 10px: height matches with text: true+atomic 10px: position matches with text: true+atomic -10px: height matches with text: true+atomic -10px: position matches with text: truediff --git a/Tests/LibWeb/Text/input/css/inline-box-line-height-preserves-strut.html b/Tests/LibWeb/Text/input/css/inline-box-line-height-preserves-strut.htmlnew file mode 100644index 00000000000..f2b201ee7f9--- /dev/null+++ b/Tests/LibWeb/Text/input/css/inline-box-line-height-preserves-strut.html@@ -0,0 +1,20 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ @font-face {+ font-family: Ahem;+ src: url("../../../Ref/data/fonts/Ahem.ttf");+ }+ .line { font: 100px/100px Ahem; }+ .outer { font-size: 200px; line-height: 100px; }+ .inner { font-size: 10px; line-height: 10px; }+</style>+<div class="line" id="single"><span class="outer"><span class="inner">A</span></span></div>+<div class="line" id="multiple"><span class="outer"><span class="inner">A</span></span><br>B</div>+<script>+ promiseTest(async () => {+ await document.fonts.ready;+ println(`single line: ${document.getElementById("single").getBoundingClientRect().height}`);+ println(`two lines: ${document.getElementById("multiple").getBoundingClientRect().height}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/css/inline-box-line-height-top-bottom.html b/Tests/LibWeb/Text/input/css/inline-box-line-height-top-bottom.htmlnew file mode 100644index 00000000000..3892df224d8--- /dev/null+++ b/Tests/LibWeb/Text/input/css/inline-box-line-height-top-bottom.html@@ -0,0 +1,45 @@+<!doctype html>+<script src="../include.js"></script>+<style>+ @font-face {+ font-family: Ahem;+ src: url("../../../Ref/data/fonts/Ahem.ttf");+ }+ .line {+ display: flow-root;+ width: 200px;+ font: 24px/24px Ahem;+ }+ .outer {+ line-height: 100px;+ }+ .inner {+ line-height: 24px;+ }+ .atomic {+ display: inline-block;+ width: 24px;+ height: 24px;+ }+</style>+<script>+ promiseTest(async () => {+ await document.fonts.load("24px Ahem");+ for (const alignment of ["top", "bottom"]) {+ for (const [name, content] of [+ ["direct", "AA"],+ ["nested", '<span class="inner">AA</span>'],+ ["atomic", '<span class="atomic"></span>'],+ ["shifted", '<span class="inner" style="vertical-align: 60px">AA</span>'],+ ]) {+ const line = document.createElement("div");+ line.className = "line";+ line.innerHTML = `<span class="outer" style="vertical-align: ${alignment}">${content}</span><br><span class="next">B</span>`;+ document.body.appendChild(line);+ const rect = line.getBoundingClientRect();+ const next = line.querySelector(".next").getBoundingClientRect();+ println(`${alignment} ${name}: height=${rect.height}, next=${next.top - rect.top}`);+ }+ }+ });+</script>diff --git a/Tests/LibWeb/Text/input/css/inline-box-line-height-with-only-inline-content.html b/Tests/LibWeb/Text/input/css/inline-box-line-height-with-only-inline-content.htmlnew file mode 100644index 00000000000..bb16bf1cffd--- /dev/null+++ b/Tests/LibWeb/Text/input/css/inline-box-line-height-with-only-inline-content.html@@ -0,0 +1,83 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ .line {+ width: 100px;+ font: 24px/1 serif;+ }+ .tall {+ line-height: 50px;+ }+ .tall::before {+ content: "A";+ line-height: 1;+ }+ .inner {+ line-height: 1;+ }+ .atomic {+ display: inline-block;+ width: 10px;+ height: 10px;+ }+</style>+<div class="line" id="pseudo"><i class="tall"></i></div>+<div class="line" id="nested"><b style="line-height: 50px"><span class="inner">A</span></b></div>+<div class="line" id="own-text"><i style="line-height: 50px">A</i></div>+<div class="line" id="nested-break"><span style="line-height: 50px"><span class="inner">A</span></span><br><span>B</span></div>+<div class="line" id="own-text-break"><span style="line-height: 50px">A</span><br><span>B</span></div>+<div class="line" id="nested-wrap" style="width: 24px"><span style="line-height: 50px"><span class="inner">A</span></span> <span>B</span></div>+<div class="line" id="own-text-wrap" style="width: 24px"><span style="line-height: 50px">A</span> <span>B</span></div>+<div class="line" id="atomic"><span style="line-height: 50px"><span class="atomic"></span></span></div>+<div class="line" id="atomic-break"><span style="line-height: 50px"><span class="atomic"></span></span><br><span>B</span></div>+<script>+ test(() => {+ const glyphTop = line => {+ const range = document.createRange();+ range.selectNodeContents(line.querySelector("span, i"));+ return range.getBoundingClientRect().top - line.getBoundingClientRect().top;+ };+ for (const id of ["pseudo", "nested", "own-text"])+ println(`${id}: ${document.getElementById(id).getBoundingClientRect().height}`);+ // Text in an inner box sits where text the outer box holds directly does.+ const ownText = glyphTop(document.getElementById("own-text"));+ println(`nested text sits where own text does: ${glyphTop(document.getElementById("nested")) === ownText}`);+ println(`own text is below the line's top: ${ownText > 0}`);+ const secondLineTop = line =>+ line.lastElementChild.getBoundingClientRect().top - line.getBoundingClientRect().top;+ for (const ending of ["break", "wrap"]) {+ const nested = document.getElementById(`nested-${ending}`);+ const ownText = document.getElementById(`own-text-${ending}`);+ println(`nested-${ending}: ${nested.getBoundingClientRect().height}`);+ println(`${ending}: second lines align: ${secondLineTop(nested) === secondLineTop(ownText)}`);+ }+ for (const id of ["atomic", "atomic-break"])+ println(`${id}: ${document.getElementById(id).getBoundingClientRect().height}`);+ println(`atomic: second lines align: ${secondLineTop(document.getElementById("atomic-break")) === secondLineTop(document.getElementById("own-text-break"))}`);+ const changing = document.createElement("div");+ changing.className = "line";+ changing.innerHTML = '<span style="line-height: 50px"><span class="inner">A</span></span><br>B<br>C<br><span>D</span>';+ document.body.appendChild(changing);+ changing.getBoundingClientRect();+ changing.lastElementChild.firstChild.data = "E";+ const reusedTop = secondLineTop(changing);+ changing.style.width = "101px";+ println(`later text change matches fresh layout: ${secondLineTop(changing) === reusedTop}`);+ println(`four lines after text change: ${changing.getBoundingClientRect().height}`);+ for (const alignment of ["baseline", "10px", "-10px"]) {+ const makeLine = text => {+ const line = document.createElement("div");+ line.className = "line";+ line.innerHTML = `<span style="line-height: 50px">${text}<span class="atomic" style="padding: 3px; border: 2px solid; margin: 4px; vertical-align: ${alignment}"></span></span><br>B`;+ document.body.appendChild(line);+ return line;+ };+ const atomicOnly = makeLine("");+ const withText = makeLine("A");+ const atomicTop = line =>+ line.querySelector(".atomic").getBoundingClientRect().top - line.getBoundingClientRect().top;+ println(`atomic ${alignment}: height matches with text: ${atomicOnly.getBoundingClientRect().height === withText.getBoundingClientRect().height}`);+ println(`atomic ${alignment}: position matches with text: ${atomicTop(atomicOnly) === atomicTop(withText)}`);+ }+ });+</script>