Implement :valid :invalid pseudo classes (#26729)

Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
John Poge II 2023-07-21 12:42:03 +02:00 committed by GitHub
parent 2b67392fd5
commit 5f2c6c09cd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 324 additions and 241 deletions

View file

@ -4,17 +4,24 @@
use crate::dom::bindings::cell::{DomRefCell, Ref};
use crate::dom::bindings::codegen::Bindings::ValidityStateBinding::ValidityStateMethods;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::reflector::{reflect_dom_object, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::DOMString;
use crate::dom::element::Element;
use crate::dom::htmlfieldsetelement::HTMLFieldSetElement;
use crate::dom::htmlformelement::FormControlElementHelpers;
use crate::dom::node::Node;
use crate::dom::window::Window;
use dom_struct::dom_struct;
use itertools::Itertools;
use std::cell::Cell;
use std::fmt;
use style::element_state::ElementState;
// https://html.spec.whatwg.org/multipage/#validity-states
bitflags! {
#[derive(JSTraceable, MallocSizeOf)]
pub struct ValidationFlags: u32 {
const VALUE_MISSING = 0b0000000001;
const TYPE_MISMATCH = 0b0000000010;
@ -64,6 +71,7 @@ pub struct ValidityState {
reflector_: Reflector,
element: Dom<Element>,
custom_error_message: DomRefCell<DOMString>,
invalid_flags: Cell<ValidationFlags>,
}
impl ValidityState {
@ -72,6 +80,7 @@ impl ValidityState {
reflector_: Reflector::new(),
element: Dom::from_ref(element),
custom_error_message: DomRefCell::new(DOMString::new()),
invalid_flags: Cell::new(ValidationFlags::empty()),
}
}
@ -87,84 +96,130 @@ impl ValidityState {
// https://html.spec.whatwg.org/multipage/#custom-validity-error-message
pub fn set_custom_error_message(&self, error: DOMString) {
*self.custom_error_message.borrow_mut() = error;
self.perform_validation_and_update(ValidationFlags::CUSTOM_ERROR);
}
/// Given a set of [ValidationFlags], recalculate their value by performing
/// validation on this [ValidityState]'s associated element. Additionally,
/// if [ValidationFlags::CUSTOM_ERROR] is in `update_flags` and a custom
/// error has been set on this [ValidityState], the state will be updated
/// to reflect the existance of a custom error.
pub fn perform_validation_and_update(&self, update_flags: ValidationFlags) {
let mut invalid_flags = self.invalid_flags.get();
invalid_flags.remove(update_flags);
if let Some(validatable) = self.element.as_maybe_validatable() {
let new_flags = validatable.perform_validation(update_flags);
invalid_flags.insert(new_flags);
}
// https://html.spec.whatwg.org/multipage/#suffering-from-a-custom-error
if update_flags.contains(ValidationFlags::CUSTOM_ERROR) &&
!self.custom_error_message().is_empty()
{
invalid_flags.insert(ValidationFlags::CUSTOM_ERROR);
}
self.invalid_flags.set(invalid_flags);
self.update_pseudo_classes();
}
pub fn invalid_flags(&self) -> ValidationFlags {
self.invalid_flags.get()
}
fn update_pseudo_classes(&self) {
if let Some(validatable) = self.element.as_maybe_validatable() {
if validatable.is_instance_validatable() {
let is_valid = self.invalid_flags.get().is_empty();
self.element
.set_state(ElementState::IN_VALID_STATE, is_valid);
self.element
.set_state(ElementState::IN_INVALID_STATE, !is_valid);
} else {
self.element.set_state(ElementState::IN_VALID_STATE, false);
self.element
.set_state(ElementState::IN_INVALID_STATE, false);
}
}
if let Some(form_control) = self.element.as_maybe_form_control() {
if let Some(form_owner) = form_control.form_owner() {
form_owner.update_validity();
}
}
if let Some(fieldset) = self
.element
.upcast::<Node>()
.ancestors()
.filter_map(DomRoot::downcast::<HTMLFieldSetElement>)
.next()
{
fieldset.update_validity();
}
}
}
impl ValidityStateMethods for ValidityState {
// https://html.spec.whatwg.org/multipage/#dom-validitystate-valuemissing
fn ValueMissing(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::VALUE_MISSING).is_empty()
})
self.invalid_flags()
.contains(ValidationFlags::VALUE_MISSING)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-typemismatch
fn TypeMismatch(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::TYPE_MISMATCH).is_empty()
})
self.invalid_flags()
.contains(ValidationFlags::TYPE_MISMATCH)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-patternmismatch
fn PatternMismatch(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::PATTERN_MISMATCH).is_empty()
})
self.invalid_flags()
.contains(ValidationFlags::PATTERN_MISMATCH)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-toolong
fn TooLong(&self) -> bool {
self.element
.as_maybe_validatable()
.map_or(false, |e| !e.validate(ValidationFlags::TOO_LONG).is_empty())
self.invalid_flags().contains(ValidationFlags::TOO_LONG)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-tooshort
fn TooShort(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::TOO_SHORT).is_empty()
})
self.invalid_flags().contains(ValidationFlags::TOO_SHORT)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-rangeunderflow
fn RangeUnderflow(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::RANGE_UNDERFLOW).is_empty()
})
self.invalid_flags()
.contains(ValidationFlags::RANGE_UNDERFLOW)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-rangeoverflow
fn RangeOverflow(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::RANGE_OVERFLOW).is_empty()
})
self.invalid_flags()
.contains(ValidationFlags::RANGE_OVERFLOW)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-stepmismatch
fn StepMismatch(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::STEP_MISMATCH).is_empty()
})
self.invalid_flags()
.contains(ValidationFlags::STEP_MISMATCH)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-badinput
fn BadInput(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::BAD_INPUT).is_empty()
})
self.invalid_flags().contains(ValidationFlags::BAD_INPUT)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-customerror
fn CustomError(&self) -> bool {
self.element.as_maybe_validatable().map_or(false, |e| {
!e.validate(ValidationFlags::CUSTOM_ERROR).is_empty()
})
self.invalid_flags().contains(ValidationFlags::CUSTOM_ERROR)
}
// https://html.spec.whatwg.org/multipage/#dom-validitystate-valid
fn Valid(&self) -> bool {
self.element
.as_maybe_validatable()
.map_or(true, |e| e.validate(ValidationFlags::all()).is_empty())
self.invalid_flags().is_empty()
}
}