From a8004a77bba17f02667e22a8e850d2e9caa00f9b Mon Sep 17 00:00:00 2001 From: Psychpsyo Date: Mon, 17 Feb 2025 20:50:56 +0100 Subject: [PATCH] LibWeb: Implement setCustomValidity IDL attribute --- .../LibWeb/HTML/FormAssociatedElement.cpp | 18 ++++++++++++++++-- Libraries/LibWeb/HTML/FormAssociatedElement.h | 6 ++++++ Libraries/LibWeb/HTML/HTMLButtonElement.idl | 2 +- Libraries/LibWeb/HTML/HTMLFieldSetElement.idl | 2 +- Libraries/LibWeb/HTML/HTMLInputElement.cpp | 7 ------- Libraries/LibWeb/HTML/HTMLInputElement.h | 1 - Libraries/LibWeb/HTML/HTMLObjectElement.idl | 2 +- Libraries/LibWeb/HTML/HTMLOutputElement.idl | 2 +- Libraries/LibWeb/HTML/HTMLSelectElement.idl | 2 +- Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp | 6 ------ Libraries/LibWeb/HTML/HTMLTextAreaElement.h | 1 - .../select-setcustomvalidity.txt | 4 ++-- 12 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp index f118bfb6f9d..c2a3444bf2e 100644 --- a/Libraries/LibWeb/HTML/FormAssociatedElement.cpp +++ b/Libraries/LibWeb/HTML/FormAssociatedElement.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -56,6 +57,18 @@ GC::Ref FormAssociatedElement::validity() const return realm.create(realm, *this); } +// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity +void FormAssociatedElement::set_custom_validity(String& error) +{ + // The setCustomValidity(error) method steps are: + + // 1. Set error to the result of normalizing newlines given error. + error = Infra::normalize_newlines(error); + + // 2. Set the custom validity error message to error. + m_custom_validity_error_message = error; +} + bool FormAssociatedElement::enabled() const { // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-fe-disabled @@ -301,8 +314,9 @@ bool FormAssociatedElement::suffering_from_being_too_short() const // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#suffering-from-a-custom-error bool FormAssociatedElement::suffering_from_a_custom_error() const { - // FIXME: Implement this. - return false; + // When a control's custom validity error message (as set by the element's setCustomValidity() method or ElementInternals's setValidity() method) is not the empty + // string. + return !m_custom_validity_error_message.is_empty(); } // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#concept-textarea/input-relevant-value diff --git a/Libraries/LibWeb/HTML/FormAssociatedElement.h b/Libraries/LibWeb/HTML/FormAssociatedElement.h index c7af447946f..938e809a1e5 100644 --- a/Libraries/LibWeb/HTML/FormAssociatedElement.h +++ b/Libraries/LibWeb/HTML/FormAssociatedElement.h @@ -125,6 +125,9 @@ public: // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-validity GC::Ref validity() const; + // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity + void set_custom_validity(String& error); + protected: FormAssociatedElement() = default; virtual ~FormAssociatedElement() = default; @@ -144,6 +147,9 @@ private: // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#parser-inserted-flag bool m_parser_inserted { false }; + + // https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#custom-validity-error-message + String m_custom_validity_error_message; }; enum class SelectionSource { diff --git a/Libraries/LibWeb/HTML/HTMLButtonElement.idl b/Libraries/LibWeb/HTML/HTMLButtonElement.idl index 21d70b3fa88..b1eaeacf336 100644 --- a/Libraries/LibWeb/HTML/HTMLButtonElement.idl +++ b/Libraries/LibWeb/HTML/HTMLButtonElement.idl @@ -31,7 +31,7 @@ interface HTMLButtonElement : HTMLElement { [FIXME] readonly attribute DOMString validationMessage; [FIXME] boolean checkValidity(); [FIXME] boolean reportValidity(); - [FIXME] undefined setCustomValidity(DOMString error); + undefined setCustomValidity(DOMString error); readonly attribute NodeList labels; }; diff --git a/Libraries/LibWeb/HTML/HTMLFieldSetElement.idl b/Libraries/LibWeb/HTML/HTMLFieldSetElement.idl index ce8e4b09d4e..494619e5761 100644 --- a/Libraries/LibWeb/HTML/HTMLFieldSetElement.idl +++ b/Libraries/LibWeb/HTML/HTMLFieldSetElement.idl @@ -19,5 +19,5 @@ interface HTMLFieldSetElement : HTMLElement { [FIXME] readonly attribute DOMString validationMessage; [FIXME] boolean checkValidity(); [FIXME] boolean reportValidity(); - [FIXME] undefined setCustomValidity(DOMString error); + undefined setCustomValidity(DOMString error); }; diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 2a7111fc9ba..a633630e1df 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -2457,13 +2457,6 @@ WebIDL::ExceptionOr HTMLInputElement::report_validity() return true; } -// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity -void HTMLInputElement::set_custom_validity(String const& error) -{ - dbgln("(STUBBED) HTMLInputElement::set_custom_validity(error={}). Called on: {}", error, debug_description()); - return; -} - Optional HTMLInputElement::default_role() const { // http://wpt.live/html-aam/roles-dynamic-switch.tentative.window.html "Disconnected " diff --git a/Libraries/LibWeb/HTML/HTMLInputElement.h b/Libraries/LibWeb/HTML/HTMLInputElement.h index a2f024e4851..9e0993aae9a 100644 --- a/Libraries/LibWeb/HTML/HTMLInputElement.h +++ b/Libraries/LibWeb/HTML/HTMLInputElement.h @@ -151,7 +151,6 @@ public: WebIDL::ExceptionOr check_validity(); WebIDL::ExceptionOr report_validity(); - void set_custom_validity(String const&); WebIDL::ExceptionOr show_picker(); diff --git a/Libraries/LibWeb/HTML/HTMLObjectElement.idl b/Libraries/LibWeb/HTML/HTMLObjectElement.idl index 7d8c1342f12..dc10637152e 100644 --- a/Libraries/LibWeb/HTML/HTMLObjectElement.idl +++ b/Libraries/LibWeb/HTML/HTMLObjectElement.idl @@ -23,7 +23,7 @@ interface HTMLObjectElement : HTMLElement { [FIXME] readonly attribute DOMString validationMessage; [FIXME] boolean checkValidity(); [FIXME] boolean reportValidity(); - [FIXME] undefined setCustomValidity(DOMString error); + undefined setCustomValidity(DOMString error); // Obsolete [CEReactions, Reflect] attribute DOMString align; diff --git a/Libraries/LibWeb/HTML/HTMLOutputElement.idl b/Libraries/LibWeb/HTML/HTMLOutputElement.idl index 2a3a64be0bf..fd03c3b2a3b 100644 --- a/Libraries/LibWeb/HTML/HTMLOutputElement.idl +++ b/Libraries/LibWeb/HTML/HTMLOutputElement.idl @@ -20,7 +20,7 @@ interface HTMLOutputElement : HTMLElement { [FIXME] readonly attribute DOMString validationMessage; [FIXME] boolean checkValidity(); [FIXME] boolean reportValidity(); - [FIXME] undefined setCustomValidity(DOMString error); + undefined setCustomValidity(DOMString error); readonly attribute NodeList labels; }; diff --git a/Libraries/LibWeb/HTML/HTMLSelectElement.idl b/Libraries/LibWeb/HTML/HTMLSelectElement.idl index cbaef10bcf7..f335665ca36 100644 --- a/Libraries/LibWeb/HTML/HTMLSelectElement.idl +++ b/Libraries/LibWeb/HTML/HTMLSelectElement.idl @@ -36,7 +36,7 @@ interface HTMLSelectElement : HTMLElement { [FIXME] readonly attribute DOMString validationMessage; [FIXME] boolean checkValidity(); [FIXME] boolean reportValidity(); - [FIXME] undefined setCustomValidity(DOMString error); + undefined setCustomValidity(DOMString error); undefined showPicker(); diff --git a/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp b/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp index 88c10a63358..12ccbe92ecd 100644 --- a/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp +++ b/Libraries/LibWeb/HTML/HTMLTextAreaElement.cpp @@ -251,12 +251,6 @@ bool HTMLTextAreaElement::report_validity() return true; } -// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity -void HTMLTextAreaElement::set_custom_validity(String const& error) -{ - dbgln("(STUBBED) HTMLTextAreaElement::set_custom_validity(\"{}\"). Called on: {}", error, debug_description()); -} - // https://html.spec.whatwg.org/multipage/form-elements.html#dom-textarea-maxlength WebIDL::Long HTMLTextAreaElement::max_length() const { diff --git a/Libraries/LibWeb/HTML/HTMLTextAreaElement.h b/Libraries/LibWeb/HTML/HTMLTextAreaElement.h index a98b075724f..5ebafec8018 100644 --- a/Libraries/LibWeb/HTML/HTMLTextAreaElement.h +++ b/Libraries/LibWeb/HTML/HTMLTextAreaElement.h @@ -97,7 +97,6 @@ public: bool check_validity(); bool report_validity(); - void set_custom_validity(String const& error); WebIDL::Long max_length() const; WebIDL::ExceptionOr set_max_length(WebIDL::Long); diff --git a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-setcustomvalidity.txt b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-setcustomvalidity.txt index b71135715ee..862adbe589a 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-setcustomvalidity.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/html/semantics/forms/the-select-element/select-setcustomvalidity.txt @@ -2,5 +2,5 @@ Harness status: OK Found 1 tests -1 Fail -Fail select setCustomValidity is correct \ No newline at end of file +1 Pass +Pass select setCustomValidity is correct \ No newline at end of file