Skip to content

Truly independent web browser

LibWeb: Keep the empty line after a trailing newline in a text control

Andreas Kling1fb41851d2aa
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index dcbe84d055b..e3cced7e77f 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -729,13 +729,20 @@ impl<'iterator, 'context> InlineLevelIteratorGenerator<'iterator, 'context> {
&& is_last_chunk
&& text_context.text.is_empty()
&& self.context().facts(text_node).produces_line_box_fragment_when_empty();
+ // INTEROP: A text control's text that ends in a preserved newline keeps the empty line after it, which gives
+ // that line its height and the caret on it a fragment.
+ let synthesize_chunk_after_trailing_newline = chunk.is_none()
+ && text_context.next_chunk_index == chunks.len()
+ && text_context.should_respect_linebreaks
+ && chunks.last().is_some_and(|last| last.has_breaking_newline)
+ && self.context().facts(text_node).is_text_control_text();
let chunk = if let Some(chunk) = chunk {
chunk
- } else if synthesize_zero_length_chunk {
- text_context.next_chunk_index = 1;
+ } else if synthesize_zero_length_chunk || synthesize_chunk_after_trailing_newline {
+ text_context.next_chunk_index = chunks.len() + 1;
let parent_style = self.context().style(self.context().parent_node(text_node));
text_chunker::TextChunk {
- start: 0,
+ start: text_context.text.len(),
length: 0,
font: parent_style.first_available_font(),
has_breaking_newline: false,
@@ -832,6 +839,7 @@ impl<'iterator, 'context> InlineLevelIteratorGenerator<'iterator, 'context> {
}
let chunk_inline_size = CssPixels::nearest_value_for_f32(glyphs.width + inline_offset);
let generated_empty = synthesize_zero_length_chunk
+ || synthesize_chunk_after_trailing_newline
|| (self.context().facts(text_node).is_generated_for_pseudo_element() && chunk.length == 0);
let mut item = Item::new(ItemType::Text, text_node);
item.glyphs = Some(glyphs);
diff --git a/Libraries/LibWeb/Rust/src/layout/node_facts.rs b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
index 97461ebe013..78968882dd3 100644
--- a/Libraries/LibWeb/Rust/src/layout/node_facts.rs
+++ b/Libraries/LibWeb/Rust/src/layout/node_facts.rs
@@ -1154,6 +1154,12 @@ impl<'pass> NodeFacts<'pass> {
has_flag(self.data(), NodeFlag::ProducesLineBoxFragmentWhenEmpty)
}
+ /// Whether the node is the text of a text control, such as a textarea's value. Only that text and the text directly
+ /// in an editing host produce a line box fragment when empty, and no editing host is in a user agent shadow tree.
+ pub(crate) fn is_text_control_text(&self) -> bool {
+ has_flag(self.data(), NodeFlag::IsInUserAgentShadowTree) && self.produces_line_box_fragment_when_empty()
+ }
+
pub(crate) fn uses_button_layout(&self) -> bool {
has_flag(self.data(), NodeFlag::UsesButtonLayout)
}
diff --git a/Libraries/LibWeb/Rust/src/painting/visual_lines.rs b/Libraries/LibWeb/Rust/src/painting/visual_lines.rs
index 401f306e130..ed34d27b3bd 100644
--- a/Libraries/LibWeb/Rust/src/painting/visual_lines.rs
+++ b/Libraries/LibWeb/Rust/src/painting/visual_lines.rs
@@ -117,7 +117,17 @@ pub(crate) fn collect_visual_lines(layout_arena: &impl PaintRead, node_slots: &[
if let Some(&primary) = node_slots.last()
&& text_preserves_newlines(layout_arena, primary)
{
+ // The empty line after a text control's trailing newline holds a zero-length fragment, so its line is already
+ // in the list.
+ let empty_lines_with_fragments: Vec<usize> = lines
+ .iter()
+ .filter(|line| line.start_offset == line.end_offset)
+ .map(|line| line.start_offset)
+ .collect();
for_each_empty_visual_line_position(layout_arena, node_slots, |position| {
+ if empty_lines_with_fragments.contains(&position) {
+ return true;
+ }
lines.push(RecordVisualLine {
start_offset: position,
end_offset: position,
diff --git a/Tests/LibWeb/Text/expected/textarea-empty-line-caret.txt b/Tests/LibWeb/Text/expected/textarea-empty-line-caret.txt
index 8b14c0dc7dd..ccfb23a7739 100644
--- a/Tests/LibWeb/Text/expected/textarea-empty-line-caret.txt
+++ b/Tests/LibWeb/Text/expected/textarea-empty-line-caret.txt
@@ -1,5 +1,5 @@
-a trailing newline adds a line: false
-a trailing newline adds a line with white-space: pre-line: false
+a trailing newline adds a line: true
+a trailing newline adds a line with white-space: pre-line: true
typed: caret on the empty last line is visible: true
execCommand: caret on the empty last line is visible: true
caret moved onto an empty middle line is visible: true