/
Range.ts
291 lines (260 loc) · 7.59 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
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 = RangeHowEnum.endToEnd;
public static readonly END_TO_START = RangeHowEnum.endToStart;
public static readonly START_TO_END = RangeHowEnum.startToEnd;
public static readonly START_TO_START = RangeHowEnum.startToStart;
public readonly END_TO_END = RangeHowEnum.endToEnd;
public readonly END_TO_START = RangeHowEnum.endToStart;
public readonly START_TO_END = RangeHowEnum.startToEnd;
public readonly START_TO_START = 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 startAncestors = [];
const endAncestors = [];
let parent = this.startContainer;
while (parent !== null) {
startAncestors.push(parent);
parent = parent.parentNode;
}
parent = this.endContainer;
while (parent !== null) {
endAncestors.push(parent);
parent = parent.parentNode;
}
for (const ancestor of startAncestors) {
if (endAncestors.includes(ancestor)) {
return ancestor;
}
}
return (<typeof Range>this.constructor)._ownerDocument;
}
/**
* 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 {
// TODO: Implement
}
/**
* Sets the start position of a Range.
*
* @param _startNode Start node.
* @param _startOffset Start offset.
*/
public setStart(_startNode: INode, _startOffset = 0): void {
// TODO: Implement
}
/**
* Sets the end position of a Range relative to another Node.
*
* @param _referenceNode Reference node.
*/
public setEndAfter(_referenceNode: INode): void {
// TODO: Implement
}
/**
* Sets the end position of a Range relative to another Node.
*
* @param _referenceNode Reference node.
*/
public setEndBefore(_referenceNode: INode): void {
// TODO: Implement
}
/**
* Sets the start position of a Range relative to a Node.
*
* @param _referenceNode Reference node.
*/
public setStartAfter(_referenceNode: INode): void {
// TODO: Implement
}
/**
* Sets the start position of a Range relative to another Node.
*
* @param _referenceNode Reference node.
*/
public setStartBefore(_referenceNode: INode): void {
// TODO: Implement
}
/**
* 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 '';
}
}