Add comments for the "Constructing the form data set" algorithm

This commit is contained in:
Arthur Skobara 2015-12-13 16:25:29 +06:00
parent e4f86829d7
commit 8d2f9fc586
2 changed files with 22 additions and 10 deletions

View file

@ -219,16 +219,21 @@ impl HTMLFormElement {
win.pipeline(), load_data)).unwrap(); win.pipeline(), load_data)).unwrap();
} }
/// https://html.spec.whatwg.org/multipage/#constructing-the-form-data-set
/// Steps range from 1 to 3
fn get_unclean_dataset(&self, submitter: Option<FormSubmitter>) -> Vec<FormDatum> { fn get_unclean_dataset(&self, submitter: Option<FormSubmitter>) -> Vec<FormDatum> {
let node = self.upcast::<Node>(); let node = self.upcast::<Node>();
// TODO: This is an incorrect way of getting controls owned // TODO: This is an incorrect way of getting controls owned
// by the form, but good enough until html5ever lands // by the form, but good enough until html5ever lands
// Step 1-2
node.traverse_preorder().filter_map(|child| { node.traverse_preorder().filter_map(|child| {
// Step 3.1: The field element is disabled.
match child.downcast::<Element>() { match child.downcast::<Element>() {
Some(el) if !el.get_disabled_state() => (), Some(el) if !el.get_disabled_state() => (),
_ => return None, _ => return None,
} }
// Step 3.1: The field element has a datalist element ancestor.
if child.ancestors() if child.ancestors()
.any(|a| Root::downcast::<HTMLDataListElement>(a).is_some()) { .any(|a| Root::downcast::<HTMLDataListElement>(a).is_some()) {
return None; return None;
@ -238,6 +243,7 @@ impl HTMLFormElement {
match element { match element {
HTMLElementTypeId::HTMLInputElement => { HTMLElementTypeId::HTMLInputElement => {
let input = child.downcast::<HTMLInputElement>().unwrap(); let input = child.downcast::<HTMLInputElement>().unwrap();
// Step 3.2-3.7
input.get_form_datum(submitter) input.get_form_datum(submitter)
} }
HTMLElementTypeId::HTMLButtonElement | HTMLElementTypeId::HTMLButtonElement |
@ -257,9 +263,9 @@ impl HTMLFormElement {
// https://html.spec.whatwg.org/multipage/#the-directionality // https://html.spec.whatwg.org/multipage/#the-directionality
} }
/// https://html.spec.whatwg.org/multipage/#constructing-the-form-data-set
pub fn get_form_dataset(&self, submitter: Option<FormSubmitter>) -> Vec<FormDatum> { pub fn get_form_dataset(&self, submitter: Option<FormSubmitter>) -> Vec<FormDatum> {
fn clean_crlf(s: &str) -> DOMString { fn clean_crlf(s: &str) -> DOMString {
// https://html.spec.whatwg.org/multipage/#constructing-the-form-data-set
// Step 4 // Step 4
let mut buf = "".to_owned(); let mut buf = "".to_owned();
let mut prev = ' '; let mut prev = ' ';
@ -290,8 +296,8 @@ impl HTMLFormElement {
DOMString::from(buf) DOMString::from(buf)
} }
// Step 1-3
let mut ret = self.get_unclean_dataset(submitter); let mut ret = self.get_unclean_dataset(submitter);
// https://html.spec.whatwg.org/multipage/#constructing-the-form-data-set
// Step 4 // Step 4
for datum in &mut ret { for datum in &mut ret {
match &*datum.ty { match &*datum.ty {
@ -302,6 +308,7 @@ impl HTMLFormElement {
} }
} }
}; };
// Step 5
ret ret
} }

View file

@ -423,8 +423,12 @@ impl HTMLInputElement {
} }
} }
/// https://html.spec.whatwg.org/multipage/#constructing-the-form-data-set
/// Steps range from 3.1 to 3.7 which related to the HTMLInputElement
pub fn get_form_datum(&self, submitter: Option<FormSubmitter>) -> Option<FormDatum> { pub fn get_form_datum(&self, submitter: Option<FormSubmitter>) -> Option<FormDatum> {
// Step 3.2
let ty = self.type_(); let ty = self.type_();
// Step 3.4
let name = self.Name(); let name = self.Name();
let is_submitter = match submitter { let is_submitter = match submitter {
Some(FormSubmitter::InputElement(s)) => { Some(FormSubmitter::InputElement(s)) => {
@ -434,21 +438,22 @@ impl HTMLInputElement {
}; };
match ty { match ty {
// Step 3.1: it's a button but it is not submitter.
atom!("submit") | atom!("button") | atom!("reset") if !is_submitter => return None, atom!("submit") | atom!("button") | atom!("reset") if !is_submitter => return None,
atom!("radio") | atom!("checkbox") => { // Step 3.1: it's the "Checkbox" or "Radio Button" and whose checkedness is false.
if !self.Checked() || name.is_empty() { atom!("radio") | atom!("checkbox") => if !self.Checked() || name.is_empty() {
return None; return None;
}
}, },
atom!("image") | atom!("file") => return None, // Unimplemented atom!("image") | atom!("file") => return None, // Unimplemented
_ => { // Step 3.1: it's not the "Image Button" and doesn't have a name attribute.
if name.is_empty() { _ => if name.is_empty() {
return None; return None;
}
} }
} }
let mut value = self.Value(); let mut value = self.Value();
// Step 3.6
if ty == atom!("radio") || ty == atom!("checkbox") { if ty == atom!("radio") || ty == atom!("checkbox") {
if value.is_empty() { if value.is_empty() {
value = DOMString::from("on"); value = DOMString::from("on");