mirror of
https://github.com/servo/servo.git
synced 2025-08-03 20:50:07 +01:00
Changed offset_to_text_point function and added unit tests for the same
This commit is contained in:
parent
1662fd7357
commit
7b58fb5fdd
5 changed files with 63 additions and 4 deletions
|
@ -871,13 +871,12 @@ impl<T: ClipboardProvider> TextInput<T> {
|
||||||
fn offset_to_text_point(&self, abs_point: usize) -> TextPoint {
|
fn offset_to_text_point(&self, abs_point: usize) -> TextPoint {
|
||||||
let mut index = abs_point;
|
let mut index = abs_point;
|
||||||
let mut line = 0;
|
let mut line = 0;
|
||||||
|
|
||||||
let last_line_idx = self.lines.len() - 1;
|
let last_line_idx = self.lines.len() - 1;
|
||||||
self.lines.iter().enumerate().fold(0, |acc, (i, val)| {
|
self.lines.iter().enumerate().fold(0, |acc, (i, val)| {
|
||||||
if i != last_line_idx {
|
if i != last_line_idx {
|
||||||
let line_end = max(val.len(), 1);
|
let line_end = val.len();
|
||||||
let new_acc = acc + line_end;
|
let new_acc = acc + line_end + 1;
|
||||||
if abs_point > new_acc && index > line_end {
|
if abs_point >= new_acc && index > line_end {
|
||||||
index -= line_end + 1;
|
index -= line_end + 1;
|
||||||
line += 1;
|
line += 1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -600,6 +600,27 @@ fn test_textinput_set_selection_with_direction() {
|
||||||
assert!(textinput.selection_origin.is_some());
|
assert!(textinput.selection_origin.is_some());
|
||||||
assert_eq!(textinput.selection_origin.unwrap().line, 0);
|
assert_eq!(textinput.selection_origin.unwrap().line, 0);
|
||||||
assert_eq!(textinput.selection_origin.unwrap().index, 6);
|
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]
|
#[test]
|
||||||
|
@ -629,4 +650,11 @@ fn test_selection_bounds() {
|
||||||
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_end());
|
assert_eq!(TextPoint { line: 0, index: 6 }, textinput.selection_end());
|
||||||
assert_eq!(3, textinput.selection_start_offset());
|
assert_eq!(3, textinput.selection_start_offset());
|
||||||
assert_eq!(6, textinput.selection_end_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());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -353734,6 +353734,12 @@
|
||||||
{}
|
{}
|
||||||
]
|
]
|
||||||
],
|
],
|
||||||
|
"selection/setSelectionRange.html": [
|
||||||
|
[
|
||||||
|
"/selection/setSelectionRange.html",
|
||||||
|
{}
|
||||||
|
]
|
||||||
|
],
|
||||||
"selection/type.html": [
|
"selection/type.html": [
|
||||||
[
|
[
|
||||||
"/selection/type.html",
|
"/selection/type.html",
|
||||||
|
@ -581157,6 +581163,10 @@
|
||||||
"856ba9771d2c1e2eaae00dcf6b8378a42003cc7a",
|
"856ba9771d2c1e2eaae00dcf6b8378a42003cc7a",
|
||||||
"testharness"
|
"testharness"
|
||||||
],
|
],
|
||||||
|
"selection/setSelectionRange.html": [
|
||||||
|
"a047ea5e4c00894fcc30e972f84bcedc4a8226cf",
|
||||||
|
"testharness"
|
||||||
|
],
|
||||||
"selection/test-iframe.html": [
|
"selection/test-iframe.html": [
|
||||||
"3803c785b4a2fe2bbf9ecb895e6d3e1ae9e40164",
|
"3803c785b4a2fe2bbf9ecb895e6d3e1ae9e40164",
|
||||||
"support"
|
"support"
|
||||||
|
|
4
tests/wpt/metadata/selection/setSelectionRange.html.ini
Normal file
4
tests/wpt/metadata/selection/setSelectionRange.html.ini
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
[setSelectionRange.html]
|
||||||
|
type: testharness
|
||||||
|
[setSelectionRange on line boundaries]
|
||||||
|
expected: FAIL
|
|
@ -0,0 +1,18 @@
|
||||||
|
<!doctype html>
|
||||||
|
<meta charset="utf-8">
|
||||||
|
<title></title>
|
||||||
|
<script src="/resources/testharness.js"></script>
|
||||||
|
<script src="/resources/testharnessreport.js"></script>
|
||||||
|
<textarea>
|
||||||
|
|
||||||
|
</textarea>
|
||||||
|
<script>
|
||||||
|
test(function() {
|
||||||
|
let textarea = document.querySelector('textarea');
|
||||||
|
assert_equals(textarea.selectionStart, 0);
|
||||||
|
assert_equals(textarea.selectionEnd, 0);
|
||||||
|
textarea.setSelectionRange(0, 1);
|
||||||
|
assert_equals(textarea.selectionStart, 0);
|
||||||
|
assert_equals(textarea.selectionEnd, 1);
|
||||||
|
}, "setSelectionRange on line boundaries");
|
||||||
|
</script>
|
Loading…
Add table
Add a link
Reference in a new issue