/
typecheckable.ts
505 lines (478 loc) · 17.3 KB
/
typecheckable.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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
/**
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import type AttributeElement from './attributeelement.js';
import type ContainerElement from './containerelement.js';
import type DocumentFragment from './documentfragment.js';
import type DocumentSelection from './documentselection.js';
import type EditableElement from './editableelement.js';
import type Element from './element.js';
import type EmptyElement from './emptyelement.js';
import type Node from './node.js';
import type Position from './position.js';
import type Range from './range.js';
import type RawElement from './rawelement.js';
import type RootEditableElement from './rooteditableelement.js';
import type Selection from './selection.js';
import type Text from './text.js';
import type TextProxy from './textproxy.js';
import type UIElement from './uielement.js';
/**
* @module engine/view/typecheckable
*/
export default abstract class TypeCheckable {
/**
* Checks whether this object is of type {@link module:engine/view/node~Node} or its subclass.
*
* This method is useful when processing view objects that are of unknown type. For example, a function
* may return a {@link module:engine/view/documentfragment~DocumentFragment} or a {@link module:engine/view/node~Node}
* that can be either a text node or an element. This method can be used to check what kind of object is returned.
*
* ```ts
* someObject.is( 'element' ); // -> true if this is an element
* someObject.is( 'node' ); // -> true if this is a node (a text node or an element)
* someObject.is( 'documentFragment' ); // -> true if this is a document fragment
* ```
*
* Since this method is also available on a range of model objects, you can prefix the type of the object with
* `model:` or `view:` to check, for example, if this is the model's or view's element:
*
* ```ts
* viewElement.is( 'view:element' ); // -> true
* viewElement.is( 'model:element' ); // -> false
* ```
*
* By using this method it is also possible to check a name of an element:
*
* ```ts
* imgElement.is( 'element', 'img' ); // -> true
* imgElement.is( 'view:element', 'img' ); // -> same as above, but more precise
* ```
* @label NODE
*/
public is( type: 'node' | 'view:node' ): this is (
Node |
Text |
Element |
AttributeElement |
ContainerElement |
EditableElement |
EmptyElement |
RawElement |
RootEditableElement |
UIElement
);
/**
* Checks whether this object is of type {@link module:engine/view/element~Element} or its subclass.
*
* ```ts
* element.is( 'element' ); // -> true
* element.is( 'node' ); // -> true
* element.is( 'view:element' ); // -> true
* element.is( 'view:node' ); // -> true
*
* element.is( 'model:element' ); // -> false
* element.is( 'documentSelection' ); // -> false
* ```
*
* Assuming that the object being checked is an element, you can also check its
* {@link module:engine/view/element~Element#name name}:
*
* ```ts
* element.is( 'element', 'img' ); // -> true if this is an <img> element
* text.is( 'element', 'img' ); -> false
* ```
*
* @label ELEMENT
*/
public is( type: 'element' | 'view:element' ): this is (
Element |
AttributeElement |
ContainerElement |
EditableElement |
EmptyElement |
RawElement |
RootEditableElement |
UIElement
);
/**
* Checks whether this object is of type {@link module:engine/view/attributeelement~AttributeElement}.
*
* ```ts
* attributeElement.is( 'attributeElement' ); // -> true
* attributeElement.is( 'element' ); // -> true
* attributeElement.is( 'node' ); // -> true
* attributeElement.is( 'view:attributeElement' ); // -> true
* attributeElement.is( 'view:element' ); // -> true
* attributeElement.is( 'view:node' ); // -> true
*
* attributeElement.is( 'model:element' ); // -> false
* attributeElement.is( 'documentFragment' ); // -> false
* ```
*
* Assuming that the object being checked is an attribute element, you can also check its
* {@link module:engine/view/attributeelement~AttributeElement#name name}:
*
* ```ts
* attributeElement.is( 'element', 'b' ); // -> true if this is a bold element
* attributeElement.is( 'attributeElement', 'b' ); // -> same as above
* text.is( 'element', 'b' ); -> false
* ```
*
* @label ATTRIBUTE_ELEMENT
*/
public is( type: 'attributeElement' | 'view:attributeElement' ): this is AttributeElement;
/**
* Checks whether this object is of type {@link module:engine/view/containerelement~ContainerElement} or its subclass.
*
* ```ts
* containerElement.is( 'containerElement' ); // -> true
* containerElement.is( 'element' ); // -> true
* containerElement.is( 'node' ); // -> true
* containerElement.is( 'view:containerElement' ); // -> true
* containerElement.is( 'view:element' ); // -> true
* containerElement.is( 'view:node' ); // -> true
*
* containerElement.is( 'model:element' ); // -> false
* containerElement.is( 'documentFragment' ); // -> false
* ```
*
* Assuming that the object being checked is a container element, you can also check its
* {@link module:engine/view/containerelement~ContainerElement#name name}:
*
* ```ts
* containerElement.is( 'element', 'div' ); // -> true if this is a div container element
* containerElement.is( 'contaienrElement', 'div' ); // -> same as above
* text.is( 'element', 'div' ); -> false
* ```
*
* @label CONTAINER_ELEMENT
*/
public is( type: 'containerElement' | 'view:containerElement' ): this is ContainerElement | EditableElement | RootEditableElement;
/**
* Checks whether this object is of type {@link module:engine/view/editableelement~EditableElement} or its subclass.
*
* ```ts
* editableElement.is( 'editableElement' ); // -> true
* editableElement.is( 'element' ); // -> true
* editableElement.is( 'node' ); // -> true
* editableElement.is( 'view:editableElement' ); // -> true
* editableElement.is( 'view:element' ); // -> true
* editableElement.is( 'view:node' ); // -> true
*
* editableElement.is( 'model:element' ); // -> false
* editableElement.is( 'documentFragment' ); // -> false
* ```
*
* Assuming that the object being checked is an editbale element, you can also check its
* {@link module:engine/view/editableelement~EditableElement#name name}:
*
* ```ts
* editableElement.is( 'element', 'div' ); // -> true if this is a div element
* editableElement.is( 'editableElement', 'div' ); // -> same as above
* text.is( 'element', 'div' ); -> false
* ```
*
* @label EDITABLE_ELEMENT
*/
public is( type: 'editableElement' | 'view:editableElement' ): this is EditableElement | RootEditableElement;
/**
* Checks whether this object is of type {@link module:engine/view/emptyelement~EmptyElement}.
*
* ```ts
* emptyElement.is( 'emptyElement' ); // -> true
* emptyElement.is( 'element' ); // -> true
* emptyElement.is( 'node' ); // -> true
* emptyElement.is( 'view:emptyElement' ); // -> true
* emptyElement.is( 'view:element' ); // -> true
* emptyElement.is( 'view:node' ); // -> true
*
* emptyElement.is( 'model:element' ); // -> false
* emptyElement.is( 'documentFragment' ); // -> false
* ```
*
* Assuming that the object being checked is an empty element, you can also check its
* {@link module:engine/view/emptyelement~EmptyElement#name name}:
*
* ```ts
* emptyElement.is( 'element', 'img' ); // -> true if this is a img element
* emptyElement.is( 'emptyElement', 'img' ); // -> same as above
* text.is( 'element', 'img' ); -> false
* ```
*
* @label EMPTY_ELEMENT
*/
public is( type: 'emptyElement' | 'view:emptyElement' ): this is EmptyElement;
/**
* Checks whether this object is of type {@link module:engine/view/rawelement~RawElement}.
*
* ```ts
* rawElement.is( 'rawElement' ); // -> true
* rawElement.is( 'element' ); // -> true
* rawElement.is( 'node' ); // -> true
* rawElement.is( 'view:rawElement' ); // -> true
* rawElement.is( 'view:element' ); // -> true
* rawElement.is( 'view:node' ); // -> true
*
* rawElement.is( 'model:element' ); // -> false
* rawElement.is( 'documentFragment' ); // -> false
* ```
*
* Assuming that the object being checked is a raw element, you can also check its
* {@link module:engine/view/rawelement~RawElement#name name}:
*
* ```ts
* rawElement.is( 'img' ); // -> true if this is an img element
* rawElement.is( 'rawElement', 'img' ); // -> same as above
* text.is( 'img' ); -> false
* ```
*
* @label RAW_ELEMENT
*/
public is( type: 'rawElement' | 'view:rawElement' ): this is RawElement;
/**
* Checks whether this object is of type {@link module:engine/view/rooteditableelement~RootEditableElement}.
*
* ```ts
* rootEditableElement.is( 'rootElement' ); // -> true
* rootEditableElement.is( 'editableElement' ); // -> true
* rootEditableElement.is( 'element' ); // -> true
* rootEditableElement.is( 'node' ); // -> true
* rootEditableElement.is( 'view:editableElement' ); // -> true
* rootEditableElement.is( 'view:element' ); // -> true
* rootEditableElement.is( 'view:node' ); // -> true
*
* rootEditableElement.is( 'model:element' ); // -> false
* rootEditableElement.is( 'documentFragment' ); // -> false
* ```
*
* Assuming that the object being checked is a root editable element, you can also check its
* {@link module:engine/view/rooteditableelement~RootEditableElement#name name}:
*
* ```ts
* rootEditableElement.is( 'element', 'div' ); // -> true if this is a div root editable element
* rootEditableElement.is( 'rootElement', 'div' ); // -> same as above
* text.is( 'element', 'div' ); -> false
* ```
*
* @label ROOT_ELEMENT
*/
public is( type: 'rootElement' | 'view:rootElement' ): this is RootEditableElement;
/**
* Checks whether this object is of type {@link module:engine/view/uielement~UIElement}.
*
* ```ts
* uiElement.is( 'uiElement' ); // -> true
* uiElement.is( 'element' ); // -> true
* uiElement.is( 'node' ); // -> true
* uiElement.is( 'view:uiElement' ); // -> true
* uiElement.is( 'view:element' ); // -> true
* uiElement.is( 'view:node' ); // -> true
*
* uiElement.is( 'model:element' ); // -> false
* uiElement.is( 'documentFragment' ); // -> false
* ```
*
* Assuming that the object being checked is an ui element, you can also check its
* {@link module:engine/view/uielement~UIElement#name name}:
*
* ```ts
* uiElement.is( 'element', 'span' ); // -> true if this is a span ui element
* uiElement.is( 'uiElement', 'span' ); // -> same as above
* text.is( 'element', 'span' ); -> false
* ```
*
* @label UI_ELEMENT
*/
public is( type: 'uiElement' | 'view:uiElement' ): this is UIElement;
/**
* Checks whether this object is of type {@link module:engine/view/text~Text}.
*
* ```ts
* text.is( '$text' ); // -> true
* text.is( 'node' ); // -> true
* text.is( 'view:$text' ); // -> true
* text.is( 'view:node' ); // -> true
*
* text.is( 'model:$text' ); // -> false
* text.is( 'element' ); // -> false
* text.is( 'range' ); // -> false
* ```
*
* @label TEXT
*/
public is( type: '$text' | 'view:$text' ): this is Text;
/**
* hecks whether this object is of type {@link module:engine/view/documentfragment~DocumentFragment}.
*
* ```ts
* docFrag.is( 'documentFragment' ); // -> true
* docFrag.is( 'view:documentFragment' ); // -> true
*
* docFrag.is( 'model:documentFragment' ); // -> false
* docFrag.is( 'element' ); // -> false
* docFrag.is( 'node' ); // -> false
* ```
*
* @label DOCUMENT_FRAGMENT
*/
public is( type: 'documentFragment' | 'view:documentFragment' ): this is DocumentFragment;
/**
* Checks whether this object is of type {@link module:engine/view/textproxy~TextProxy}.
*
* ```ts
* textProxy.is( '$textProxy' ); // -> true
* textProxy.is( 'view:$textProxy' ); // -> true
*
* textProxy.is( 'model:$textProxy' ); // -> false
* textProxy.is( 'element' ); // -> false
* textProxy.is( 'range' ); // -> false
* ```
*
* **Note:** Until version 20.0.0 this method wasn't accepting `'$textProxy'` type. The legacy `'textProxy'` type is still
* accepted for backward compatibility.
*
* @label TEXT_PROXY
*/
public is( type: '$textProxy' | 'view:$textProxy' ): this is TextProxy;
/**
* Checks whether this object is of type {@link module:engine/view/position~Position}.
*
* ```ts
* position.is( 'position' ); // -> true
* position.is( 'view:position' ); // -> true
*
* position.is( 'model:position' ); // -> false
* position.is( 'element' ); // -> false
* position.is( 'range' ); // -> false
* ```
*
* @label POSITION
*/
public is( type: 'position' | 'view:position' ): this is Position;
/**
* Checks whether this object is of type {@link module:engine/view/range~Range}.
*
* ```ts
* range.is( 'range' ); // -> true
* range.is( 'view:range' ); // -> true
*
* range.is( 'model:range' ); // -> false
* range.is( 'element' ); // -> false
* range.is( 'selection' ); // -> false
* ```
*
* @label RANGE
*/
public is( type: 'range' | 'view:range' ): this is Range;
/**
* Checks whether this object is of type {@link module:engine/view/selection~Selection} or
* {@link module:engine/view/documentselection~DocumentSelection}.
*
* ```ts
* selection.is( 'selection' ); // -> true
* selection.is( 'view:selection' ); // -> true
*
* selection.is( 'model:selection' ); // -> false
* selection.is( 'element' ); // -> false
* selection.is( 'range' ); // -> false
* ```
*
* @label SELECTION
*/
public is( type: 'selection' | 'view:selection' ): this is Selection | DocumentSelection;
/**
* Checks whether this object is of type {@link module:engine/view/documentselection~DocumentSelection}.
*
* ```ts
* `docSelection.is( 'selection' ); // -> true
* docSelection.is( 'documentSelection' ); // -> true
* docSelection.is( 'view:selection' ); // -> true
* docSelection.is( 'view:documentSelection' ); // -> true
*
* docSelection.is( 'model:documentSelection' ); // -> false
* docSelection.is( 'element' ); // -> false
* docSelection.is( 'node' ); // -> false
* ```
*
* @label DOCUMENT_SELECTION
*/
public is( type: 'documentSelection' | 'view:documentSelection' ): this is DocumentSelection;
/**
* Checks whether the object is of type {@link module:engine/view/element~Element} or its subclass and has the specified `name`.
*
* @label ELEMENT_NAME
*/
public is<N extends string>( type: 'element' | 'view:element', name: N ): this is (
Element |
AttributeElement |
ContainerElement |
EditableElement |
EmptyElement |
RawElement |
RootEditableElement |
UIElement
) & { name: N };
/**
* Checks whether the object is of type {@link module:engine/view/attributeelement~AttributeElement} and has the specified `name`.
*
* @label ATTRIBUTE_ELEMENT_NAME
*/
public is<N extends string>( type: 'attributeElement' | 'view:attributeElement', name: N ): this is AttributeElement & { name: N };
/**
* Checks whether the object is of type {@link module:engine/view/containerelement~ContainerElement}
* or its subclass and has the specified `name`.
*
* @label CONTAINER_ELEMENT_NAME
*/
public is<N extends string>( type: 'containerElement' | 'view:containerElement', name: N ): this is (
ContainerElement |
EditableElement |
RootEditableElement
) & { name: N };
/**
* Checks whether the object is of type {@link module:engine/view/editableelement~EditableElement}
* or its subclass and has the specified `name`.
*
* @label EDITABLE_ELEMENT_NAME
*/
public is<N extends string>( type: 'editableElement' | 'view:editableElement', name: N ): this is (
EditableElement |
RootEditableElement
) & { name: N };
/**
* Checks whether the object is of type {@link module:engine/view/emptyelement~EmptyElement} has the specified `name`.
*
* @label EMPTY_ELEMENT_NAME
*/
public is<N extends string>( type: 'emptyElement' | 'view:emptyElement', name: N ): this is EmptyElement & { name: N };
/**
* Checks whether the object is of type {@link module:engine/view/rawelement~RawElement} and has the specified `name`.
*
* @label RAW_ELEMENT_NAME
*/
public is<N extends string>( type: 'rawElement' | 'view:rawElement', name: N ): this is RawElement & { name: N };
/**
* Checks whether the object is of type {@link module:engine/view/rooteditableelement~RootEditableElement} and has the specified `name`.
*
* @label ROOT_ELEMENT_NAME
*/
public is<N extends string>( type: 'rootElement' | 'view:rootElement', name: N ): this is RootEditableElement & { name: N };
/**
* Checks whether the object is of type {@link module:engine/view/uielement~UIElement} and has the specified `name`.
*
* @label UI_ELEMENT_NAME
*/
public is<N extends string>( type: 'uiElement' | 'view:uiElement', name: N ): this is UIElement & { name: N };
/* istanbul ignore next -- @preserve */
public is(): boolean {
// There are a lot of overloads above.
// Overriding method in derived classes remove them and only `is( type: string ): boolean` is visible which we don't want.
// One option would be to copy them all to all classes, but that's ugly.
// It's best when TypeScript compiler doesn't see those overloads, except the one in the top base class.
// To overload a method, but not let the compiler see it, do after class definition:
// `MyClass.prototype.is = function( type: string ) {...}`
throw new Error( 'is() method is abstract' );
}
}