mirror of
https://github.com/servo/servo.git
synced 2025-08-02 20:20:14 +01:00
Use correct byte indices in replace_selection
This commit is contained in:
parent
0397e2a24d
commit
d7e6f8b0f1
2 changed files with 22 additions and 3 deletions
|
@ -107,6 +107,16 @@ fn is_printable_key(key: Key) -> bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The length in bytes of the first n characters in a UTF-8 string.
|
||||||
|
///
|
||||||
|
/// If the string has fewer than n characters, returns the length of the whole string.
|
||||||
|
fn len_of_first_n_chars(text: &str, n: usize) -> usize {
|
||||||
|
match text.char_indices().take(n).last() {
|
||||||
|
Some((index, ch)) => index + ch.len_utf8(),
|
||||||
|
None => 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: ClipboardProvider> TextInput<T> {
|
impl<T: ClipboardProvider> TextInput<T> {
|
||||||
/// Instantiate a new text input control
|
/// 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>) -> TextInput<T> {
|
||||||
|
@ -213,8 +223,8 @@ impl<T: ClipboardProvider> TextInput<T> {
|
||||||
usize::MAX
|
usize::MAX
|
||||||
};
|
};
|
||||||
|
|
||||||
let last_char_to_insert = min(allowed_to_insert_count, insert.chars().count());
|
let last_char_index = len_of_first_n_chars(&*insert, allowed_to_insert_count);
|
||||||
let chars_to_insert = (&insert[0 .. last_char_to_insert]).to_owned();
|
let chars_to_insert = &insert[..last_char_index];
|
||||||
|
|
||||||
self.clear_selection();
|
self.clear_selection();
|
||||||
|
|
||||||
|
@ -225,7 +235,7 @@ impl<T: ClipboardProvider> TextInput<T> {
|
||||||
let lines_suffix = &self.lines[end.line + 1..];
|
let lines_suffix = &self.lines[end.line + 1..];
|
||||||
|
|
||||||
let mut insert_lines = if self.multiline {
|
let mut insert_lines = if self.multiline {
|
||||||
chars_to_insert.split('\n').map(|s| DOMString::from(s.to_owned())).collect()
|
chars_to_insert.split('\n').map(|s| DOMString::from(s)).collect()
|
||||||
} else {
|
} else {
|
||||||
vec!(DOMString::from(chars_to_insert))
|
vec!(DOMString::from(chars_to_insert))
|
||||||
};
|
};
|
||||||
|
|
|
@ -206,6 +206,15 @@ fn test_textinput_replace_selection() {
|
||||||
assert_eq!(textinput.get_content(), "abxyzefg");
|
assert_eq!(textinput.get_content(), "abxyzefg");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_textinput_replace_selection_multibyte_char() {
|
||||||
|
let mut textinput = text_input(Lines::Single, "é");
|
||||||
|
textinput.adjust_horizontal_by_one(Direction::Forward, Selection::Selected);
|
||||||
|
|
||||||
|
textinput.replace_selection(DOMString::from("e"));
|
||||||
|
assert_eq!(textinput.get_content(), "e");
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_textinput_current_line_length() {
|
fn test_textinput_current_line_length() {
|
||||||
let mut textinput = text_input(Lines::Multiple, "abc\nde\nf");
|
let mut textinput = text_input(Lines::Multiple, "abc\nde\nf");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue