Skip to content

Truly independent web browser

LibWeb: Don’t crash on a tab after text when tab-size is 0

sideshowbarker11829b98a008
diff --git a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
index 5d85f9be135..f038d3d5a62 100644
--- a/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
+++ b/Libraries/LibWeb/Rust/src/layout/inline_level_iterator.rs
@@ -2202,24 +2202,30 @@ impl<'context> RunItemResolver<'_, 'context> {
let tab_stop = prepared.tab_stops[self.next_tab_stop];
self.next_tab_stop += 1;
let tab_inline_size = tab_stop.tab_inline_size;
- let accumulated = self.accumulated_inline_size_for_tabs;
- let mut tab_stop_distance = if accumulated > CssPixels::default() {
- accumulated.div_as_fraction(tab_inline_size).ceil() * tab_inline_size - accumulated
- } else {
- tab_inline_size
- };
- if tab_stop_distance.to_double() < f64::from(tab_stop.zero_advance) * 0.5 {
- tab_stop_distance += tab_inline_size;
+ // https://drafts.csswg.org/css-text-3/#white-space-phase-2
+ // "If the tab size is zero, preserved tabs are not rendered."
+ if tab_inline_size != CssPixels::default() {
+ let accumulated = self.accumulated_inline_size_for_tabs;
+ let mut tab_stop_distance = if accumulated > CssPixels::default() {
+ accumulated.div_as_fraction(tab_inline_size).ceil() * tab_inline_size - accumulated
+ } else {
+ tab_inline_size
+ };
+ if tab_stop_distance.to_double() < f64::from(tab_stop.zero_advance) * 0.5 {
+ tab_stop_distance += tab_inline_size;
+ }
+ tab_stop_distance = tab_stop_distance * tab_stop.tab_count as usize;
+ inline_offset = tab_stop_distance.to_double() as f32;
}
- tab_stop_distance = tab_stop_distance * tab_stop.tab_count as usize;
- inline_offset = tab_stop_distance.to_double() as f32;
upright_advance_after_tabs = tab_stop.upright_advance;
tab_count = tab_stop.tab_count as usize;
}
let shape = prepared_item.shape();
let mut width = shape.width();
let mut owned_glyph_run = None;
- if inline_offset != 0.0 || upright_advance_after_tabs.is_some() {
+ // NB: Tabs that tab-size: 0 doesn't render still get the spacer glyph, so that the code units of the item's
+ // glyphs line up with those of its text.
+ if inline_offset != 0.0 || upright_advance_after_tabs.is_some() || tab_count > 0 {
let mut glyphs = shaped_glyphs_from(shape, inline_offset);
if let Some(advance) = upright_advance_after_tabs {
width = set_glyphs_upright(&mut glyphs, inline_offset, advance);
diff --git a/Tests/LibWeb/Text/expected/css/tab-size-zero-tabs-after-text.txt b/Tests/LibWeb/Text/expected/css/tab-size-zero-tabs-after-text.txt
new file mode 100644
index 00000000000..826a5c97d18
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/tab-size-zero-tabs-after-text.txt
@@ -0,0 +1,3 @@
+after-text: X at 10px
+at-line-start: X at 0px
+in-two-nodes: X at 10px
diff --git a/Tests/LibWeb/Text/input/css/tab-size-zero-tabs-after-text.html b/Tests/LibWeb/Text/input/css/tab-size-zero-tabs-after-text.html
new file mode 100644
index 00000000000..b41a696be56
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/tab-size-zero-tabs-after-text.html
@@ -0,0 +1,28 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ pre { font: 10px/20px Ahem; margin: 0; tab-size: 0; }
+</style>
+<!-- With tab-size: 0, preserved tabs aren't rendered, so the X comes right after what's before the tabs. -->
+<div id="host">
+<pre id="after-text">a X</pre>
+<pre id="at-line-start"> X</pre>
+<pre id="in-two-nodes">a <span> </span>X</pre>
+</div>
+<script>
+ test(() => {
+ for (const pre of document.querySelectorAll("#host pre")) {
+ const walker = document.createTreeWalker(pre, NodeFilter.SHOW_TEXT);
+ let node;
+ while ((node = walker.nextNode())) {
+ const offset = node.data.indexOf("X");
+ if (offset < 0)
+ continue;
+ const range = document.createRange();
+ range.setStart(node, offset);
+ range.setEnd(node, offset + 1);
+ println(`${pre.id}: X at ${range.getBoundingClientRect().left - pre.getBoundingClientRect().left}px`);
+ }
+ }
+ });
+</script>