Skip to content

Truly independent web browser

LibWeb: Use the element’s own color for an auto caret-color

sideshowbarker2cc73a172e9b
diff --git a/Libraries/LibWeb/CSS/ComputedValues.h b/Libraries/LibWeb/CSS/ComputedValues.h
index 436f26ebe4e..454f73ceb24 100644
--- a/Libraries/LibWeb/CSS/ComputedValues.h
+++ b/Libraries/LibWeb/CSS/ComputedValues.h
@@ -488,7 +488,9 @@ public:
Float float_() const { return static_cast<Float>(m_noninherited.box->float_); }
Clear clear() const { return static_cast<Clear>(m_noninherited.box->clear); }
- Color caret_color() const { return m_inherited.ui->caret_color_value(); }
+ // https://drafts.csswg.org/css-ui-4/#caret-color
+ // auto: "User agents should use currentColor." It computes to auto, so the element's own color is read here.
+ Color caret_color() const { return m_inherited.ui->caret_color.is_auto ? color() : m_inherited.ui->caret_color_value(); }
ColorInterpolation color_interpolation() const { return m_inherited.svg->color_interpolation_value(); }
FillRule fill_rule() const { return m_inherited.svg->fill_rule_value(); }
PreferredColorScheme color_scheme() const { return m_inherited.ui->color_scheme_value(); }
diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.h
index bbe826b93d6..7e1a8e2323a 100644
--- a/Libraries/LibWeb/Layout/Node.h
+++ b/Libraries/LibWeb/Layout/Node.h
@@ -458,7 +458,13 @@ public:
VERIFY_NOT_REACHED();
}
CSS::Visibility visibility() const { return static_cast<CSS::Visibility>(style_group<CSS::ComputedValues::InheritedBoxValues>().visibility); }
- Color caret_color() const { return style_group<CSS::ComputedValues::InheritedUIValues>().caret_color_value(); }
+ // https://drafts.csswg.org/css-ui-4/#caret-color
+ // auto: "User agents should use currentColor." It computes to auto, so the box's own color is read here.
+ Color caret_color() const
+ {
+ auto const& ui = style_group<CSS::ComputedValues::InheritedUIValues>();
+ return ui.caret_color.is_auto ? color() : ui.caret_color_value();
+ }
CSS::PreferredColorScheme color_scheme() const { return style_group<CSS::ComputedValues::InheritedUIValues>().color_scheme_value(); }
ReadonlySpan<Utf16FlyString> color_schemes() const { return style_group<CSS::ComputedValues::InheritedUIValues>().color_schemes_span(); }
bool color_scheme_only() const { return style_group<CSS::ComputedValues::InheritedUIValues>().color_scheme_only; }
diff --git a/Tests/LibWeb/Ref/expected/caret-color-auto-follows-color-ref.html b/Tests/LibWeb/Ref/expected/caret-color-auto-follows-color-ref.html
new file mode 100644
index 00000000000..ea7c3b03774
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/caret-color-auto-follows-color-ref.html
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<style>
+ div { font: 20px/1.5 sans-serif; color: blue; }
+</style>
+<div id="editable" contenteditable>Some text</div>
+<script>
+ const editable = document.getElementById("editable");
+ editable.focus();
+ getSelection().collapse(editable.firstChild, 9);
+</script>
diff --git a/Tests/LibWeb/Ref/input/caret-color-auto-follows-color.html b/Tests/LibWeb/Ref/input/caret-color-auto-follows-color.html
new file mode 100644
index 00000000000..96d360303d1
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/caret-color-auto-follows-color.html
@@ -0,0 +1,17 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/caret-color-auto-follows-color-ref.html">
+<style>
+ div { font: 20px/1.5 sans-serif; }
+</style>
+<div id="editable" contenteditable>Some text</div>
+<script>
+ const editable = document.getElementById("editable");
+ editable.focus();
+ getSelection().collapse(editable.firstChild, 9);
+ requestAnimationFrame(() => requestAnimationFrame(() => {
+ editable.style.color = "blue";
+ requestAnimationFrame(() => requestAnimationFrame(() => document.documentElement.classList.remove("reftest-wait")));
+ }));
+</script>
+</html>
diff --git a/Tests/LibWeb/Text/expected/css/caret-color-auto-follows-color.txt b/Tests/LibWeb/Text/expected/css/caret-color-auto-follows-color.txt
new file mode 100644
index 00000000000..d46926e3128
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/caret-color-auto-follows-color.txt
@@ -0,0 +1,4 @@
+initial: outer rgb(0, 0, 0), inherits rgb(0, 0, 0), own rgb(255, 0, 0), declared rgb(0, 128, 0)
+outer color set: outer rgb(0, 0, 255), inherits rgb(0, 0, 255), own rgb(255, 0, 0), declared rgb(0, 128, 0)
+own color removed: outer rgb(0, 0, 255), inherits rgb(0, 0, 255), own rgb(0, 0, 255), declared rgb(0, 128, 0)
+declared color changed: outer rgb(0, 0, 255), inherits rgb(0, 0, 255), own rgb(0, 0, 255), declared rgb(128, 0, 128)
diff --git a/Tests/LibWeb/Text/input/css/caret-color-auto-follows-color.html b/Tests/LibWeb/Text/input/css/caret-color-auto-follows-color.html
new file mode 100644
index 00000000000..ad05021a524
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/caret-color-auto-follows-color.html
@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ .red { color: red; }
+</style>
+<div id="outer">outer <span id="inherits">inherits</span> <span id="own" class="red">own</span> <span id="declared" style="caret-color: auto; color: green">declared</span></div>
+<script>
+ test(() => {
+ const caretColor = id => getComputedStyle(document.getElementById(id)).caretColor;
+ const report = label => println(`${label}: outer ${caretColor("outer")}, inherits ${caretColor("inherits")}, own ${caretColor("own")}, declared ${caretColor("declared")}`);
+
+ report("initial");
+ document.getElementById("outer").style.color = "blue";
+ report("outer color set");
+ document.getElementById("own").className = "";
+ report("own color removed");
+ document.getElementById("declared").style.color = "purple";
+ report("declared color changed");
+ });
+</script>