CaretPosition.js
10.7 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/**
* CaretPosition.js
*
* Released under LGPL License.
* Copyright (c) 1999-2015 Ephox Corp. All rights reserved
*
* License: http://www.tinymce.com/license
* Contributing: http://www.tinymce.com/contributing
*/
/**
* This module contains logic for creating caret positions within a document a caretposition
* is similar to a DOMRange object but it doesn't have two endpoints and is also more lightweight
* since it's now updated live when the DOM changes.
*
* @private
* @class tinymce.caret.CaretPosition
* @example
* var caretPos1 = new CaretPosition(container, offset);
* var caretPos2 = CaretPosition.fromRangeStart(someRange);
*/
define("tinymce/caret/CaretPosition", [
"tinymce/util/Fun",
"tinymce/dom/NodeType",
"tinymce/dom/DOMUtils",
"tinymce/dom/RangeUtils",
"tinymce/caret/CaretCandidate",
"tinymce/geom/ClientRect",
"tinymce/text/ExtendingChar"
], function(Fun, NodeType, DOMUtils, RangeUtils, CaretCandidate, ClientRect, ExtendingChar) {
var isElement = NodeType.isElement,
isCaretCandidate = CaretCandidate.isCaretCandidate,
isBlock = NodeType.matchStyleValues('display', 'block table'),
isFloated = NodeType.matchStyleValues('float', 'left right'),
isValidElementCaretCandidate = Fun.and(isElement, isCaretCandidate, Fun.negate(isFloated)),
isNotPre = Fun.negate(NodeType.matchStyleValues('white-space', 'pre pre-line pre-wrap')),
isText = NodeType.isText,
isBr = NodeType.isBr,
nodeIndex = DOMUtils.nodeIndex,
resolveIndex = RangeUtils.getNode;
function createRange(doc) {
return "createRange" in doc ? doc.createRange() : DOMUtils.DOM.createRng();
}
function isWhiteSpace(chr) {
return chr && /[\r\n\t ]/.test(chr);
}
function isHiddenWhiteSpaceRange(range) {
var container = range.startContainer,
offset = range.startOffset,
text;
if (isWhiteSpace(range.toString()) && isNotPre(container.parentNode)) {
text = container.data;
if (isWhiteSpace(text[offset - 1]) || isWhiteSpace(text[offset + 1])) {
return true;
}
}
return false;
}
function getCaretPositionClientRects(caretPosition) {
var clientRects = [], beforeNode, node;
// Hack for older WebKit versions that doesn't
// support getBoundingClientRect on BR elements
function getBrClientRect(brNode) {
var doc = brNode.ownerDocument,
rng = createRange(doc),
nbsp = doc.createTextNode('\u00a0'),
parentNode = brNode.parentNode,
clientRect;
parentNode.insertBefore(nbsp, brNode);
rng.setStart(nbsp, 0);
rng.setEnd(nbsp, 1);
clientRect = ClientRect.clone(rng.getBoundingClientRect());
parentNode.removeChild(nbsp);
return clientRect;
}
function getBoundingClientRect(item) {
var clientRect, clientRects;
clientRects = item.getClientRects();
if (clientRects.length > 0) {
clientRect = ClientRect.clone(clientRects[0]);
} else {
clientRect = ClientRect.clone(item.getBoundingClientRect());
}
if (isBr(item) && clientRect.left === 0) {
return getBrClientRect(item);
}
return clientRect;
}
function collapseAndInflateWidth(clientRect, toStart) {
clientRect = ClientRect.collapse(clientRect, toStart);
clientRect.width = 1;
clientRect.right = clientRect.left + 1;
return clientRect;
}
function addUniqueAndValidRect(clientRect) {
if (clientRect.height === 0) {
return;
}
if (clientRects.length > 0) {
if (ClientRect.isEqual(clientRect, clientRects[clientRects.length - 1])) {
return;
}
}
clientRects.push(clientRect);
}
function addCharacterOffset(container, offset) {
var range = createRange(container.ownerDocument);
if (offset < container.data.length) {
if (ExtendingChar.isExtendingChar(container.data[offset])) {
return clientRects;
}
// WebKit returns two client rects for a position after an extending
// character a\uxxx|b so expand on "b" and collapse to start of "b" box
if (ExtendingChar.isExtendingChar(container.data[offset - 1])) {
range.setStart(container, offset);
range.setEnd(container, offset + 1);
if (!isHiddenWhiteSpaceRange(range)) {
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(range), false));
return clientRects;
}
}
}
if (offset > 0) {
range.setStart(container, offset - 1);
range.setEnd(container, offset);
if (!isHiddenWhiteSpaceRange(range)) {
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(range), false));
}
}
if (offset < container.data.length) {
range.setStart(container, offset);
range.setEnd(container, offset + 1);
if (!isHiddenWhiteSpaceRange(range)) {
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(range), true));
}
}
}
if (isText(caretPosition.container())) {
addCharacterOffset(caretPosition.container(), caretPosition.offset());
return clientRects;
}
if (isElement(caretPosition.container())) {
if (caretPosition.isAtEnd()) {
node = resolveIndex(caretPosition.container(), caretPosition.offset());
if (isText(node)) {
addCharacterOffset(node, node.data.length);
}
if (isValidElementCaretCandidate(node) && !isBr(node)) {
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(node), false));
}
} else {
node = resolveIndex(caretPosition.container(), caretPosition.offset());
if (isText(node)) {
addCharacterOffset(node, 0);
}
if (isValidElementCaretCandidate(node) && caretPosition.isAtEnd()) {
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(node), false));
return clientRects;
}
beforeNode = resolveIndex(caretPosition.container(), caretPosition.offset() - 1);
if (isValidElementCaretCandidate(beforeNode) && !isBr(beforeNode)) {
if (isBlock(beforeNode) || isBlock(node) || !isValidElementCaretCandidate(node)) {
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(beforeNode), false));
}
}
if (isValidElementCaretCandidate(node)) {
addUniqueAndValidRect(collapseAndInflateWidth(getBoundingClientRect(node), true));
}
}
}
return clientRects;
}
/**
* Represents a location within the document by a container and an offset.
*
* @constructor
* @param {Node} container Container node.
* @param {Number} offset Offset within that container node.
* @param {Array} clientRects Optional client rects array for the position.
*/
function CaretPosition(container, offset, clientRects) {
function isAtStart() {
if (isText(container)) {
return offset === 0;
}
return offset === 0;
}
function isAtEnd() {
if (isText(container)) {
return offset >= container.data.length;
}
return offset >= container.childNodes.length;
}
function toRange() {
var range;
range = createRange(container.ownerDocument);
range.setStart(container, offset);
range.setEnd(container, offset);
return range;
}
function getClientRects() {
if (!clientRects) {
clientRects = getCaretPositionClientRects(new CaretPosition(container, offset));
}
return clientRects;
}
function isVisible() {
return getClientRects().length > 0;
}
function isEqual(caretPosition) {
return caretPosition && container === caretPosition.container() && offset === caretPosition.offset();
}
function getNode(before) {
return resolveIndex(container, before ? offset - 1 : offset);
}
return {
/**
* Returns the container node.
*
* @method container
* @return {Node} Container node.
*/
container: Fun.constant(container),
/**
* Returns the offset within the container node.
*
* @method offset
* @return {Number} Offset within the container node.
*/
offset: Fun.constant(offset),
/**
* Returns a range out of a the caret position.
*
* @method toRange
* @return {DOMRange} range for the caret position.
*/
toRange: toRange,
/**
* Returns the client rects for the caret position. Might be multiple rects between
* block elements.
*
* @method getClientRects
* @return {Array} Array of client rects.
*/
getClientRects: getClientRects,
/**
* Returns true if the caret location is visible/displayed on screen.
*
* @method isVisible
* @return {Boolean} true/false if the position is visible or not.
*/
isVisible: isVisible,
/**
* Returns true if the caret location is at the beginning of text node or container.
*
* @method isVisible
* @return {Boolean} true/false if the position is at the beginning.
*/
isAtStart: isAtStart,
/**
* Returns true if the caret location is at the end of text node or container.
*
* @method isVisible
* @return {Boolean} true/false if the position is at the end.
*/
isAtEnd: isAtEnd,
/**
* Compares the caret position to another caret position. This will only compare the
* container and offset not it's visual position.
*
* @method isEqual
* @param {tinymce.caret.CaretPosition} caretPosition Caret position to compare with.
* @return {Boolean} true if the caret positions are equal.
*/
isEqual: isEqual,
/**
* Returns the closest resolved node from a node index. That means if you have an offset after the
* last node in a container it will return that last node.
*
* @method getNode
* @return {Node} Node that is closest to the index.
*/
getNode: getNode
};
}
/**
* Creates a caret position from the start of a range.
*
* @method fromRangeStart
* @param {DOMRange} range DOM Range to create caret position from.
* @return {tinymce.caret.CaretPosition} Caret position from the start of DOM range.
*/
CaretPosition.fromRangeStart = function(range) {
return new CaretPosition(range.startContainer, range.startOffset);
};
/**
* Creates a caret position from the end of a range.
*
* @method fromRangeEnd
* @param {DOMRange} range DOM Range to create caret position from.
* @return {tinymce.caret.CaretPosition} Caret position from the end of DOM range.
*/
CaretPosition.fromRangeEnd = function(range) {
return new CaretPosition(range.endContainer, range.endOffset);
};
/**
* Creates a caret position from a node and places the offset after it.
*
* @method after
* @param {Node} node Node to get caret position from.
* @return {tinymce.caret.CaretPosition} Caret position from the node.
*/
CaretPosition.after = function(node) {
return new CaretPosition(node.parentNode, nodeIndex(node) + 1);
};
/**
* Creates a caret position from a node and places the offset before it.
*
* @method before
* @param {Node} node Node to get caret position from.
* @return {tinymce.caret.CaretPosition} Caret position from the node.
*/
CaretPosition.before = function(node) {
return new CaretPosition(node.parentNode, nodeIndex(node));
};
return CaretPosition;
});