forked from syncfusion/ej2-javascript-ui-controls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselection.ts
476 lines (441 loc) · 15.5 KB
/
selection.ts
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import { isNullOrUndefined } from '@syncfusion/ej2-base';
/**
* `Selection` module is used to handle RTE Selections.
*/
export class NodeSelection {
public range: Range;
public rootNode: Node;
public body: HTMLBodyElement;
public html: string;
public startContainer: number[];
public endContainer: number[];
public startOffset: number;
public endOffset: number;
public startNodeName: string[] = [];
public endNodeName: string[] = [];
private saveInstance(range: Range, body: HTMLBodyElement): NodeSelection {
this.range = range.cloneRange();
this.rootNode = this.documentFromRange(range);
this.body = body;
this.startContainer = this.getNodeArray(range.startContainer, true);
this.endContainer = this.getNodeArray(range.endContainer, false);
this.startOffset = range.startOffset;
this.endOffset = range.endOffset;
this.html = this.body.innerHTML;
return this;
}
private documentFromRange(range: Range): Node {
return (9 === range.startContainer.nodeType) ? range.startContainer : range.startContainer.ownerDocument;
}
public getRange(docElement: Document): Range {
const select: Selection = this.get(docElement);
const range: Range = select && select.rangeCount > 0 ? select.getRangeAt(select.rangeCount - 1) : docElement.createRange();
return (range.startContainer !== docElement || range.endContainer !== docElement
|| range.startOffset || range.endOffset || (range.setStart(docElement.body, 0),
range.collapse(!0)),
range);
}
/**
* get method
*
* @param {Document} docElement - specifies the get function
* @returns {void}
* @hidden
* @deprecated
*/
public get(docElement: Document): Selection {
return docElement.defaultView.getSelection();
}
/**
* save method
*
* @param {Range} range - range value.
* @param {Document} docElement - specifies the document.
* @returns {void}
* @hidden
* @deprecated
*/
public save(range: Range, docElement: Document): NodeSelection {
range = (range) ? range.cloneRange() : this.getRange(docElement);
return this.saveInstance(range, docElement.body as HTMLBodyElement);
}
/**
* getIndex method
*
* @param {Node} node - specifies the node value.
* @returns {void}
* @hidden
* @deprecated
*/
public getIndex(node: Node): number {
let index: number;
let num: number = 0;
node = !node.previousSibling && (node as Element).tagName === 'BR' ? node : node.previousSibling;
if (node) {
for (let type: number = node.nodeType; node; null) {
index = node.nodeType;
num++;
//eslint-disable-next-line
type = index;
node = node.previousSibling;
}
}
return num;
}
private isChildNode(nodeCollection: Node[], parentNode: Node): boolean {
for (let index: number = 0; index < parentNode.childNodes.length; index++) {
if (nodeCollection.indexOf(parentNode.childNodes[index]) > -1) {
return true;
}
}
return false;
}
private getNode(startNode: Node, endNode: Node, nodeCollection: Node[]): Node {
if (endNode === startNode &&
(startNode.nodeType === 3 || !startNode.firstChild || nodeCollection.indexOf(startNode.firstChild) !== -1
|| this.isChildNode(nodeCollection, startNode))) {
return null;
}
if (nodeCollection.indexOf(startNode.firstChild) === -1 && startNode.firstChild && !this.isChildNode(nodeCollection, startNode)) {
return startNode.firstChild;
}
if (startNode.nextSibling) {
return startNode.nextSibling;
}
if (!startNode.parentNode) {
return null;
} else {
return startNode.parentNode;
}
}
/**
* getNodeCollection method
*
* @param {Range} range -specifies the range.
* @returns {void}
* @hidden
* @deprecated
*/
public getNodeCollection(range: Range): Node[] {
let startNode: Node = range.startContainer.childNodes[range.startOffset]
|| range.startContainer;
const endNode: Node = range.endContainer.childNodes[
(range.endOffset > 0) ? (range.endOffset - 1) : range.endOffset]
|| range.endContainer;
if (startNode === endNode && startNode.childNodes.length === 0) {
return [startNode];
}
if (range.startOffset === range.endOffset && range.startOffset !== 0 && range.startContainer.nodeName === 'PRE') {
return [startNode.nodeName === 'BR' || startNode.nodeName === '#text' ? startNode : startNode.childNodes[0]];
}
const nodeCollection: Node[] = [];
do {
if (nodeCollection.indexOf(startNode) === -1) {
nodeCollection.push(startNode);
}
startNode = this.getNode(startNode, endNode, nodeCollection);
}
while (startNode);
return nodeCollection;
}
/**
* getParentNodeCollection method
*
* @param {Range} range - specifies the range value.
* @returns {void}
* @hidden
* @deprecated
*/
public getParentNodeCollection(range: Range): Node[] {
return this.getParentNodes(this.getNodeCollection(range), range);
}
/**
* getParentNodes method
*
* @param {Node[]} nodeCollection - specifies the collection of nodes.
* @param {Range} range - specifies the range values.
* @returns {void}
* @hidden
* @deprecated
*/
public getParentNodes(nodeCollection: Node[], range: Range): Node[] {
nodeCollection = nodeCollection.reverse();
for (let index: number = 0; index < nodeCollection.length; index++) {
if ((nodeCollection.indexOf(nodeCollection[index].parentNode) !== -1)
|| (nodeCollection[index].nodeType === 3 &&
range.startContainer !== range.endContainer &&
range.startContainer.parentNode !== range.endContainer.parentNode)) {
nodeCollection.splice(index, 1);
index--;
} else if (nodeCollection[index].nodeType === 3) {
nodeCollection[index] = nodeCollection[index].parentNode;
}
}
return nodeCollection;
}
/**
* getSelectionNodeCollection method
*
* @param {Range} range - specifies the range value.
* @returns {void}
* @hidden
* @deprecated
*/
public getSelectionNodeCollection(range: Range): Node[] {
return this.getSelectionNodes(this.getNodeCollection(range));
}
/**
* getSelectionNodeCollection along with BR node method
*
* @param {Range} range - specifies the range value.
* @returns {void}
* @hidden
* @deprecated
*/
public getSelectionNodeCollectionBr(range: Range): Node[] {
return this.getSelectionNodesBr(this.getNodeCollection(range));
}
/**
* getParentNodes method
*
* @param {Node[]} nodeCollection - specifies the collection of nodes.
* @returns {void}
* @hidden
* @deprecated
*/
public getSelectionNodes(nodeCollection: Node[]): Node[] {
nodeCollection = nodeCollection.reverse();
const regEx: RegExp = new RegExp(String.fromCharCode(8203), 'g');
for (let index: number = 0; index < nodeCollection.length; index++) {
if (nodeCollection[index].nodeType !== 3 || (nodeCollection[index].textContent.trim() === '' ||
(nodeCollection[index].textContent.length === 1 && nodeCollection[index].textContent.match(regEx)))) {
nodeCollection.splice(index, 1);
index--;
}
}
return nodeCollection.reverse();
}
/**
* Get selection text nodes with br method.
*
* @param {Node[]} nodeCollection - specifies the collection of nodes.
* @returns {void}
* @hidden
* @deprecated
*/
public getSelectionNodesBr(nodeCollection: Node[]): Node[] {
nodeCollection = nodeCollection.reverse();
const regEx: RegExp = new RegExp(String.fromCharCode(8203), 'g');
for (let index: number = 0; index < nodeCollection.length; index++) {
if (nodeCollection[index].nodeName !== 'BR' &&
(nodeCollection[index].nodeType !== 3 || (nodeCollection[index].textContent.trim() === '' ||
(nodeCollection[index].textContent.length === 1 && nodeCollection[index].textContent.match(regEx))))) {
nodeCollection.splice(index, 1);
index--;
}
}
return nodeCollection.reverse();
}
/**
* getInsertNodeCollection method
*
* @param {Range} range - specifies the range value.
* @returns {void}
* @hidden
* @deprecated
*/
public getInsertNodeCollection(range: Range): Node[] {
return this.getInsertNodes(this.getNodeCollection(range));
}
/**
* getInsertNodes method
*
* @param {Node[]} nodeCollection - specifies the collection of nodes.
* @returns {void}
* @hidden
* @deprecated
*/
public getInsertNodes(nodeCollection: Node[]): Node[] {
nodeCollection = nodeCollection.reverse();
for (let index: number = 0; index < nodeCollection.length; index++) {
if ((nodeCollection[index].childNodes.length !== 0 &&
nodeCollection[index].nodeType !== 3) ||
(nodeCollection[index].nodeType === 3 &&
nodeCollection[index].textContent === '')) {
nodeCollection.splice(index, 1);
index--;
}
}
return nodeCollection.reverse();
}
/**
* getNodeArray method
*
* @param {Node} node - specifies the node content.
* @param {boolean} isStart - specifies the boolean value.
* @param {Document} root - specifies the root document.
* @returns {void}
* @hidden
* @deprecated
*/
public getNodeArray(node: Node, isStart: boolean, root?: Document): number[] {
const array: number[] = [];
// eslint-disable-next-line
((isStart) ? (this.startNodeName = []) : (this.endNodeName = []));
for (; node !== (root ? root : this.rootNode); null) {
if (isNullOrUndefined(node)) {
break;
}
// eslint-disable-next-line
(isStart) ? this.startNodeName.push(node.nodeName.toLowerCase()) : this.endNodeName.push(node.nodeName.toLowerCase());
array.push(this.getIndex(node));
node = node.parentNode;
}
return array;
}
private setRangePoint(range: Range, isvalid: boolean, num: number[], size: number): Range {
let node: Node = this.rootNode;
let index: number = num.length;
const constant: number = size;
for (; index--; null) {
node = node && node.childNodes[num[index]];
}
if (node && constant >= 0 && node.nodeName !== 'html') {
range[isvalid ? 'setStart' : 'setEnd'](node, constant);
}
return range;
}
/**
* restore method
*
* @returns {void}
* @hidden
* @deprecated
*/
public restore(): Range {
let range: Range = this.range.cloneRange();
range = this.setRangePoint(range, true, this.startContainer, this.startOffset);
range = this.setRangePoint(range, false, this.endContainer, this.endOffset);
this.selectRange(this.rootNode as Document, range);
return range;
}
public selectRange(docElement: Document, range: Range): void {
this.setRange(docElement, range);
this.save(range, docElement);
}
/**
* setRange method
*
* @param {Document} docElement - specifies the document.
* @param {Range} range - specifies the range.
* @returns {void}
* @hidden
* @deprecated
*/
public setRange(docElement: Document, range: Range): void {
const selection: Selection = this.get(docElement);
selection.removeAllRanges();
selection.addRange(range);
}
/**
* setSelectionText method
*
* @param {Document} docElement - specifies the documrent
* @param {Node} startNode - specifies the starting node.
* @param {Node} endNode - specifies the the end node.
* @param {number} startIndex - specifies the starting index.
* @param {number} endIndex - specifies the end index.
* @returns {void}
* @hidden
* @deprecated
*/
public setSelectionText(docElement: Document, startNode: Node, endNode: Node, startIndex: number, endIndex: number
): void {
const range: Range = docElement.createRange();
range.setStart(startNode, startIndex);
range.setEnd(endNode, endIndex);
this.setRange(docElement, range);
}
/**
* setSelectionContents method
*
* @param {Document} docElement - specifies the document.
* @param {Node} element - specifies the node.
* @returns {void}
* @hidden
* @deprecated
*/
public setSelectionContents(docElement: Document, element: Node): void {
const range: Range = docElement.createRange();
range.selectNode(element);
this.setRange(docElement, range);
}
/**
* setSelectionNode method
*
* @param {Document} docElement - specifies the document.
* @param {Node} element - specifies the node.
* @returns {void}
* @hidden
* @deprecated
*/
public setSelectionNode(docElement: Document, element: Node): void {
const range: Range = docElement.createRange();
range.selectNodeContents(element);
this.setRange(docElement, range);
}
/**
* getSelectedNodes method
*
* @param {Document} docElement - specifies the document.
* @returns {void}
* @hidden
* @deprecated
*/
public getSelectedNodes(docElement: Document): Node[] {
return this.getNodeCollection(this.getRange(docElement));
}
/**
* Clear method
*
* @param {Document} docElement - specifies the document.
* @returns {void}
* @hidden
* @deprecated
*/
public Clear(docElement: Document): void {
this.get(docElement).removeAllRanges();
}
/**
* insertParentNode method
*
* @param {Document} docElement - specifies the document.
* @param {Node} newNode - specicfies the new node.
* @param {Range} range - specifies the range.
* @returns {void}
* @hidden
* @deprecated
*/
public insertParentNode(docElement: Document, newNode: Node, range: Range): void {
range.surroundContents(newNode);
this.selectRange(docElement, range);
}
/**
* setCursorPoint method
*
* @param {Document} docElement - specifies the document.
* @param {Element} element - specifies the element.
* @param {number} point - specifies the point.
* @returns {void}
* @hidden
* @deprecated
*/
public setCursorPoint(docElement: Document, element: Element, point: number): void {
const range: Range = docElement.createRange();
const selection: Selection = docElement.defaultView.getSelection();
range.setStart(element, point);
range.collapse(true);
selection.removeAllRanges();
selection.addRange(range);
}
}