diff --git a/moli-layout/src/overflow.rs b/moli-layout/src/overflow.rs index e6f58c22cf7..119edb25128 100644 --- a/moli-layout/src/overflow.rs +++ b/moli-layout/src/overflow.rs @@ -617,7 +617,7 @@ where } else if world.is_viewport_defining_body(id) { false } else { - world.boxes[id.index()].style.establishes_scroll_container() + world.boxes[id.index()].establishes_scroll_container() } } diff --git a/moli-layout/src/projection.rs b/moli-layout/src/projection.rs index 13ec16f80c4..ad2a8fdf1f6 100644 --- a/moli-layout/src/projection.rs +++ b/moli-layout/src/projection.rs @@ -319,9 +319,7 @@ where } else if self.world.is_viewport_defining_body(id) { false } else { - self.world.boxes[id.index()] - .style - .establishes_scroll_container() + self.world.boxes[id.index()].establishes_scroll_container() } } diff --git a/moli-layout/src/source.rs b/moli-layout/src/source.rs index afa396ea0ec..1a8253b7f82 100644 --- a/moli-layout/src/source.rs +++ b/moli-layout/src/source.rs @@ -254,6 +254,13 @@ impl LayoutInputControlKind { Self::Week => "form-input-week", } } + + pub(crate) const fn has_single_line_text_editor(self) -> bool { + matches!( + self, + Self::Email | Self::Password | Self::Search | Self::Telephone | Self::Text | Self::Url + ) + } } /// Replaced-content family. Pixel resources are deliberately not part of this type. @@ -429,6 +436,14 @@ impl LayoutElementSemantics { ) } + pub(crate) const fn has_internal_text_control_scroller(&self) -> bool { + matches!( + self.category, + LayoutElementCategory::FormControl(LayoutFormControlKind::Input(kind)) + if kind.has_single_line_text_editor() + ) + } + /// Whether `display: contents` has a used value of `display: none`. /// /// This is the unusual-elements list from CSS Display's HTML appendix: diff --git a/moli-layout/src/world.rs b/moli-layout/src/world.rs index 28fb21b7581..3cce278b2b9 100644 --- a/moli-layout/src/world.rs +++ b/moli-layout/src/world.rs @@ -610,6 +610,14 @@ impl LayoutBox { .is_some_and(LayoutElementSemantics::is_replaced) } + pub(crate) fn establishes_scroll_container(&self) -> bool { + self.style.establishes_scroll_container() + || self + .element_semantics + .as_ref() + .is_some_and(LayoutElementSemantics::has_internal_text_control_scroller) + } + /// Resolve the used ratio at the layout-node boundary, after both authored /// style and natural replaced-element sizing are available. pub(crate) fn resolved_aspect_ratio(&self) -> Option { diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/geometry_and_scrolling.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/geometry_and_scrolling.rs index 390847a51c9..f322d2d74ee 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/geometry_and_scrolling.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/dom_surface/extracted/geometry_and_scrolling.rs @@ -1393,3 +1393,66 @@ fn element_scroll_methods_leave_a_detached_element_at_zero() { r#"{"protoScroll":"function","protoScrollTo":"function","protoScrollBy":"function","first":[0,0],"second":[0,0],"third":[0,0],"fourth":[0,0],"fifth":[0,0],"enumerable":false}"# ); } + +#[test] +fn single_line_text_input_preserves_programmatic_scroll_across_select() { + let mut vm = new_storage_test_vm("https://text-input-scroll.test/"); + vm.eval( + r#" + (() => { + if (!document.documentElement) { + document.appendChild(document.createElement("html")); + } + if (!document.body) { + document.documentElement.appendChild(document.createElement("body")); + } + const previous = document.createElement("input"); + previous.value = "0123456789".repeat(100); + document.body.append(previous); + globalThis.__previousScrollInput = previous; + return "installed"; + })() + "#, + ) + .expect("text input scroll fixture should initialize"); + refresh_layout_for_test(&mut vm); + vm.eval("__previousScrollInput.scrollLeft") + .expect("the previous input should retain the frozen layout"); + + vm.eval( + r#" + (() => { + __previousScrollInput.remove(); + const input = document.createElement("input"); + input.value = "0123456789".repeat(100); + document.body.append(input); + globalThis.__scrollInput = input; + return "replaced"; + })() + "#, + ) + .expect("the previous text input should be replaced"); + + vm.eval("__scrollInput.scrollLeft = 33") + .expect("text input should accept a programmatic scroll"); + refresh_layout_for_test(&mut vm); + assert_eq!( + vm.eval("__scrollInput.scrollWidth > __scrollInput.clientWidth") + .expect("text input overflow should be observable"), + "true" + ); + assert_eq!( + vm.eval("__scrollInput.scrollLeft") + .expect("text input scroll should remain observable"), + "33" + ); + + vm.eval("__scrollInput.select()") + .expect("selecting the input contents should succeed"); + refresh_layout_for_test(&mut vm); + assert_eq!( + vm.eval("__scrollInput.scrollLeft") + .expect("selection should preserve the text input scroll"), + "33" + ); +}