diff options
author | Charles Vandevoorde <charles.vandevoorde@hotmail.be> | 2017-03-05 10:21:47 +0100 |
---|---|---|
committer | charlesvdv <charles.vandevoorde@hotmail.be> | 2017-03-05 13:58:33 +0100 |
commit | 26e6c097767f4d64978b15f0974d9471447e6190 (patch) | |
tree | 69a1ce7bfbd4b086820206d38b050a7e3260ccd1 /components/script/textinput.rs | |
parent | 78210c3c942da9ca5dd8fda1b6bc77d696c25938 (diff) | |
download | servo-26e6c097767f4d64978b15f0974d9471447e6190.tar.gz servo-26e6c097767f4d64978b15f0974d9471447e6190.zip |
Correct unicode handling for text input
Diffstat (limited to 'components/script/textinput.rs')
-rw-r--r-- | components/script/textinput.rs | 19 |
1 files changed, 13 insertions, 6 deletions
diff --git a/components/script/textinput.rs b/components/script/textinput.rs index 5f04b5b187e..c23d8547e01 100644 --- a/components/script/textinput.rs +++ b/components/script/textinput.rs @@ -14,6 +14,7 @@ use std::cmp::{max, min}; use std::default::Default; use std::ops::Range; use std::usize; +use unicode_segmentation::UnicodeSegmentation; #[derive(Copy, Clone, PartialEq)] pub enum Selection { @@ -376,18 +377,16 @@ impl<T: ClipboardProvider> TextInput<T> { } let adjust = { let current_line = &self.lines[self.edit_point.line]; - // FIXME: We adjust by one code point, but it proably should be one grapheme cluster - // https://github.com/unicode-rs/unicode-segmentation match direction { Direction::Forward => { - match current_line[self.edit_point.index..].chars().next() { - Some(c) => c.len_utf8() as isize, + match current_line[self.edit_point.index..].graphemes(true).next() { + Some(c) => c.len() as isize, None => 1, // Going to the next line is a "one byte" offset } } Direction::Backward => { - match current_line[..self.edit_point.index].chars().next_back() { - Some(c) => -(c.len_utf8() as isize), + match current_line[..self.edit_point.index].graphemes(true).next_back() { + Some(c) => -(c.len() as isize), None => -1, // Going to the previous line is a "one byte" offset } } @@ -677,4 +676,12 @@ impl<T: ClipboardProvider> TextInput<T> { selection_start as u32 } + + pub fn set_edit_point_index(&mut self, index: usize) { + let byte_size = self.lines[self.edit_point.line] + .graphemes(true) + .take(index) + .fold(0, |acc, x| acc + x.len()); + self.edit_point.index = byte_size; + } } |