Truly independent web browser
LibWeb: Add a clear button to search inputs
sideshowbarkerd660c52b14b9
diff --git a/Libraries/LibWeb/CSS/Default.css b/Libraries/LibWeb/CSS/Default.cssindex da716c35d51..eceb4c8f9f8 100644--- a/Libraries/LibWeb/CSS/Default.css+++ b/Libraries/LibWeb/CSS/Default.css@@ -62,6 +62,29 @@ textarea {overflow-wrap: break-word;}+input::-webkit-search-cancel-button {+ appearance: none;+ flex: none;+ box-sizing: border-box;+ inline-size: 1em;+ block-size: 1em;+ margin: 0 2px;+ padding: 0;+ border: none;+ border-radius: 50%;+ background: AccentColor;+ color: AccentColorText;+ font: inherit;+ font-weight: bold;+ line-height: 1;+ text-align: center;+}++input[type="search" i]:read-only::-webkit-search-cancel-button,+input[type="search" i]:disabled::-webkit-search-cancel-button {+ display: none;+}+input::placeholder, textarea::placeholder {color: GrayText;line-height: initial;diff --git a/Libraries/LibWeb/CSS/PseudoElements.json b/Libraries/LibWeb/CSS/PseudoElements.jsonindex c43d8b8f683..9d1247fc2ff 100644--- a/Libraries/LibWeb/CSS/PseudoElements.json+++ b/Libraries/LibWeb/CSS/PseudoElements.json@@ -39,6 +39,10 @@"-webkit-progress-value": {"alias-for": "slider-fill"},+ "-webkit-search-cancel-button": {+ "spec": "https://drafts.csswg.org/css-forms-1/#selectordef-clear-icon",+ "implementation": "element-reference"+ },"-webkit-slider-runnable-track": {"alias-for": "slider-track"},diff --git a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp b/Libraries/LibWeb/CSS/StyleEngineBridge.cppindex 6c6fc3f14bd..947485a5090 100644--- a/Libraries/LibWeb/CSS/StyleEngineBridge.cpp+++ b/Libraries/LibWeb/CSS/StyleEngineBridge.cpp@@ -237,6 +237,7 @@ 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(WebkitSearchCancelButton)ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(ColorSwatch)ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(DetailsContent)ASSERT_DEMANDED_PSEUDO_ELEMENT_KIND(SliderTrack)diff --git a/Libraries/LibWeb/HTML/HTMLButtonElement.cpp b/Libraries/LibWeb/HTML/HTMLButtonElement.cppindex 66793bbf760..3922a407630 100644--- a/Libraries/LibWeb/HTML/HTMLButtonElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLButtonElement.cpp@@ -5,13 +5,16 @@*/#include <LibWeb/CSS/Invalidation/FormControlInvalidator.h>+#include <LibWeb/CSS/PseudoElement.h>#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>#include <LibWeb/DOM/Document.h>#include <LibWeb/DOM/Event.h>#include <LibWeb/DOM/Node.h>+#include <LibWeb/DOM/ShadowRoot.h>#include <LibWeb/HTML/CommandEvent.h>#include <LibWeb/HTML/HTMLButtonElement.h>#include <LibWeb/HTML/HTMLFormElement.h>+#include <LibWeb/HTML/HTMLInputElement.h>#include <LibWeb/HTML/Scripting/Environments.h>#include <LibWeb/HighResolutionTime/TimeOrigin.h>#include <LibWeb/Namespace.h>@@ -188,6 +191,12 @@ void HTMLButtonElement::activation_behavior(DOM::Event const& event)if (!this->document().is_fully_active())return;+ if (associated_shadow_host_pseudo_element() == CSS::PseudoElement::WebkitSearchCancelButton) {+ if (auto root = containing_shadow_root())+ static_cast<HTMLInputElement&>(*root->host()).clear_search_value({});+ return;+ }+// 3. If element has a form owner:if (form() != nullptr) {// 1. If element is a submit button, then submit element's form owner from element with userInvolvement set to event's user navigation involvement, and return.@@ -295,7 +304,7 @@ void HTMLButtonElement::activation_behavior(DOM::Event const& event)bool HTMLButtonElement::is_focusable() const{- return enabled() && meets_focusable_area_rendering_requirements();+ return associated_shadow_host_pseudo_element() != CSS::PseudoElement::WebkitSearchCancelButton && enabled() && meets_focusable_area_rendering_requirements();}// https://html.spec.whatwg.org/multipage/form-elements.html#dom-button-commanddiff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cppindex 4550999c8d6..2e46bb00033 100644--- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp+++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp@@ -136,6 +136,7 @@ void HTMLInputElement::visit_edges(Cell::Visitor& visitor)visitor.visit(field.element);visitor.visit(m_image_button_alt_text_node);visitor.visit(m_placeholder_element);+ visitor.visit(m_search_clear_button);visitor.visit(m_placeholder_text_node);visitor.visit(m_up_button_element);visitor.visit(m_down_button_element);@@ -767,7 +768,7 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_relevant_value(Utf16View value)m_value = value_sanitization_algorithm(value);}- update_placeholder_visibility();+ update_text_input_decorations();return {};}@@ -806,7 +807,7 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value(Utf16View value)if (m_text_node) {m_text_node->set_data(m_value);- update_placeholder_visibility();+ update_text_input_decorations();}// NB: The relevant value is read from the text node — so, this has to come after the text node is updated.@@ -848,6 +849,15 @@ WebIDL::ExceptionOr<void> HTMLInputElement::set_value(Utf16View value)return {};}+void HTMLInputElement::clear_search_value(Badge<HTMLButtonElement>)+{+ if (type_state() != TypeAttributeState::Search || !is_mutable() || m_value.is_empty())+ return;+ MUST(set_value({}));+ m_has_uncommitted_changes = true;+ dispatch_event(create_event_for_element(*this, EventNames::input, { .bubbles = true, .composed = true }));+}+void HTMLInputElement::commit_pending_changes(){// The change event fires when the value is committed, if that makes sense for the control,@@ -929,7 +939,7 @@ static GC::Ref<CSS::CSSStyleProperties> placeholder_style_when_visible()return *style;}-static GC::Ref<CSS::CSSStyleProperties> placeholder_style_when_hidden()+static GC::Ref<CSS::CSSStyleProperties> hidden_text_input_decoration_style(){static auto& style = *new GC::Root<CSS::CSSStyleProperties>;if (!style) {@@ -939,18 +949,25 @@ static GC::Ref<CSS::CSSStyleProperties> placeholder_style_when_hidden()return *style;}-void HTMLInputElement::update_placeholder_visibility()+void HTMLInputElement::update_text_input_decorations(){// Whether the placeholder shows is decided by the control's value, which no attribute carries,// so `:placeholder-shown` moves with it and the style engine has to be told.CSS::Invalidation::invalidate_style_after_placeholder_shown_change(*this);+ if (m_search_clear_button) {+ if (m_value.is_empty())+ set_own_inline_style(*m_search_clear_button, hidden_text_input_decoration_style());+ else+ m_search_clear_button->set_inline_style(nullptr);+ }+if (!m_placeholder_element)return;if (this->placeholder_value().has_value())set_own_inline_style(*m_placeholder_element, placeholder_style_when_visible());else- set_own_inline_style(*m_placeholder_element, placeholder_style_when_hidden());+ set_own_inline_style(*m_placeholder_element, hidden_text_input_decoration_style());}Utf16String HTMLInputElement::button_label() const@@ -983,13 +1000,13 @@ void HTMLInputElement::update_button_input_shadow_tree(){if (m_text_node) {m_text_node->set_data(button_label());- update_placeholder_visibility();+ update_text_input_decorations();}}void HTMLInputElement::update_text_input_shadow_tree(){- update_placeholder_visibility();+ update_text_input_decorations();update_date_time_fields();if (m_type == TypeAttributeState::Number && m_up_button_element && m_down_button_element) {@@ -1314,6 +1331,14 @@ void HTMLInputElement::create_text_input_shadow_tree()m_placeholder_text_node = DOM::Text::create(document(), placeholder());MUST(m_placeholder_element->append_child(*m_placeholder_text_node));+ if (type_state() == TypeAttributeState::Search) {+ m_search_clear_button = MUST(DOM::create_element(document(), TagNames::button, Namespace::HTML));+ m_search_clear_button->set_attribute_value(AttributeNames::type, "button"_utf16);+ m_search_clear_button->set_attribute_value(ARIA::AttributeNames::aria_label, "Clear search"_utf16);+ MUST(m_search_clear_button->set_text_content(u"\N{MULTIPLICATION SIGN}"_utf16));+ MUST(element->append_child(*m_search_clear_button));+ m_search_clear_button->set_associated_shadow_host_pseudo_element(CSS::PseudoElement::WebkitSearchCancelButton);+ }if (type_state() == TypeAttributeState::Number) {// NB: The buttons start out with the style they have when shown, so that the style application that follows// only rewrites their declarations when appearance hides them.@@ -1875,7 +1900,7 @@ void HTMLInputElement::form_associated_element_attribute_changed(Utf16FlyString} else if (name == HTML::AttributeNames::placeholder) {if (m_placeholder_text_node) {m_placeholder_text_node->set_data(placeholder());- update_placeholder_visibility();+ update_text_input_decorations();}} else if (name == HTML::AttributeNames::src) {handle_src_attribute(value.has_value() ? value->utf16_view() : u""sv).release_value_but_fixme_should_propagate_errors();@@ -1963,6 +1988,7 @@ void HTMLInputElement::type_attribute_changed(TypeAttributeState old_state, Type// 4. Update the element's rendering and behavior to the new state's.m_type = new_state;+ m_search_clear_button = nullptr;// NB: Which box the element gets depends on its state.set_needs_layout_tree_update(true, DOM::SetNeedsLayoutTreeUpdateReason::HTMLInputElementTypeAttribute);// Only the Image Button state maps align, border, width, height, hspace and vspace to hints,@@ -2504,7 +2530,7 @@ void HTMLInputElement::reset_algorithm()if (m_text_node) {m_text_node->set_data(m_value);- update_placeholder_visibility();+ update_text_input_decorations();}// NB: The relevant value is read from the text node — so, this has to come after the text node is updated.@@ -2543,7 +2569,7 @@ void HTMLInputElement::clear_algorithm()if (m_text_node) {m_text_node->set_data(m_value);- update_placeholder_visibility();+ update_text_input_decorations();}// NB: The relevant value is read from the text node — so, this has to come after the text node is updated.diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.hindex 2b2c81a88d1..c9e62973fbf 100644--- a/Libraries/LibWeb/HTML/HTMLInputElement.h+++ b/Libraries/LibWeb/HTML/HTMLInputElement.h@@ -107,6 +107,7 @@ public:void set_user_validity(bool);void commit_pending_changes();+ void clear_search_value(Badge<HTMLButtonElement>);bool has_uncommitted_changes() { return m_has_uncommitted_changes; }void ensure_user_agent_shadow_tree(Badge<Internals::Internals>) { create_shadow_tree_if_needed(); }@@ -418,8 +419,9 @@ private:static ValueAttributeMode value_attribute_mode_for_type_state(TypeAttributeState);ValueAttributeMode value_attribute_mode() const;- void update_placeholder_visibility();+ void update_text_input_decorations();GC::Ptr<DOM::Element> m_placeholder_element;+ GC::Ptr<DOM::Element> m_search_clear_button;GC::Ptr<DOM::Text> m_placeholder_text_node;void update_button_input_shadow_tree();diff --git a/Libraries/LibWeb/Rust/src/css/style/bridge.rs b/Libraries/LibWeb/Rust/src/css/style/bridge.rsindex ba5d796cbf0..e19bdaf21a5 100644--- a/Libraries/LibWeb/Rust/src/css/style/bridge.rs+++ b/Libraries/LibWeb/Rust/src/css/style/bridge.rs@@ -635,7 +635,7 @@ pub struct FfiElementArrival {}pub const FIRST_ELEMENT_REFERENCE_PSEUDO_ELEMENT_KIND: u8 = 11;-pub const LAST_ELEMENT_REFERENCE_PSEUDO_ELEMENT_KIND: u8 = 18;+pub const LAST_ELEMENT_REFERENCE_PSEUDO_ELEMENT_KIND: u8 = 19;/// 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.rsindex e8ece8c270f..720c67fe03f 100644--- a/Libraries/LibWeb/Rust/src/css/style/bridge/records.rs+++ b/Libraries/LibWeb/Rust/src/css/style/bridge/records.rs@@ -216,18 +216,19 @@ pub enum FfiDemandedPseudoElement {SpellingError = 9,ViewTransition = 10,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,+ WebkitSearchCancelButton = 12,+ ColorSwatch = 13,+ DetailsContent = 14,+ FileSelectorButton = 15,+ Placeholder = 16,+ SliderFill = 17,+ SliderThumb = 18,+ SliderTrack = 19,+ Highlight = 20,+ ViewTransitionGroup = 23,+ ViewTransitionImagePair = 24,+ ViewTransitionNew = 25,+ ViewTransitionOld = 26,}/// The answer to a record demand: the record, or that the pseudo-element read generates no boxdiff --git a/Tests/LibWeb/Text/expected/HTML/search-input-clear-button.txt b/Tests/LibWeb/Text/expected/HTML/search-input-clear-button.txtnew file mode 100644index 00000000000..ba69c7742be--- /dev/null+++ b/Tests/LibWeb/Text/expected/HTML/search-input-clear-button.txt@@ -0,0 +1,9 @@+canceled | sample | true | none | none+initial | | true | input/Event | input/Event,change/Event+empty | | true | none | none+reset RTL | | true | input/Event | input/Event,change/Event+read-only | value | true | none | none+disabled | value | false | none | none+hidden by author | value | true | none | none+text | value | true | none | none+back to search | | true | input/Event | input/Event,change/Eventdiff --git a/Tests/LibWeb/Text/expected/css/style-engine/matching-selector-shapes.txt b/Tests/LibWeb/Text/expected/css/style-engine/matching-selector-shapes.txtindex fe0f8d7ffe4..b86abd3cf4b 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(:noti: * * > * * :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 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 *+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 16] :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)diff --git a/Tests/LibWeb/Text/input/HTML/search-input-clear-button.html b/Tests/LibWeb/Text/input/HTML/search-input-clear-button.htmlnew file mode 100644index 00000000000..24cdfc61c4d--- /dev/null+++ b/Tests/LibWeb/Text/input/HTML/search-input-clear-button.html@@ -0,0 +1,46 @@+<!doctype html>+<script src="../include.js"></script>+<style>+ input { width: 200px; height: 32px; padding: 0; border: 0; font-size: 16px }+ input::-webkit-search-cancel-button { width: 16px; height: 16px; margin: 0 }+ .hide::-webkit-search-cancel-button { display: none }+</style>+<form><input id="field" type="search" value="sample"><button type="button" id="other">Next</button></form>+<script>+ test(() => {+ const events = [];+ for (const name of ["beforeinput", "input", "change"])+ field.addEventListener(name, event => events.push(event.type + "/" + event.constructor.name));+ function clickClear(label) {+ field.focus();+ const box = field.getBoundingClientRect();+ internals.click(field.dir === "rtl" ? box.left + 8 : box.right - 8, box.top + box.height / 2);+ const focused = document.activeElement === field;+ const immediate = events.join(",");+ other.focus();+ println([label, field.value, focused, immediate || "none", events.join(",") || "none"].join(" | "));+ events.length = 0;+ }+ field.addEventListener("click", event => event.preventDefault(), { once: true });+ clickClear("canceled");+ clickClear("initial");+ clickClear("empty");+ field.form.reset();+ field.dir = "rtl";+ clickClear("reset RTL");+ field.value = "value";+ field.readOnly = true;+ clickClear("read-only");+ field.readOnly = false;+ field.disabled = true;+ clickClear("disabled");+ field.disabled = false;+ field.className = "hide";+ clickClear("hidden by author");+ field.className = "";+ field.type = "text";+ clickClear("text");+ field.type = "search";+ clickClear("back to search");+ });+</script>