Skip to content

Truly independent web browser

LibWeb: Put text-indent at the start edge of right-to-left lines

sideshowbarkerec36d36cce2f
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_content.rs b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
index 4167fcdaad7..e3f8c84c9cc 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_content.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_content.rs
@@ -170,6 +170,8 @@ pub struct LineRecord {
pub(crate) horizontal_extent: CssPixels,
pub(crate) reusable_atomic_prefix: bool,
pub inline_length: CssPixels,
+ /// The text-indent the line begins with, which its inline length includes.
+ pub(crate) indent: CssPixels,
pub inline_length_before_block_ellipsis: Option<CssPixels>,
pub block_length: CssPixels,
pub block_start: CssPixels,
diff --git a/Libraries/LibWeb/Rust/src/layout/line_builder.rs b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
index f0e2edc7cea..02feed7748d 100644
--- a/Libraries/LibWeb/Rust/src/layout/line_builder.rs
+++ b/Libraries/LibWeb/Rust/src/layout/line_builder.rs
@@ -325,7 +325,11 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
self.line_count() <= 1 && !self.is_continuation,
forced == ForcedBreak::Yes,
);
- self.line_mut(line_index).inline_length += indent;
+ {
+ let mut line = self.line_mut(line_index);
+ line.inline_length += indent;
+ line.indent = indent;
+ }
self.current_line_committed_pending_margin = false;
}
@@ -1667,6 +1671,12 @@ impl<'builder, 'context> LineBuilder<'builder, 'context> {
self.line(line_index).inline_length
- snapshot.inline_offset
- self.line(line_index).fragments[fragment_index].inline_length
+ } else if self.line(line_index).direction == direction::RTL {
+ // https://drafts.csswg.org/css-text-3/#text-indent-property
+ // "The indent is treated as a margin applied to the start edge of the line box."
+ // NB: The start edge of a right-to-left line is its right end, so the content comes before the indent
+ // from the left, even though the line's inline length counts the indent first.
+ snapshot.inline_offset - self.line(line_index).indent
} else {
snapshot.inline_offset
};
diff --git a/Tests/LibWeb/Text/expected/css/text-indent-in-right-to-left-block.txt b/Tests/LibWeb/Text/expected/css/text-indent-in-right-to-left-block.txt
new file mode 100644
index 00000000000..456e540790a
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/text-indent-in-right-to-left-block.txt
@@ -0,0 +1,5 @@
+rtl: first word from 238.0625 to 260, at 5
+rtl-left: first word from 46.5625 to 68.5, at 5
+rtl-hanging: first word from 278.0625 to 300, at 5
+rtl-negative: first word from 298.0625 to 320, at 5
+ltr: first word from 40 to 75.875, at 5
diff --git a/Tests/LibWeb/Text/input/css/text-indent-in-right-to-left-block.html b/Tests/LibWeb/Text/input/css/text-indent-in-right-to-left-block.html
new file mode 100644
index 00000000000..48dbfcadffb
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/text-indent-in-right-to-left-block.html
@@ -0,0 +1,27 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<script src="../include.js"></script>
+<style>
+ div { font: 20px/30px SerenitySans; width: 300px; text-indent: 40px; }
+</style>
+<!-- text-indent indents the start of the first line: its right end in a right-to-left block. -->
+<div id="rtl" dir="rtl">אחת שתיים שלוש ארבע חמש שש שבע שמונה תשע עשר</div>
+<div id="rtl-left" dir="rtl" style="text-align: left">אחת שתיים</div>
+<div id="rtl-hanging" dir="rtl" style="text-indent: 40px hanging">אחת שתיים שלוש ארבע חמש שש שבע שמונה תשע עשר</div>
+<div id="rtl-negative" dir="rtl" style="text-indent: -20px">אחת שתיים</div>
+<div id="ltr">one two</div>
+<script>
+ test(() => {
+ // The first word is the rightmost on the first line of a right-to-left block, and the leftmost in the
+ // left-to-right one.
+ for (const div of document.querySelectorAll("div")) {
+ const text = div.firstChild;
+ const range = document.createRange();
+ range.setStart(text, 0);
+ range.setEnd(text, text.data.indexOf(" "));
+ const word = range.getBoundingClientRect();
+ const box = div.getBoundingClientRect();
+ println(`${div.id}: first word from ${word.left - box.left} to ${word.right - box.left}, at ${word.top - box.top}`);
+ }
+ });
+</script>