Nacre/vendor/servo-script/dom/form/validation.rs
astar76 0ecfd0f573 S031: root due timers in vendored servo-script (R5-01 SIGSEGV), crash-adblock regression smoke
fire_timer moved due timers into an unrooted Vec; a GC inside one callback swept
the functions of the timers still waiting. Heavy-web reruns and cargo test still pending.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-12 20:38:47 -06:00

115 lines
4.3 KiB
Rust
Executable file

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use js::context::{JSContext, NoGC};
use crate::dom::bindings::codegen::Bindings::HTMLElementBinding::HTMLElementMethods;
use crate::dom::bindings::codegen::Bindings::HTMLOrSVGElementBinding::FocusOptions;
use crate::dom::bindings::inheritance::Castable;
use crate::dom::bindings::root::DomRoot;
use crate::dom::bindings::str::DOMString;
use crate::dom::element::Element;
use crate::dom::eventtarget::EventTarget;
use crate::dom::html::htmldatalistelement::HTMLDataListElement;
use crate::dom::html::htmlelement::HTMLElement;
use crate::dom::node::Node;
use crate::dom::validitystate::{ValidationFlags, ValidityState};
/// Trait for elements with constraint validation support
pub(crate) trait Validatable {
fn as_element(&self) -> &Element;
/// <https://html.spec.whatwg.org/multipage/#dom-cva-validity>
fn validity_state(&self, cx: &mut JSContext) -> DomRoot<ValidityState>;
/// <https://html.spec.whatwg.org/multipage/#candidate-for-constraint-validation>
fn is_instance_validatable(&self, no_gc: &NoGC) -> bool;
// Check if element satisfies its constraints, excluding custom errors
fn perform_validation(
&self,
_cx: &mut JSContext,
_validate_flags: ValidationFlags,
) -> ValidationFlags {
ValidationFlags::empty()
}
/// <https://html.spec.whatwg.org/multipage/#concept-fv-valid>
fn satisfies_constraints(&self, cx: &mut JSContext) -> bool {
self.validity_state(cx).invalid_flags().is_empty()
}
/// <https://html.spec.whatwg.org/multipage/#check-validity-steps>
fn check_validity(&self, cx: &mut JSContext) -> bool {
if self.is_instance_validatable(cx.no_gc()) && !self.satisfies_constraints(cx) {
self.as_element()
.upcast::<EventTarget>()
.fire_cancelable_event(cx, atom!("invalid"));
false
} else {
true
}
}
/// <https://html.spec.whatwg.org/multipage/#report-validity-steps>
fn report_validity(&self, cx: &mut JSContext) -> bool {
// Step 1.
if !self.is_instance_validatable(cx.no_gc()) {
return true;
}
if self.satisfies_constraints(cx) {
return true;
}
// Step 1.1: Let `report` be the result of firing an event named invalid at element,
// with the cancelable attribute initialized to true.
let report = self
.as_element()
.upcast::<EventTarget>()
.fire_cancelable_event(cx, atom!("invalid"));
// Step 1.2. If `report` is true, for the element,
// report the problem, run focusing steps, scroll into view.
if report {
let flags = self.validity_state(cx).invalid_flags();
println!(
"Validation error: {}",
validation_message_for_flags(&self.validity_state(cx), flags)
);
if let Some(html_elem) = self.as_element().downcast::<HTMLElement>() {
// Run focusing steps and scroll into view.
html_elem.Focus(cx, &FocusOptions::default());
}
}
// Step 1.3.
false
}
/// <https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage>
fn validation_message(&self, cx: &mut JSContext) -> DOMString {
if self.is_instance_validatable(cx.no_gc()) {
let flags = self.validity_state(cx).invalid_flags();
validation_message_for_flags(&self.validity_state(cx), flags)
} else {
DOMString::new()
}
}
}
/// <https://html.spec.whatwg.org/multipage/#the-datalist-element%3Abarred-from-constraint-validation>
pub(crate) fn is_barred_by_datalist_ancestor(no_gc: &NoGC, elem: &Node) -> bool {
elem.upcast::<Node>()
.ancestors_unrooted(no_gc)
.any(|node| node.is::<HTMLDataListElement>())
}
// Get message for given validation flags or custom error message
fn validation_message_for_flags(state: &ValidityState, failed_flags: ValidationFlags) -> DOMString {
if failed_flags.contains(ValidationFlags::CUSTOM_ERROR) {
state.custom_error_message().clone()
} else {
DOMString::from(failed_flags.to_string())
}
}