mirror of
https://github.com/servo/servo.git
synced 2025-07-24 15:50:21 +01:00
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
This commit is contained in:
parent
f932a6947a
commit
f0ce2af89c
3 changed files with 96 additions and 1 deletions
|
@ -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,6 +118,11 @@ 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 {
|
||||
|
@ -124,11 +130,18 @@ pub enum SubmittedFrom {
|
|||
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();
|
||||
|
||||
|
|
23
tests/html/form_reset_handsfree.html
Normal file
23
tests/html/form_reset_handsfree.html
Normal file
|
@ -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>
|
Loading…
Add table
Add a link
Reference in a new issue