diff options
author | Alberto Corona <ac@albertocorona.com> | 2016-03-15 09:03:05 -0500 |
---|---|---|
committer | Adrian Utrilla <adrianutrilla@gmail.com> | 2016-04-17 17:27:26 +0200 |
commit | 5e863f2eb8c261ed05c46310b8f8a125b8ed9f04 (patch) | |
tree | 9da74c7c6e9fd27720668bd96cdbccffa94c5e9d /components/script/textinput.rs | |
parent | db4481b4509db4be83e0f77a66884feccd1878e0 (diff) | |
download | servo-5e863f2eb8c261ed05c46310b8f8a125b8ed9f04.tar.gz servo-5e863f2eb8c261ed05c46310b8f8a125b8ed9f04.zip |
Implement HTMLTextArea.setSelectionRange
Diffstat (limited to 'components/script/textinput.rs')
-rw-r--r-- | components/script/textinput.rs | 65 |
1 files changed, 62 insertions, 3 deletions
diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 1be7962003c..ffd707c5d27 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -21,6 +21,33 @@ pub enum Selection { NotSelected } +#[derive(JSTraceable, PartialEq, Copy, Clone, HeapSizeOf)] +pub enum SelectionDirection { + Forward, + Backward, + None, +} + +impl From<DOMString> for SelectionDirection { + fn from(direction: DOMString) -> SelectionDirection { + match direction.as_ref() { + "forward" => SelectionDirection::Forward, + "backward" => SelectionDirection::Backward, + _ => SelectionDirection::None, + } + } +} + +impl From<SelectionDirection> for DOMString { + fn from(direction: SelectionDirection) -> DOMString { + match direction { + SelectionDirection::Forward => DOMString::from("forward"), + SelectionDirection::Backward => DOMString::from("backward"), + SelectionDirection::None => DOMString::from("none"), + } + } +} + #[derive(JSTraceable, Copy, Clone, HeapSizeOf, PartialEq)] pub struct TextPoint { /// 0-based line number @@ -45,7 +72,8 @@ pub struct TextInput<T: ClipboardProvider> { /// The maximum number of UTF-16 code units this text input is allowed to hold. /// /// https://html.spec.whatwg.org/multipage/#attr-fe-maxlength - pub max_length: Option<usize> + pub max_length: Option<usize>, + pub selection_direction: SelectionDirection, } /// Resulting action to be taken by the owner of a text input that is handling an event. @@ -138,14 +166,17 @@ fn len_of_first_n_code_units(text: &str, n: usize) -> usize { impl<T: ClipboardProvider> TextInput<T> { /// Instantiate a new text input control - pub fn new(lines: Lines, initial: DOMString, clipboard_provider: T, max_length: Option<usize>) -> TextInput<T> { + pub fn new(lines: Lines, initial: DOMString, + clipboard_provider: T, max_length: Option<usize>, + selection_direction: SelectionDirection) -> TextInput<T> { let mut i = TextInput { lines: vec!(), edit_point: Default::default(), selection_begin: None, multiline: lines == Lines::Multiple, clipboard_provider: clipboard_provider, - max_length: max_length + max_length: max_length, + selection_direction: selection_direction, }; i.set_content(initial); i @@ -607,4 +638,32 @@ impl<T: ClipboardProvider> TextInput<T> { line: line, index: index } } + + pub fn set_selection_range(&mut self, start: u32, end: u32) { + let mut start = start as usize; + let mut end = end as usize; + let text_end = self.get_content().len(); + + if start > text_end { + start = text_end; + } else if end > text_end { + end = text_end; + } else if start >= end { + start = end; + } + + self.selection_begin = Some(self.get_text_point_for_absolute_point(start)); + self.edit_point = self.get_text_point_for_absolute_point(end); + } + + pub fn get_selection_start(&self) -> u32 { + let selection_start = match self.selection_begin { + Some(selection_begin_point) => { + self.get_absolute_point_for_text_point(&selection_begin_point) + }, + None => self.get_absolute_insertion_point() + }; + + selection_start as u32 + } } |