LibWeb: Implement setCustomValidity IDL attribute

This commit is contained in:
Psychpsyo 2025-02-17 20:50:56 +01:00 committed by Andrew Kaster
parent 0926720c2a
commit a8004a77bb
Notes: github-actions[bot] 2025-02-18 17:18:11 +00:00
12 changed files with 29 additions and 24 deletions

View file

@ -24,6 +24,7 @@
#include <LibWeb/HTML/HTMLTextAreaElement.h>
#include <LibWeb/HTML/Parser/HTMLParser.h>
#include <LibWeb/HTML/ValidityState.h>
#include <LibWeb/Infra/Strings.h>
#include <LibWeb/Painting/Paintable.h>
#include <LibWeb/Selection/Selection.h>
@ -56,6 +57,18 @@ GC::Ref<ValidityState const> FormAssociatedElement::validity() const
return realm.create<ValidityState>(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

View file

@ -125,6 +125,9 @@ public:
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-validity
GC::Ref<ValidityState const> 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 {

View file

@ -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;
};

View file

@ -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);
};

View file

@ -2457,13 +2457,6 @@ WebIDL::ExceptionOr<bool> 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<ARIA::Role> HTMLInputElement::default_role() const
{
// http://wpt.live/html-aam/roles-dynamic-switch.tentative.window.html "Disconnected <input type=checkbox switch>"

View file

@ -151,7 +151,6 @@ public:
WebIDL::ExceptionOr<bool> check_validity();
WebIDL::ExceptionOr<bool> report_validity();
void set_custom_validity(String const&);
WebIDL::ExceptionOr<void> show_picker();

View file

@ -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;

View file

@ -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;
};

View file

@ -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();

View file

@ -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
{

View file

@ -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<void> set_max_length(WebIDL::Long);

View file

@ -2,5 +2,5 @@ Harness status: OK
Found 1 tests
1 Fail
Fail select setCustomValidity is correct
1 Pass
Pass select setCustomValidity is correct