Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements basic form resetting #4133

Merged
merged 12 commits into from Dec 16, 2014

Implements basic form resetting

What can this do? Reset `<input type=text>` fields back to their default
value through a call to a form's reset method. That's all for now!

Fixes compile error after rebase
  • Loading branch information
mttr committed Dec 16, 2014
commit f0ce2af89ce264ddb707c67822ae1f4e4fb622e8
@@ -15,6 +15,7 @@ use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::{Document, DocumentHelpers};
use dom::element::{Element, AttributeHandlers, HTMLFormElementTypeId, HTMLTextAreaElementTypeId, HTMLDataListElementTypeId};
use dom::element::{HTMLInputElementTypeId, HTMLButtonElementTypeId, HTMLObjectElementTypeId, HTMLSelectElementTypeId};
use dom::element::{HTMLOutputElementTypeId};
use dom::event::{Event, EventHelpers, Bubbles, Cancelable};
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
@@ -117,18 +118,30 @@ impl<'a> HTMLFormElementMethods for JSRef<'a, HTMLFormElement> {
fn Submit(self) {
self.submit(FromFormSubmitMethod, FormElement(self));
}

// https://html.spec.whatwg.org/multipage/forms.html#dom-form-reset
fn Reset(self) {
self.reset(FromFormResetMethod);
}
}

pub enum SubmittedFrom {
FromFormSubmitMethod,
NotFromFormSubmitMethod
}

pub enum ResetFrom {
FromFormResetMethod,
NotFromFormResetMethod
}

pub trait HTMLFormElementHelpers {
// https://html.spec.whatwg.org/multipage/forms.html#concept-form-submit
fn submit(self, submit_method_flag: SubmittedFrom, submitter: FormSubmitter);
// https://html.spec.whatwg.org/multipage/forms.html#constructing-the-form-data-set
fn get_form_dataset(self, submitter: Option<FormSubmitter>) -> Vec<FormDatum>;
// https://html.spec.whatwg.org/multipage/forms.html#dom-form-reset
fn reset(self, submit_method_flag: ResetFrom);
}

impl<'a> HTMLFormElementHelpers for JSRef<'a, HTMLFormElement> {
@@ -316,6 +329,65 @@ impl<'a> HTMLFormElementHelpers for JSRef<'a, HTMLFormElement> {
};
ret
}

fn reset(self, _reset_method_flag: ResetFrom) {
let win = window_from_node(self).root();
let event = Event::new(Window(*win),
"reset".to_string(),
Bubbles, Cancelable).root();
let target: JSRef<EventTarget> = EventTargetCast::from_ref(self);
target.DispatchEvent(*event).ok();
if event.DefaultPrevented() {
return;
}

let node: JSRef<Node> = NodeCast::from_ref(self);

// TODO: This is an incorrect way of getting controls owned
// by the form, but good enough until html5ever lands
for child in node.traverse_preorder() {
// TODO This is the wrong place to do this. Each resettable
// element should implement its own reset method (trait?)
//
// List of resettable elements:
// https://html.spec.whatwg.org/multipage/forms.html#category-reset
match child.type_id() {
ElementNodeTypeId(HTMLInputElementTypeId) => {
let input: JSRef<HTMLInputElement> = HTMLInputElementCast::to_ref(child)
.unwrap();
let ty = input.Type();

match ty.as_slice() {
"radio" | "checkbox" => {
// TODO Reset radios/checkboxes here
},
"image" => (),
_ => ()
}

input.SetValue(input.DefaultValue());
}
// TODO HTMLKeygenElement unimplemented
/*ElementNodeTypeID(HTMLKeygenElementTypeId) => {
// Unimplemented
{}
}*/
ElementNodeTypeId(HTMLSelectElementTypeId) => {
// Unimplemented
{}
}
ElementNodeTypeId(HTMLTextAreaElementTypeId) => {
// Unimplemented
{}
}
ElementNodeTypeId(HTMLOutputElementTypeId) => {
// Unimplemented
{}
}
_ => {}
}
};
}
}

impl Reflectable for HTMLFormElement {
@@ -22,7 +22,7 @@ interface HTMLFormElement : HTMLElement {
//getter (RadioNodeList or Element) (DOMString name);

void submit();
//void reset();
void reset();
//boolean checkValidity();
//boolean reportValidity();

@@ -0,0 +1,23 @@
<html>
<head></head>
<body>
<!-- Run with nc -l 8000 -->
<form action="http://localhost:8000" method=get id="foo">
<input name=bar type=checkbox checked>
<input name=baz value="baz1" type=radio checked>
<input name=baz value="baz2" type=radio>
<input type=text id=hi name=bye value="hi!">
<input type=text id=aloha name=empty value="">
<input type=text id=welcome name=reallyempty>
<script>
// setTimeout because https://github.com/servo/servo/issues/3628
setTimeout(function(){
document.getElementById("hi").value=("bloop");
document.getElementById("aloha").value=("bloop");
document.getElementById("welcome").value=("bloop");
setTimeout(function(){document.getElementById("foo").reset()},2000);
},2000)
</script>
</form>
</body>
</html>
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.