servo: Merge #19946 - Change Offset_to_text_point function and add unit tests (from paavininanda:Issue18613); r=jdm

<!-- Please describe your changes on the following line: -->

---
<!-- 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 #18613.

<!-- Either: -->
- [x] Added unit tests and web platform tests

<!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.-->

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

Source-Repo: https://github.com/servo/servo
Source-Revision: 36eb711d3b324de5437cc586bb6a8e09b2219108

--HG--
extra : subtree_source : https%3A//hg.mozilla.org/projects/converted-servo-linear
extra : subtree_revision : d46bcef667987e9e24be95160713ec0c5ffe868b
This commit is contained in:
paavininanda 2018-02-06 15:11:54 -05:00
Родитель 5f9a96be9c
Коммит 032cddc15e
2 изменённых файлов: 31 добавлений и 4 удалений

Просмотреть файл

@ -871,13 +871,12 @@ impl<T: ClipboardProvider> TextInput<T> {
fn offset_to_text_point(&self, abs_point: usize) -> TextPoint {
let mut index = abs_point;
let mut line = 0;
let last_line_idx = self.lines.len() - 1;
self.lines.iter().enumerate().fold(0, |acc, (i, val)| {
if i != last_line_idx {
let line_end = max(val.len(), 1);
let new_acc = acc + line_end;
if abs_point > new_acc && index > line_end {
let line_end = val.len();
let new_acc = acc + line_end + 1;
if abs_point >= new_acc && index > line_end {
index -= line_end + 1;
line += 1;
}

Просмотреть файл

@ -600,6 +600,27 @@ fn test_textinput_set_selection_with_direction() {
assert!(textinput.selection_origin.is_some());
assert_eq!(textinput.selection_origin.unwrap().line, 0);
assert_eq!(textinput.selection_origin.unwrap().index, 6);
textinput = text_input(Lines::Multiple, "\n\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(textinput.edit_point.line, 1);
assert_eq!(textinput.edit_point.index, 0);
assert_eq!(textinput.selection_direction, SelectionDirection::Forward);
assert!(textinput.selection_origin.is_some());
assert_eq!(textinput.selection_origin.unwrap().line, 0);
assert_eq!(textinput.selection_origin.unwrap().index, 0);
textinput = text_input(Lines::Multiple, "\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(textinput.edit_point.line, 1);
assert_eq!(textinput.edit_point.index, 0);
assert_eq!(textinput.selection_direction, SelectionDirection::Forward);
assert!(textinput.selection_origin.is_some());
assert_eq!(textinput.selection_origin.unwrap().line, 0);
assert_eq!(textinput.selection_origin.unwrap().index, 0);
}
#[test]
@ -629,4 +650,11 @@ fn test_selection_bounds() {
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_end());
assert_eq!(3, textinput.selection_start_offset());
assert_eq!(6, textinput.selection_end_offset());
textinput = text_input(Lines::Multiple, "\n\n");
textinput.set_selection_range(0, 1, SelectionDirection::Forward);
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_origin_or_edit_point());
assert_eq!(TextPoint { line: 0, index: 0 }, textinput.selection_start());
assert_eq!(TextPoint { line: 1, index: 0 }, textinput.selection_end());
}