Auto merge of #13969 - bbansalWolfPack:master, r=jdm

html form validation initial steps with  test html file

<!-- Please describe your changes on the following line: -->

Added code for initial steps in html form validation.
1. Added methods for trait validatable
2. implemented stub methods for  elements like HTMLInputElement, HTMLButtonElement, etc

<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [X ] `./mach build -d` does not report any errors
- [ X] `./mach test-tidy` does not report any errors
- [ ] These changes fix #__ (github issue number if applicable).

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->

---

This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/13969)

<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2016-11-25 06:49:44 -08:00 committed by GitHub
commit beec035eb0
9 changed files with 124 additions and 15 deletions

45
components/script/dom/htmlformelement.rs Normal file → Executable file
View file

@ -35,6 +35,7 @@ use dom::htmloutputelement::HTMLOutputElement;
use dom::htmlselectelement::HTMLSelectElement;
use dom::htmltextareaelement::HTMLTextAreaElement;
use dom::node::{Node, document_from_node, window_from_node};
use dom::validitystate::ValidationFlags;
use dom::virtualmethods::VirtualMethods;
use encoding::EncodingRef;
use encoding::all::UTF_8;
@ -470,12 +471,22 @@ impl HTMLFormElement {
// form, refactor this when html5ever's form owner PR lands
// Step 1-3
let invalid_controls = node.traverse_preorder().filter_map(|field| {
if let Some(_el) = field.downcast::<Element>() {
None // Remove this line if you decide to refactor
// XXXKiChjang: Form control elements should each have a candidate_for_validation
// and satisfies_constraints methods
if let Some(el) = field.downcast::<Element>() {
if el.disabled_state() {
None
} else {
let validatable = match el.as_maybe_validatable() {
Some(v) => v,
None => return None
};
if !validatable.is_instance_validatable() {
None
} else if validatable.validate(ValidationFlags::empty()) {
None
} else {
Some(FormSubmittableElement::from_element(&el))
}
}
} else {
None
}
@ -697,7 +708,7 @@ pub enum FormSubmittableElement {
// KeygenElement(&'a HTMLKeygenElement),
ObjectElement(Root<HTMLObjectElement>),
SelectElement(Root<HTMLSelectElement>),
TextAreaElement(Root<HTMLTextAreaElement>)
TextAreaElement(Root<HTMLTextAreaElement>),
}
impl FormSubmittableElement {
@ -710,6 +721,26 @@ impl FormSubmittableElement {
FormSubmittableElement::TextAreaElement(ref textarea) => textarea.upcast()
}
}
fn from_element(element: &Element) -> FormSubmittableElement {
if let Some(input) = element.downcast::<HTMLInputElement>() {
FormSubmittableElement::InputElement(Root::from_ref(&input))
}
else if let Some(input) = element.downcast::<HTMLButtonElement>() {
FormSubmittableElement::ButtonElement(Root::from_ref(&input))
}
else if let Some(input) = element.downcast::<HTMLObjectElement>() {
FormSubmittableElement::ObjectElement(Root::from_ref(&input))
}
else if let Some(input) = element.downcast::<HTMLSelectElement>() {
FormSubmittableElement::SelectElement(Root::from_ref(&input))
}
else if let Some(input) = element.downcast::<HTMLTextAreaElement>() {
FormSubmittableElement::TextAreaElement(Root::from_ref(&input))
} else {
unreachable!()
}
}
}
#[derive(Copy, Clone, HeapSizeOf)]