Skip to content

Truly independent web browser

LibWeb: Draw selected text in black or white where its color is unclear

sideshowbarker770c596a1c80
diff --git a/Libraries/LibWeb/Rust/src/painting/chrome_geometry.rs b/Libraries/LibWeb/Rust/src/painting/chrome_geometry.rs
index 5d73e3aaf3b..a5af97d3f5d 100644
--- a/Libraries/LibWeb/Rust/src/painting/chrome_geometry.rs
+++ b/Libraries/LibWeb/Rust/src/painting/chrome_geometry.rs
@@ -477,6 +477,36 @@ pub(crate) struct ScrollbarColors {
pub track: Color,
}
+/// The color behind what `slot` paints, as far as background colors tell: the canvas's, with the backgrounds of `slot`
+/// and its ancestors composited over it. Background images, and content of other boxes painted underneath, don't count.
+pub(crate) fn estimated_backdrop_color(
+ arena: &impl PaintRead,
+ slot: NodeSlotId,
+ root_background_source: RootBackgroundSource,
+ canvas_background_color: Color,
+) -> Color {
+ let mut ancestors = Vec::new();
+ let mut current = Some(slot);
+ while let Some(ancestor) = current {
+ ancestors.push(ancestor);
+ current = arena.node_parent_if_live(ancestor);
+ }
+ let mut background_color = canvas_background_color;
+ for ancestor in ancestors.into_iter().rev() {
+ if is_canvas_background_source(arena, ancestor, root_background_source) {
+ continue;
+ }
+ let Some(style) = arena.node_style_if_live(ancestor) else {
+ continue;
+ };
+ let color = Color(style.background_color());
+ if color.alpha() != 0 {
+ background_color = background_color.blend(color);
+ }
+ }
+ background_color
+}
+
pub(crate) fn scrollbar_colors_for_paint(
arena: &impl PaintRead,
slot: NodeSlotId,
@@ -509,25 +539,7 @@ pub(crate) fn scrollbar_colors_for_paint(
};
}
- let mut ancestors = Vec::new();
- let mut current = Some(slot);
- while let Some(ancestor) = current {
- ancestors.push(ancestor);
- current = arena.node_parent_if_live(ancestor);
- }
- let mut background_color = canvas_background_color;
- for ancestor in ancestors.into_iter().rev() {
- if is_canvas_background_source(arena, ancestor, root_background_source) {
- continue;
- }
- let Some(style) = arena.node_style_if_live(ancestor) else {
- continue;
- };
- let color = Color(style.background_color());
- if color.alpha() != 0 {
- background_color = background_color.blend(color);
- }
- }
+ let background_color = estimated_backdrop_color(arena, slot, root_background_source, canvas_background_color);
let black_thumb = Color::from_rgb(0, 0, 0).with_alpha(128);
let white_thumb = Color::from_rgb(255, 255, 255).with_alpha(128);
let black_contrast = background_color.contrast_ratio(background_color.blend(black_thumb));
diff --git a/Libraries/LibWeb/Rust/src/painting/record/mod.rs b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
index 4ab029bfc16..5138671197c 100644
--- a/Libraries/LibWeb/Rust/src/painting/record/mod.rs
+++ b/Libraries/LibWeb/Rust/src/painting/record/mod.rs
@@ -421,8 +421,8 @@ impl<O: Observer> PaintRecorder<'_, O> {
answer
}
- /// A committed style that authored neither color takes the paired default colors and keeps
- /// its shadows and decorations.
+ /// A committed style that authored neither color takes the paired default colors (the text color among them) and
+ /// keeps its shadows and decorations.
fn highlight_style_answer(
committed: Option<Arc<paint::text::SelectionStyleAnswer>>,
defaults: impl FnOnce() -> paint::text::SelectionStyleAnswer,
@@ -435,6 +435,7 @@ impl<O: Observer> PaintRecorder<'_, O> {
defaults.facts = crate::painting::host::FfiSelectionStyleFacts {
background_color: defaults.facts.background_color,
background_color_is_current_color: defaults.facts.background_color_is_current_color,
+ text_color: defaults.facts.text_color,
wash_color: defaults.facts.wash_color,
..answer.facts
};
@@ -500,6 +501,9 @@ impl<O: Observer> PaintRecorder<'_, O> {
paint::text::SelectionStyleAnswer {
facts: crate::painting::host::FfiSelectionStyleFacts {
background_color,
+ text_color: self
+ .readable_selection_text_color(node, style_source, background_color)
+ .into(),
wash_color: background_color,
..Default::default()
},
@@ -508,6 +512,41 @@ impl<O: Observer> PaintRecorder<'_, O> {
}
}
+ /// The color the default selection draws its text in, where the text's own color would be hard to read on the
+ /// selection: black or white, whichever stands out more. The text otherwise keeps its own color.
+ fn readable_selection_text_color(
+ &self,
+ node: crate::layout::node_data::NodeSlotId,
+ style_source: crate::layout::node_data::NodeSlotId,
+ selection_background: libgfx_rust::Color,
+ ) -> Option<libgfx_rust::Color> {
+ use libgfx_rust::Color;
+ // The contrast WCAG asks of normal-size text.
+ const MINIMUM_CONTRAST_RATIO: f64 = 4.5;
+
+ let style = self.source.node_style_if_live(style_source)?;
+ let text_color = Color(style.inherited_text().webkit_text_fill_color);
+ let uncaptured = &self.inputs.uncaptured;
+ let backdrop = crate::painting::chrome_geometry::estimated_backdrop_color(
+ self.source,
+ node,
+ uncaptured.root_background_source,
+ uncaptured.canvas_color.blend(uncaptured.background_color),
+ );
+ let behind_text = backdrop.blend(selection_background);
+ if behind_text.contrast_ratio(behind_text.blend(text_color)) >= MINIMUM_CONTRAST_RATIO {
+ return None;
+ }
+ let (black, white) = (Color::from_rgb(0, 0, 0), Color::from_rgb(255, 255, 255));
+ Some(
+ if behind_text.contrast_ratio(black) >= behind_text.contrast_ratio(white) {
+ black
+ } else {
+ white
+ },
+ )
+ }
+
fn default_search_text_style(
&self,
node: crate::layout::node_data::NodeSlotId,
diff --git a/Tests/LibWeb/Ref/expected/selection-default-color-scheme-ref.html b/Tests/LibWeb/Ref/expected/selection-default-color-scheme-ref.html
index 1b8c1990f93..34037109954 100644
--- a/Tests/LibWeb/Ref/expected/selection-default-color-scheme-ref.html
+++ b/Tests/LibWeb/Ref/expected/selection-default-color-scheme-ref.html
@@ -7,7 +7,7 @@ p {
}
.selected-light {
background: rgba(101, 2, 0, 0.8);
- color: black;
+ color: white;
}
.selected-dark {
background: rgba(61, 174, 233, 0.5);
diff --git a/Tests/LibWeb/Ref/expected/selection-default-link-ref.html b/Tests/LibWeb/Ref/expected/selection-default-link-ref.html
index 6b34ad4075c..f8ba6bad3e9 100644
--- a/Tests/LibWeb/Ref/expected/selection-default-link-ref.html
+++ b/Tests/LibWeb/Ref/expected/selection-default-link-ref.html
@@ -6,7 +6,7 @@ td {
}
.selected {
background: rgba(101, 2, 0, 0.8);
- color: #000000;
+ color: white;
text-decoration: none;
}
</style>
diff --git a/Tests/LibWeb/Ref/expected/selection-default-normal-forced-dark-color-scheme-ref.html b/Tests/LibWeb/Ref/expected/selection-default-normal-forced-dark-color-scheme-ref.html
index beb18f68d5c..306b3669ba1 100644
--- a/Tests/LibWeb/Ref/expected/selection-default-normal-forced-dark-color-scheme-ref.html
+++ b/Tests/LibWeb/Ref/expected/selection-default-normal-forced-dark-color-scheme-ref.html
@@ -7,7 +7,7 @@ p {
}
.selected {
background: rgba(101, 2, 0, 0.8);
- color: black;
+ color: white;
}
</style>
diff --git a/Tests/LibWeb/Ref/expected/selection-with-image-ref.html b/Tests/LibWeb/Ref/expected/selection-with-image-ref.html
index 3b6d2a3432c..28e97742d48 100644
--- a/Tests/LibWeb/Ref/expected/selection-with-image-ref.html
+++ b/Tests/LibWeb/Ref/expected/selection-with-image-ref.html
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<style>
img { width: 50px; height: 50px; vertical-align: middle; }
-.selected { background: rgba(101, 2, 0, 0.8); color: black; }
+.selected { background: rgba(101, 2, 0, 0.8); color: white; }
.img-wrapper {
display: inline-block;
position: relative;
diff --git a/Tests/LibWeb/Screenshot/expected-macos/selection-text-color.png b/Tests/LibWeb/Screenshot/expected-macos/selection-text-color.png
index e44388ffc2d..c1cab0b8e7a 100644
Binary files a/Tests/LibWeb/Screenshot/expected-macos/selection-text-color.png and b/Tests/LibWeb/Screenshot/expected-macos/selection-text-color.png differ
diff --git a/Tests/LibWeb/Screenshot/expected/selection-text-color.png b/Tests/LibWeb/Screenshot/expected/selection-text-color.png
index 93f88f9b9db..3f7b9e2e80d 100644
Binary files a/Tests/LibWeb/Screenshot/expected/selection-text-color.png and b/Tests/LibWeb/Screenshot/expected/selection-text-color.png differ
diff --git a/Tests/LibWeb/Text/expected/display_list/page-focus-rendering.txt b/Tests/LibWeb/Text/expected/display_list/page-focus-rendering.txt
index 9715f0c9a7d..1cf4e0b1d07 100644
--- a/Tests/LibWeb/Text/expected/display_list/page-focus-rendering.txt
+++ b/Tests/LibWeb/Text/expected/display_list/page-focus-rendering.txt
@@ -13,7 +13,7 @@ FillRectWithRoundedCorners@s1/e0 rect=[0,0 70x20] color=rgb(255, 255, 255)
FillRoundedRectRing@s1/e0 rect=[0,0 70x20] corner_radii=[4x4,4x4,4x4,4x4] widths=[1,1,1,1] color=rgb(118, 118, 118)
DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(0, 0, 0) inline_clips=[clip=[3,4 8x12]]
FillRect@s1/c0/e0 rect=[11,3 15x14] color=rgba(101, 2, 0, 0.8)
-DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(0, 0, 0) inline_clips=[clip=[11,4 15x12]]
+DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(255, 255, 255) inline_clips=[clip=[11,4 15x12]]
FillRoundedRectRing@s1/e0 rect=[-2,-2 74x24] corner_radii=[6x6,6x6,6x6,6x6] widths=[2,2,2,2] color=rgb(61, 174, 233)
DrawLine@s1/e0 from=[65,17] to=[67,15] color=rgba(255, 255, 255, 0.392) thickness=1 style=Solid
DrawLine@s1/e0 from=[64,17] to=[67,14] color=rgba(0, 0, 0, 0.392) thickness=1 style=Solid
@@ -22,7 +22,7 @@ DrawLine@s1/e0 from=[61,17] to=[67,11] color=rgba(0, 0, 0, 0.392) thickness=1 st
DrawLine@s1/e0 from=[59,17] to=[67,9] color=rgba(255, 255, 255, 0.392) thickness=1 style=Solid
DrawLine@s1/e0 from=[58,17] to=[67,8] color=rgba(0, 0, 0, 0.392) thickness=1 style=Solid
FillRect@s1/e0 rect=[0,23 23x16] color=rgba(101, 2, 0, 0.8)
-DrawGlyphRun@s1/e0 rect=[0,23 24x16] translation=[0,36] color=rgb(0, 0, 0)
+DrawGlyphRun@s1/e0 rect=[0,23 24x16] translation=[0,36] color=rgb(255, 255, 255)
-- unfocused --
AccumulatedVisualContext Tree:
@@ -39,7 +39,7 @@ FillRectWithRoundedCorners@s1/e0 rect=[0,0 70x20] color=rgb(255, 255, 255)
FillRoundedRectRing@s1/e0 rect=[0,0 70x20] corner_radii=[4x4,4x4,4x4,4x4] widths=[1,1,1,1] color=rgb(118, 118, 118)
DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(0, 0, 0) inline_clips=[clip=[3,4 8x12]]
FillRect@s1/c0/e0 rect=[11,3 15x14] color=rgba(16, 16, 16, 0.667)
-DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(0, 0, 0) inline_clips=[clip=[11,4 15x12]]
+DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(255, 255, 255) inline_clips=[clip=[11,4 15x12]]
DrawLine@s1/e0 from=[65,17] to=[67,15] color=rgba(255, 255, 255, 0.392) thickness=1 style=Solid
DrawLine@s1/e0 from=[64,17] to=[67,14] color=rgba(0, 0, 0, 0.392) thickness=1 style=Solid
DrawLine@s1/e0 from=[62,17] to=[67,12] color=rgba(255, 255, 255, 0.392) thickness=1 style=Solid
@@ -47,7 +47,7 @@ DrawLine@s1/e0 from=[61,17] to=[67,11] color=rgba(0, 0, 0, 0.392) thickness=1 st
DrawLine@s1/e0 from=[59,17] to=[67,9] color=rgba(255, 255, 255, 0.392) thickness=1 style=Solid
DrawLine@s1/e0 from=[58,17] to=[67,8] color=rgba(0, 0, 0, 0.392) thickness=1 style=Solid
FillRect@s1/e0 rect=[0,23 23x16] color=rgba(16, 16, 16, 0.667)
-DrawGlyphRun@s1/e0 rect=[0,23 24x16] translation=[0,36] color=rgb(0, 0, 0)
+DrawGlyphRun@s1/e0 rect=[0,23 24x16] translation=[0,36] color=rgb(255, 255, 255)
-- focused again --
AccumulatedVisualContext Tree:
@@ -64,7 +64,7 @@ FillRectWithRoundedCorners@s1/e0 rect=[0,0 70x20] color=rgb(255, 255, 255)
FillRoundedRectRing@s1/e0 rect=[0,0 70x20] corner_radii=[4x4,4x4,4x4,4x4] widths=[1,1,1,1] color=rgb(118, 118, 118)
DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(0, 0, 0) inline_clips=[clip=[3,4 8x12]]
FillRect@s1/c0/e0 rect=[11,3 15x14] color=rgba(101, 2, 0, 0.8)
-DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(0, 0, 0) inline_clips=[clip=[11,4 15x12]]
+DrawGlyphRun@s1/c0/e0 rect=[3,3 24x14] translation=[3,13.984375] color=rgb(255, 255, 255) inline_clips=[clip=[11,4 15x12]]
FillRoundedRectRing@s1/e0 rect=[-2,-2 74x24] corner_radii=[6x6,6x6,6x6,6x6] widths=[2,2,2,2] color=rgb(61, 174, 233)
DrawLine@s1/e0 from=[65,17] to=[67,15] color=rgba(255, 255, 255, 0.392) thickness=1 style=Solid
DrawLine@s1/e0 from=[64,17] to=[67,14] color=rgba(0, 0, 0, 0.392) thickness=1 style=Solid
@@ -73,7 +73,7 @@ DrawLine@s1/e0 from=[61,17] to=[67,11] color=rgba(0, 0, 0, 0.392) thickness=1 st
DrawLine@s1/e0 from=[59,17] to=[67,9] color=rgba(255, 255, 255, 0.392) thickness=1 style=Solid
DrawLine@s1/e0 from=[58,17] to=[67,8] color=rgba(0, 0, 0, 0.392) thickness=1 style=Solid
FillRect@s1/e0 rect=[0,23 23x16] color=rgba(101, 2, 0, 0.8)
-DrawGlyphRun@s1/e0 rect=[0,23 24x16] translation=[0,36] color=rgb(0, 0, 0)
+DrawGlyphRun@s1/e0 rect=[0,23 24x16] translation=[0,36] color=rgb(255, 255, 255)
-- caret, focused --
AccumulatedVisualContext Tree:
diff --git a/Tests/LibWeb/Text/expected/display_list/selection-default-text-contrast.txt b/Tests/LibWeb/Text/expected/display_list/selection-default-text-contrast.txt
new file mode 100644
index 00000000000..7452ad2dbe8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/display_list/selection-default-text-contrast.txt
@@ -0,0 +1,3 @@
+off-black: selection rgba(101, 2, 0, 0.8), text rgb(255, 255, 255)
+light: selection rgba(101, 2, 0, 0.8), text rgb(244, 244, 244)
+decorated: selection rgba(101, 2, 0, 0.8), text rgb(255, 255, 255)
diff --git a/Tests/LibWeb/Text/expected/display_list/text-decoration-underline-partial-selection.txt b/Tests/LibWeb/Text/expected/display_list/text-decoration-underline-partial-selection.txt
index 3e7184a9223..db65ff03bdb 100644
--- a/Tests/LibWeb/Text/expected/display_list/text-decoration-underline-partial-selection.txt
+++ b/Tests/LibWeb/Text/expected/display_list/text-decoration-underline-partial-selection.txt
@@ -13,6 +13,6 @@ DrawGlyphRun@s1/e0 rect=[8,-17 169x100] translation=[8,62.984375] color=rgb(0, 0
DrawLine@s1/e0 from=[116,70] to=[153,70] color=rgb(0, 0, 0) thickness=10 style=Solid
DrawLine@s1/e0 from=[165,70] to=[177,70] color=rgb(0, 0, 0) thickness=10 style=Solid
FillRect@s1/e0 rect=[69,-17 47x100] color=rgba(101, 2, 0, 0.8)
-DrawGlyphRun@s1/e0 rect=[8,-17 169x100] translation=[8,62.984375] color=rgb(0, 0, 0) inline_clips=[clip=[69,-15 47x91]]
-DrawLine@s1/e0 from=[109,70] to=[116,70] color=rgb(0, 0, 0) thickness=10 style=Solid
+DrawGlyphRun@s1/e0 rect=[8,-17 169x100] translation=[8,62.984375] color=rgb(255, 255, 255) inline_clips=[clip=[69,-15 47x91]]
+DrawLine@s1/e0 from=[109,70] to=[116,70] color=rgb(255, 255, 255) thickness=10 style=Solid
diff --git a/Tests/LibWeb/Text/expected/display_list/text-input-clip-is-not-rounded.txt b/Tests/LibWeb/Text/expected/display_list/text-input-clip-is-not-rounded.txt
index 94999a6d791..b9887d7bd83 100644
--- a/Tests/LibWeb/Text/expected/display_list/text-input-clip-is-not-rounded.txt
+++ b/Tests/LibWeb/Text/expected/display_list/text-input-clip-is-not-rounded.txt
@@ -10,6 +10,6 @@ AccumulatedVisualContext Tree:
DisplayList:
FillRect@s1/c1/e0 rect=[8,10 23x13] color=rgba(101, 2, 0, 0.8)
-DrawGlyphRun@s1/c1/e0 rect=[8,10 23x13] translation=[8,20.390625] color=rgb(0, 0, 0)
+DrawGlyphRun@s1/c1/e0 rect=[8,10 23x13] translation=[8,20.390625] color=rgb(255, 255, 255)
FillRoundedRectRing@s1/e0 rect=[6,8 160x17] corner_radii=[8x8,8x8,8x8,8x8] widths=[2,2,2,2] color=rgb(61, 174, 233)
diff --git a/Tests/LibWeb/Text/input/display_list/selection-default-text-contrast.html b/Tests/LibWeb/Text/input/display_list/selection-default-text-contrast.html
new file mode 100644
index 00000000000..8cd046f396e
--- /dev/null
+++ b/Tests/LibWeb/Text/input/display_list/selection-default-text-contrast.html
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ #off-black { color: #1a1a2e; }
+ #light { color: #f4f4f4; }
+ #decorated { color: #1a1a2e; }
+ #decorated::selection { text-decoration: underline; }
+</style>
+<p><span id="off-black">Off-black text</span></p>
+<p><span id="light">Light text</span></p>
+<p><span id="decorated">Off-black text with a decorated selection</span></p>
+<script>
+test(() => {
+ const selection = window.getSelection();
+ const color = line => line.match(/color=(rgba?\([^)]*\))/)[1];
+ for (const id of ["off-black", "light", "decorated"]) {
+ const span = document.getElementById(id);
+ const range = document.createRange();
+ range.selectNodeContents(span);
+ selection.removeAllRanges();
+ selection.addRange(range);
+
+ const rect = span.getBoundingClientRect();
+ const at = `rect=[${Math.round(rect.left)},${Math.round(rect.top)} `;
+ const lines = internals.dumpDisplayList().split("\n").filter(line => line.includes(at));
+ const background = color(lines.find(line => line.includes("FillRect")));
+ const text = color(lines.find(line => line.includes("DrawGlyphRun")));
+ println(`${id}: selection ${background}, text ${text}`);
+ }
+});
+</script>