-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathmylib-textselection.js
194 lines (173 loc) · 6.44 KB
/
mylib-textselection.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
193
194
// My Library Text Selection add-on
var global = this, API, D, C;
if (API && API.attachDocumentReadyListener) {
API.attachDocumentReadyListener(function() {
var isHostMethod = API.isHostMethod, isHostObjectProperty = API.isHostObjectProperty, getDocumentWindow = API.getDocumentWindow;
var getSelection, clearDocumentSelection, selectionToRange, getControlSelection, setControlSelection, clearControlSelection, getSelectionText;
var body, el, doc = global.document;
if (isHostMethod(global, 'getSelection') && getDocumentWindow) {
getSelection = function(doc) {
return getDocumentWindow(doc).getSelection();
};
} else if (isHostObjectProperty(global.document, 'selection')) {
getSelection = function(doc) {
return (doc || global.document).selection;
};
}
API.getHostSelection = getSelection;
var rangeText = API.getHostRangeText = function(range) {
if (typeof range.text == 'string') {
return range.text;
}
if (isHostMethod(range, 'toString')) {
return range.toString();
}
};
if (getSelection) {
getSelectionText = function(selection) {
var range = selectionToRange(selection);
if (range) {
return rangeText(range);
}
if (isHostMethod(selection, 'toString')) {
return selection.toString();
}
return '';
};
selectionToRange = API.selectionToHostRange = function(selection) {
if (isHostMethod(selection, 'getRangeAt')) {
return selection.rangeCount ? selection.getRangeAt(0) : null;
}
if (isHostMethod(selection, 'createRange')) {
return selection.createRange();
}
return null;
};
}
if (selectionToRange) {
clearDocumentSelection = function(doc) {
var selection = getSelection(doc);
if (isHostMethod(selection, 'empty')) {
selection.empty();
} else if (isHostMethod(selection, 'collapseToStart')) {
selection.collapseToStart();
}
var range = selectionToRange(selection);
if (range && isHostMethod(range, 'collapse')) {
range.collapse(true);
range.select();
}
};
API.getHostRange = function(doc) {
return selectionToRange(getSelection(doc));
};
}
if (clearDocumentSelection) {
API.clearDocumentSelection = function(doc) {
clearDocumentSelection(doc);
};
}
API.getDocumentSelectionText = function(doc) {
return getSelectionText(getSelection(doc));
};
el = doc.createElement('input');
body = API.getBodyElement();
var reCrLf = /\r\n/g;
var reCrLfTrailing = /\r\n$/;
if (isHostMethod(doc, 'createElement') && body && isHostMethod(body, 'appendChild') && el) {
body.appendChild(el);
if (typeof el.selectionStart == 'number') {
getControlSelection = function(el) {
var start = el.selectionStart, end = el.selectionEnd;
return [start, end, el.value.substring(start, end)];
};
setControlSelection = function(el, start, end) {
el.selectionStart = start;
el.selectionEnd = end;
};
} else if (selectionToRange) {
getControlSelection = function(el) {
var documentRange, elementRange, len, start, tempRange, text;
if (isHostMethod(el, 'focus')) {
el.focus();
}
documentRange = selectionToRange(getSelection());
if (documentRange) {
if (isHostMethod(documentRange, 'duplicate')) {
elementRange = documentRange.duplicate();
if (el.tagName == 'INPUT') {
elementRange.expand('textedit');
} else {
// TEXTRANGE elements have issues with trailing CRLF's
// Creates a temporary range with a single space between
// the selected range and the remaining text
if (reCrLfTrailing.test(el.value)) {
elementRange.collapse(false);
elementRange.text = ' ';
tempRange = elementRange.duplicate();
}
elementRange.moveToElementText(el);
}
text = elementRange.text.replace(reCrLf, '\n');
len = text.length;
elementRange.setEndPoint('StartToStart', documentRange);
text = elementRange.text.replace(reCrLf, '\n');
start = len - text.length;
text = documentRange.text.replace(reCrLf, '\n');
if (tempRange) {
tempRange.moveStart("character", -1);
tempRange.text = '';
}
return ([start, start + text.length, text]);
}
}
return null;
};
if (isHostMethod(el, 'createTextRange')) {
setControlSelection = function(el, start, end) {
var range = el.createTextRange();
range.collapse();
range.moveStart('character', start);
range.moveEnd('character', end - start);
range.select();
};
}
}
body.removeChild(el);
}
API.getControlSelection = getControlSelection;
API.setControlSelection = setControlSelection;
if (setControlSelection) {
clearControlSelection = API.clearControlSelection = function(el) {
setControlSelection(el, 0, 0);
};
}
if (C && C.prototype && getControlSelection) {
if (setControlSelection) {
C.prototype.getSelection = function() {
return getControlSelection(this.element());
};
C.prototype.setSelection = function(start, end) {
setControlSelection(this.element(), start, end);
return this;
};
C.prototype.clearSelection = function() {
clearControlSelection(this.element());
return this;
};
}
}
if (D && D.prototype && getSelectionText) {
D.prototype.getSelectionText = function() {
return getSelectionText(getSelection(this.node()));
};
if (clearDocumentSelection) {
D.prototype.clearSelection = function() {
clearDocumentSelection(this.node());
return this;
};
}
}
doc = el = null;
});
}