Implement form-associated custom elements and their ElementInternals (#31980)

* FACEs work, setFormValue test is awful so now has _mozilla backup

* 1. Impl Validatable in ElementInternals instead of HTMLElement. 2. Reuse the code in Validatable trait. 3. The form associated custom element is not a customized built-in element.

* add some comments

* support readonly attribute and complete barred from constraint validation

* Addressed the code review comments

* Updated the legacy-layout results

* Fixed the WPT failures in ElementInternals-validation.html

* Addressed the code review comments

* Review suggestions

* Fixed silly mistakes and update the test result outside elementinternals

* update the test results

---------

Co-authored-by: Patrick Shaughnessy <pshaughn@comcast.net>
Co-authored-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
cathiechen 2024-04-11 15:17:11 +02:00 committed by GitHub
parent 2eb959a159
commit 4e4a4c0a28
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
67 changed files with 1641 additions and 619 deletions

View file

@ -17,10 +17,10 @@ use crate::dom::validitystate::{ValidationFlags, ValidityState};
pub trait Validatable {
fn as_element(&self) -> &Element;
// https://html.spec.whatwg.org/multipage/#dom-cva-validity
/// <https://html.spec.whatwg.org/multipage/#dom-cva-validity>
fn validity_state(&self) -> DomRoot<ValidityState>;
// https://html.spec.whatwg.org/multipage/#candidate-for-constraint-validation
/// <https://html.spec.whatwg.org/multipage/#candidate-for-constraint-validation>
fn is_instance_validatable(&self) -> bool;
// Check if element satisfies its constraints, excluding custom errors
@ -28,9 +28,14 @@ pub trait Validatable {
ValidationFlags::empty()
}
// https://html.spec.whatwg.org/multipage/#check-validity-steps
/// <https://html.spec.whatwg.org/multipage/#concept-fv-valid>
fn satisfies_constraints(&self) -> bool {
self.validity_state().invalid_flags().is_empty()
}
/// <https://html.spec.whatwg.org/multipage/#check-validity-steps>
fn check_validity(&self) -> bool {
if self.is_instance_validatable() && !self.validity_state().invalid_flags().is_empty() {
if self.is_instance_validatable() && !self.satisfies_constraints() {
self.as_element()
.upcast::<EventTarget>()
.fire_cancelable_event(atom!("invalid"));
@ -40,15 +45,14 @@ pub trait Validatable {
}
}
// https://html.spec.whatwg.org/multipage/#report-validity-steps
/// <https://html.spec.whatwg.org/multipage/#report-validity-steps>
fn report_validity(&self) -> bool {
// Step 1.
if !self.is_instance_validatable() {
return true;
}
let flags = self.validity_state().invalid_flags();
if flags.is_empty() {
if self.satisfies_constraints() {
return true;
}
@ -60,6 +64,7 @@ pub trait Validatable {
// Step 1.2.
if !event.DefaultPrevented() {
let flags = self.validity_state().invalid_flags();
println!(
"Validation error: {}",
validation_message_for_flags(&self.validity_state(), flags)
@ -73,7 +78,7 @@ pub trait Validatable {
false
}
// https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage
/// <https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage>
fn validation_message(&self) -> DOMString {
if self.is_instance_validatable() {
let flags = self.validity_state().invalid_flags();
@ -84,7 +89,7 @@ pub trait Validatable {
}
}
// https://html.spec.whatwg.org/multipage/#the-datalist-element%3Abarred-from-constraint-validation
/// <https://html.spec.whatwg.org/multipage/#the-datalist-element%3Abarred-from-constraint-validation>
pub fn is_barred_by_datalist_ancestor(elem: &Node) -> bool {
elem.upcast::<Node>()
.ancestors()