1 use webcore::value::Reference;
2 use webcore::try_from::TryInto;
3 use webapi::dom_exception::InvalidStateError;
4 use webapi::event_target::{IEventTarget, EventTarget};
5 use webapi::node::{INode, Node};
6 use webapi::element::{IElement, Element};
7 use webapi::html_element::{IHtmlElement, HtmlElement};
8 
9 /// The HTML input element is used to create interactive controls
10 /// for web-based forms in order to accept data from the user.
11 ///
12 /// [(JavaScript docs)](https://developer.mozilla.org/en/docs/Web/HTML/Element/input)
13 // https://html.spec.whatwg.org/#htmlinputelement
14 #[derive(Clone, Debug, PartialEq, Eq, ReferenceType)]
15 #[reference(instance_of = "HTMLInputElement")]
16 #[reference(subclass_of(EventTarget, Node, Element, HtmlElement))]
17 pub struct InputElement( Reference );
18 
19 impl IEventTarget for InputElement {}
20 impl INode for InputElement {}
21 impl IElement for InputElement {}
22 impl IHtmlElement for InputElement {}
23 
24 impl InputElement {
25     /// The value of the control. This attribute is optional except when the input is a radio button or a checkbox.
26     ///
27     // https://html.spec.whatwg.org/#the-input-element:dom-input-value
28     #[inline]
raw_value( &self ) -> String29     pub fn raw_value( &self ) -> String {
30         js! (
31             return @{self}.value;
32         ).try_into().unwrap()
33     }
34 
35     /// Sets the value of the control.
36     ///
37     // https://html.spec.whatwg.org/#dom-input-value
38     #[inline]
set_raw_value( &self, value: &str )39     pub fn set_raw_value( &self, value: &str ) {
40         js! { @(no_return)
41             @{self}.value = @{value};
42         }
43     }
44 
45     /// The offset to the start of the selection.
46     /// This attribute only applies when the input is a text, search, url, telephone or password.
47     ///
48     // https://html.spec.whatwg.org/#the-input-element:dom-textarea/input-selectionstart
49     #[inline]
selection_start( &self ) -> Option<u32>50     pub fn selection_start( &self ) -> Option<u32> {
51         js! (
52             return @{self}.selectionStart;
53         ).try_into().ok()
54     }
55 
56     /// Sets the offset to the start of the selection.
57     /// This attribute only applies when the input is a text, search, url, telephone or password.
58     ///
59     // https://html.spec.whatwg.org/#the-input-element:dom-textarea/input-selectionstart
60     #[inline]
set_selection_start( &self, value: u32 ) -> Result<(), InvalidStateError>61     pub fn set_selection_start( &self, value: u32 ) -> Result<(), InvalidStateError> {
62         js_try! ( @(no_return)
63             @{self}.selectionStart = @{value};
64         ).unwrap()
65     }
66 
67     /// The offset to the end of the selection.
68     /// This attribute only applies when the input is a text, search, url, telephone or password.
69     ///
70     // https://html.spec.whatwg.org/#the-input-element:dom-textarea/input-selectionstart
71     #[inline]
selection_end( &self ) -> Option<u32>72     pub fn selection_end( &self ) -> Option<u32> {
73         js! (
74             return @{self}.selectionEnd;
75         ).try_into().ok()
76     }
77 
78     /// Sets the offset to the end of the selection.
79     /// This attribute only applies when the input is a text, search, url, telephone or password.
80     ///
81     // https://html.spec.whatwg.org/#the-input-element:dom-textarea/input-selectionstart
82     #[inline]
set_selection_end( &self, value: u32 ) -> Result<(), InvalidStateError>83     pub fn set_selection_end( &self, value: u32 ) -> Result<(), InvalidStateError> {
84         js_try! ( @(no_return)
85             @{self}.selectionEnd = @{value};
86         ).unwrap()
87     }
88 }
89