use std::cell::Cell;
use std::fmt;
use bitflags::bitflags;
use dom_struct::dom_struct;
use itertools::Itertools;
use style_dom::ElementState;
use super::bindings::codegen::Bindings::ElementInternalsBinding::ValidityStateFlags;
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 crate::script_runtime::CanGc;
#[derive(Clone, Copy, JSTraceable, MallocSizeOf)]
pub struct ValidationFlags(u32);
bitflags! {
impl ValidationFlags: u32 {
const VALUE_MISSING = 0b0000000001;
const TYPE_MISMATCH = 0b0000000010;
const PATTERN_MISMATCH = 0b0000000100;
const TOO_LONG = 0b0000001000;
const TOO_SHORT = 0b0000010000;
const RANGE_UNDERFLOW = 0b0000100000;
const RANGE_OVERFLOW = 0b0001000000;
const STEP_MISMATCH = 0b0010000000;
const BAD_INPUT = 0b0100000000;
const CUSTOM_ERROR = 0b1000000000;
}
}
impl fmt::Display for ValidationFlags {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
let flag_to_message = [
(ValidationFlags::VALUE_MISSING, "Value missing"),
(ValidationFlags::TYPE_MISMATCH, "Type mismatch"),
(ValidationFlags::PATTERN_MISMATCH, "Pattern mismatch"),
(ValidationFlags::TOO_LONG, "Too long"),
(ValidationFlags::TOO_SHORT, "Too short"),
(ValidationFlags::RANGE_UNDERFLOW, "Range underflow"),
(ValidationFlags::RANGE_OVERFLOW, "Range overflow"),
(ValidationFlags::STEP_MISMATCH, "Step mismatch"),
(ValidationFlags::BAD_INPUT, "Bad input"),
(ValidationFlags::CUSTOM_ERROR, "Custom error"),
];
flag_to_message
.iter()
.filter_map(|&(flag, flag_str)| {
if self.contains(flag) {
Some(flag_str)
} else {
None
}
})
.format(", ")
.fmt(formatter)
}
}
#[dom_struct]
pub struct ValidityState {
reflector_: Reflector,
element: Dom<Element>,
custom_error_message: DomRefCell<DOMString>,
invalid_flags: Cell<ValidationFlags>,
}
impl ValidityState {
fn new_inherited(element: &Element) -> ValidityState {
ValidityState {
reflector_: Reflector::new(),
element: Dom::from_ref(element),
custom_error_message: DomRefCell::new(DOMString::new()),
invalid_flags: Cell::new(ValidationFlags::empty()),
}
}
pub fn new(window: &Window, element: &Element) -> DomRoot<ValidityState> {
reflect_dom_object(
Box::new(ValidityState::new_inherited(element)),
window,
CanGc::note(),
)
}
pub fn custom_error_message(&self) -> Ref<DOMString> {
self.custom_error_message.borrow()
}
pub fn set_custom_error_message(&self, error: DOMString) {
*self.custom_error_message.borrow_mut() = error;
self.perform_validation_and_update(ValidationFlags::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);
}
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 update_invalid_flags(&self, update_flags: ValidationFlags) {
self.invalid_flags.set(update_flags);
}
pub fn invalid_flags(&self) -> ValidationFlags {
self.invalid_flags.get()
}
pub fn update_pseudo_classes(&self) {
if self.element.is_instance_validatable() {
let is_valid = self.invalid_flags.get().is_empty();
self.element.set_state(ElementState::VALID, is_valid);
self.element.set_state(ElementState::INVALID, !is_valid);
} else {
self.element.set_state(ElementState::VALID, false);
self.element.set_state(ElementState::INVALID, 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<crate::DomTypeHolder> for ValidityState {
fn ValueMissing(&self) -> bool {
self.invalid_flags()
.contains(ValidationFlags::VALUE_MISSING)
}
fn TypeMismatch(&self) -> bool {
self.invalid_flags()
.contains(ValidationFlags::TYPE_MISMATCH)
}
fn PatternMismatch(&self) -> bool {
self.invalid_flags()
.contains(ValidationFlags::PATTERN_MISMATCH)
}
fn TooLong(&self) -> bool {
self.invalid_flags().contains(ValidationFlags::TOO_LONG)
}
fn TooShort(&self) -> bool {
self.invalid_flags().contains(ValidationFlags::TOO_SHORT)
}
fn RangeUnderflow(&self) -> bool {
self.invalid_flags()
.contains(ValidationFlags::RANGE_UNDERFLOW)
}
fn RangeOverflow(&self) -> bool {
self.invalid_flags()
.contains(ValidationFlags::RANGE_OVERFLOW)
}
fn StepMismatch(&self) -> bool {
self.invalid_flags()
.contains(ValidationFlags::STEP_MISMATCH)
}
fn BadInput(&self) -> bool {
self.invalid_flags().contains(ValidationFlags::BAD_INPUT)
}
fn CustomError(&self) -> bool {
self.invalid_flags().contains(ValidationFlags::CUSTOM_ERROR)
}
fn Valid(&self) -> bool {
self.invalid_flags().is_empty()
}
}
impl From<&ValidityStateFlags> for ValidationFlags {
fn from(flags: &ValidityStateFlags) -> Self {
let mut bits = ValidationFlags::empty();
if flags.valueMissing {
bits |= ValidationFlags::VALUE_MISSING;
}
if flags.typeMismatch {
bits |= ValidationFlags::TYPE_MISMATCH;
}
if flags.patternMismatch {
bits |= ValidationFlags::PATTERN_MISMATCH;
}
if flags.tooLong {
bits |= ValidationFlags::TOO_LONG;
}
if flags.tooShort {
bits |= ValidationFlags::TOO_SHORT;
}
if flags.rangeUnderflow {
bits |= ValidationFlags::RANGE_UNDERFLOW;
}
if flags.rangeOverflow {
bits |= ValidationFlags::RANGE_OVERFLOW;
}
if flags.stepMismatch {
bits |= ValidationFlags::STEP_MISMATCH;
}
if flags.badInput {
bits |= ValidationFlags::BAD_INPUT;
}
if flags.customError {
bits |= ValidationFlags::CUSTOM_ERROR;
}
bits
}
}