1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
//! Common handling of keyboard input and state management for text input controls
use dom::bindings::codegen::Bindings::KeyboardEventBinding::KeyboardEventMethods;
use dom::bindings::js::JSRef;
use dom::keyboardevent::KeyboardEvent;
use servo_util::str::DOMString;
use std::cmp::{min, max};
use std::default::Default;
#[jstraceable]
struct TextPoint {
line: uint,
index: uint,
}
#[jstraceable]
pub struct TextInput {
/// Current text input content, split across lines without trailing '\n'
lines: Vec<DOMString>,
/// Current cursor input point
edit_point: TextPoint,
/// Selection range, beginning and end point that can span multiple lines.
_selection: Option<(TextPoint, TextPoint)>,
/// Is this ia multiline input?
multiline: bool,
}
pub enum KeyReaction {
TriggerDefaultAction,
DispatchInput,
Nothing,
}
impl Default for TextPoint {
fn default() -> TextPoint {
TextPoint {
line: 0,
index: 0,
}
}
}
impl TextInput {
pub fn new(multiline: bool, initial: DOMString) -> TextInput {
let mut i = TextInput {
lines: vec!(),
edit_point: Default::default(),
_selection: None,
multiline: multiline,
};
i.set_content(initial);
i
}
fn get_current_line(&self) -> &DOMString {
&self.lines[self.edit_point.line]
}
fn insert_char(&mut self, ch: char) {
//TODO: handle replacing selection with character
let new_line = {
let prefix = self.get_current_line().as_slice().slice_chars(0, self.edit_point.index);
let suffix = self.get_current_line().as_slice().slice_chars(self.edit_point.index,
self.current_line_length());
let mut new_line = prefix.to_string();
new_line.push(ch);
new_line.push_str(suffix.as_slice());
new_line
};
self.lines[self.edit_point.line] = new_line;
self.edit_point.index += 1;
}
fn delete_char(&mut self, forward: bool) {
//TODO: handle deleting selection
let prefix_end = if forward {
self.edit_point.index
} else {
//TODO: handle backspacing from position 0 of current line
if self.multiline {
assert!(self.edit_point.index > 0);
} else if self.edit_point.index == 0 {
return;
}
self.edit_point.index - 1
};
let suffix_start = if forward {
let is_eol = self.edit_point.index == self.current_line_length() - 1;
if self.multiline {
//TODO: handle deleting from end position of current line
assert!(!is_eol);
} else if is_eol {
return;
}
self.edit_point.index + 1
} else {
self.edit_point.index
};
let new_line = {
let prefix = self.get_current_line().as_slice().slice_chars(0, prefix_end);
let suffix = self.get_current_line().as_slice().slice_chars(suffix_start,
self.current_line_length());
let mut new_line = prefix.to_string();
new_line.push_str(suffix);
new_line
};
self.lines[self.edit_point.line] = new_line;
if !forward {
self.adjust_horizontal(-1);
}
}
fn current_line_length(&self) -> uint {
self.lines[self.edit_point.line].len()
}
fn adjust_vertical(&mut self, adjust: int) {
if !self.multiline {
return;
}
if adjust < 0 && self.edit_point.line as int + adjust < 0 {
self.edit_point.index = 0;
self.edit_point.line = 0;
return;
} else if adjust > 0 && self.edit_point.line >= min(0, self.lines.len() - adjust as uint) {
self.edit_point.index = self.current_line_length();
self.edit_point.line = self.lines.len() - 1;
return;
}
self.edit_point.line = (self.edit_point.line as int + adjust) as uint;
self.edit_point.index = min(self.current_line_length(), self.edit_point.index);
}
fn adjust_horizontal(&mut self, adjust: int) {
if adjust < 0 {
if self.multiline {
let remaining = self.edit_point.index;
if adjust.abs() as uint > remaining {
self.edit_point.index = 0;
self.adjust_vertical(-1);
self.edit_point.index = self.current_line_length();
self.adjust_horizontal(adjust + remaining as int);
} else {
self.edit_point.index = (self.edit_point.index as int + adjust) as uint;
}
} else {
self.edit_point.index = max(0, self.edit_point.index as int + adjust) as uint;
}
} else {
if self.multiline {
let remaining = self.current_line_length() - self.edit_point.index;
if adjust as uint > remaining {
self.edit_point.index = 0;
self.adjust_vertical(1);
self.adjust_horizontal(adjust - remaining as int);
} else {
self.edit_point.index += adjust as uint;
}
} else {
self.edit_point.index = min(self.current_line_length(),
self.edit_point.index + adjust as uint);
}
}
}
fn handle_return(&mut self) -> KeyReaction {
if !self.multiline {
return TriggerDefaultAction;
}
//TODO: support replacing selection with newline
let prefix = self.get_current_line().as_slice().slice_chars(0, self.edit_point.index).to_string();
let suffix = self.get_current_line().as_slice().slice_chars(self.edit_point.index,
self.current_line_length()).to_string();
self.lines[self.edit_point.line] = prefix;
self.lines.insert(self.edit_point.line + 1, suffix);
return DispatchInput;
}
pub fn handle_keydown(&mut self, event: JSRef<KeyboardEvent>) -> KeyReaction {
match event.Key().as_slice() {
c if c.len() == 1 => {
self.insert_char(c.char_at(0));
return DispatchInput;
}
"Space" => {
self.insert_char(' ');
DispatchInput
}
"Delete" => {
self.delete_char(true);
DispatchInput
}
"Backspace" => {
self.delete_char(false);
DispatchInput
}
"ArrowLeft" => {
self.adjust_horizontal(-1);
Nothing
}
"ArrowRight" => {
self.adjust_horizontal(1);
Nothing
}
"ArrowUp" => {
self.adjust_vertical(-1);
Nothing
}
"ArrowDown" => {
self.adjust_vertical(1);
Nothing
}
"Enter" => self.handle_return(),
"Home" => {
self.edit_point.index = 0;
Nothing
}
"End" => {
self.edit_point.index = self.current_line_length();
Nothing
}
"Tab" => TriggerDefaultAction,
_ => Nothing,
}
}
pub fn get_content(&self) -> DOMString {
let mut content = "".to_string();
for (i, line) in self.lines.iter().enumerate() {
content.push_str(line.as_slice());
if i < self.lines.len() - 1 {
content.push('\n');
}
}
content
}
pub fn set_content(&mut self, content: DOMString) {
self.lines = if self.multiline {
content.as_slice().split('\n').map(|s| s.to_string()).collect()
} else {
vec!(content)
};
self.edit_point.line = min(self.edit_point.line, self.lines.len() - 1);
self.edit_point.index = min(self.edit_point.index, self.current_line_length() - 1);
}
}
|