Skip to content

Truly independent web browser

LibWeb: Decide a break before an atomic inline by its common ancestor

sideshowbarkerf4b64a8a08b1
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
index c1eeb60fdfd..7afc0a8a5df 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_formatting_context.rs
@@ -2032,17 +2032,11 @@ impl<'context> InlineFormattingContext<'context> {
prepared: &prepared_inline_content::PreparedInlineContent,
) -> sizing_context::ContentInlineSizes {
let containing_style = self.style(self.containing_block);
- let container_wraps = containing_style.text_wrap_mode() == text_wrap_mode::WRAP;
let text_indentation =
TextIndentation::of(containing_style, self.input.containing_block_constraints.inline_basis());
- let mut max_content_lines = ContentSizingLines::new(
- LinesWithoutLineBoxes::indented_by(false, text_indentation),
- container_wraps,
- );
- let mut min_content_lines = ContentSizingLines::new(
- LinesWithoutLineBoxes::indented_by(true, text_indentation),
- container_wraps,
- );
+ let mut max_content_lines =
+ ContentSizingLines::new(LinesWithoutLineBoxes::indented_by(false, text_indentation));
+ let mut min_content_lines = ContentSizingLines::new(LinesWithoutLineBoxes::indented_by(true, text_indentation));
for index in 0..items.len() {
max_content_lines.append(self, items, prepared, index);
min_content_lines.append(self, items, prepared, index);
@@ -2878,9 +2872,22 @@ impl<'context> InlineFormattingContext<'context> {
placed_child.with_in_flow_box_insets(insets)
});
}
- if self.style(self.containing_block).text_wrap_mode() == text_wrap_mode::WRAP {
- let minimum = item.margin_start + item.border_box_inline_size() + item.margin_end;
- line_builder.break_if_needed(minimum);
+ if item.can_break_before
+ || (line_builder.current_line_is_empty()
+ && self.style(self.containing_block).text_wrap_mode() == text_wrap_mode::WRAP)
+ {
+ // NB: The box goes on the line with whatever can't be broken from it after it, such as a box in
+ // the same nowrap inline box. A box by itself needs only its advance, which a negative end
+ // margin reduces.
+ let box_inline_size = item.margin_start + item.border_box_inline_size() + item.margin_end;
+ let needed_inline_size = if iterator.next_line_content_continues_run(self) {
+ iterator
+ .unbreakable_run_inline_size(self, &item, false)
+ .max(box_inline_size)
+ } else {
+ box_inline_size
+ };
+ line_builder.break_if_needed(needed_inline_size);
line_builder.note_soft_wrap_opportunity();
}
if self.stops_at_line_clamp_point(line_builder, &mut state_at_clamp_point) {
@@ -4208,8 +4215,8 @@ impl LinesWithoutLineBoxes {
}
}
- pub(super) fn append_atomic_inline(&mut self, border_box_inline_size: CssPixels, container_wraps: bool) {
- if container_wraps && self.breaks_before_next_fragment() {
+ pub(super) fn append_atomic_inline(&mut self, border_box_inline_size: CssPixels, can_break_before: bool) {
+ if can_break_before && self.breaks_before_next_fragment() {
self.finish();
}
self.append(border_box_inline_size, false, CssPixels::default());
@@ -4324,7 +4331,6 @@ impl LinesWithoutLineBoxes {
struct ContentSizingLines {
lines: LinesWithoutLineBoxes,
- container_wraps: bool,
pending_start_margin: CssPixels,
pending_start_border: CssPixels,
pending_start_padding: CssPixels,
@@ -4332,10 +4338,9 @@ struct ContentSizingLines {
}
impl ContentSizingLines {
- fn new(lines: LinesWithoutLineBoxes, container_wraps: bool) -> Self {
+ fn new(lines: LinesWithoutLineBoxes) -> Self {
Self {
lines,
- container_wraps,
pending_start_margin: CssPixels::default(),
pending_start_border: CssPixels::default(),
pending_start_padding: CssPixels::default(),
@@ -4423,7 +4428,7 @@ impl ContentSizingLines {
}
inline_level_iterator::ItemType::Element => {
self.lines
- .append_atomic_inline(border_box_inline_size, self.container_wraps);
+ .append_atomic_inline(border_box_inline_size, item.can_break_before);
}
// An absolutely positioned box only takes a static position, which comes after the pending inline edges.
inline_level_iterator::ItemType::AbsolutelyPositionedElement => {
@@ -4455,7 +4460,7 @@ impl ContentSizingLines {
};
// NB: Min-content breaks at every soft wrap opportunity, so content that ends in a soft hyphen ends
// its line in a hyphen wherever the content after it can start a line. And min-content breaks
- // before every atomic inline in a container that wraps, as append_atomic_inline() does.
+ // before every atomic inline that a line can break before, as append_atomic_inline() does.
let next_content = remaining_items.iter().find(|next| {
!matches!(
next.type_,
@@ -4465,8 +4470,9 @@ impl ContentSizingLines {
)
});
let breaks_after_item = next_content.is_some_and(|next| match next.type_ {
- inline_level_iterator::ItemType::Text => next.can_break_before,
- inline_level_iterator::ItemType::Element => self.container_wraps,
+ inline_level_iterator::ItemType::Text | inline_level_iterator::ItemType::Element => {
+ next.can_break_before
+ }
_ => false,
});
if style.text_wrap_mode() == text_wrap_mode::WRAP {
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index 1a8958339e9..bc6bf47365c 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -1861,19 +1861,27 @@ impl<'pass> InlineContentPreparer<'pass> {
// NO-BREAK SPACE. However, with the exception of U+00A0 NO-BREAK SPACE, there must be no soft wrap
// opportunity between atomic inlines and adjacent characters belonging to the Unicode GL, WJ, or ZWJ line
// breaking classes."
- // NB: The line breaker allows the break before the box, and the text after it looks for the one after it.
+ // https://drafts.csswg.org/css-text-3/#valdef-white-space-nowrap
+ // "For soft wrap opportunities defined by the boundary between two characters or atomic inlines, the
+ // white-space property on the nearest common ancestor of the two characters controls breaking"
+ // NB: The opportunity before the box is decided here, and the text after it looks for the one after it.
// The box stands in the text before that text as U+FFFC OBJECT REPLACEMENT CHARACTER.
+ let can_break_before = self.previous_node.is_some_and(|previous_node| {
+ self.style(self.nearest_common_ancestor(previous_node, node))
+ .text_wrap_mode()
+ == text_wrap_mode::WRAP
+ });
self.previous_text = Some(&OBJECT_REPLACEMENT_CHARACTER);
self.previous_node = Some(node);
self.previous_text_breaks_all = false;
self.previous_text_regional_indicators = 0;
self.code_unit_before_previous_text = None;
self.skip_to_next_within_item();
- return Some(PreparedItem::new(
- ItemType::Element,
- node,
- PreparedItem::TAKES_LEADING_EDGES | PreparedItem::TAKES_TRAILING_EDGES,
- ));
+ let mut flags = PreparedItem::TAKES_LEADING_EDGES | PreparedItem::TAKES_TRAILING_EDGES;
+ if can_break_before {
+ flags |= PreparedItem::CAN_BREAK_BEFORE;
+ }
+ return Some(PreparedItem::new(ItemType::Element, node, flags));
}
}
}
@@ -2379,6 +2387,7 @@ impl<'context> RunItemResolver<'_, 'context> {
fn resolve_element_item(&mut self, item_index: usize, prepared_item: &PreparedItem) -> Item {
let node = prepared_item.node();
let mut item = Item::new(ItemType::Element, node, item_index);
+ item.can_break_before = prepared_item.has(PreparedItem::CAN_BREAK_BEFORE);
if self.atomic_sizing == AtomicInlineSizing::Contributions {
self.size_item_by_outer_inline_contributions(&mut item);
} else {
@@ -2728,6 +2737,26 @@ impl InlineLevelIterator {
edges + content
}
+ /// Whether the next text or atomic inline is part of the same unbreakable run as the item before it.
+ pub(crate) fn next_line_content_continues_run(
+ &self,
+ context: &inline_formatting_context::InlineFormattingContext<'_>,
+ ) -> bool {
+ self.items()
+ .iter()
+ .find(|item| !sits_between_line_content(item))
+ .is_some_and(|next| match next.type_ {
+ ItemType::Element => !next.can_break_before,
+ // NB: White space ends the run only where it wraps.
+ ItemType::Text => {
+ !next.can_break_before
+ && (!(next.is_collapsible_whitespace || next.is_ascii_whitespace(context))
+ || context.style(context.parent_node(next.node)).text_wrap_mode() != text_wrap_mode::WRAP)
+ }
+ _ => false,
+ })
+ }
+
pub(crate) fn unbreakable_run_inline_size(
&self,
context: &inline_formatting_context::InlineFormattingContext<'_>,
@@ -2849,6 +2878,15 @@ pub(crate) fn whitespace_and_sequence_inline_size(
whitespace: &Item,
items: &[Item],
) -> Option<CssPixels> {
+ // NB: An atomic inline that a line can break before decides that break itself when the line builder comes to it, so
+ // the white space before it stays on the line, as it would before a line break there.
+ if items
+ .iter()
+ .find(|item| !sits_between_line_content(item))
+ .is_some_and(|item| item.type_ == ItemType::Element && item.can_break_before)
+ {
+ return None;
+ }
let (length, end) = sequence_length(context, prepared, items, false, false);
let prefix =
(end == SequenceEnd::SoftWrapInText).then(|| first_breakable_segment(context, prepared, &items[length], end));
@@ -2921,8 +2959,7 @@ fn starts_line_content<'item>(mut items: impl Iterator<Item = &'item Item>) -> b
items
.find(|item| !sits_between_line_content(item))
.is_some_and(|item| match item.type_ {
- ItemType::Text => item.can_break_before,
- ItemType::Element => true,
+ ItemType::Text | ItemType::Element => item.can_break_before,
_ => false,
})
}
@@ -2983,16 +3020,23 @@ fn sequence_length<'item>(
if length > 0 && item.can_break_before {
break;
}
- let style = context.style(context.style_source(item.node));
- if style.text_wrap_mode() == text_wrap_mode::WRAP {
- if item.type_ != ItemType::Text {
- break;
- }
- if item.is_collapsible_whitespace || item.is_ascii_whitespace(context) {
- return (length, SequenceEnd::Whitespace);
+ match item.type_ {
+ ItemType::Text => {
+ if context.style(context.style_source(item.node)).text_wrap_mode() == text_wrap_mode::WRAP {
+ if item.is_collapsible_whitespace || item.is_ascii_whitespace(context) {
+ return (length, SequenceEnd::Whitespace);
+ }
+ if stop_at_overflow_breakable_text && context.overflow_break_applies(item.node) {
+ return (length, SequenceEnd::OverflowBreakableText);
+ }
+ }
}
- if stop_at_overflow_breakable_text && context.overflow_break_applies(item.node) {
- return (length, SequenceEnd::OverflowBreakableText);
+ // NB: Whether a line can break before an atomic inline is what its item records, as checked above.
+ ItemType::Element => {}
+ _ => {
+ if context.style(context.style_source(item.node)).text_wrap_mode() == text_wrap_mode::WRAP {
+ break;
+ }
}
}
if !(length == 0 && include_first_whole_item) && item.has_soft_wraps(prepared) {
diff --git a/Tests/LibWeb/Text/expected/soft-wrap-opportunity-next-to-atomic-inline.txt b/Tests/LibWeb/Text/expected/soft-wrap-opportunity-next-to-atomic-inline.txt
new file mode 100644
index 00000000000..4f2b54f715b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/soft-wrap-opportunity-next-to-atomic-inline.txt
@@ -0,0 +1,11 @@
+box-at-end-of-nowrap-text: height 20, boxes at 3, min-content 130
+image-at-end-of-nobr: height 20, boxes at 3, min-content 120
+box-after-space-in-nowrap-text: height 20, boxes at 3, min-content 120
+nowrap-boxes-in-wrapping-text: height 40, boxes at 3, 23, min-content 40
+nowrap-span-of-boxes-after-wrapping-text: height 40, boxes at 23, 23, min-content 40
+box-after-word-joiner: height 40, boxes at 23, min-content 70
+box-after-word-joiner-at-line-end: height 40, boxes at 23, min-content 90
+box-after-no-break-space: height 40, boxes at 23, min-content 70
+nowrap-span-of-boxes-and-a-space: height 40, boxes at 23, 23, min-content 60
+nowrap-boxes-ending-together-by-negative-margin: height 40, boxes at 23, 23, min-content 80
+nowrap-boxes-overlapping-by-negative-margin: height 20, boxes at 3, 3, min-content 40
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-001.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-001.txt
new file mode 100644
index 00000000000..2c52632e850
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-001.txt
@@ -0,0 +1,9 @@
+Harness status: OK
+
+Found 4 tests
+
+4 Pass
+Pass CSS Text Test: Check atomic inline+NBSP quirk does not apply if nowrap
+Pass CSS Text Test: Check atomic inline+NBSP quirk does not apply if nowrap 1
+Pass CSS Text Test: Check atomic inline+NBSP quirk does not apply if nowrap 2
+Pass CSS Text Test: Check atomic inline+NBSP quirk does not apply if nowrap 3
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-002.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-002.txt
new file mode 100644
index 00000000000..50df502d780
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-002.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass #test 1
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/layout-run-cache-optimization-scenarios.html b/Tests/LibWeb/Text/input/layout-run-cache-optimization-scenarios.html
index fda5303310a..c7682c9e055 100644
--- a/Tests/LibWeb/Text/input/layout-run-cache-optimization-scenarios.html
+++ b/Tests/LibWeb/Text/input/layout-run-cache-optimization-scenarios.html
@@ -330,7 +330,9 @@
const maximum = sequence.max + (testCase.content === "atomicPair" ? 10 : 0);
let minimum = sequence.min;
if (testCase.wrapping === "nowrap") {
- if (testCase.content === "direct" || testCase.content === "inline")
+ // NB: Boxes in a span that wraps can break apart, since the span is their nearest common
+ // ancestor, and only the nowrap container's own children can't.
+ if (testCase.content === "direct")
minimum = sequence.max;
else if (testCase.content === "atomicPair")
minimum += 10;
diff --git a/Tests/LibWeb/Text/input/selection-drag-past-overflowing-text-and-image.html b/Tests/LibWeb/Text/input/selection-drag-past-overflowing-text-and-image.html
index 63715667913..38f23044832 100644
--- a/Tests/LibWeb/Text/input/selection-drag-past-overflowing-text-and-image.html
+++ b/Tests/LibWeb/Text/input/selection-drag-past-overflowing-text-and-image.html
@@ -55,7 +55,7 @@
<tr><td><strong id="latest-label">Latest Release</strong></td></tr>
<tr>
<td>
- <a id="latest-link"><span id="latest-version">7.0.10</span><img id="download-arrow" alt=""></a>
+ <a id="latest-link"><span id="latest-version">7.0.10</span><br><img id="download-arrow" alt=""></a>
</td>
</tr>
</table>
diff --git a/Tests/LibWeb/Text/input/soft-wrap-opportunity-next-to-atomic-inline.html b/Tests/LibWeb/Text/input/soft-wrap-opportunity-next-to-atomic-inline.html
new file mode 100644
index 00000000000..cfab8cbe3a6
--- /dev/null
+++ b/Tests/LibWeb/Text/input/soft-wrap-opportunity-next-to-atomic-inline.html
@@ -0,0 +1,36 @@
+<!DOCTYPE html>
+<style>
+ @font-face { font-family: Ahem; src: url("../../Ref/data/fonts/Ahem.ttf"); }
+ body { font: 10px/20px Ahem; }
+ .c { width: 100px; margin-bottom: 10px; }
+ .box { display: inline-block; width: 20px; height: 10px; }
+ .nowrap { white-space: nowrap; }
+ img { width: 20px; height: 10px; }
+</style>
+<div class="c" id="box-at-end-of-nowrap-text"><span class="nowrap">aa bb <span>cc</span><span class="box"></span> or</span></div>
+<div class="c" id="image-at-end-of-nobr"><nobr>aa bb cccc<img></nobr></div>
+<div class="c" id="box-after-space-in-nowrap-text"><span class="nowrap">aa bb ccc <span class="box"></span></span></div>
+<div class="c" id="nowrap-boxes-in-wrapping-text">aa <span>bbbb</span><span class="box nowrap"></span><span class="box nowrap"></span></div>
+<div class="c" id="nowrap-span-of-boxes-after-wrapping-text">aa <span>bbbb</span><span class="nowrap"><span class="box"></span><span class="box"></span></span></div>
+<div class="c" id="box-after-word-joiner">aa bbbbbbb&#x2060;<span class="box"></span></div>
+<div class="c" id="box-after-word-joiner-at-line-end">aaaaaaaaa&#x2060;<span class="box"></span></div>
+<div class="c" id="box-after-no-break-space">aa bbbbbb&nbsp;<span class="box"></span></div>
+<div class="c" id="nowrap-span-of-boxes-and-a-space">aaaaaa<span class="nowrap"><span class="box"></span> <span class="box"></span></span></div>
+<div class="c" id="nowrap-boxes-ending-together-by-negative-margin">aa <span class="nowrap"><span class="box" style="width: 80px; margin-right: -40px"></span><span class="box" style="width: 40px"></span></span></div>
+<div class="c" id="nowrap-boxes-overlapping-by-negative-margin">aaaa <span class="nowrap"><span class="box" style="margin-right: -10px"></span><span class="box"></span></span></div>
+<script src="include.js"></script>
+<script>
+ asyncTest(async done => {
+ await document.fonts.load("10px Ahem");
+ for (const c of document.querySelectorAll(".c")) {
+ const top = c.getBoundingClientRect().top;
+ const boxes = [...c.querySelectorAll(".box, img")].map(box => box.getBoundingClientRect().top - top);
+ const width = c.style.width;
+ c.style.width = "min-content";
+ const minContent = c.getBoundingClientRect().width;
+ c.style.width = width;
+ println(`${c.id}: height ${c.getBoundingClientRect().height}, boxes at ${boxes.join(", ")}, min-content ${minContent}`);
+ }
+ done();
+ });
+</script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-001.html b/Tests/LibWeb/Text/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-001.html
new file mode 100644
index 00000000000..ae570a6ee79
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-001.html
@@ -0,0 +1,40 @@
+<!DOCTYPE html>
+<title>CSS Text Test: Check atomic inline+NBSP quirk does not apply if nowrap</title>
+<link rel="help" href="https://www.w3.org/TR/css-text-3/#line-break-details">
+<link rel="help" href="https://crbug.com/1002442">
+<link rel="author" title="Koji Ishii" href="mailto:[email protected]">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<style>
+div {
+ white-space: nowrap;
+ font-size: 10px;
+ line-height: 1;
+ width: 5ch;
+}
+.inline-block {
+ display: inline-block;
+ width: 3ch;
+ height: 1em;
+}
+img {
+ width: 3ch;
+ height: 1em;
+}
+</style>
+<body>
+ <div>000<span class="inline-block"></span>&nbsp;111</div>
+ <div>000<img>&nbsp;111</div>
+ <div>000&nbsp;<span class="inline-block"></span>111</div>
+ <div>000&nbsp;<img>111</div>
+<script>
+(function () {
+ for (let e of document.getElementsByTagName('div')) {
+ test(() => {
+ let height = e.offsetHeight;
+ assert_equals(Math.round(height / 10), 1);
+ });
+ }
+})();
+</script>
+</body>
diff --git a/Tests/LibWeb/Text/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-002.html b/Tests/LibWeb/Text/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-002.html
new file mode 100644
index 00000000000..d32285e6845
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/css/css-text/line-breaking/line-breaking-atomic-nowrap-002.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+
+<title>CSS Text Test: `text-wrap-mode: none` should prevent a soft wrap opportunity between atomics</title>
+<link rel="author" title="Martin Robinson" href="mailto:[email protected]">
+<link rel="help" href="https://drafts.csswg.org/css-text-4/#white-space-property">
+<link rel="help" href="https://drafts.csswg.org/css-sizing-3/#intrinsic-sizes">
+<link rel="help" href="https://www.w3.org/TR/css-text-3/#line-break-details">
+<script src="../../../resources/testharness.js"></script>
+<script src="../../../resources/testharnessreport.js"></script>
+<script src="../../../resources/check-layout-th.js"></script>
+
+<style>
+ img {
+ width: 50px;
+ height: 100px;
+ border: 0;
+ }
+ span {
+ display: inline-block;
+ background: green;
+ text-wrap-mode: nowrap;
+ width: min-content;
+ }
+</style>
+
+<div style="width: 0;">
+ <span data-expected-client-width="100" id="test"><img><img></span>
+</div>
+
+<script>
+ document.fonts.ready.then(() => checkLayout("#test"));
+</script>