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 FormControl for HTMLTextAreaElement

  • Loading branch information
mttr committed Dec 16, 2014
commit 7e0c39a82d5ca9d766c0065f4cb6ba79b1cbe037
@@ -8,7 +8,7 @@ use dom::bindings::codegen::Bindings::HTMLFormElementBinding;
use dom::bindings::codegen::Bindings::HTMLFormElementBinding::HTMLFormElementMethods;
use dom::bindings::codegen::Bindings::HTMLInputElementBinding::HTMLInputElementMethods;
use dom::bindings::codegen::InheritTypes::{EventTargetCast, HTMLFormElementDerived, NodeCast};
use dom::bindings::codegen::InheritTypes::HTMLInputElementCast;
use dom::bindings::codegen::InheritTypes::{HTMLInputElementCast, HTMLTextAreaElementCast};
use dom::bindings::global::Window;
use dom::bindings::js::{JSRef, Temporary};
use dom::bindings::utils::{Reflectable, Reflector};
@@ -20,6 +20,7 @@ use dom::event::{Event, EventHelpers, Bubbles, Cancelable};
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::htmlinputelement::HTMLInputElement;
use dom::htmltextareaelement::HTMLTextAreaElement;
use dom::node::{Node, NodeHelpers, ElementNodeTypeId, document_from_node, window_from_node};
use hyper::method::Post;
use servo_msg::constellation_msg::LoadData;
@@ -362,8 +363,9 @@ impl<'a> HTMLFormElementHelpers for JSRef<'a, HTMLFormElement> {
{}
}
ElementNodeTypeId(HTMLTextAreaElementTypeId) => {
// Unimplemented
{}
let textarea: JSRef<HTMLTextAreaElement> = HTMLTextAreaElementCast::to_ref(child)
.unwrap();
textarea.reset()
}
ElementNodeTypeId(HTMLOutputElementTypeId) => {
// Unimplemented
@@ -5,20 +5,22 @@
use dom::attr::{Attr, AttrValue};
use dom::attr::AttrHelpers;
use dom::bindings::cell::DOMRefCell;
use dom::bindings::codegen::Bindings::DocumentBinding::DocumentMethods;
use dom::bindings::codegen::Bindings::EventBinding::EventMethods;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding;
use dom::bindings::codegen::Bindings::HTMLTextAreaElementBinding::HTMLTextAreaElementMethods;
use dom::bindings::codegen::Bindings::NodeBinding::NodeMethods;
use dom::bindings::codegen::InheritTypes::{ElementCast, HTMLElementCast, NodeCast};
use dom::bindings::codegen::InheritTypes::{HTMLTextAreaElementDerived, HTMLFieldSetElementDerived};
use dom::bindings::codegen::InheritTypes::{KeyboardEventCast, TextDerived};
use dom::bindings::js::{JS, JSRef, Temporary};
use dom::bindings::codegen::InheritTypes::{KeyboardEventCast, TextDerived, HTMLFormElementCast};
use dom::bindings::js::{JS, JSRef, Temporary, OptionalRootable};
use dom::bindings::utils::{Reflectable, Reflector};
use dom::document::{Document, DocumentHelpers};
use dom::element::{AttributeHandlers, HTMLTextAreaElementTypeId};
use dom::element::{AttributeHandlers, HTMLTextAreaElementTypeId, Element};
use dom::event::Event;
use dom::eventtarget::{EventTarget, NodeTargetTypeId};
use dom::htmlelement::HTMLElement;
use dom::htmlformelement::{FormControl, HTMLFormElement};
use dom::keyboardevent::KeyboardEvent;
use dom::node::{DisabledStateHelpers, Node, NodeHelpers, OtherNodeDamage, ElementNodeTypeId};
use dom::node::{document_from_node};
@@ -103,6 +105,12 @@ impl<'a> HTMLTextAreaElementMethods for JSRef<'a, HTMLTextAreaElement> {
// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-placeholder
make_setter!(SetPlaceholder, "placeholder")

// https://html.spec.whatwg.org/multipage/forms.html#attr-textarea-readonly
make_bool_getter!(ReadOnly)

// https://html.spec.whatwg.org/multipage/forms.html#attr-textarea-readonly
make_bool_setter!(SetReadOnly, "readOnly")

// https://html.spec.whatwg.org/multipage/forms.html#dom-textarea-required
make_bool_getter!(Required)

@@ -248,7 +256,7 @@ impl<'a> VirtualMethods for JSRef<'a, HTMLTextAreaElement> {
}

if child.is_text() {
self.SetValue(self.DefaultValue());
self.reset();
}
}

@@ -287,3 +295,44 @@ impl Reflectable for HTMLTextAreaElement {
self.htmlelement.reflector()
}
}

impl<'a> FormControl<'a> for JSRef<'a, HTMLTextAreaElement> {
// FIXME: This is wrong (https://github.com/servo/servo/issues/3553)
// but we need html5ever to do it correctly
fn form_owner(self) -> Option<Temporary<HTMLFormElement>> {
// https://html.spec.whatwg.org/multipage/forms.html#reset-the-form-owner
let elem: JSRef<Element> = ElementCast::from_ref(self);
let owner = elem.get_string_attribute(&atom!("form"));
if !owner.is_empty() {
let doc = document_from_node(self).root();
let owner = doc.GetElementById(owner).root();
match owner {
Some(o) => {
let maybe_form: Option<JSRef<HTMLFormElement>> = HTMLFormElementCast::to_ref(*o);
if maybe_form.is_some() {
return maybe_form.map(Temporary::from_rooted);
}
},
_ => ()
}
}
let node: JSRef<Node> = NodeCast::from_ref(self);
node.ancestors().filter_map(|a| HTMLFormElementCast::to_ref(a)).next()
.map(Temporary::from_rooted)
}

fn to_element(self) -> JSRef<'a, Element> {
ElementCast::from_ref(self)
}

// https://html.spec.whatwg.org/multipage/forms.html#concept-fe-mutable
fn mutable(self) -> bool {
// https://html.spec.whatwg.org/multipage/forms.html#the-textarea-element:concept-fe-mutable
!(self.Disabled() || self.ReadOnly())
}

fn reset(self) {
self.SetValue(self.DefaultValue());
self.value_changed.set(false);
}
}
@@ -16,7 +16,7 @@ interface HTMLTextAreaElement : HTMLElement {
// attribute long minLength;
attribute DOMString name;
attribute DOMString placeholder;
// attribute boolean readOnly;
attribute boolean readOnly;
attribute boolean required;
attribute unsigned long rows;
attribute DOMString wrap;
@@ -9,12 +9,15 @@
<input type=text id=hi name=bye value="hi!">
<input type=text id=aloha name=empty value="">
<input type=text id=welcome name=reallyempty>
<textarea id=textarea>Hello
TextArea!</textarea>
<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");
document.getElementById("textarea").value=("bloop");
setTimeout(function(){document.getElementById("foo").reset()},2000);
},2000)
</script>
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.