Truly independent web browser
LibWeb: Keep empty inline boxes in place under the line-height quirk
sideshowbarker1700bdde0aa8
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rsindex 528fb31c7c8..1922e970585 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs@@ -2696,9 +2696,6 @@ impl<'context> InlineFormattingContext<'context> {let mut items_after_clamp_point = Vec::new();let mut stopped_at_line_limit = false;let mut needs_float_fitting = !float_fit_trial;- // https://quirks.spec.whatwg.org/#the-line-height-calculation-quirk- let takes_struts_of_inline_boxes_without_text =- !self.facts(self.containing_block).document_in_line_height_quirks_mode();loop {if float_fit_trial && line_builder.float_refit().is_some() {break;@@ -2751,8 +2748,9 @@ impl<'context> InlineFormattingContext<'context> {}}// NB: The inline boxes around whitespace are on this line whether the whitespace stays there, collapses,- // or ends up hanging off the end of the line.- if item.is_collapsible_whitespace && takes_struts_of_inline_boxes_without_text {+ // or ends up hanging off the end of the line. Under the line-height quirk, the line builder takes+ // their line height away unless they have a border or padding at their start or end.+ if item.is_collapsible_whitespace {line_builder.add_struts_of_inline_boxes_around(item.node,pending_leading_edges + item.margin_start + item.border_start + item.padding_start,@@ -2896,9 +2894,7 @@ impl<'context> InlineFormattingContext<'context> {// https://drafts.csswg.org/css2/#anonymous-block-level// NB: The inline boxes around the block-level box are broken around it, so they're on the line// before it and on the line after it.- if takes_struts_of_inline_boxes_without_text {- line_builder.add_struts_of_inline_boxes_around(item.node, CssPixels::default());- }+ line_builder.add_struts_of_inline_boxes_around(item.node, CssPixels::default());line_builder.finish_current_line_before_block_level_box();if float_fit_trial {break;@@ -2928,9 +2924,7 @@ impl<'context> InlineFormattingContext<'context> {self.margins_collapse_through_interrupting_blocks.borrow_mut().insert(item.node, collapses_through);- if takes_struts_of_inline_boxes_without_text {- line_builder.add_struts_of_inline_boxes_around(item.node, CssPixels::default());- }+ line_builder.add_struts_of_inline_boxes_around(item.node, CssPixels::default());}inline_level_iterator::ItemType::AbsolutelyPositionedElement => {if !self.facts(item.node).is_box() {diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rsindex 9cedbebcdf9..aeebb5eb619 100644--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs@@ -2138,18 +2138,9 @@ impl<'context> RunItemResolver<'_, 'context> {fn exit_node_with_box_model_metrics(&mut self, before_item: bool, within_item: bool) {let (node, in_flow_item_count_at_start) = self.box_model_node_stack.pop().unwrap();let sides = self.context().sides_of_entered_inline_box(node);- // https://quirks.spec.whatwg.org/#the-line-height-calculation-quirk- // No item tells the line builder about an inline box that holds no in-flow item, so it's kept apart, for the- // line it's on to take its strut from — except in quirks mode, where an inline box without text has none- // unless it has a border or padding at its start or end.- let none = CssPixels::default();- if in_flow_item_count_at_start == self.in_flow_item_count- && (!self.context().facts(node).document_in_line_height_quirks_mode()- || sides.border_left != none- || sides.border_right != none- || sides.padding_left != none- || sides.padding_right != none)- {+ // NB: No item tells the line builder about an inline box that holds no in-flow item, so it's kept apart, for+ // the line it's on to place it and take its strut from.+ if in_flow_item_count_at_start == self.in_flow_item_count {let empty_box = EmptyInlineBox {position: self.item_count,node,diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rsindex 6739c8abaeb..9356f1ffed9 100644--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs@@ -1312,10 +1312,14 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {return true;}let line = self.line(line_index);- let has_content = line- .fragments- .iter()- .any(|fragment| fragment.takes_part_in_line_alignment() && !Self::is_collapsible_whitespace(fragment));+ let has_content = line.fragments.iter().enumerate().any(|(fragment_index, fragment)| {+ fragment.takes_part_in_line_alignment()+ && if fragment_index < text.strut_fragment_start {+ !Self::is_collapsible_whitespace(fragment)+ } else {+ self.strut_brings_line_height(line_index, text, fragment.style_source)+ }+ });!has_content&& line.forced_break.is_some_and(|forced_break| {!forced_break.node.is_invalid() && self.context().parent_node(forced_break.node) == containing_block@@ -1352,10 +1356,37 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {// https://quirks.spec.whatwg.org/#the-line-height-calculation-quirk// NB: The collapsible whitespace that a line starts or ends with isn't text the line keeps, so in quirks mode,// it has no line height to bring to it either.- !text.in_line_height_quirks_mode- || fragment_index >= text.strut_fragment_start- || text.kept_fragments.contains(&fragment_index)- || !Self::is_collapsible_whitespace(&self.line(line_index).fragments[fragment_index])+ if !text.in_line_height_quirks_mode {+ return true;+ }+ let fragment = &self.line(line_index).fragments[fragment_index];+ if fragment_index >= text.strut_fragment_start {+ return self.strut_brings_line_height(line_index, text, fragment.style_source);+ }+ text.kept_fragments.contains(&fragment_index) || !Self::is_collapsible_whitespace(fragment)+ }++ /// Whether the strut of `box_`, an inline box on the line at `line_index` with none of its content there, brings+ /// its line height to the line.+ fn strut_brings_line_height(&self, line_index: usize, text: &LineTextFacts, box_: Node) -> bool {+ // https://quirks.spec.whatwg.org/#the-line-height-calculation-quirk+ // NB: In quirks mode, such a box has a line height only if it has a border or padding at its start or end, or+ // if the line's forced break is inside it.+ if !text.in_line_height_quirks_mode || self.inline_box_has_strut_on_line(text, box_) {+ return true;+ }+ let Some(forced_break) = self.line(line_index).forced_break else {+ return false;+ };+ let containing_block = self.context().containing_block;+ let mut node = forced_break.node;+ while !node.is_invalid() && node != containing_block {+ if node == box_ {+ return true;+ }+ node = self.context().parent_node(node);+ }+ false}fn is_collapsible_whitespace(fragment: &line_box_fragment::LineBoxFragmentData) -> bool {@@ -1487,6 +1518,9 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {let strut_count = self.line(line_index).inline_box_struts.len();for strut_index in 0..strut_count {let box_ = self.line(line_index).inline_box_struts[strut_index].box_;+ if !self.strut_brings_line_height(line_index, &text, box_) {+ continue;+ }let block_size = TextLayoutBounds::new(self.style(box_), None).block_size();self.max_block_size_on_current_line = self.max_block_size_on_current_line.max(block_size);}diff --git a/Tests/LibWeb/Text/expected/css/empty-inline-box-position-under-line-height-quirk-limited-quirks.txt b/Tests/LibWeb/Text/expected/css/empty-inline-box-position-under-line-height-quirk-limited-quirks.txtnew file mode 100644index 00000000000..1ae39632717--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/empty-inline-box-position-under-line-height-quirk-limited-quirks.txt@@ -0,0 +1,8 @@+CSS1Compat+between: h=20 span@(20,-24 0x50)+between-plain: h=20 span@(20,0 0x20)+margin-then-text: h=20 span@(5,-24 0x50)+margin-then-ib: h=10 span@(5,-30 0x50)+padded-collapsed-space: h=60 span@(0,5 5x50)+plain-collapsed-space: h=20 span@(0,-24 0x50)+after-wrap: h=40 span@(40,-4 0x50)diff --git a/Tests/LibWeb/Text/expected/css/empty-inline-box-position-under-line-height-quirk.txt b/Tests/LibWeb/Text/expected/css/empty-inline-box-position-under-line-height-quirk.txtnew file mode 100644index 00000000000..e6921dcacb2--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/empty-inline-box-position-under-line-height-quirk.txt@@ -0,0 +1,8 @@+BackCompat+between: h=20 span@(20,-24 0x50)+between-plain: h=20 span@(20,0 0x20)+margin-then-text: h=20 span@(5,-24 0x50)+margin-then-ib: h=10 span@(5,-30 0x50)+padded-collapsed-space: h=60 span@(0,5 5x50)+plain-collapsed-space: h=20 span@(0,-24 0x50)+after-wrap: h=40 span@(40,-4 0x50)diff --git a/Tests/LibWeb/Text/input/css/empty-inline-box-position-under-line-height-quirk-limited-quirks.html b/Tests/LibWeb/Text/input/css/empty-inline-box-position-under-line-height-quirk-limited-quirks.htmlnew file mode 100644index 00000000000..434a27676e9--- /dev/null+++ b/Tests/LibWeb/Text/input/css/empty-inline-box-position-under-line-height-quirk-limited-quirks.html@@ -0,0 +1,17 @@+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">+<style>+ @font-face { font-family: Ahem; src: url("../wpt-import/fonts/Ahem.ttf"); }+ body { margin: 0; font: 20px/20px Ahem; }+ .s { font-size: 50px; line-height: 60px; }+ .ib { display: inline-block; width: 10px; height: 10px; }+ .case { position: relative; }+</style>+<div class="case" id="between">A<span class="s"></span>B</div>+<div class="case" id="between-plain">A<span></span>B</div>+<div class="case" id="margin-then-text"><span class="s" style="margin-left: 5px"></span>x</div>+<div class="case" id="margin-then-ib"><span class="s" style="margin-left: 5px"></span><span class="ib"></span></div>+<div class="case" id="padded-collapsed-space"><span class="s" style="padding-left: 5px"> </span>X</div>+<div class="case" id="plain-collapsed-space"><span class="s"> </span>X</div>+<div class="case" id="after-wrap" style="width: 30px">AA BB<span class="s"></span></div>+<script src="../include.js"></script>+<script>promiseTest(async () => { await document.fonts.ready; const out=[document.compatMode]; for (const d of document.querySelectorAll(".case")) { const r=d.getBoundingClientRect(); const s=d.querySelector("span").getBoundingClientRect(); out.push(d.id+": h="+r.height+" span@("+(s.left-r.left)+","+(s.top-r.top)+" "+s.width+"x"+s.height+")"); } println(out.join("\n")); });</script>diff --git a/Tests/LibWeb/Text/input/css/empty-inline-box-position-under-line-height-quirk.html b/Tests/LibWeb/Text/input/css/empty-inline-box-position-under-line-height-quirk.htmlnew file mode 100644index 00000000000..7885ca9f6e6--- /dev/null+++ b/Tests/LibWeb/Text/input/css/empty-inline-box-position-under-line-height-quirk.html@@ -0,0 +1,17 @@+<!DOCTYPE quirks>+<style>+ @font-face { font-family: Ahem; src: url("../wpt-import/fonts/Ahem.ttf"); }+ body { margin: 0; font: 20px/20px Ahem; }+ .s { font-size: 50px; line-height: 60px; }+ .ib { display: inline-block; width: 10px; height: 10px; }+ .case { position: relative; }+</style>+<div class="case" id="between">A<span class="s"></span>B</div>+<div class="case" id="between-plain">A<span></span>B</div>+<div class="case" id="margin-then-text"><span class="s" style="margin-left: 5px"></span>x</div>+<div class="case" id="margin-then-ib"><span class="s" style="margin-left: 5px"></span><span class="ib"></span></div>+<div class="case" id="padded-collapsed-space"><span class="s" style="padding-left: 5px"> </span>X</div>+<div class="case" id="plain-collapsed-space"><span class="s"> </span>X</div>+<div class="case" id="after-wrap" style="width: 30px">AA BB<span class="s"></span></div>+<script src="../include.js"></script>+<script>promiseTest(async () => { await document.fonts.ready; const out=[document.compatMode]; for (const d of document.querySelectorAll(".case")) { const r=d.getBoundingClientRect(); const s=d.querySelector("span").getBoundingClientRect(); out.push(d.id+": h="+r.height+" span@("+(s.left-r.left)+","+(s.top-r.top)+" "+s.width+"x"+s.height+")"); } println(out.join("\n")); });</script>