-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.selections.js
192 lines (169 loc) · 5.22 KB
/
jquery.selections.js
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
/**
* jQuery Selections plugin
*
* Provides text input utilities
*
* Note that most functions act only on the first of
* the given elements, since browsers can only have
* one in focus at a time
*
* Copyright (C) 2010, Jordi Boggiano
* http://seld.be/ - [email protected]
*
* Licensed under the new BSD License
* See the LICENSE file for details
*
* Source on Github http://github.com/Seldaek/jquery-selections
*/
(function($) {
/**
* sets the caret position
*
* @param int index
* @see $.fn.setSelection
*/
$.fn.setCaret = function(index) {
this.setSelection(index, index);
};
/**
* returns the caret position, or 0 if the element has no caret
*
* @return int
*/
$.fn.getCaret = function() {
var elem, range, elemRange, elemRangeCopy, value, caret, pos, match;
elem = this.get(0);
value = $(elem).val();
// standard browsers
if (elem.selectionStart) {
caret = elem.selectionStart;
} else if (document.selection) {
// old IE handling
elem.focus();
range = document.selection.createRange();
// handle input texts
if (elem.nodeName === 'INPUT') {
elemRange = elem.createTextRange();
elemRangeCopy = elemRange.duplicate();
elemRange.moveToBookmark(range.getBookmark());
elemRangeCopy.setEndPoint('EndToStart', elemRange);
caret = elemRangeCopy.text.length;
} else {
// handle textareas
elemRangeCopy = range.duplicate();
elemRangeCopy.moveToElementText(elem);
pos = 0;
if (range.text.length > 1) {
pos = Math.max(0, pos - range.text.length);
}
caret = -1 + pos;
elemRangeCopy .moveStart('character', pos);
while (elemRangeCopy.inRange(range)) {
elemRangeCopy.moveStart('character');
caret++;
}
}
} else {
caret = 0;
}
if ($.browser.opera) {
match = value.replace(/\r?\n/g, "\r\n").substr(0, caret).match(/\r\n/g);
if (match) {
caret -= match.length;
}
}
return caret;
};
/**
* selects a range of text
*
* @param int start index of the beginning of the selection
* @param int end index of the end of the selection
* @return object chainable
*/
$.fn.setSelection = function(start, end) {
var elem, range, match;
elem = this.get(0);
end = end || start;
// standard browsers
if (elem.setSelectionRange) {
elem.focus();
if ($.browser.opera) {
match = this.val().replace(/\r?\n/g, "\n").substr(0, start).match(/\n/g);
if (match) {
start += match.length;
}
match = this.val().replace(/\r?\n/g, "\n").substr(0, end).match(/\n/g);
if (match) {
end += match.length;
}
}
elem.setSelectionRange(start, end);
} else if (elem.createTextRange) {
// old IE handling
range = elem.createTextRange();
range.collapse(true);
range.moveEnd('character', end);
range.moveStart('character', start);
range.select();
}
return this;
};
/**
* reads the text the user selected
*
* @param int start index of the beginning of the selection
* @param int end index of the end of the selection
* @return string
*/
$.fn.getSelectedText = function() {
var elem = this.get(0);
// standard browsers
if (elem.selectionStart) {
return elem.value.substring(elem.selectionStart, elem.selectionEnd);
}
// old IE
if (document.selection) {
elem.focus();
return document.selection.createRange().text;
}
return '';
};
/**
* injects the given text in place of the current selection
*
* @param string text
* @return object chainable
*/
$.fn.replaceSelection = function(text) {
var elem = this.get(0);
// standard browsers
if (elem.selectionStart) {
elem.value = elem.value.substr(0, elem.selectionStart) + text + elem.value.substr(elem.selectionEnd);
return this;
}
// old IE
if (document.selection) {
elem.focus();
document.selection.createRange().text = text;
return this;
}
return this;
};
/**
* injects the given text at the current caret position
*
* @param string text
* @return object chainable
*/
$.fn.injectText = function(text) {
var $elem, caret;
$elem = this.first();
caret = $elem.getCaret();
$elem.val(function(idx, val) {
return val.substring(0, caret) + text + val.substring(caret);
});
$elem.setCaret(caret + text.length);
return this;
};
}(jQuery));