Auto merge of #15822 - charlesvdv:unicode-panic, r=emilio

Correct unicode handling for text input

<!-- Please describe your changes on the following line: -->
Allow proprer handling of unicode sequence in text input.

---
<!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `__` with appropriate data: -->
- [x] `./mach build -d` does not report any errors
- [x] `./mach test-tidy` does not report any errors
- [x] These changes fix #15819

<!-- Either: -->
- [ ] There are tests for these changes OR
- [ ] These changes do not require tests because _____

<!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/servo/servo/15822)
<!-- Reviewable:end -->
This commit is contained in:
bors-servo 2017-04-16 15:08:59 -05:00 committed by GitHub
commit e45546edf0
3 changed files with 23 additions and 7 deletions

View file

@ -377,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
}
}
@ -845,4 +843,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;
}
}