-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
writer.js
1572 lines (1388 loc) · 58.2 KB
/
writer.js
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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
/**
* @module engine/model/writer
*/
import AttributeOperation from './operation/attributeoperation';
import DetachOperation from './operation/detachoperation';
import InsertOperation from './operation/insertoperation';
import MarkerOperation from './operation/markeroperation';
import MoveOperation from './operation/moveoperation';
import RenameOperation from './operation/renameoperation';
import RootAttributeOperation from './operation/rootattributeoperation';
import SplitOperation from './operation/splitoperation';
import MergeOperation from './operation/mergeoperation';
import DocumentFragment from './documentfragment';
import Text from './text';
import Element from './element';
import RootElement from './rootelement';
import Position from './position';
import Range from './range.js';
import DocumentSelection from './documentselection';
import toMap from '@ckeditor/ckeditor5-utils/src/tomap';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
/**
* The model can only be modified by using the writer. It should be used whenever you want to create a node, modify
* child nodes, attributes or text, set the selection's position and its attributes.
*
* The instance of the writer is only available in the {@link module:engine/model/model~Model#change `change()`} or
* {@link module:engine/model/model~Model#enqueueChange `enqueueChange()`}.
*
* model.change( writer => {
* writer.insertText( 'foo', paragraph, 'end' );
* } );
*
* Note that the writer should never be stored and used outside of the `change()` and
* `enqueueChange()` blocks.
*
* Note that writer's methods do not check the {@link module:engine/model/schema~Schema}. It is possible
* to create incorrect model structures by using the writer. Read more about in
* {@glink framework/guides/deep-dive/schema#who-checks-the-schema "Who checks the schema?"}.
*
* @see module:engine/model/model~Model#change
* @see module:engine/model/model~Model#enqueueChange
*/
export default class Writer {
/**
* Creates a writer instance.
*
* **Note:** It is not recommended to use it directly. Use {@link module:engine/model/model~Model#change `Model#change()`} or
* {@link module:engine/model/model~Model#enqueueChange `Model#enqueueChange()`} instead.
*
* @protected
* @param {module:engine/model/model~Model} model
* @param {module:engine/model/batch~Batch} batch
*/
constructor( model, batch ) {
/**
* Instance of the model on which this writer operates.
*
* @readonly
* @type {module:engine/model/model~Model}
*/
this.model = model;
/**
* The batch to which this writer will add changes.
*
* @readonly
* @type {module:engine/model/batch~Batch}
*/
this.batch = batch;
}
/**
* Creates a new {@link module:engine/model/text~Text text node}.
*
* writer.createText( 'foo' );
* writer.createText( 'foo', { bold: true } );
*
* @param {String} data Text data.
* @param {Object} [attributes] Text attributes.
* @returns {module:engine/model/text~Text} Created text node.
*/
createText( data, attributes ) {
return new Text( data, attributes );
}
/**
* Creates a new {@link module:engine/model/element~Element element}.
*
* writer.createElement( 'paragraph' );
* writer.createElement( 'paragraph', { alignment: 'center' } );
*
* @param {String} name Name of the element.
* @param {Object} [attributes] Elements attributes.
* @returns {module:engine/model/element~Element} Created element.
*/
createElement( name, attributes ) {
return new Element( name, attributes );
}
/**
* Creates a new {@link module:engine/model/documentfragment~DocumentFragment document fragment}.
*
* @returns {module:engine/model/documentfragment~DocumentFragment} Created document fragment.
*/
createDocumentFragment() {
return new DocumentFragment();
}
/**
* Creates a copy of the element and returns it. Created element has the same name and attributes as the original element.
* If clone is deep, the original element's children are also cloned. If not, then empty element is returned.
*
* @param {module:engine/model/element~Element} element The element to clone.
* @param {Boolean} [deep=true] If set to `true` clones element and all its children recursively. When set to `false`,
* element will be cloned without any child.
*/
cloneElement( element, deep = true ) {
return element._clone( deep );
}
/**
* Inserts item on given position.
*
* const paragraph = writer.createElement( 'paragraph' );
* writer.insert( paragraph, position );
*
* Instead of using position you can use parent and offset:
*
* const text = writer.createText( 'foo' );
* writer.insert( text, paragraph, 5 );
*
* You can also use `end` instead of the offset to insert at the end:
*
* const text = writer.createText( 'foo' );
* writer.insert( text, paragraph, 'end' );
*
* Or insert before or after another element:
*
* const paragraph = writer.createElement( 'paragraph' );
* writer.insert( paragraph, anotherParagraph, 'after' );
*
* These parameters works the same way as {@link #createPositionAt `writer.createPositionAt()`}.
*
* Note that if the item already has parent it will be removed from the previous parent.
*
* Note that you cannot re-insert a node from a document to a different document or a document fragment. In this case,
* `model-writer-insert-forbidden-move` is thrown.
*
* If you want to move {@link module:engine/model/range~Range range} instead of an
* {@link module:engine/model/item~Item item} use {@link module:engine/model/writer~Writer#move `Writer#move()`}.
*
* **Note:** For a paste-like content insertion mechanism see
* {@link module:engine/model/model~Model#insertContent `model.insertContent()`}.
*
* @param {module:engine/model/item~Item|module:engine/model/documentfragment~DocumentFragment} item Item or document
* fragment to insert.
* @param {module:engine/model/item~Item|module:engine/model/position~Position} itemOrPosition
* @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
* second parameter is a {@link module:engine/model/item~Item model item}.
*/
insert( item, itemOrPosition, offset = 0 ) {
this._assertWriterUsedCorrectly();
if ( item instanceof Text && item.data == '' ) {
return;
}
const position = Position._createAt( itemOrPosition, offset );
// If item has a parent already.
if ( item.parent ) {
// We need to check if item is going to be inserted within the same document.
if ( isSameTree( item.root, position.root ) ) {
// If it's we just need to move it.
this.move( Range._createOn( item ), position );
return;
}
// If it isn't the same root.
else {
if ( item.root.document ) {
/**
* Cannot move a node from a document to a different tree.
* It is forbidden to move a node that was already in a document outside of it.
*
* @error model-writer-insert-forbidden-move
*/
throw new CKEditorError(
'model-writer-insert-forbidden-move',
this
);
} else {
// Move between two different document fragments or from document fragment to a document is possible.
// In that case, remove the item from it's original parent.
this.remove( item );
}
}
}
const version = position.root.document ? position.root.document.version : null;
const insert = new InsertOperation( position, item, version );
if ( item instanceof Text ) {
insert.shouldReceiveAttributes = true;
}
this.batch.addOperation( insert );
this.model.applyOperation( insert );
// When element is a DocumentFragment we need to move its markers to Document#markers.
if ( item instanceof DocumentFragment ) {
for ( const [ markerName, markerRange ] of item.markers ) {
// We need to migrate marker range from DocumentFragment to Document.
const rangeRootPosition = Position._createAt( markerRange.root, 0 );
const range = new Range(
markerRange.start._getCombined( rangeRootPosition, position ),
markerRange.end._getCombined( rangeRootPosition, position )
);
const options = { range, usingOperation: true, affectsData: true };
if ( this.model.markers.has( markerName ) ) {
this.updateMarker( markerName, options );
} else {
this.addMarker( markerName, options );
}
}
}
}
/**
* Creates and inserts text on given position. You can optionally set text attributes:
*
* writer.insertText( 'foo', position );
* writer.insertText( 'foo', { bold: true }, position );
*
* Instead of using position you can use parent and offset or define that text should be inserted at the end
* or before or after other node:
*
* // Inserts 'foo' in paragraph, at offset 5:
* writer.insertText( 'foo', paragraph, 5 );
* // Inserts 'foo' at the end of a paragraph:
* writer.insertText( 'foo', paragraph, 'end' );
* // Inserts 'foo' after an image:
* writer.insertText( 'foo', image, 'after' );
*
* These parameters work in the same way as {@link #createPositionAt `writer.createPositionAt()`}.
*
* @param {String} data Text data.
* @param {Object} [attributes] Text attributes.
* @param {module:engine/model/item~Item|module:engine/model/position~Position} itemOrPosition
* @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
* third parameter is a {@link module:engine/model/item~Item model item}.
*/
insertText( text, attributes, itemOrPosition, offset ) {
if ( attributes instanceof DocumentFragment || attributes instanceof Element || attributes instanceof Position ) {
this.insert( this.createText( text ), attributes, itemOrPosition );
} else {
this.insert( this.createText( text, attributes ), itemOrPosition, offset );
}
}
/**
* Creates and inserts element on given position. You can optionally set attributes:
*
* writer.insertElement( 'paragraph', position );
* writer.insertElement( 'paragraph', { alignment: 'center' }, position );
*
* Instead of using position you can use parent and offset or define that text should be inserted at the end
* or before or after other node:
*
* // Inserts paragraph in the root at offset 5:
* writer.insertElement( 'paragraph', root, 5 );
* // Inserts paragraph at the end of a blockquote:
* writer.insertElement( 'paragraph', blockquote, 'end' );
* // Inserts after an image:
* writer.insertElement( 'paragraph', image, 'after' );
*
* These parameters works the same way as {@link #createPositionAt `writer.createPositionAt()`}.
*
* @param {String} name Name of the element.
* @param {Object} [attributes] Elements attributes.
* @param {module:engine/model/item~Item|module:engine/model/position~Position} itemOrPosition
* @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
* third parameter is a {@link module:engine/model/item~Item model item}.
*/
insertElement( name, attributes, itemOrPosition, offset ) {
if ( attributes instanceof DocumentFragment || attributes instanceof Element || attributes instanceof Position ) {
this.insert( this.createElement( name ), attributes, itemOrPosition );
} else {
this.insert( this.createElement( name, attributes ), itemOrPosition, offset );
}
}
/**
* Inserts item at the end of the given parent.
*
* const paragraph = writer.createElement( 'paragraph' );
* writer.append( paragraph, root );
*
* Note that if the item already has parent it will be removed from the previous parent.
*
* If you want to move {@link module:engine/model/range~Range range} instead of an
* {@link module:engine/model/item~Item item} use {@link module:engine/model/writer~Writer#move `Writer#move()`}.
*
* @param {module:engine/model/item~Item|module:engine/model/documentfragment~DocumentFragment}
* item Item or document fragment to insert.
* @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} parent
*/
append( item, parent ) {
this.insert( item, parent, 'end' );
}
/**
* Creates text node and inserts it at the end of the parent. You can optionally set text attributes:
*
* writer.appendText( 'foo', paragraph );
* writer.appendText( 'foo', { bold: true }, paragraph );
*
* @param {String} text Text data.
* @param {Object} [attributes] Text attributes.
* @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} parent
*/
appendText( text, attributes, parent ) {
if ( attributes instanceof DocumentFragment || attributes instanceof Element ) {
this.insert( this.createText( text ), attributes, 'end' );
} else {
this.insert( this.createText( text, attributes ), parent, 'end' );
}
}
/**
* Creates element and inserts it at the end of the parent. You can optionally set attributes:
*
* writer.appendElement( 'paragraph', root );
* writer.appendElement( 'paragraph', { alignment: 'center' }, root );
*
* @param {String} name Name of the element.
* @param {Object} [attributes] Elements attributes.
* @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} parent
*/
appendElement( name, attributes, parent ) {
if ( attributes instanceof DocumentFragment || attributes instanceof Element ) {
this.insert( this.createElement( name ), attributes, 'end' );
} else {
this.insert( this.createElement( name, attributes ), parent, 'end' );
}
}
/**
* Sets value of the attribute with given key on a {@link module:engine/model/item~Item model item}
* or on a {@link module:engine/model/range~Range range}.
*
* @param {String} key Attribute key.
* @param {*} value Attribute new value.
* @param {module:engine/model/item~Item|module:engine/model/range~Range} itemOrRange
* Model item or range on which the attribute will be set.
*/
setAttribute( key, value, itemOrRange ) {
this._assertWriterUsedCorrectly();
if ( itemOrRange instanceof Range ) {
const ranges = itemOrRange.getMinimalFlatRanges();
for ( const range of ranges ) {
setAttributeOnRange( this, key, value, range );
}
} else {
setAttributeOnItem( this, key, value, itemOrRange );
}
}
/**
* Sets values of attributes on a {@link module:engine/model/item~Item model item}
* or on a {@link module:engine/model/range~Range range}.
*
* writer.setAttributes( {
* bold: true,
* italic: true
* }, range );
*
* @param {Object} attributes Attributes keys and values.
* @param {module:engine/model/item~Item|module:engine/model/range~Range} itemOrRange
* Model item or range on which the attributes will be set.
*/
setAttributes( attributes, itemOrRange ) {
for ( const [ key, val ] of toMap( attributes ) ) {
this.setAttribute( key, val, itemOrRange );
}
}
/**
* Removes an attribute with given key from a {@link module:engine/model/item~Item model item}
* or from a {@link module:engine/model/range~Range range}.
*
* @param {String} key Attribute key.
* @param {module:engine/model/item~Item|module:engine/model/range~Range} itemOrRange
* Model item or range from which the attribute will be removed.
*/
removeAttribute( key, itemOrRange ) {
this._assertWriterUsedCorrectly();
if ( itemOrRange instanceof Range ) {
const ranges = itemOrRange.getMinimalFlatRanges();
for ( const range of ranges ) {
setAttributeOnRange( this, key, null, range );
}
} else {
setAttributeOnItem( this, key, null, itemOrRange );
}
}
/**
* Removes all attributes from all elements in the range or from the given item.
*
* @param {module:engine/model/item~Item|module:engine/model/range~Range} itemOrRange
* Model item or range from which all attributes will be removed.
*/
clearAttributes( itemOrRange ) {
this._assertWriterUsedCorrectly();
const removeAttributesFromItem = item => {
for ( const attribute of item.getAttributeKeys() ) {
this.removeAttribute( attribute, item );
}
};
if ( !( itemOrRange instanceof Range ) ) {
removeAttributesFromItem( itemOrRange );
} else {
for ( const item of itemOrRange.getItems() ) {
removeAttributesFromItem( item );
}
}
}
/**
* Moves all items in the source range to the target position.
*
* writer.move( sourceRange, targetPosition );
*
* Instead of the target position you can use parent and offset or define that range should be moved to the end
* or before or after chosen item:
*
* // Moves all items in the range to the paragraph at offset 5:
* writer.move( sourceRange, paragraph, 5 );
* // Moves all items in the range to the end of a blockquote:
* writer.move( sourceRange, blockquote, 'end' );
* // Moves all items in the range to a position after an image:
* writer.move( sourceRange, image, 'after' );
*
* These parameters works the same way as {@link #createPositionAt `writer.createPositionAt()`}.
*
* Note that items can be moved only within the same tree. It means that you can move items within the same root
* (element or document fragment) or between {@link module:engine/model/document~Document#roots documents roots},
* but you can not move items from document fragment to the document or from one detached element to another. Use
* {@link module:engine/model/writer~Writer#insert} in such cases.
*
* @param {module:engine/model/range~Range} range Source range.
* @param {module:engine/model/item~Item|module:engine/model/position~Position} itemOrPosition
* @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
* second parameter is a {@link module:engine/model/item~Item model item}.
*/
move( range, itemOrPosition, offset ) {
this._assertWriterUsedCorrectly();
if ( !( range instanceof Range ) ) {
/**
* Invalid range to move.
*
* @error writer-move-invalid-range
*/
throw new CKEditorError( 'writer-move-invalid-range', this );
}
if ( !range.isFlat ) {
/**
* Range to move is not flat.
*
* @error writer-move-range-not-flat
*/
throw new CKEditorError( 'writer-move-range-not-flat', this );
}
const position = Position._createAt( itemOrPosition, offset );
// Do not move anything if the move target is same as moved range start.
if ( position.isEqual( range.start ) ) {
return;
}
// If part of the marker is removed, create additional marker operation for undo purposes.
this._addOperationForAffectedMarkers( 'move', range );
if ( !isSameTree( range.root, position.root ) ) {
/**
* Range is going to be moved within not the same document. Please use
* {@link module:engine/model/writer~Writer#insert insert} instead.
*
* @error writer-move-different-document
*/
throw new CKEditorError( 'writer-move-different-document', this );
}
const version = range.root.document ? range.root.document.version : null;
const operation = new MoveOperation( range.start, range.end.offset - range.start.offset, position, version );
this.batch.addOperation( operation );
this.model.applyOperation( operation );
}
/**
* Removes given model {@link module:engine/model/item~Item item} or {@link module:engine/model/range~Range range}.
*
* @param {module:engine/model/item~Item|module:engine/model/range~Range} itemOrRange Model item or range to remove.
*/
remove( itemOrRange ) {
this._assertWriterUsedCorrectly();
const rangeToRemove = itemOrRange instanceof Range ? itemOrRange : Range._createOn( itemOrRange );
const ranges = rangeToRemove.getMinimalFlatRanges().reverse();
for ( const flat of ranges ) {
// If part of the marker is removed, create additional marker operation for undo purposes.
this._addOperationForAffectedMarkers( 'move', flat );
applyRemoveOperation( flat.start, flat.end.offset - flat.start.offset, this.batch, this.model );
}
}
/**
* Merges two siblings at the given position.
*
* Node before and after the position have to be an element. Otherwise `writer-merge-no-element-before` or
* `writer-merge-no-element-after` error will be thrown.
*
* @param {module:engine/model/position~Position} position Position between merged elements.
*/
merge( position ) {
this._assertWriterUsedCorrectly();
const nodeBefore = position.nodeBefore;
const nodeAfter = position.nodeAfter;
// If part of the marker is removed, create additional marker operation for undo purposes.
this._addOperationForAffectedMarkers( 'merge', position );
if ( !( nodeBefore instanceof Element ) ) {
/**
* Node before merge position must be an element.
*
* @error writer-merge-no-element-before
*/
throw new CKEditorError( 'writer-merge-no-element-before', this );
}
if ( !( nodeAfter instanceof Element ) ) {
/**
* Node after merge position must be an element.
*
* @error writer-merge-no-element-after
*/
throw new CKEditorError( 'writer-merge-no-element-after', this );
}
if ( !position.root.document ) {
this._mergeDetached( position );
} else {
this._merge( position );
}
}
/**
* Shortcut for {@link module:engine/model/model~Model#createPositionFromPath `Model#createPositionFromPath()`}.
*
* @param {module:engine/model/element~Element|module:engine/model/documentfragment~DocumentFragment} root Root of the position.
* @param {Array.<Number>} path Position path. See {@link module:engine/model/position~Position#path}.
* @param {module:engine/model/position~PositionStickiness} [stickiness='toNone'] Position stickiness.
* See {@link module:engine/model/position~PositionStickiness}.
* @returns {module:engine/model/position~Position}
*/
createPositionFromPath( root, path, stickiness ) {
return this.model.createPositionFromPath( root, path, stickiness );
}
/**
* Shortcut for {@link module:engine/model/model~Model#createPositionAt `Model#createPositionAt()`}.
*
* @param {module:engine/model/item~Item|module:engine/model/position~Position} itemOrPosition
* @param {Number|'end'|'before'|'after'} [offset] Offset or one of the flags. Used only when
* first parameter is a {@link module:engine/model/item~Item model item}.
* @returns {module:engine/model/position~Position}
*/
createPositionAt( itemOrPosition, offset ) {
return this.model.createPositionAt( itemOrPosition, offset );
}
/**
* Shortcut for {@link module:engine/model/model~Model#createPositionAfter `Model#createPositionAfter()`}.
*
* @param {module:engine/model/item~Item} item Item after which the position should be placed.
* @returns {module:engine/model/position~Position}
*/
createPositionAfter( item ) {
return this.model.createPositionAfter( item );
}
/**
* Shortcut for {@link module:engine/model/model~Model#createPositionBefore `Model#createPositionBefore()`}.
*
* @param {module:engine/model/item~Item} item Item after which the position should be placed.
* @returns {module:engine/model/position~Position}
*/
createPositionBefore( item ) {
return this.model.createPositionBefore( item );
}
/**
* Shortcut for {@link module:engine/model/model~Model#createRange `Model#createRange()`}.
*
* @param {module:engine/model/position~Position} start Start position.
* @param {module:engine/model/position~Position} [end] End position. If not set, range will be collapsed at `start` position.
* @returns {module:engine/model/range~Range}
*/
createRange( start, end ) {
return this.model.createRange( start, end );
}
/**
* Shortcut for {@link module:engine/model/model~Model#createRangeIn `Model#createRangeIn()`}.
*
* @param {module:engine/model/element~Element} element Element which is a parent for the range.
* @returns {module:engine/model/range~Range}
*/
createRangeIn( element ) {
return this.model.createRangeIn( element );
}
/**
* Shortcut for {@link module:engine/model/model~Model#createRangeOn `Model#createRangeOn()`}.
*
* @param {module:engine/model/element~Element} element Element which is a parent for the range.
* @returns {module:engine/model/range~Range}
*/
createRangeOn( element ) {
return this.model.createRangeOn( element );
}
/**
* Shortcut for {@link module:engine/model/model~Model#createSelection `Model#createSelection()`}.
*
* @param {module:engine/model/selection~Selectable} selectable
* @param {Number|'before'|'end'|'after'|'on'|'in'} [placeOrOffset] Sets place or offset of the selection.
* @param {Object} [options]
* @param {Boolean} [options.backward] Sets this selection instance to be backward.
* @returns {module:engine/model/selection~Selection}
*/
createSelection( selectable, placeOrOffset, options ) {
return this.model.createSelection( selectable, placeOrOffset, options );
}
/**
* Performs merge action in a detached tree.
*
* @private
* @param {module:engine/model/position~Position} position Position between merged elements.
*/
_mergeDetached( position ) {
const nodeBefore = position.nodeBefore;
const nodeAfter = position.nodeAfter;
this.move( Range._createIn( nodeAfter ), Position._createAt( nodeBefore, 'end' ) );
this.remove( nodeAfter );
}
/**
* Performs merge action in a non-detached tree.
*
* @private
* @param {module:engine/model/position~Position} position Position between merged elements.
*/
_merge( position ) {
const targetPosition = Position._createAt( position.nodeBefore, 'end' );
const sourcePosition = Position._createAt( position.nodeAfter, 0 );
const graveyard = position.root.document.graveyard;
const graveyardPosition = new Position( graveyard, [ 0 ] );
const version = position.root.document.version;
const merge = new MergeOperation( sourcePosition, position.nodeAfter.maxOffset, targetPosition, graveyardPosition, version );
this.batch.addOperation( merge );
this.model.applyOperation( merge );
}
/**
* Renames the given element.
*
* @param {module:engine/model/element~Element} element The element to rename.
* @param {String} newName New element name.
*/
rename( element, newName ) {
this._assertWriterUsedCorrectly();
if ( !( element instanceof Element ) ) {
/**
* Trying to rename an object which is not an instance of Element.
*
* @error writer-rename-not-element-instance
*/
throw new CKEditorError(
'writer-rename-not-element-instance',
this
);
}
const version = element.root.document ? element.root.document.version : null;
const renameOperation = new RenameOperation( Position._createBefore( element ), element.name, newName, version );
this.batch.addOperation( renameOperation );
this.model.applyOperation( renameOperation );
}
/**
* Splits elements starting from the given position and going to the top of the model tree as long as given
* `limitElement` is reached. When `limitElement` is not defined then only the parent of the given position will be split.
*
* The element needs to have a parent. It cannot be a root element nor a document fragment.
* The `writer-split-element-no-parent` error will be thrown if you try to split an element with no parent.
*
* @param {module:engine/model/position~Position} position Position of split.
* @param {module:engine/model/node~Node} [limitElement] Stop splitting when this element will be reached.
* @returns {Object} result Split result.
* @returns {module:engine/model/position~Position} result.position Position between split elements.
* @returns {module:engine/model/range~Range} result.range Range that stars from the end of the first split element and ends
* at the beginning of the first copy element.
*/
split( position, limitElement ) {
this._assertWriterUsedCorrectly();
let splitElement = position.parent;
if ( !splitElement.parent ) {
/**
* Element with no parent can not be split.
*
* @error writer-split-element-no-parent
*/
throw new CKEditorError( 'writer-split-element-no-parent', this );
}
// When limit element is not defined lets set splitElement parent as limit.
if ( !limitElement ) {
limitElement = splitElement.parent;
}
if ( !position.parent.getAncestors( { includeSelf: true } ).includes( limitElement ) ) {
/**
* Limit element is not a position ancestor.
*
* @error writer-split-invalid-limit-element
*/
throw new CKEditorError( 'writer-split-invalid-limit-element', this );
}
// We need to cache elements that will be created as a result of the first split because
// we need to create a range from the end of the first split element to the beginning of the
// first copy element. This should be handled by LiveRange but it doesn't work on detached nodes.
let firstSplitElement, firstCopyElement;
do {
const version = splitElement.root.document ? splitElement.root.document.version : null;
const howMany = splitElement.maxOffset - position.offset;
const split = new SplitOperation( position, howMany, null, version );
this.batch.addOperation( split );
this.model.applyOperation( split );
// Cache result of the first split.
if ( !firstSplitElement && !firstCopyElement ) {
firstSplitElement = splitElement;
firstCopyElement = position.parent.nextSibling;
}
position = this.createPositionAfter( position.parent );
splitElement = position.parent;
} while ( splitElement !== limitElement );
return {
position,
range: new Range( Position._createAt( firstSplitElement, 'end' ), Position._createAt( firstCopyElement, 0 ) )
};
}
/**
* Wraps the given range with the given element or with a new element (if a string was passed).
*
* **Note:** range to wrap should be a "flat range" (see {@link module:engine/model/range~Range#isFlat `Range#isFlat`}).
* If not, an error will be thrown.
*
* @param {module:engine/model/range~Range} range Range to wrap.
* @param {module:engine/model/element~Element|String} elementOrString Element or name of element to wrap the range with.
*/
wrap( range, elementOrString ) {
this._assertWriterUsedCorrectly();
if ( !range.isFlat ) {
/**
* Range to wrap is not flat.
*
* @error writer-wrap-range-not-flat
*/
throw new CKEditorError( 'writer-wrap-range-not-flat', this );
}
const element = elementOrString instanceof Element ? elementOrString : new Element( elementOrString );
if ( element.childCount > 0 ) {
/**
* Element to wrap with is not empty.
*
* @error writer-wrap-element-not-empty
*/
throw new CKEditorError( 'writer-wrap-element-not-empty', this );
}
if ( element.parent !== null ) {
/**
* Element to wrap with is already attached to a tree model.
*
* @error writer-wrap-element-attached
*/
throw new CKEditorError( 'writer-wrap-element-attached', this );
}
this.insert( element, range.start );
// Shift the range-to-wrap because we just inserted an element before that range.
const shiftedRange = new Range( range.start.getShiftedBy( 1 ), range.end.getShiftedBy( 1 ) );
this.move( shiftedRange, Position._createAt( element, 0 ) );
}
/**
* Unwraps children of the given element – all its children are moved before it and then the element is removed.
* Throws error if you try to unwrap an element which does not have a parent.
*
* @param {module:engine/model/element~Element} element Element to unwrap.
*/
unwrap( element ) {
this._assertWriterUsedCorrectly();
if ( element.parent === null ) {
/**
* Trying to unwrap an element which has no parent.
*
* @error writer-unwrap-element-no-parent
*/
throw new CKEditorError( 'writer-unwrap-element-no-parent', this );
}
this.move( Range._createIn( element ), this.createPositionAfter( element ) );
this.remove( element );
}
/**
* Adds a {@link module:engine/model/markercollection~Marker marker}. Marker is a named range, which tracks
* changes in the document and updates its range automatically, when model tree changes.
*
* As the first parameter you can set marker name.
*
* The required `options.usingOperation` parameter lets you decide if the marker should be managed by operations or not. See
* {@link module:engine/model/markercollection~Marker marker class description} to learn about the difference between
* markers managed by operations and not-managed by operations.
*
* The `options.affectsData` parameter, which defaults to `false`, allows you to define if a marker affects the data. It should be
* `true` when the marker change changes the data returned by the
* {@link module:core/editor/utils/dataapimixin~DataApi#getData `editor.getData()`} method.
* When set to `true` it fires the {@link module:engine/model/document~Document#event:change:data `change:data`} event.
* When set to `false` it fires the {@link module:engine/model/document~Document#event:change `change`} event.
*
* Create marker directly base on marker's name:
*
* addMarker( markerName, { range, usingOperation: false } );
*
* Create marker using operation:
*
* addMarker( markerName, { range, usingOperation: true } );
*
* Create marker that affects the editor data:
*
* addMarker( markerName, { range, usingOperation: false, affectsData: true } );
*
* Note: For efficiency reasons, it's best to create and keep as little markers as possible.
*
* @see module:engine/model/markercollection~Marker
* @param {String} name Name of a marker to create - must be unique.
* @param {Object} options
* @param {Boolean} options.usingOperation Flag indicating that the marker should be added by MarkerOperation.
* See {@link module:engine/model/markercollection~Marker#managedUsingOperations}.
* @param {module:engine/model/range~Range} options.range Marker range.
* @param {Boolean} [options.affectsData=false] Flag indicating that the marker changes the editor data.
* @returns {module:engine/model/markercollection~Marker} Marker that was set.
*/
addMarker( name, options ) {
this._assertWriterUsedCorrectly();
if ( !options || typeof options.usingOperation != 'boolean' ) {
/**
* The `options.usingOperation` parameter is required when adding a new marker.
*
* @error writer-addmarker-no-usingoperation
*/
throw new CKEditorError( 'writer-addmarker-no-usingoperation', this );
}
const usingOperation = options.usingOperation;
const range = options.range;
const affectsData = options.affectsData === undefined ? false : options.affectsData;
if ( this.model.markers.has( name ) ) {
/**
* Marker with provided name already exists.
*
* @error writer-addmarker-marker-exists
*/
throw new CKEditorError( 'writer-addmarker-marker-exists', this );
}
if ( !range ) {
/**
* Range parameter is required when adding a new marker.
*
* @error writer-addmarker-no-range
*/
throw new CKEditorError( 'writer-addmarker-no-range', this );
}
if ( !usingOperation ) {
return this.model.markers._set( name, range, usingOperation, affectsData );
}
applyMarkerOperation( this, name, null, range, affectsData );
return this.model.markers.get( name );
}
/**
* Adds, updates or refreshes a {@link module:engine/model/markercollection~Marker marker}. Marker is a named range, which tracks
* changes in the document and updates its range automatically, when model tree changes. Still, it is possible to change the
* marker's range directly using this method.
*
* As the first parameter you can set marker name or instance. If none of them is provided, new marker, with a unique
* name is created and returned.
*
* As the second parameter you can set the new marker data or leave this parameter as empty which will just refresh
* the marker by triggering downcast conversion for it. Refreshing the marker is useful when you want to change
* the marker {@link module:engine/view/element~Element view element} without changing any marker data.
*
* let isCommentActive = false;
*
* model.conversion.markerToHighlight( {
* model: 'comment',
* view: data => {
* const classes = [ 'comment-marker' ];
*
* if ( isCommentActive ) {
* classes.push( 'comment-marker--active' );
* }
*
* return { classes };
* }
* } );
*
* // Change the property that indicates if marker is displayed as active or not.
* isCommentActive = true;
*
* // And refresh the marker to convert it with additional class.
* model.change( writer => writer.updateMarker( 'comment' ) );
*
* The `options.usingOperation` parameter lets you change if the marker should be managed by operations or not. See
* {@link module:engine/model/markercollection~Marker marker class description} to learn about the difference between
* markers managed by operations and not-managed by operations. It is possible to change this option for an existing marker.
*
* The `options.affectsData` parameter, which defaults to `false`, allows you to define if a marker affects the data. It should be
* `true` when the marker change changes the data returned by
* the {@link module:core/editor/utils/dataapimixin~DataApi#getData `editor.getData()`} method.