Truly independent web browser
LibWeb: Scroll local backgrounds with their contents
sideshowbarker454ab63fc003
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rsindex 643299e4f01..400580c9d6a 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs@@ -549,6 +549,37 @@ fn paint_image_layer<O: Observer>();};+ let scroll_node = recorder.data(paintable).own_scroll_node_index;+ if layer.attachment == css_enums::background_attachment::LOCAL+ && recorder.source.node_kind_if_live(paintable) != Some(NodeKind::Viewport)+ && scroll_node != VISUAL_VIEWPORT_NODE_INDEX+ && !recorder.recorder.is_recording_inside_group()+ && let Some(scrollable_area) =+ crate::painting::paintable_geometry::scrollable_overflow_rect(recorder.source, paintable)+ {+ // https://drafts.csswg.org/css-backgrounds-3/#valdef-background-attachment-local+ // "the background scrolls with the box's contents"+ // NB: Keep the box's clips outside its scrolling space, and record every part scrolling can reveal.+ let previous_context = recorder.recorder.accumulated_visual_context();+ let previous_inline_clips = recorder+ .recorder+ .move_ambient_inline_clips_to_space(previous_context.spatial);+ recorder.recorder.set_accumulated_visual_context(ContextRef {+ spatial: scroll_node,+ clip: recorder.data(paintable).accumulated_visual_context_for_descendants.clip,+ ..previous_context+ });+ paint_in_current_space(+ recorder,+ scrollable_area,+ recorder.converter.enclosing_device_rect(scrollable_area),+ None,+ );+ recorder.recorder.set_accumulated_visual_context(previous_context);+ recorder.recorder.replace_ambient_inline_clips(previous_inline_clips);+ return;+ }+if layer.attachment != css_enums::background_attachment::FIXED|| recorder.recorder.is_recording_inside_group()|| !background_has_fixed_attachment(@@ -638,7 +669,9 @@ fn paint_image_layer_in_current_space<O: Observer>(match layer.attachment {css_enums::background_attachment::LOCAL- if recorder.source.node_kind_if_live(paintable) != Some(NodeKind::Viewport) =>+ if recorder.source.node_kind_if_live(paintable) != Some(NodeKind::Viewport)+ && recorder.recorder.accumulated_visual_context().spatial+ != recorder.data(paintable).own_scroll_node_index =>{let scroll_offset = recorder.own_scroll_container_offset(paintable);background_positioning_area = background_positioning_area.translated(-scroll_offset.x, -scroll_offset.y);@@ -730,6 +763,18 @@ fn paint_image_layer_in_current_space<O: Observer>()});+ let tile_device_rect = |rect: CssPixelRect| -> IntRect {+ let mut rect = converter.rounded_device_rect(rect);+ // If the image's dimensions were rounded to zero then they need to be restored to avoid a crash.+ if rect.width == 0 {+ rect.width = 1;+ }+ if rect.height == 0 {+ rect.height = 1;+ }+ rect+ };+let device_rects = |image_rect: CssPixelRect| -> Vec<IntRect> {let mut rects = Vec::new();let mut rect = image_rect;@@ -739,14 +784,7 @@ fn paint_image_layer_in_current_space<O: Observer>(let mut image_x = initial_image_x;while image_x < css_clip_rect.right() {rect.x = image_x;- let mut image_device_rect = converter.rounded_device_rect(rect);- // If the image's dimensions were rounded to zero then they need to be restored to avoid a crash.- if image_device_rect.width == 0 {- image_device_rect.width = 1;- }- if image_device_rect.height == 0 {- image_device_rect.height = 1;- }+ let image_device_rect = tile_device_rect(rect);if visible_device_rect.is_none_or(|visible| !visible.intersected(image_device_rect).is_empty()) {rects.push(image_device_rect);}@@ -777,27 +815,35 @@ fn paint_image_layer_in_current_space<O: Observer>(let inline_operator = compositing_and_blending_operator;- if let (None, Some(single_pixel_color)) = (&resolved_gradient, facts.single_pixel_color) {- // OPTIMIZATION: If the image is a single pixel, we can just fill the whole area with it.- // However, we must first figure out the real coverage area, taking repeat etc into account.-- // FIXME: This could be written in a far more efficient way.- let mut fill_rect: Option<IntRect> = None;- for image_device_rect in device_rects(image_rect) {- fill_rect = Some(match fill_rect {- None => image_device_rect,- Some(current) => current.united(image_device_rect),- });+ if let (None, Some(single_pixel_color)) = (&resolved_gradient, facts.single_pixel_color)+ && !repeat_x_has_gap+ && !repeat_y_has_gap+ {+ // OPTIMIZATION: The first and last tiles bound the fill, without enumerating every tile in the scrollable area.+ // Round both tiles separately to preserve the one-device-pixel minimum size at the end of each repeated axis.+ let mut fill_rect = IntRect::default();+ if image_rect.x < css_clip_rect.right() && image_rect.y < css_clip_rect.bottom() {+ let mut last_tile = image_rect;+ if repeat_x {+ last_tile.x =+ first_css_tile_start(image_rect.x, css_clip_rect.right() - CssPixels::from_raw(1), x_step);+ }+ if repeat_y {+ last_tile.y =+ first_css_tile_start(image_rect.y, css_clip_rect.bottom() - CssPixels::from_raw(1), y_step);+ }+ fill_rect = tile_device_rect(image_rect).united(tile_device_rect(last_tile));+ if let Some(visible) = visible_device_rect {+ fill_rect = fill_rect.intersected(visible);+ }}if inline_operator == CompositingAndBlendingOperator::Normal {- recorder.recorder.fill_rect(- fill_rect.unwrap_or_default(),- single_pixel_color,- ForceDarkRole::Background,- );+ recorder+ .recorder+ .fill_rect(fill_rect, single_pixel_color, ForceDarkRole::Background);} else {recorder.recorder.fill_rect_with_compositing_and_blending_operator(- fill_rect.unwrap_or_default(),+ fill_rect,single_pixel_color,inline_operator,ForceDarkRole::Background,diff --git a/Tests/LibWeb/Ref/expected/background-attachment-local-scroll-space-ref.html b/Tests/LibWeb/Ref/expected/background-attachment-local-scroll-space-ref.htmlnew file mode 100644index 00000000000..27a1d6b07e3--- /dev/null+++ b/Tests/LibWeb/Ref/expected/background-attachment-local-scroll-space-ref.html@@ -0,0 +1,20 @@+<!doctype html>+<style>+body { margin: 0; }+.scroller { display: inline-block; width: 150px; height: 100px; overflow: auto; scrollbar-width: none; outline: 4px solid black; margin: 18px; }+.content { width: 300px; height: 400px; }+.clipped { clip-path: inset(10px 20px); }+.transformed { transform: translateY(30px); transform-origin: top left; }+.content { background-image: linear-gradient(90deg, transparent 100px, cyan 100px 200px, transparent 200px), linear-gradient(red 0 50px, blue 50px 100px, yellow 100px 150px, green 150px 400px); background-repeat: no-repeat; }+.repeating .content { background-image: linear-gradient(45deg, blue 50%, gold 50%); background-size: 40px 40px; background-repeat: repeat; }+</style>+<div class="scroller"><div class="content"></div></div>+<div class="scroller clipped"><div class="content"></div></div>+<div class="scroller transformed"><div class="content"></div></div>+<div class="scroller repeating"><div class="content"></div></div>+<script>+for (const scroller of document.querySelectorAll(".scroller")) {+ scroller.scrollLeft = 70;+ scroller.scrollTop = 75;+}+</script>\ No newline at end of filediff --git a/Tests/LibWeb/Ref/expected/single-pixel-background-repeat-space-ref.html b/Tests/LibWeb/Ref/expected/single-pixel-background-repeat-space-ref.htmlnew file mode 100644index 00000000000..f8cf4f72dbb--- /dev/null+++ b/Tests/LibWeb/Ref/expected/single-pixel-background-repeat-space-ref.html@@ -0,0 +1,19 @@+<!DOCTYPE html>+<style>+ body { margin: 0; padding: 10px; display: flex; flex-direction: column; align-items: start; gap: 10px; }+ .tiles { position: relative; background: yellow; }+ .horizontal { width: 25px; height: 10px; }+ .vertical { width: 10px; height: 25px; }+ .both { width: 25px; height: 25px; }+ .tile { position: absolute; width: 10px; height: 10px; background: rgb(17, 93, 151); }+ .right { right: 0; }+ .bottom { bottom: 0; }+ .gap { width: 5px; height: 10px; background: yellow; }+ .solid { width: 25px; height: 25px; background: rgb(17, 93, 151); }+</style>+<div class="tiles horizontal"><div class="tile"></div><div class="tile right"></div></div>+<div class="tiles vertical"><div class="tile"></div><div class="tile bottom"></div></div>+<div class="tiles both"><div class="tile"></div><div class="tile right"></div><div class="tile bottom"></div><div class="tile right bottom"></div></div>+<div class="gap"></div>+<div class="solid"></div>+<div class="solid"></div>diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-backgrounds/background-attachment-local/aqua-yellow-32x32.png b/Tests/LibWeb/Ref/expected/wpt-import/css/css-backgrounds/background-attachment-local/aqua-yellow-32x32.pngnew file mode 100644index 00000000000..42f8a2100b2Binary files /dev/null and b/Tests/LibWeb/Ref/expected/wpt-import/css/css-backgrounds/background-attachment-local/aqua-yellow-32x32.png differdiff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/css-backgrounds/background-attachment-local/attachment-local-positioning-2-ref.html b/Tests/LibWeb/Ref/expected/wpt-import/css/css-backgrounds/background-attachment-local/attachment-local-positioning-2-ref.htmlnew file mode 100644index 00000000000..e61ccba6c7d--- /dev/null+++ b/Tests/LibWeb/Ref/expected/wpt-import/css/css-backgrounds/background-attachment-local/attachment-local-positioning-2-ref.html@@ -0,0 +1,25 @@+<!doctype html>+<title>CSS Reftest Reference: background-attachment: local</title>+<link rel="author" title="Simon Sapin" href="http://exyr.org/about/" />+<style>+#outer {+ border: solid;+ background: url(aqua-yellow-32x32.png) local no-repeat 178px 278px;+ overflow: hidden;+ width: 200px;+ height: 300px;+}+div div {+ width: 250px;+ height: 370px;+}+p {+ margin: 0 0 0 -40px;+ padding-top: 40px;+}+</style>+<div id=outer>+ <div>+ <p>Lorem ipsum dolor sit amet</p>+ </div>+</div>diff --git a/Tests/LibWeb/Ref/input/background-attachment-local-scroll-space.html b/Tests/LibWeb/Ref/input/background-attachment-local-scroll-space.htmlnew file mode 100644index 00000000000..fe74f062931--- /dev/null+++ b/Tests/LibWeb/Ref/input/background-attachment-local-scroll-space.html@@ -0,0 +1,21 @@+<!doctype html>+<link rel="match" href="../expected/background-attachment-local-scroll-space-ref.html">+<style>+body { margin: 0; }+.scroller { display: inline-block; width: 150px; height: 100px; overflow: auto; scrollbar-width: none; outline: 4px solid black; margin: 18px; }+.content { width: 300px; height: 400px; }+.clipped { clip-path: inset(10px 20px); }+.transformed { transform: translateY(30px); transform-origin: top left; }+.scroller { background-image: linear-gradient(90deg, transparent 100px, cyan 100px 200px, transparent 200px), linear-gradient(red 0 50px, blue 50px 100px, yellow 100px 150px, green 150px 400px); background-attachment: local; background-repeat: no-repeat; }+.repeating { background-image: linear-gradient(45deg, blue 50%, gold 50%); background-size: 40px 40px; background-repeat: repeat; }+</style>+<div class="scroller"><div class="content"></div></div>+<div class="scroller clipped"><div class="content"></div></div>+<div class="scroller transformed"><div class="content"></div></div>+<div class="scroller repeating"><div class="content"></div></div>+<script>+for (const scroller of document.querySelectorAll(".scroller")) {+ scroller.scrollLeft = 70;+ scroller.scrollTop = 75;+}+</script>\ No newline at end of filediff --git a/Tests/LibWeb/Ref/input/single-pixel-background-repeat-space.html b/Tests/LibWeb/Ref/input/single-pixel-background-repeat-space.htmlnew file mode 100644index 00000000000..085466dbdb9--- /dev/null+++ b/Tests/LibWeb/Ref/input/single-pixel-background-repeat-space.html@@ -0,0 +1,23 @@+<!DOCTYPE html>+<link rel="match" href="../expected/single-pixel-background-repeat-space-ref.html" />+<style>+ body { margin: 0; padding: 10px; display: flex; flex-direction: column; align-items: start; gap: 10px; }+ .tiles {+ background-color: yellow;+ background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGMQjJ3+HwADjQIFbmxN3gAAAABJRU5ErkJggg==");+ background-size: 10px 10px;+ }+ .horizontal { width: 25px; height: 10px; background-repeat: space no-repeat; }+ .vertical { width: 10px; height: 25px; background-repeat: no-repeat space; }+ .both { width: 25px; height: 25px; background-repeat: space; }+ .round { width: 25px; height: 25px; background-repeat: round; }+ .repeat { width: 25px; height: 25px; background-repeat: repeat; }+ .clip { width: 5px; height: 10px; overflow: hidden; }+ .clip > div { position: relative; left: -10px; }+</style>+<div class="tiles horizontal"></div>+<div class="tiles vertical"></div>+<div class="tiles both"></div>+<div class="clip"><div class="tiles horizontal"></div></div>+<div class="tiles round"></div>+<div class="tiles repeat"></div>diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-backgrounds/background-attachment-local/aqua-yellow-32x32.png b/Tests/LibWeb/Ref/input/wpt-import/css/css-backgrounds/background-attachment-local/aqua-yellow-32x32.pngnew file mode 100644index 00000000000..42f8a2100b2Binary files /dev/null and b/Tests/LibWeb/Ref/input/wpt-import/css/css-backgrounds/background-attachment-local/aqua-yellow-32x32.png differdiff --git a/Tests/LibWeb/Ref/input/wpt-import/css/css-backgrounds/background-attachment-local/attachment-local-positioning-2.html b/Tests/LibWeb/Ref/input/wpt-import/css/css-backgrounds/background-attachment-local/attachment-local-positioning-2.htmlnew file mode 100644index 00000000000..e18549fee8f--- /dev/null+++ b/Tests/LibWeb/Ref/input/wpt-import/css/css-backgrounds/background-attachment-local/attachment-local-positioning-2.html@@ -0,0 +1,35 @@+<!doctype html>+<title>CSS Test: background-attachment: local; positioning area</title>+<link rel="match" href="../../../../../expected/wpt-import/css/css-backgrounds/background-attachment-local/attachment-local-positioning-2-ref.html" />+<meta name="flags" content="dom" />+<link rel="author" title="Simon Sapin" href="http://exyr.org/about/" />+<link rel="help" href="http://www.w3.org/TR/css3-background/#the-background-attachment" />+<style>+#outer {+ border: solid;+ /* 100% 100% == (250px - 32px) (370px - 32px) == 218px 338px */+ /* With scrolling, effective position is 178px 278px */+ background: url(aqua-yellow-32x32.png) local no-repeat 100% 100%;+ overflow: hidden;+ width: 200px;+ height: 300px;+}+div div {+ width: 250px;+ height: 370px;+}+p {+ margin: 0;+ padding-top: 100px;+}+</style>+<div id=outer>+ <div>+ <p>Lorem ipsum dolor sit amet</p>+ </div>+</div>+<script>+var outer = document.getElementById('outer');+outer.scrollLeft = 40;+outer.scrollTop = 60;+</script>diff --git a/Tests/LibWeb/Text/expected/display_list/local-background-scroll-space.txt b/Tests/LibWeb/Text/expected/display_list/local-background-scroll-space.txtnew file mode 100644index 00000000000..4090eb9a95a--- /dev/null+++ b/Tests/LibWeb/Text/expected/display_list/local-background-scroll-space.txt@@ -0,0 +1,7 @@+linear: background moves with its content: true+linear after scrolling: background moves with its content: true+conic: background moves with its content: true+rounded radial: background moves with its content: true+transformed radial: background moves with its content: true+repeating: background moves with its content: true+Repeated background covers content below the scrollport: truediff --git a/Tests/LibWeb/Text/expected/display_list/single-pixel-local-background.txt b/Tests/LibWeb/Text/expected/display_list/single-pixel-local-background.txtnew file mode 100644index 00000000000..cc99dcc4751--- /dev/null+++ b/Tests/LibWeb/Text/expected/display_list/single-pixel-local-background.txt@@ -0,0 +1,9 @@+1 trillion tiles: 1 fill, rect=[0,0 1000000x1000000]+scrolled to the end: 1 fill, rect=[0,0 1000000x1000000]+repeat-x: 1 fill, rect=[-4,5 210x11]+repeat-y: 1 fill, rect=[3,-6 7x319]+no-repeat: 1 fill, rect=[3,5 7x11]+fractional tiles: 1 fill, rect=[0,-1 205x309]+tile rounded to zero: 1 fill, rect=[3,5 1x1]+repeating tiles rounded to zero: 1 fill, rect=[0,0 204x308]+tile outside the clip: 0 fill, undefineddiff --git a/Tests/LibWeb/Text/input/display_list/local-background-scroll-space.html b/Tests/LibWeb/Text/input/display_list/local-background-scroll-space.htmlnew file mode 100644index 00000000000..37149cf6d84--- /dev/null+++ b/Tests/LibWeb/Text/input/display_list/local-background-scroll-space.html@@ -0,0 +1,35 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ #scroller { width: 100px; height: 100px; overflow: auto; background: linear-gradient(red, blue) local; }+ #content { width: 200px; height: 400px; background: rgba(0, 255, 0, 0.1); }+</style>+<div id="scroller"><div id="content"></div></div>+<script>+ test(() => {+ function checkBackground(label) {+ const list = internals.dumpDisplayList();+ const gradient = list.match(/Paint(?:Linear|Radial|Conic)Gradient@(s[0-9]+)/);+ const content = list.match(/FillRect@(s[0-9]+)[^\n]*color=rgba\(0, 255, 0,/);+ println(`${label}: background moves with its content: ${!!gradient && !!content && gradient[1] === content[1]}`);+ }+ checkBackground("linear");+ scroller.scrollLeft = 50;+ scroller.scrollTop = 75;+ checkBackground("linear after scrolling");+ scroller.style.backgroundImage = "conic-gradient(red, yellow, green, blue, purple, magenta)";+ checkBackground("conic");+ scroller.style.backgroundImage = "radial-gradient(red, blue)";+ scroller.style.borderRadius = "30px";+ checkBackground("rounded radial");+ scroller.style.transform = "scale(1.5)";+ checkBackground("transformed radial");+ scroller.style.backgroundImage = "linear-gradient(45deg, blue 50%, gold 50%)";+ scroller.style.backgroundSize = "40px 40px";+ checkBackground("repeating");+ const list = internals.dumpDisplayList();+ const repeated = list.match(/DrawRepeatedTile@[^\n]*clip_rect=\[(-?\d+),(-?\d+) (\d+)x(\d+)\]/);+ const bottom = repeated ? Number(repeated[2]) + Number(repeated[4]) : 0;+ println(`Repeated background covers content below the scrollport: ${bottom >= 408}`);+ });+</script>diff --git a/Tests/LibWeb/Text/input/display_list/single-pixel-local-background.html b/Tests/LibWeb/Text/input/display_list/single-pixel-local-background.htmlnew file mode 100644index 00000000000..86f78e8e468--- /dev/null+++ b/Tests/LibWeb/Text/input/display_list/single-pixel-local-background.html@@ -0,0 +1,51 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<style>+ body { margin: 0; }+ #scroller { width: 100px; height: 100px; overflow: auto; background-attachment: local; }+ #content { width: 1000000px; height: 1000000px; }+</style>+<div id="scroller"><div id="content"></div></div>+<script>+ promiseTest(async () => {+ const image = new Image();+ image.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGMQjJ3+HwADjQIFbmxN3gAAAABJRU5ErkJggg==";+ await image.decode();+ scroller.style.backgroundImage = `url(${image.src})`;++ function fill(label) {+ const list = internals.dumpDisplayList();+ const fills = list.split("\n").filter(line => line.includes("FillRect@") && line.includes("color=rgb(17, 93, 151)"));+ println(`${label}: ${fills.length} fill, ${fills[0]?.match(/rect=\[[^\]]+\]/)?.[0]}`);+ }++ fill("1 trillion tiles");+ scroller.scrollLeft = 999900;+ scroller.scrollTop = 999900;+ fill("scrolled to the end");++ content.style.width = "203px";+ content.style.height = "307px";+ scroller.scrollTo(0, 0);+ scroller.style.backgroundSize = "7px 11px";+ scroller.style.backgroundPosition = "3px 5px";+ for (const repeat of ["repeat-x", "repeat-y", "no-repeat"]) {+ scroller.style.backgroundRepeat = repeat;+ fill(repeat);+ }+ scroller.style.backgroundSize = "2.5px 1.5px";+ scroller.style.backgroundPosition = "2.25px 3.5px";+ scroller.style.backgroundRepeat = "repeat";+ fill("fractional tiles");+ scroller.style.backgroundRepeat = "no-repeat";+ scroller.style.backgroundSize = "0.125px 0.125px";+ scroller.style.backgroundPosition = "3px 5px";+ fill("tile rounded to zero");+ scroller.style.backgroundRepeat = "repeat";+ scroller.style.backgroundPosition = "0px 0px";+ fill("repeating tiles rounded to zero");+ scroller.style.backgroundRepeat = "no-repeat";+ scroller.style.backgroundPosition = "1000px 1000px";+ fill("tile outside the clip");+ });+</script>