Skip to content

Truly independent web browser

LibWeb: Let pages style the swatch of a color input

sideshowbarkera468d7109e4d
diff --git a/Libraries/LibWeb/CSS/Default.css b/Libraries/LibWeb/CSS/Default.css
index e55ffc44f79..179648d920e 100644
--- a/Libraries/LibWeb/CSS/Default.css
+++ b/Libraries/LibWeb/CSS/Default.css
@@ -124,6 +124,20 @@ input[type=color] {
width: 50px;
height: 27px;
padding: 1px 2px;
+
+ &::-webkit-color-swatch-wrapper {
+ display: flex;
+ width: 100%;
+ height: 100%;
+ padding: 4px 2px;
+ box-sizing: border-box;
+ }
+
+ &::color-swatch {
+ flex: 1;
+ min-width: 0;
+ border: 1px solid #777777;
+ }
}
input[type=image] {
diff --git a/Libraries/LibWeb/CSS/PseudoElements.json b/Libraries/LibWeb/CSS/PseudoElements.json
index 836722b77a2..c43d8b8f683 100644
--- a/Libraries/LibWeb/CSS/PseudoElements.json
+++ b/Libraries/LibWeb/CSS/PseudoElements.json
@@ -1,4 +1,7 @@
{
+ "-moz-color-swatch": {
+ "alias-for": "color-swatch"
+ },
"-moz-meter-bar": {
"alias-for": "slider-fill"
},
@@ -17,6 +20,13 @@
"-moz-range-thumb": {
"alias-for": "slider-thumb"
},
+ "-webkit-color-swatch": {
+ "alias-for": "color-swatch"
+ },
+ "-webkit-color-swatch-wrapper": {
+ "spec": "https://drafts.csswg.org/css-forms-1/#vendor-pseudo-element-compatibility",
+ "implementation": "element-reference"
+ },
"-webkit-input-placeholder": {
"alias-for": "placeholder"
},
@@ -49,6 +59,10 @@
"is-tree-abiding": true,
"implementation": "synthetic"
},
+ "color-swatch": {
+ "spec": "https://drafts.csswg.org/css-forms-1/#selectordef-color-swatch",
+ "implementation": "element-reference"
+ },
"details-content": {
"spec": "https://drafts.csswg.org/css-pseudo-4/#selectordef-details-content",
"is-element-backed": true,
diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
index 1f4c270f488..a3d8c271713 100644
--- a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp
@@ -199,6 +199,8 @@ ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(SearchText)
ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(SearchTextCurrent)
ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(SpellingError)
ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(ViewTransition)
+ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(WebkitColorSwatchWrapper)
+ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(ColorSwatch)
ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(DetailsContent)
ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(SliderTrack)
ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(ViewTransitionGroup)
diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
index ccec3dbf98a..4ac8384516f 100644
--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp
+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp
@@ -1428,25 +1428,16 @@ void HTMLInputElement::create_color_input_shadow_tree()
auto color = value_sanitization_algorithm(m_value);
+ // NB: The default stylesheet styles the wrapper and the swatch through their pseudo-elements, so that author styles
+ // can override it.
auto swatch_wrapper = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
- swatch_wrapper->set_attribute_value(HTML::AttributeNames::style, R"~~~(
- display: flex;
- width: 100%;
- height: 100%;
- padding: 4px 2px;
- box-sizing: border-box;
-)~~~"_utf16);
+ MUST(shadow_root->append_child(swatch_wrapper));
+ swatch_wrapper->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::WebkitColorSwatchWrapper);
m_color_well_element = DOM::create_element(document(), HTML::TagNames::div, Namespace::HTML).release_value_but_fixme_should_propagate_errors();
- m_color_well_element->set_attribute_value(HTML::AttributeNames::style, R"~~~(
- flex: 1;
- min-width: 0;
- border: 1px solid #777777;
-)~~~"_utf16);
MUST(m_color_well_element->style()->set_property(CSS::PropertyID::BackgroundColor, color.utf16_view()));
-
MUST(swatch_wrapper->append_child(*m_color_well_element));
- MUST(shadow_root->append_child(swatch_wrapper));
+ m_color_well_element->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::ColorSwatch);
}
void HTMLInputElement::update_color_well_element()
diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
index 6be19fa7c6b..9565f0a174c 100644
--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs
@@ -619,7 +619,7 @@ pub struct FfiElementArrival {
}
pub const FIRST_ELEMENT_REFERENCE_PSEUDO_ELEMENT_KIND: u8 = 11;
-pub const LAST_ELEMENT_REFERENCE_PSEUDO_ELEMENT_KIND: u8 = 16;
+pub const LAST_ELEMENT_REFERENCE_PSEUDO_ELEMENT_KIND: u8 = 18;
/// The element facts the style computation's box-type transformation and element style
/// adjustments read. Mirrors C++ `ElementStyleAdjustmentFact`.
diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge/records.rs b/Libraries/LibWeb/Rust/src/css/style/bridge/records.rs
index 6d888b0317b..e8ece8c270f 100644
--- a/Libraries/LibWeb/Rust/src/css/style/bridge/records.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/bridge/records.rs
@@ -215,17 +215,19 @@ pub enum FfiDemandedPseudoElement {
Selection = 8,
SpellingError = 9,
ViewTransition = 10,
- DetailsContent = 11,
- FileSelectorButton = 12,
- Placeholder = 13,
- SliderFill = 14,
- SliderThumb = 15,
- SliderTrack = 16,
- Highlight = 17,
- ViewTransitionGroup = 20,
- ViewTransitionImagePair = 21,
- ViewTransitionNew = 22,
- ViewTransitionOld = 23,
+ WebkitColorSwatchWrapper = 11,
+ ColorSwatch = 12,
+ DetailsContent = 13,
+ FileSelectorButton = 14,
+ Placeholder = 15,
+ SliderFill = 16,
+ SliderThumb = 17,
+ SliderTrack = 18,
+ Highlight = 19,
+ ViewTransitionGroup = 22,
+ ViewTransitionImagePair = 23,
+ ViewTransitionNew = 24,
+ ViewTransitionOld = 25,
}
/// The answer to a record demand: the record, or that the pseudo-element read generates no box
diff --git a/Libraries/LibWeb/Rust/src/css/style/publication.rs b/Libraries/LibWeb/Rust/src/css/style/publication.rs
index 8fa51ba8bf4..a7ff8ff6f1b 100644
--- a/Libraries/LibWeb/Rust/src/css/style/publication.rs
+++ b/Libraries/LibWeb/Rust/src/css/style/publication.rs
@@ -860,8 +860,7 @@ pub(super) mod pseudo_kind {
pub(in crate::css::style) const SELECTION: u8 = 8;
pub(in crate::css::style) const SPELLING_ERROR: u8 = 9;
pub(in crate::css::style) const SYNTHETIC_COUNT: usize = 11;
- pub(in crate::css::style) const HIGHLIGHT: u8 = 17;
- const _: () = assert!(HIGHLIGHT as u16 == crate::css::selector::PseudoElementType::Highlight as u16);
+ pub(in crate::css::style) const HIGHLIGHT: u8 = crate::css::selector::PseudoElementType::Highlight as u8;
/// https://drafts.csswg.org/css-pseudo-4/#highlight-pseudos
pub(in crate::css::style) const fn is_highlight(kind: u8) -> bool {
diff --git a/Tests/LibWeb/Ref/expected/color-input-swatch-pseudo-elements-ref.html b/Tests/LibWeb/Ref/expected/color-input-swatch-pseudo-elements-ref.html
new file mode 100644
index 00000000000..dfdf25cb9c5
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/color-input-swatch-pseudo-elements-ref.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<style>
+ div {
+ width: 60px;
+ height: 30px;
+ margin: 0 0 10px;
+ background-color: #00ff00;
+ }
+</style>
+<div></div>
+<div></div>
+<div></div>
diff --git a/Tests/LibWeb/Ref/input/color-input-swatch-pseudo-elements.html b/Tests/LibWeb/Ref/input/color-input-swatch-pseudo-elements.html
new file mode 100644
index 00000000000..51b39787685
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/color-input-swatch-pseudo-elements.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<link rel="match" href="../expected/color-input-swatch-pseudo-elements-ref.html">
+<style>
+ input {
+ display: block;
+ width: 60px;
+ height: 30px;
+ margin: 0 0 10px;
+ padding: 0;
+ border: none;
+ }
+ .webkit::-webkit-color-swatch-wrapper {
+ padding: 0;
+ }
+ .webkit::-webkit-color-swatch {
+ border: 0;
+ }
+ .standard::-webkit-color-swatch-wrapper {
+ padding: 0;
+ }
+ .standard::color-swatch {
+ border: 0;
+ }
+ .moz::-webkit-color-swatch-wrapper {
+ padding: 0;
+ }
+ .moz::-moz-color-swatch {
+ border: 0;
+ }
+</style>
+<input class="webkit" type="color" value="#00ff00">
+<input class="standard" type="color" value="#00ff00">
+<input class="moz" type="color" value="#00ff00">
diff --git a/Tests/LibWeb/Text/expected/css/style-engine/matching-selector-shapes.txt b/Tests/LibWeb/Text/expected/css/style-engine/matching-selector-shapes.txt
index a256d4ad91a..fe0f8d7ffe4 100644
--- a/Tests/LibWeb/Text/expected/css/style-engine/matching-selector-shapes.txt
+++ b/Tests/LibWeb/Text/expected/css/style-engine/matching-selector-shapes.txt
@@ -12,7 +12,7 @@ p#p2.b: * :where(p, section) * > * * + * * :is(p, section) :is(:is(p)) :not(:not
i: * * > * * :is(p, section) > * :not(p) :not(p, section) :not(:has(em)) :not(:is(p, li)) :is(:not(p), :not(li)) p > * ::before[pseudo 2] :first-child :last-child :only-child :nth-child(2n+1) :nth-last-child(1) :nth-child(2n+1) :nth-child(-n+2) :first-of-type :last-of-type :only-of-type :nth-last-of-type(1) :read-only :defined :lang(en) :lang(en-GB) :dir(rtl) :root *
section.c: * :where(p, section) * > * * + * * :is(p, section) :not(p) :not(:has(em)) :not(:is(p, li)) :is(:not(p), :not(li)) p ~ * :has(> :not(em)) ::before[pseudo 2] p ~ section :nth-child(2n+1) :nth-child(2n+1) :first-of-type :last-of-type :only-of-type :nth-last-of-type(1) :read-only :defined :lang(en) :lang(en-GB) :dir(rtl) :has(:checked) :root * :has(:not(.c))
a.a: * * > * * :is(p, section) > * :not(p) :not(p, section) :not(:has(em)) :not(:is(p, li)) :is(:not(p), :not(li)) ::before[pseudo 2] .a :first-child :nth-child(2n+1) :nth-child(2n+1) :nth-child(-n+2) :first-of-type :last-of-type :only-of-type :nth-last-of-type(1) :link :any-link :read-only :defined :lang(en) :lang(en-GB) :dir(rtl) :root * :is(.a, #root):not(.c)
-input: * * > * * + * * :is(p, section) > * :not(p) :not(p, section) :not(:has(em)) :not(:is(p, li)) :is(:not(p), :not(li)) ::before[pseudo 2] section > :not(a) input::placeholder[pseudo 13] :last-child :nth-child(2) :nth-last-child(1) :nth-child(2n) :nth-child(-n+2) :first-of-type :last-of-type :only-of-type :nth-last-of-type(1) :empty :checked :disabled :optional :read-only :default :defined :lang(en) :lang(en-GB) :dir(rtl) :root *
+input: * * > * * + * * :is(p, section) > * :not(p) :not(p, section) :not(:has(em)) :not(:is(p, li)) :is(:not(p), :not(li)) ::before[pseudo 2] section > :not(a) input::placeholder[pseudo 15] :last-child :nth-child(2) :nth-last-child(1) :nth-child(2n) :nth-child(-n+2) :first-of-type :last-of-type :only-of-type :nth-last-of-type(1) :empty :checked :disabled :optional :read-only :default :defined :lang(en) :lang(en-GB) :dir(rtl) :root *
ul: * * > * * + * * :not(p) :not(p, section) :not(:has(em)) :not(:is(p, li)) :is(:not(p), :not(li)) p ~ * :has(> :not(em)) ::before[pseudo 2] :nth-child(2n) :first-of-type :last-of-type :only-of-type :nth-last-of-type(1) :read-only :defined :lang(en) :lang(en-GB) :dir(rtl) :root * :has(:not(.c))
li.x: * * > * * :not(p) :not(p, section) :not(:has(em)) :is(:not(p), :not(li)) ::before[pseudo 2] li::marker[pseudo 5] :first-child :nth-child(2n+1) :nth-child(2n+1) :nth-child(-n+2) :first-of-type :read-only :defined :lang(en) :lang(en-GB) :dir(rtl) :root * :is(p, li):is(.a, .x) li.x:nth-child(2n+1):not(:last-child)
li.y: * * > * * + * * :not(p) :not(p, section) :not(:has(em)) :is(:not(p), :not(li)) ::before[pseudo 2] li::marker[pseudo 5] :nth-child(2) :nth-child(2n) :nth-child(-n+2) :nth-of-type(2) :read-only :defined :lang(en) :lang(en-GB) :dir(rtl) :root * ul > li:nth-child(2)