Truly independent web browser
LibWeb: Don’t paint a background clipped to text with no text
sideshowbarkerde5c69811d0e
diff --git a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rsindex 495a6dfc962..27432ef334a 100644--- a/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs+++ b/Libraries/LibWeb/Rust/src/painting/record/paint/background.rs@@ -139,8 +139,21 @@ pub(crate) fn paint_resolved_background<O: Observer>(// https://drafts.csswg.org/css-backgrounds-4/#valdef-background-clip-textlet needs_text_clip = resolved.needs_text_clip && !inputs.is_root_element;+ let text_clip_fragments = if needs_text_clip {+ text_clip_fragments(recorder, paintable)+ } else {+ Vec::new()+ };+ // With no text, nothing clipped to it shows: Paint just the layers clipped to some other box, without a mask.+ let omit_text_clipped = needs_text_clip && text_clip_fragments.is_empty();+ let needs_text_clip = needs_text_clip && !omit_text_clipped;+ // A blending layer can't blend with the color under it when the color is left out.+ let backdrop = match backdrop {+ LayerBackdrop::OpaqueColorUnderLoneLayer(_) if omit_text_clipped => LayerBackdrop::IsolatedGroup,+ backdrop => backdrop,+ };if !backdrop.is_isolated_group() && !needs_text_clip {- paint_background_layers(recorder, paintable, inputs, backdrop);+ paint_background_layers(recorder, paintable, inputs, backdrop, omit_text_clipped);return;}@@ -157,12 +170,12 @@ pub(crate) fn paint_resolved_background<O: Observer>(};let mut group = recorder.recorder.begin_isolated_group();recorder.trace_paint(Operation::Named(Some(paintable), "background-group"), |recorder| {- paint_background_layers(recorder, paintable, inputs, backdrop);+ paint_background_layers(recorder, paintable, inputs, backdrop, omit_text_clipped);});if needs_text_clip {recorder.recorder.begin_group_mask(&mut group);recorder.trace_paint(Operation::Named(Some(paintable), "background-text-mask"), |recorder| {- append_text_clip_paths(recorder, paintable);+ append_text_clip_paths(recorder, &text_clip_fragments);});}recorder.recorder.finish_isolated_group(@@ -178,6 +191,8 @@ fn paint_background_layers<O: Observer>(paintable: NodeSlotId,inputs: &BackgroundPaintInputs<'_>,backdrop: LayerBackdrop,+ // Leaves out the color and the layers clipped to the text, which the caller found none of.+ omit_text_clipped: bool,) {let converter = recorder.converter;let resolved = &inputs.resolved;@@ -237,7 +252,9 @@ fn paint_background_layers<O: Observer>(})};- if is_root_element {+ if omit_text_clipped {+ // The color is clipped to the text.+ } else if is_root_element {recorder.recorder.fill_animated_background_color(converter.enclosing_device_rect(color_box.rect),color,@@ -311,6 +328,9 @@ fn paint_background_layers<O: Observer>(// Background layers are ordered front-to-back, so we paint them in reverse.for layer in layers.iter().rev() {+ if omit_text_clipped && layer.clip == css_enums::background_box::TEXT {+ continue;+ }let clip_box = background_box_for(layer.clip, border_box, padding, border);let css_clip_rect = clip_box.rect;let unshrunken_clip_rect = converter.rounded_device_rect(css_clip_rect);@@ -1087,26 +1107,16 @@ fn source_rect_for_visible_image_part()}-fn append_text_clip_paths<O: Observer>(recorder: &mut PaintRecorder<'_, O>, paintable: NodeSlotId) {+fn append_text_clip_paths<O: Observer>(recorder: &mut PaintRecorder<'_, O>, fragments: &[(NodeSlotId, usize)]) {let converter = recorder.converter;let scale = recorder.inputs.device_pixels_per_css_pixel;- let append_fragment = |recorder: &mut PaintRecorder<'_, O>, owner: NodeSlotId, fragment_index: usize| {+ for &(owner, fragment_index) in fragments {let side = recorder.source.committed_side_data(owner);let fragment = &side.fragments()[fragment_index];- let is_text = recorder- .source- .node_kind_if_live(fragment.layout_node)- .is_some_and(node_facts::kind_is_text);- if !is_text {- return;- }let Some(run) = &fragment.glyph_run else {- return;+ continue;};- if run.glyphs.is_empty() {- return;- }let fragment_absolute_rect = crate::painting::text_fragment::absolute_rect(recorder.source, fragment);let fragment_absolute_device_rect = converter.enclosing_device_rect(fragment_absolute_rect);let font_id = recorder.register_font(&run.font);@@ -1139,6 +1149,26 @@ fn append_text_clip_paths<O: Observer>(recorder: &mut PaintRecorder<'_, O>, painemission.glyph_bounding_rect,ForceDarkRole::None,);+ }+}++/// The text fragments, in the paintable and its in-flow and floated descendants, whose glyphs the background is clipped+/// to: each one as its owner and its index among the owner's fragments.+fn text_clip_fragments<O: Observer>(+ recorder: &PaintRecorder<'_, O>,+ paintable: NodeSlotId,+) -> Vec<(NodeSlotId, usize)> {+ let mut fragments = Vec::new();+ let mut append_fragment = |owner: NodeSlotId, fragment_index: usize| {+ let side = recorder.source.committed_side_data(owner);+ let fragment = &side.fragments()[fragment_index];+ let is_text = recorder+ .source+ .node_kind_if_live(fragment.layout_node)+ .is_some_and(node_facts::kind_is_text);+ if is_text && fragment.glyph_run.as_ref().is_some_and(|run| !run.glyphs.is_empty()) {+ fragments.push((owner, fragment_index));+ }};let data = recorder.data(paintable);@@ -1153,7 +1183,7 @@ fn append_text_clip_paths<O: Observer>(recorder: &mut PaintRecorder<'_, O>, painfor piece_index in side.inline_box_piece_indices(paintable) {let piece = &side.inline_box_pieces()[*piece_index as usize];for fragment_index in piece.first_fragment_index..piece.first_fragment_index + piece.fragment_count {- append_fragment(recorder, root, fragment_index as usize);+ append_fragment(root, fragment_index as usize);}}}@@ -1179,8 +1209,9 @@ fn append_text_clip_paths<O: Observer>(recorder: &mut PaintRecorder<'_, O>, painif node_painting::has_lines(recorder.source, current) {let count = recorder.source.committed_side_data(current).fragments().len();for fragment_index in 0..count {- append_fragment(recorder, current, fragment_index);+ append_fragment(current, fragment_index);}}}+ fragments}diff --git a/Tests/LibWeb/Ref/expected/background-clip-text-without-text-ref.html b/Tests/LibWeb/Ref/expected/background-clip-text-without-text-ref.htmlnew file mode 100644index 00000000000..74a91f420b3--- /dev/null+++ b/Tests/LibWeb/Ref/expected/background-clip-text-without-text-ref.html@@ -0,0 +1,13 @@+<!DOCTYPE html>+<style>+ html, body {+ margin: 0;+ background: white;+ }+ div {+ height: 80px;+ margin-top: 120px;+ background-color: blue;+ }+</style>+<div></div>diff --git a/Tests/LibWeb/Ref/input/background-clip-text-without-text.html b/Tests/LibWeb/Ref/input/background-clip-text-without-text.htmlnew file mode 100644index 00000000000..444ff62bb52--- /dev/null+++ b/Tests/LibWeb/Ref/input/background-clip-text-without-text.html@@ -0,0 +1,34 @@+<!DOCTYPE html>+<link rel="match" href="../expected/background-clip-text-without-text-ref.html" />+<style>+ html, body {+ margin: 0;+ background: white;+ }+ div {+ height: 40px;+ background-color: red;+ background-clip: text;+ }+ #blending {+ background-image: linear-gradient(red, red);+ background-blend-mode: multiply;+ }+ section {+ background: yellow;+ }+ #blending-border-box-layer {+ background-image: linear-gradient(blue, blue), none;+ background-clip: border-box, text;+ background-blend-mode: multiply, normal;+ }+ #border-box-layer {+ background-image: linear-gradient(blue, blue), linear-gradient(red, red);+ background-clip: border-box, text;+ }+</style>+<div></div>+<div><span></span></div>+<div id="blending"></div>+<div id="border-box-layer"></div>+<section><div id="blending-border-box-layer"></div></section>