aboutsummaryrefslogtreecommitdiffstats
path: root/components/script/dom/htmlbuttonelement.rs
diff options
context:
space:
mode:
authorteapotd <teapot404@gmail.com>2020-04-01 11:08:02 +0200
committerteapotd <teapot404@gmail.com>2020-04-02 10:16:46 +0200
commit779552ee7ddbbde1055c7202e16b9a13c3961988 (patch)
treef3dac563783cfeaa5ca69851c426d7e7b4dff9c3 /components/script/dom/htmlbuttonelement.rs
parente47e884cc738a5cb472416a4fbdd9d2a32a2385c (diff)
downloadservo-779552ee7ddbbde1055c7202e16b9a13c3961988.tar.gz
servo-779552ee7ddbbde1055c7202e16b9a13c3961988.zip
Form constraints validation
Diffstat (limited to 'components/script/dom/htmlbuttonelement.rs')
-rwxr-xr-xcomponents/script/dom/htmlbuttonelement.rs63
1 files changed, 49 insertions, 14 deletions
diff --git a/components/script/dom/htmlbuttonelement.rs b/components/script/dom/htmlbuttonelement.rs
index 45d1630e6d4..cb6e1d0c056 100755
--- a/components/script/dom/htmlbuttonelement.rs
+++ b/components/script/dom/htmlbuttonelement.rs
@@ -19,8 +19,8 @@ use crate::dom::htmlformelement::{FormControl, FormDatum, FormDatumValue};
use crate::dom::htmlformelement::{FormSubmitter, ResetFrom, SubmittedFrom};
use crate::dom::node::{window_from_node, BindContext, Node, UnbindContext};
use crate::dom::nodelist::NodeList;
-use crate::dom::validation::Validatable;
-use crate::dom::validitystate::{ValidationFlags, ValidityState};
+use crate::dom::validation::{is_barred_by_datalist_ancestor, Validatable};
+use crate::dom::validitystate::ValidityState;
use crate::dom::virtualmethods::VirtualMethods;
use dom_struct::dom_struct;
use html5ever::{LocalName, Prefix};
@@ -41,6 +41,7 @@ pub struct HTMLButtonElement {
button_type: Cell<ButtonType>,
form_owner: MutNullableDom<HTMLFormElement>,
labels_node_list: MutNullableDom<NodeList>,
+ validity_state: MutNullableDom<ValidityState>,
}
impl HTMLButtonElement {
@@ -59,6 +60,7 @@ impl HTMLButtonElement {
button_type: Cell::new(ButtonType::Submit),
form_owner: Default::default(),
labels_node_list: Default::default(),
+ validity_state: Default::default(),
}
}
@@ -78,12 +80,6 @@ impl HTMLButtonElement {
}
impl HTMLButtonElementMethods for HTMLButtonElement {
- // https://html.spec.whatwg.org/multipage/#dom-cva-validity
- fn Validity(&self) -> DomRoot<ValidityState> {
- let window = window_from_node(self);
- ValidityState::new(&window, self.upcast())
- }
-
// https://html.spec.whatwg.org/multipage/#dom-fe-disabled
make_bool_getter!(Disabled, "disabled");
@@ -150,6 +146,36 @@ impl HTMLButtonElementMethods for HTMLButtonElement {
// https://html.spec.whatwg.org/multipage/#dom-lfe-labels
make_labels_getter!(Labels, labels_node_list);
+
+ // https://html.spec.whatwg.org/multipage/#dom-cva-willvalidate
+ fn WillValidate(&self) -> bool {
+ self.is_instance_validatable()
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-cva-validity
+ fn Validity(&self) -> DomRoot<ValidityState> {
+ self.validity_state()
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-cva-checkvalidity
+ fn CheckValidity(&self) -> bool {
+ self.check_validity()
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-cva-reportvalidity
+ fn ReportValidity(&self) -> bool {
+ self.report_validity()
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-cva-validationmessage
+ fn ValidationMessage(&self) -> DOMString {
+ self.validation_message()
+ }
+
+ // https://html.spec.whatwg.org/multipage/#dom-cva-setcustomvalidity
+ fn SetCustomValidity(&self, error: DOMString) {
+ self.validity_state().set_custom_error_message(error);
+ }
}
impl HTMLButtonElement {
@@ -268,13 +294,22 @@ impl FormControl for HTMLButtonElement {
}
impl Validatable for HTMLButtonElement {
- fn is_instance_validatable(&self) -> bool {
- true
+ fn as_element(&self) -> &Element {
+ self.upcast()
+ }
+
+ fn validity_state(&self) -> DomRoot<ValidityState> {
+ self.validity_state
+ .or_init(|| ValidityState::new(&window_from_node(self), self.upcast()))
}
- fn validate(&self, validate_flags: ValidationFlags) -> bool {
- if validate_flags.is_empty() {}
- // Need more flag check for different validation types later
- true
+
+ fn is_instance_validatable(&self) -> bool {
+ // https://html.spec.whatwg.org/multipage/#the-button-element%3Abarred-from-constraint-validation
+ // https://html.spec.whatwg.org/multipage/#enabling-and-disabling-form-controls%3A-the-disabled-attribute%3Abarred-from-constraint-validation
+ // https://html.spec.whatwg.org/multipage/#the-datalist-element%3Abarred-from-constraint-validation
+ self.button_type.get() == ButtonType::Submit &&
+ !self.upcast::<Element>().disabled_state() &&
+ !is_barred_by_datalist_ancestor(self.upcast())
}
}