Skip to content

Truly independent web browser

LibWeb: Count atomic inline margins when fitting lines

sideshowbarker56aba0318a5d
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index 2342d729c99..6ec47ad6993 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -2301,13 +2301,7 @@ impl<'context> InlineFormattingContext<'context> {
});
}
if self.style(self.containing_block).text_wrap_mode() == text_wrap_mode::WRAP {
- let mut minimum = item.border_box_inline_size();
- if item.margin_start < CssPixels::default() {
- minimum += item.margin_start;
- }
- if item.margin_end < CssPixels::default() {
- minimum += item.margin_end;
- }
+ let minimum = item.margin_start + item.border_box_inline_size() + item.margin_end;
line_builder.break_if_needed(minimum);
line_builder.note_soft_wrap_opportunity();
}
diff --git a/Tests/LibWeb/Text/expected/css/inline-block-line-break-margins.txt b/Tests/LibWeb/Text/expected/css/inline-block-line-break-margins.txt
new file mode 100644
index 00000000000..9413eda4888
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/inline-block-line-break-margins.txt
@@ -0,0 +1,22 @@
+ltr, right margin, spaces: 3 + 1
+ltr, right margin, no spaces: 3 + 1
+ltr, wide right margin: 2 + 2
+ltr, left margin, fits: 4
+ltr, both margins: 1 + 1
+ltr, exact fit: 4
+ltr, negative right margin: 2
+ltr, negative left margin: 2
+ltr, mixed margins: 2
+ltr, padding and border: 3 + 1
+ltr, nowrap: 4
+rtl, right margin, spaces: 3 + 1
+rtl, right margin, no spaces: 3 + 1
+rtl, wide right margin: 2 + 2
+rtl, left margin, fits: 4
+rtl, both margins: 1 + 1
+rtl, exact fit: 4
+rtl, negative right margin: 2
+rtl, negative left margin: 2
+rtl, mixed margins: 2
+rtl, padding and border: 3 + 1
+rtl, nowrap: 4
diff --git a/Tests/LibWeb/Text/input/css/inline-block-line-break-margins.html b/Tests/LibWeb/Text/input/css/inline-block-line-break-margins.html
new file mode 100644
index 00000000000..7f9dd480205
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/inline-block-line-break-margins.html
@@ -0,0 +1,36 @@
+<!doctype html>
+<style>
+ .row { font-size: 0; margin-bottom: 10px; }
+ .row span { display: inline-block; width: 55px; height: 40px; }
+ .spaces { font: 10px/10px monospace; }
+</style>
+<script src="../include.js"></script>
+<script>
+ test(() => {
+ const cases = [
+ ["right margin, spaces", 270, 4, "margin-right: 10px", true],
+ ["right margin, no spaces", 252, 4, "margin-right: 10px"],
+ ["wide right margin", 230, 4, "margin-right: 30px"],
+ ["left margin, fits", 280, 4, "margin-left: 10px", true],
+ ["both margins", 358, 2, "width: 160px; margin: 0 16px"],
+ ["exact fit", 260, 4, "margin-right: 10px"],
+ ["negative right margin", 180, 2, "width: 100px; margin-right: -10px"],
+ ["negative left margin", 180, 2, "width: 100px; margin-left: -10px"],
+ ["mixed margins", 190, 2, "width: 100px; margin-left: 15px; margin-right: -20px"],
+ ["padding and border", 307, 4, "padding: 5px; border: 2px solid; margin-right: 10px"],
+ ["nowrap", 252, 4, "margin-right: 10px", false, "white-space: nowrap"],
+ ];
+ for (const direction of ["ltr", "rtl"]) {
+ for (const [label, width, count, style, spaces, extraStyle = ""] of cases) {
+ const row = document.createElement("div");
+ row.className = "row" + (spaces ? " spaces" : "");
+ row.style.cssText = `width: ${width}px; direction: ${direction}; ${extraStyle}`;
+ row.innerHTML = Array(count).fill(`<span style="${style}"></span>`).join(spaces ? " " : "");
+ document.body.append(row);
+ const tops = [...row.children].map(child => child.getBoundingClientRect().top);
+ const lines = [...new Set(tops)].map(top => tops.filter(value => value === top).length);
+ println(`${direction}, ${label}: ${lines.join(" + ")}`);
+ }
+ }
+ });
+</script>