Skip to content

Commit

Permalink
formcontrol trait to element trait
Browse files Browse the repository at this point in the history
mutable function and reset function in formcontrol
move into trait of single element
currently only TextArea element and Input element
  • Loading branch information
yodalee committed Feb 3, 2015
1 parent c5a5db6 commit c0867ec
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
7 changes: 2 additions & 5 deletions components/script/dom/htmlformelement.rs
Expand Up @@ -18,8 +18,8 @@ use dom::event::{Event, EventHelpers, EventBubbles, EventCancelable};
use dom::eventtarget::{EventTarget, EventTargetTypeId};
use dom::element::ElementTypeId;
use dom::htmlelement::{HTMLElement, HTMLElementTypeId};
use dom::htmlinputelement::HTMLInputElement;
use dom::htmltextareaelement::HTMLTextAreaElement;
use dom::htmlinputelement::{HTMLInputElement, HTMLInputElementHelpers};
use dom::htmltextareaelement::{HTMLTextAreaElement, HTMLTextAreaElementHelpers};
use dom::node::{Node, NodeHelpers, NodeTypeId, document_from_node, window_from_node};
use hyper::method::Method;
use servo_msg::constellation_msg::LoadData;
Expand Down Expand Up @@ -523,7 +523,4 @@ pub trait FormControl<'a> : Copy + Sized {
}

fn to_element(self) -> JSRef<'a, Element>;
// https://html.spec.whatwg.org/multipage/forms.html#concept-fe-mutable
fn mutable(self) -> bool;
fn reset(self);
}
46 changes: 22 additions & 24 deletions components/script/dom/htmlinputelement.rs
Expand Up @@ -303,6 +303,8 @@ pub trait HTMLInputElementHelpers {
fn update_checked_state(self, checked: bool, dirty: bool);
fn get_size(&self) -> u32;
fn get_indeterminate_state(self) -> bool;
fn mutable(self) -> bool;
fn reset(self);
}

#[allow(unsafe_blocks)]
Expand Down Expand Up @@ -392,6 +394,26 @@ impl<'a> HTMLInputElementHelpers for JSRef<'a, HTMLInputElement> {
fn get_indeterminate_state(self) -> bool {
self.indeterminate.get()
}
// https://html.spec.whatwg.org/multipage/forms.html#concept-fe-mutable
fn mutable(self) -> bool {
// https://html.spec.whatwg.org/multipage/forms.html#the-input-element:concept-fe-mutable
// https://html.spec.whatwg.org/multipage/forms.html#the-readonly-attribute:concept-fe-mutable
let node: JSRef<Node> = NodeCast::from_ref(self);
!(node.get_disabled_state() || self.ReadOnly())
}
fn reset(self) {
match self.input_type.get() {
InputType::InputRadio | InputType::InputCheckbox => {
self.update_checked_state(self.DefaultChecked(), false);
self.checked_changed.set(false);
},
InputType::InputImage => (),
_ => ()
}

self.SetValue(self.DefaultValue());
self.value_changed.set(false);
}
}

impl<'a> VirtualMethods for JSRef<'a, HTMLInputElement> {
Expand Down Expand Up @@ -583,32 +605,8 @@ impl<'a> FormControl<'a> for JSRef<'a, HTMLInputElement> {
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-input-element:concept-fe-mutable
// https://html.spec.whatwg.org/multipage/forms.html#the-readonly-attribute:concept-fe-mutable
let node: JSRef<Node> = NodeCast::from_ref(self);
!(node.get_disabled_state() || self.ReadOnly())
}

// https://html.spec.whatwg.org/multipage/forms.html#the-input-element:concept-form-reset-control
fn reset(self) {
match self.input_type.get() {
InputType::InputRadio | InputType::InputCheckbox => {
self.update_checked_state(self.DefaultChecked(), false);
self.checked_changed.set(false);
},
InputType::InputImage => (),
_ => ()
}

self.SetValue(self.DefaultValue());
self.value_changed.set(false);
}
}


impl<'a> Activatable for JSRef<'a, HTMLInputElement> {
fn as_element(&self) -> Temporary<Element> {
Temporary::from_rooted(ElementCast::from_ref(*self))
Expand Down
30 changes: 18 additions & 12 deletions components/script/dom/htmltextareaelement.rs
Expand Up @@ -187,6 +187,24 @@ impl<'a> HTMLTextAreaElementMethods for JSRef<'a, HTMLTextAreaElement> {
}
}

pub trait HTMLTextAreaElementHelpers {
fn mutable(self) -> bool;
fn reset(self);
}

impl<'a> HTMLTextAreaElementHelpers for JSRef<'a, HTMLTextAreaElement> {
// 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) {
// https://html.spec.whatwg.org/multipage/forms.html#the-textarea-element:concept-form-reset-control
self.SetValue(self.DefaultValue());
self.value_changed.set(false);
}
}

trait PrivateHTMLTextAreaElementHelpers {
fn force_relayout(self);
}
Expand Down Expand Up @@ -335,16 +353,4 @@ impl<'a> FormControl<'a> for JSRef<'a, HTMLTextAreaElement> {
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) {
// https://html.spec.whatwg.org/multipage/forms.html#the-textarea-element:concept-form-reset-control
self.SetValue(self.DefaultValue());
self.value_changed.set(false);
}
}

0 comments on commit c0867ec

Please sign in to comment.