Skip to content

Commit

Permalink
Add `InputElement::{selection_start, selection_end, set_selection_sta…
Browse files Browse the repository at this point in the history
…rt, set_selection_end}` (#365)
  • Loading branch information
Wodann authored and koute committed Sep 17, 2019
1 parent ba72aa0 commit 7617e2f
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions src/webapi/html_elements/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use webcore::value::Reference;
use webcore::try_from::TryInto;
use webapi::dom_exception::InvalidStateError;
use webapi::event_target::{IEventTarget, EventTarget};
use webapi::node::{INode, Node};
use webapi::element::{IElement, Element};
Expand All @@ -22,6 +23,7 @@ impl IHtmlElement for InputElement {}

impl InputElement {
/// The value of the control. This attribute is optional except when the input is a radio button or a checkbox.
///
// https://html.spec.whatwg.org/#the-input-element:dom-input-value
#[inline]
pub fn raw_value( &self ) -> String {
Expand All @@ -31,11 +33,56 @@ impl InputElement {
}

/// Sets the value of the control.
///
// https://html.spec.whatwg.org/#dom-input-value
#[inline]
pub fn set_raw_value( &self, value: &str ) {
js! { @(no_return)
@{self}.value = @{value};
}
}

/// The offset to the start of the selection.
/// This attribute only applies when the input is a text, search, url, telephone or password.
///
// https://html.spec.whatwg.org/#the-input-element:dom-textarea/input-selectionstart
#[inline]
pub fn selection_start( &self ) -> Option<u32> {
js! (
return @{self}.selectionStart;
).try_into().ok()
}

/// Sets the offset to the start of the selection.
/// This attribute only applies when the input is a text, search, url, telephone or password.
///
// https://html.spec.whatwg.org/#the-input-element:dom-textarea/input-selectionstart
#[inline]
pub fn set_selection_start( &self, value: u32 ) -> Result<(), InvalidStateError> {
js_try! ( @(no_return)
@{self}.selectionStart = @{value};
).unwrap()
}

/// The offset to the end of the selection.
/// This attribute only applies when the input is a text, search, url, telephone or password.
///
// https://html.spec.whatwg.org/#the-input-element:dom-textarea/input-selectionstart
#[inline]
pub fn selection_end( &self ) -> Option<u32> {
js! (
return @{self}.selectionEnd;
).try_into().ok()
}

/// Sets the offset to the end of the selection.
/// This attribute only applies when the input is a text, search, url, telephone or password.
///
// https://html.spec.whatwg.org/#the-input-element:dom-textarea/input-selectionstart
#[inline]
pub fn set_selection_end( &self, value: u32 ) -> Result<(), InvalidStateError> {
js_try! ( @(no_return)
@{self}.selectionEnd = @{value};
).unwrap()
}
}

0 comments on commit 7617e2f

Please sign in to comment.