LibWeb: Implement the form-control willValidate property

This change — part of the HTML constraint-validation API (aka
“client-side form validation”) — implements the willValidate IDL/DOM
attribute/property for all form controls that support it.
This commit is contained in:
sideshowbarker
2025-02-25 17:43:11 +09:00
committed by Tim Ledbetter
parent 7da5869b14
commit e79319ad85
24 changed files with 256 additions and 9 deletions

View File

@@ -156,6 +156,13 @@ void HTMLButtonElement::activation_behavior(DOM::Event const& event)
PopoverInvokerElement::popover_target_activation_behaviour(*this, as<DOM::Node>(*event.target()));
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-willvalidate
bool HTMLButtonElement::will_validate()
{
// The willValidate attribute's getter must return true, if this element is a candidate for constraint validation
return is_candidate_for_constraint_validation();
}
bool HTMLButtonElement::is_focusable() const
{
return enabled();

View File

@@ -44,6 +44,8 @@ public:
virtual void form_associated_element_attribute_changed(FlyString const& name, Optional<String> const& value, Optional<FlyString> const& namespace_) override;
bool will_validate();
// ^EventTarget
// https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-button-element
// https://html.spec.whatwg.org/multipage/interaction.html#focusable-area

View File

@@ -26,7 +26,7 @@ interface HTMLButtonElement : HTMLElement {
[CEReactions, ImplementedAs=type_for_bindings, Enumerated=ButtonTypeState] attribute DOMString type;
[CEReactions, Reflect] attribute DOMString value;
[FIXME] readonly attribute boolean willValidate;
readonly attribute boolean willValidate;
readonly attribute ValidityState validity;
[FIXME] readonly attribute DOMString validationMessage;
[FIXME] boolean checkValidity();

View File

@@ -88,4 +88,15 @@ GC::Ptr<Layout::Node> HTMLFieldSetElement::create_layout_node(GC::Ref<CSS::Compu
return heap().allocate<Layout::FieldSetBox>(document(), *this, style);
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-willvalidate
bool HTMLFieldSetElement::will_validate()
{
// The willValidate attribute's getter must return true, if this element is a candidate for constraint validation,
// and false otherwise (i.e., false if any conditions are barring it from constraint validation).
// A submittable element is a candidate for constraint validation
// https://html.spec.whatwg.org/multipage/forms.html#category-submit
// Submittable elements: button, input, select, textarea, form-associated custom elements [but not fieldset]
return false;
}
}

View File

@@ -42,6 +42,8 @@ public:
virtual Optional<ARIA::Role> default_role() const override { return ARIA::Role::group; }
static bool will_validate();
virtual GC::Ptr<Layout::Node> create_layout_node(GC::Ref<CSS::ComputedProperties>) override;
Layout::FieldSetBox* layout_node();
Layout::FieldSetBox const* layout_node() const;

View File

@@ -14,7 +14,7 @@ interface HTMLFieldSetElement : HTMLElement {
[SameObject] readonly attribute HTMLCollection elements;
[FIXME] readonly attribute boolean willValidate;
readonly attribute boolean willValidate;
[FIXME, SameObject] readonly attribute ValidityState validity;
[FIXME] readonly attribute DOMString validationMessage;
[FIXME] boolean checkValidity();

View File

@@ -2523,6 +2523,13 @@ WebIDL::ExceptionOr<void> HTMLInputElement::step_up_or_down(bool is_down, WebIDL
return {};
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-willvalidate
bool HTMLInputElement::will_validate()
{
// The willValidate attribute's getter must return true, if this element is a candidate for constraint validation
return is_candidate_for_constraint_validation();
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity
WebIDL::ExceptionOr<bool> HTMLInputElement::check_validity()
{

View File

@@ -149,6 +149,7 @@ public:
WebIDL::ExceptionOr<void> step_up(WebIDL::Long n = 1);
WebIDL::ExceptionOr<void> step_down(WebIDL::Long n = 1);
bool will_validate();
WebIDL::ExceptionOr<bool> check_validity();
WebIDL::ExceptionOr<bool> report_validity();

View File

@@ -51,7 +51,7 @@ interface HTMLInputElement : HTMLElement {
undefined stepUp(optional long n = 1);
undefined stepDown(optional long n = 1);
[FIXME] readonly attribute boolean willValidate;
readonly attribute boolean willValidate;
readonly attribute ValidityState validity;
[FIXME] readonly attribute DOMString validationMessage;
boolean checkValidity();

View File

@@ -79,6 +79,17 @@ void HTMLObjectElement::visit_edges(Cell::Visitor& visitor)
visitor.visit(m_document_observer);
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-willvalidate
bool HTMLObjectElement::will_validate()
{
// The willValidate attribute's getter must return true, if this element is a candidate for constraint validation,
// and false otherwise (i.e., false if any conditions are barring it from constraint validation).
// A submittable element is a candidate for constraint validation
// https://html.spec.whatwg.org/multipage/forms.html#category-submit
// Submittable elements: button, input, select, textarea, form-associated custom elements [but not object]
return false;
}
void HTMLObjectElement::form_associated_element_attribute_changed(FlyString const& name, Optional<String> const&, Optional<FlyString> const&)
{
// https://html.spec.whatwg.org/multipage/iframe-embed-object.html#the-object-element

View File

@@ -47,6 +47,8 @@ public:
virtual void visit_edges(Cell::Visitor&) override;
static bool will_validate();
private:
HTMLObjectElement(DOM::Document&, DOM::QualifiedName);

View File

@@ -18,7 +18,7 @@ interface HTMLObjectElement : HTMLElement {
readonly attribute WindowProxy? contentWindow;
Document? getSVGDocument();
[FIXME] readonly attribute boolean willValidate;
readonly attribute boolean willValidate;
readonly attribute ValidityState validity;
[FIXME] readonly attribute DOMString validationMessage;
[FIXME] boolean checkValidity();

View File

@@ -110,4 +110,15 @@ void HTMLOutputElement::clear_algorithm()
string_replace_all({});
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-willvalidate
bool HTMLOutputElement::will_validate()
{
// The willValidate attribute's getter must return true, if this element is a candidate for constraint validation,
// and false otherwise (i.e., false if any conditions are barring it from constraint validation).
// A submittable element is a candidate for constraint validation
// https://html.spec.whatwg.org/multipage/forms.html#category-submit
// Submittable elements: button, input, select, textarea, form-associated custom elements [but not output]
return false;
}
}

View File

@@ -57,6 +57,8 @@ public:
// https://www.w3.org/TR/html-aria/#el-output
virtual Optional<ARIA::Role> default_role() const override { return ARIA::Role::status; }
static bool will_validate();
private:
HTMLOutputElement(DOM::Document&, DOM::QualifiedName);

View File

@@ -15,7 +15,7 @@ interface HTMLOutputElement : HTMLElement {
[CEReactions] attribute DOMString defaultValue;
[CEReactions] attribute DOMString value;
[FIXME] readonly attribute boolean willValidate;
readonly attribute boolean willValidate;
readonly attribute ValidityState validity;
[FIXME] readonly attribute DOMString validationMessage;
[FIXME] boolean checkValidity();

View File

@@ -695,6 +695,13 @@ void HTMLSelectElement::update_selectedness()
update_inner_text_element();
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-willvalidate
bool HTMLSelectElement::will_validate()
{
// The willValidate attribute's getter must return true, if this element is a candidate for constraint validation
return is_candidate_for_constraint_validation();
}
bool HTMLSelectElement::is_focusable() const
{
return enabled();

View File

@@ -58,6 +58,8 @@ public:
Vector<GC::Root<HTMLOptionElement>> list_of_options() const;
bool will_validate();
// ^EventTarget
// https://html.spec.whatwg.org/multipage/interaction.html#the-tabindex-attribute:the-select-element
// https://html.spec.whatwg.org/multipage/interaction.html#focusable-area

View File

@@ -31,7 +31,7 @@ interface HTMLSelectElement : HTMLElement {
attribute long selectedIndex;
attribute DOMString value;
[FIXME] readonly attribute boolean willValidate;
readonly attribute boolean willValidate;
readonly attribute ValidityState validity;
[FIXME] readonly attribute DOMString validationMessage;
[FIXME] boolean checkValidity();

View File

@@ -237,6 +237,13 @@ u32 HTMLTextAreaElement::text_length() const
return AK::utf16_code_unit_length_from_utf8(api_value());
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-willvalidate
bool HTMLTextAreaElement::will_validate()
{
// The willValidate attribute's getter must return true, if this element is a candidate for constraint validation
return is_candidate_for_constraint_validation();
}
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-checkvalidity
bool HTMLTextAreaElement::check_validity()
{

View File

@@ -95,6 +95,7 @@ public:
u32 text_length() const;
bool will_validate();
bool check_validity();
bool report_validity();

View File

@@ -26,7 +26,7 @@ interface HTMLTextAreaElement : HTMLElement {
[LegacyNullToEmptyString] attribute DOMString value;
readonly attribute unsigned long textLength;
[FIXME] readonly attribute boolean willValidate;
readonly attribute boolean willValidate;
readonly attribute ValidityState validity;
[FIXME] readonly attribute DOMString validationMessage;
boolean checkValidity();