/
Range.ts
340 lines (307 loc) · 9.31 KB
/
Range.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
import INode from '../nodes/node/INode';
import IDocument from '../nodes/document/IDocument';
import IDocumentFragment from '../nodes/document-fragment/IDocumentFragment';
import DOMRect from '../nodes/element/DOMRect';
import RangeHowEnum from './RangeHowEnum';
/**
* Range.
*
* Reference:
* https://developer.mozilla.org/en-US/docs/Web/API/Range.
*/
export default class Range {
public static _ownerDocument: IDocument = null;
public static readonly END_TO_END: number = RangeHowEnum.endToEnd;
public static readonly END_TO_START: number = RangeHowEnum.endToStart;
public static readonly START_TO_END: number = RangeHowEnum.startToEnd;
public static readonly START_TO_START: number = RangeHowEnum.startToStart;
public readonly END_TO_END: number = RangeHowEnum.endToEnd;
public readonly END_TO_START: number = RangeHowEnum.endToStart;
public readonly START_TO_END: number = RangeHowEnum.startToEnd;
public readonly START_TO_START: number = RangeHowEnum.startToStart;
public readonly startOffset: number = 0;
public readonly endOffset: number = 0;
public readonly startContainer: INode = null;
public readonly endContainer: INode = null;
/**
* Constructor.
*/
constructor() {
this.startContainer = (<typeof Range>this.constructor)._ownerDocument;
this.endContainer = (<typeof Range>this.constructor)._ownerDocument;
}
/**
* Returns a boolean value indicating whether the range's start and end points are at the same position.
*
* @returns Collapsed.
*/
public get collapsed(): boolean {
return this.startContainer === this.endContainer && this.startOffset === this.endOffset;
}
/**
* Returns the deepest Node that contains the startContainer and endContainer nodes.
*
* @returns Node.
*/
public get commonAncestorContainer(): INode {
if (this.startContainer === this.endContainer) {
return this.startContainer;
}
const endAncestors = [];
let parent = this.endContainer;
while (parent) {
endAncestors.push(parent);
parent = parent.parentNode;
}
parent = this.startContainer;
while (parent) {
if (endAncestors.includes(parent)) {
return parent;
}
parent = parent.parentNode;
}
return this.endContainer || this.startContainer;
}
/**
* Returns -1, 0, or 1 depending on whether the referenceNode is before, the same as, or after the Range.
*
* @param toStart A boolean value: true collapses the Range to its start, false to its end. If omitted, it defaults to false.
*/
public collapse(toStart = false): void {
if (toStart) {
(<INode>this.endContainer) = this.startContainer;
(<number>this.endOffset) = this.startOffset;
} else {
(<INode>this.startContainer) = this.endContainer;
(<number>this.startOffset) = this.endOffset;
}
}
/**
* Compares the boundary points of the Range with those of another range.
*
* @param _how How.
* @param _sourceRange Range.
* @returns A number, -1, 0, or 1, indicating whether the corresponding boundary-point of the Range is respectively before, equal to, or after the corresponding boundary-point of sourceRange.
*/
public compareBoundaryPoints(_how: RangeHowEnum, _sourceRange: Range): number {
// TODO: Implement
return 0;
}
/**
* Returns -1, 0, or 1 depending on whether the referenceNode is before, the same as, or after the Range.
*
* @param _referenceNode Reference node.
* @param [_offset=0] Offset.
* @returns -1,0, or 1.
*/
public comparePoint(_referenceNode: INode, _offset = 0): number {
// TODO: Implement
return 0;
}
/**
* Returns a DocumentFragment copying the objects of type Node included in the Range.
*
* @returns Document fragment.
*/
public cloneContents(): IDocumentFragment {
// TODO: Implement
return null;
}
/**
* Returns a Range object with boundary points identical to the cloned Range.
*
* @returns Range.
*/
public cloneRange(): Range {
// TODO: Implement
return null;
}
/**
* Returns a DocumentFragment by invoking the HTML fragment parsing algorithm or the XML fragment parsing algorithm with the start of the range (the parent of the selected node) as the context node. The HTML fragment parsing algorithm is used if the range belongs to a Document whose HTMLness bit is set. In the HTML case, if the context node would be html, for historical reasons the fragment parsing algorithm is invoked with body as the context instead.
*
* @param _tagString Tag string.
* @returns Document fragment.
*/
public createContextualFragment(_tagString: string): IDocumentFragment {
// TODO: Implement
return null;
}
/**
* Removes the contents of the Range from the Document.
*/
public deleteContents(): void {
// TODO: Implement
}
/**
* Does nothing. It used to disable the Range object and enable the browser to release associated resources. The method has been kept for compatibility.
*/
public detach(): void {
// Do nothing
}
/**
* Moves contents of the Range from the document tree into a DocumentFragment.
*
* @returns Document fragment.
*/
public extractContents(): IDocumentFragment {
// TODO: Implement
return null;
}
/**
* Returns a DOMRect object that bounds the contents of the range; this is a rectangle enclosing the union of the bounding rectangles for all the elements in the range.
*
* @returns DOMRect object.
*/
public getBoundingClientRect(): DOMRect {
// TODO: Implement
return null;
}
/**
* The Range.getClientRects() method returns a list of DOMRect objects representing the area of the screen occupied by the range. This is created by aggregating the results of calls to Element.getClientRects() for all the elements in the range.
*
* @returns DOMRect objects.
*/
public getClientRects(): DOMRect[] {
// TODO: Implement
return null;
}
/**
* Returns a boolean indicating whether the given point is in the Range.
*
* @param _referenceNode Reference node.
* @param _offset Offset.
* @returns "true" if in range.
*/
public isPointInRange(_referenceNode: INode, _offset = 0): boolean {
// TODO: Implement
return false;
}
/**
* Inserts a node at the start of the Range.
*
* @param _newNode New node.
*/
public insertNode(_newNode: INode): void {
// TODO: Implement
return null;
}
/**
* Returns a boolean indicating whether the given Node intersects the Range.
*
* @param _referenceNode Reference node.
* @returns "true" if it intersects.
*/
public intersectsNode(_referenceNode: INode): boolean {
// TODO: Implement
return false;
}
/**
* Sets the Range to contain the Node and its contents.
*
* @param _referenceNode Reference node.
*/
public selectNode(_referenceNode: INode): void {
// TODO: Implement
}
/**
* Sets the Range to contain the contents of a Node.
*
* @param referenceNode Reference node.
*/
public selectNodeContents(referenceNode: INode): void {
(<INode>this.startContainer) = referenceNode;
(<INode>this.endContainer) = referenceNode;
(<number>this.startOffset) = 0;
(<number>this.endOffset) = referenceNode.textContent.length > 0 ? 1 : 0;
}
/**
* Sets the end position of a Range to be located at the given offset into the specified node x.
*
* @param endNode End node.
* @param endOffset End offset.
*/
public setEnd(endNode: INode, endOffset = 0): void {
(<INode>this.endContainer) = endNode;
(<number>this.endOffset) = endOffset;
}
/**
* Sets the start position of a Range.
*
* @param startNode Start node.
* @param startOffset Start offset.
*/
public setStart(startNode: INode, startOffset = 0): void {
(<INode>this.startContainer) = startNode;
(<number>this.startOffset) = startOffset;
}
/**
* Sets the end position of a Range relative to another Node.
*
* @param referenceNode Reference node.
*/
public setEndAfter(referenceNode: INode): void {
const sibling = referenceNode.nextSibling;
if (!sibling) {
throw new Error(
'Failed to set range end. "referenceNode" does not have any nodes after itself.'
);
}
this.setEnd(sibling);
}
/**
* Sets the end position of a Range relative to another Node.
*
* @param referenceNode Reference node.
*/
public setEndBefore(referenceNode: INode): void {
const sibling = referenceNode.previousSibling;
if (!sibling) {
throw new Error(
'Failed to set range end. "referenceNode" does not have any nodes before itself.'
);
}
this.setEnd(sibling);
}
/**
* Sets the start position of a Range relative to a Node.
*
* @param referenceNode Reference node.
*/
public setStartAfter(referenceNode: INode): void {
const sibling = referenceNode.nextSibling;
if (!sibling) {
throw new Error(
'Failed to set range start. "referenceNode" does not have any nodes after itself.'
);
}
this.setStart(sibling);
}
/**
* Sets the start position of a Range relative to another Node.
*
* @param referenceNode Reference node.
*/
public setStartBefore(referenceNode: INode): void {
const sibling = referenceNode.previousSibling;
if (!sibling) {
throw new Error(
'Failed to set range start. "referenceNode" does not have any nodes before itself.'
);
}
this.setStart(sibling);
}
/**
* Moves content of the Range into a new node, placing the new node at the start of the specified range.
*
* @param _newParent New parent.
*/
public surroundContents(_newParent: INode): void {
// TODO: Implement
}
/**
* Returns the text of the Range.
*/
public toString(): string {
return '';
}
}