This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 40
/
mapper.js
619 lines (552 loc) · 23.3 KB
/
mapper.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
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module engine/conversion/mapper
*/
import ModelPosition from '../model/position';
import ModelRange from '../model/range';
import ViewPosition from '../view/position';
import ViewRange from '../view/range';
import ViewText from '../view/text';
import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
/**
* Maps elements, positions and markers between {@link module:engine/view/document~Document the view} and
* {@link module:engine/model/model the model}.
*
* Mapper use bound elements to find corresponding elements and positions, so, to get proper results,
* all model elements should be {@link module:engine/conversion/mapper~Mapper#bindElements bound}.
*
* To map complex model to/from view relations, you may provide custom callbacks for
* {@link module:engine/conversion/mapper~Mapper#event:modelToViewPosition modelToViewPosition event} and
* {@link module:engine/conversion/mapper~Mapper#event:viewToModelPosition viewToModelPosition event} that are fired whenever
* a position mapping request occurs.
* Those events are fired by {@link module:engine/conversion/mapper~Mapper#toViewPosition toViewPosition}
* and {@link module:engine/conversion/mapper~Mapper#toModelPosition toModelPosition} methods. `Mapper` adds it's own default callbacks
* with `'lowest'` priority. To override default `Mapper` mapping, add custom callback with higher priority and
* stop the event.
*/
export default class Mapper {
/**
* Creates an instance of the mapper.
*/
constructor() {
/**
* Model element to view element mapping.
*
* @private
* @member {WeakMap}
*/
this._modelToViewMapping = new WeakMap();
/**
* View element to model element mapping.
*
* @private
* @member {WeakMap}
*/
this._viewToModelMapping = new WeakMap();
/**
* A map containing callbacks between view element names and functions evaluating length of view elements
* in model.
*
* @private
* @member {Map}
*/
this._viewToModelLengthCallbacks = new Map();
/**
* Model marker name to view elements mapping.
*
* Keys are `String`s while values are `Set`s with {@link module:engine/view/element~Element view elements}.
* One marker (name) can be mapped to multiple elements.
*
* @private
* @member {Map}
*/
this._markerNameToElements = new Map();
// Default mapper algorithm for mapping model position to view position.
this.on( 'modelToViewPosition', ( evt, data ) => {
if ( data.viewPosition ) {
return;
}
const viewContainer = this._modelToViewMapping.get( data.modelPosition.parent );
data.viewPosition = this._findPositionIn( viewContainer, data.modelPosition.offset );
}, { priority: 'low' } );
// Default mapper algorithm for mapping view position to model position.
this.on( 'viewToModelPosition', ( evt, data ) => {
if ( data.modelPosition ) {
return;
}
let viewBlock = data.viewPosition.parent;
let modelParent = this._viewToModelMapping.get( viewBlock );
while ( !modelParent ) {
viewBlock = viewBlock.parent;
modelParent = this._viewToModelMapping.get( viewBlock );
}
const modelOffset = this._toModelOffset( data.viewPosition.parent, data.viewPosition.offset, viewBlock );
data.modelPosition = ModelPosition._createAt( modelParent, modelOffset );
}, { priority: 'low' } );
}
/**
* Marks model and view elements as corresponding. Corresponding elements can be retrieved by using
* the {@link module:engine/conversion/mapper~Mapper#toModelElement toModelElement} and
* {@link module:engine/conversion/mapper~Mapper#toViewElement toViewElement} methods.
* The information that elements are bound is also used to translate positions.
*
* @param {module:engine/model/element~Element} modelElement Model element.
* @param {module:engine/view/element~Element} viewElement View element.
*/
bindElements( modelElement, viewElement ) {
this._modelToViewMapping.set( modelElement, viewElement );
this._viewToModelMapping.set( viewElement, modelElement );
}
/**
* Unbinds given {@link module:engine/view/element~Element view element} from the map.
*
* **Note:** view-to-model binding will be removed, if it existed. However, corresponding model-to-view binding
* will be removed only if model element is still bound to passed `viewElement`.
*
* This behavior lets for re-binding model element to another view element without fear of losing the new binding
* when the previously bound view element is unbound.
*
* @param {module:engine/view/element~Element} viewElement View element to unbind.
*/
unbindViewElement( viewElement ) {
const modelElement = this.toModelElement( viewElement );
this._viewToModelMapping.delete( viewElement );
if ( this._modelToViewMapping.get( modelElement ) == viewElement ) {
this._modelToViewMapping.delete( modelElement );
}
}
/**
* Unbinds given {@link module:engine/model/element~Element model element} from the map.
*
* **Note:** model-to-view binding will be removed, if it existed. However, corresponding view-to-model binding
* will be removed only if view element is still bound to passed `modelElement`.
*
* This behavior lets for re-binding view element to another model element without fear of losing the new binding
* when the previously bound model element is unbound.
*
* @param {module:engine/model/element~Element} modelElement Model element to unbind.
*/
unbindModelElement( modelElement ) {
const viewElement = this.toViewElement( modelElement );
this._modelToViewMapping.delete( modelElement );
if ( this._viewToModelMapping.get( viewElement ) == modelElement ) {
this._viewToModelMapping.delete( viewElement );
}
}
/**
* Binds given marker name with given {@link module:engine/view/element~Element view element}. The element
* will be added to the current set of elements bound with given marker name.
*
* @param {module:engine/view/element~Element} element Element to bind.
* @param {String} name Marker name.
*/
bindElementToMarker( element, name ) {
const elements = this._markerNameToElements.get( name ) || new Set();
elements.add( element );
this._markerNameToElements.set( name, elements );
}
/**
* Unbinds all elements from given marker name.
*
* @param {String} name Marker name.
*/
unbindElementsFromMarkerName( name ) {
this._markerNameToElements.delete( name );
}
/**
* Removes all model to view and view to model bindings.
*/
clearBindings() {
this._modelToViewMapping = new WeakMap();
this._viewToModelMapping = new WeakMap();
this._markerNameToElements = new Map();
}
/**
* Gets the corresponding model element.
*
* **Note:** {@link module:engine/view/uielement~UIElement} does not have corresponding element in model.
*
* @param {module:engine/view/element~Element} viewElement View element.
* @returns {module:engine/model/element~Element|undefined} Corresponding model element or `undefined` if not found.
*/
toModelElement( viewElement ) {
return this._viewToModelMapping.get( viewElement );
}
/**
* Gets the corresponding view element.
*
* @param {module:engine/model/element~Element} modelElement Model element.
* @returns {module:engine/view/element~Element|undefined} Corresponding view element or `undefined` if not found.
*/
toViewElement( modelElement ) {
return this._modelToViewMapping.get( modelElement );
}
/**
* Gets the corresponding model range.
*
* @param {module:engine/view/range~Range} viewRange View range.
* @returns {module:engine/model/range~Range} Corresponding model range.
*/
toModelRange( viewRange ) {
return new ModelRange( this.toModelPosition( viewRange.start ), this.toModelPosition( viewRange.end ) );
}
/**
* Gets the corresponding view range.
*
* @param {module:engine/model/range~Range} modelRange Model range.
* @returns {module:engine/view/range~Range} Corresponding view range.
*/
toViewRange( modelRange ) {
return new ViewRange( this.toViewPosition( modelRange.start ), this.toViewPosition( modelRange.end ) );
}
/**
* Gets the corresponding model position.
*
* @fires viewToModelPosition
* @param {module:engine/view/position~Position} viewPosition View position.
* @returns {module:engine/model/position~Position} Corresponding model position.
*/
toModelPosition( viewPosition ) {
const data = {
viewPosition,
mapper: this
};
this.fire( 'viewToModelPosition', data );
return data.modelPosition;
}
/**
* Gets the corresponding view position.
*
* @fires modelToViewPosition
* @param {module:engine/model/position~Position} modelPosition Model position.
* @param {Object} [options] Additional options for position mapping process.
* @param {Boolean} [options.isPhantom=false] Should be set to `true` if the model position to map is pointing to a place
* in model tree which no longer exists. For example, it could be an end of a removed model range.
* @returns {module:engine/view/position~Position} Corresponding view position.
*/
toViewPosition( modelPosition, options = { isPhantom: false } ) {
const data = {
modelPosition,
mapper: this,
isPhantom: options.isPhantom
};
this.fire( 'modelToViewPosition', data );
return data.viewPosition;
}
/**
* Gets all view elements bound to the given marker name.
*
* @param {String} name Marker name.
* @returns {Set.<module:engine/view/element~Element>|null} View elements bound with given marker name or `null`
* if no elements are bound to given marker name.
*/
markerNameToElements( name ) {
const boundElements = this._markerNameToElements.get( name );
if ( !boundElements ) {
return null;
}
const elements = new Set();
for ( const element of boundElements ) {
if ( element.is( 'attributeElement' ) ) {
for ( const clone of element.getElementsWithSameId() ) {
elements.add( clone );
}
} else {
elements.add( element );
}
}
return elements;
}
/**
* Registers a callback that evaluates the length in the model of a view element with given name.
*
* The callback is fired with one argument, which is a view element instance. The callback is expected to return
* a number representing the length of view element in model.
*
* // List item in view may contain nested list, which have other list items. In model though,
* // the lists are represented by flat structure. Because of those differences, length of list view element
* // may be greater than one. In the callback it's checked how many nested list items are in evaluated list item.
*
* function getViewListItemLength( element ) {
* let length = 1;
*
* for ( let child of element.getChildren() ) {
* if ( child.name == 'ul' || child.name == 'ol' ) {
* for ( let item of child.getChildren() ) {
* length += getViewListItemLength( item );
* }
* }
* }
*
* return length;
* }
*
* mapper.registerViewToModelLength( 'li', getViewListItemLength );
*
* @param {String} viewElementName Name of view element for which callback is registered.
* @param {Function} lengthCallback Function return a length of view element instance in model.
*/
registerViewToModelLength( viewElementName, lengthCallback ) {
this._viewToModelLengthCallbacks.set( viewElementName, lengthCallback );
}
/**
* Calculates model offset based on the view position and the block element.
*
* Example:
*
* <p>foo<b>ba|r</b></p> // _toModelOffset( b, 2, p ) -> 5
*
* Is a sum of:
*
* <p>foo|<b>bar</b></p> // _toModelOffset( p, 3, p ) -> 3
* <p>foo<b>ba|r</b></p> // _toModelOffset( b, 2, b ) -> 2
*
* @private
* @param {module:engine/view/element~Element} viewParent Position parent.
* @param {Number} viewOffset Position offset.
* @param {module:engine/view/element~Element} viewBlock Block used as a base to calculate offset.
* @returns {Number} Offset in the model.
*/
_toModelOffset( viewParent, viewOffset, viewBlock ) {
if ( viewBlock != viewParent ) {
// See example.
const offsetToParentStart = this._toModelOffset( viewParent.parent, viewParent.index, viewBlock );
const offsetInParent = this._toModelOffset( viewParent, viewOffset, viewParent );
return offsetToParentStart + offsetInParent;
}
// viewBlock == viewParent, so we need to calculate the offset in the parent element.
// If the position is a text it is simple ("ba|r" -> 2).
if ( viewParent.is( 'text' ) ) {
return viewOffset;
}
// If the position is in an element we need to sum lengths of siblings ( <b> bar </b> foo | -> 3 + 3 = 6 ).
let modelOffset = 0;
for ( let i = 0; i < viewOffset; i++ ) {
modelOffset += this.getModelLength( viewParent.getChild( i ) );
}
return modelOffset;
}
/**
* Gets the length of the view element in the model.
*
* The length is calculated as follows:
* * if {@link #registerViewToModelLength length mapping callback} is provided for given `viewNode` it is used to
* evaluate model length (`viewNode` is used as first and only parameter passed to the callback),
* * length of a {@link module:engine/view/text~Text text node} is equal to the length of it's
* {@link module:engine/view/text~Text#data data},
* * length of a {@link module:engine/view/uielement~UIElement ui element} is equal to 0,
* * length of a mapped {@link module:engine/view/element~Element element} is equal to 1,
* * length of a not-mapped {@link module:engine/view/element~Element element} is equal to the length of it's children.
*
* Examples:
*
* foo -> 3 // Text length is equal to it's data length.
* <p>foo</p> -> 1 // Length of an element which is mapped is by default equal to 1.
* <b>foo</b> -> 3 // Length of an element which is not mapped is a length of its children.
* <div><p>x</p><p>y</p></div> -> 2 // Assuming that <div> is not mapped and <p> are mapped.
*
* @param {module:engine/view/element~Element} viewNode View node.
* @returns {Number} Length of the node in the tree model.
*/
getModelLength( viewNode ) {
if ( this._viewToModelLengthCallbacks.get( viewNode.name ) ) {
const callback = this._viewToModelLengthCallbacks.get( viewNode.name );
return callback( viewNode );
} else if ( this._viewToModelMapping.has( viewNode ) ) {
return 1;
} else if ( viewNode.is( 'text' ) ) {
return viewNode.data.length;
} else if ( viewNode.is( 'uiElement' ) ) {
return 0;
} else {
let len = 0;
for ( const child of viewNode.getChildren() ) {
len += this.getModelLength( child );
}
return len;
}
}
/**
* Finds the position in the view node (or its children) with the expected model offset.
*
* Example:
*
* <p>fo<b>bar</b>bom</p> -> expected offset: 4
*
* _findPositionIn( p, 4 ):
* <p>|fo<b>bar</b>bom</p> -> expected offset: 4, actual offset: 0
* <p>fo|<b>bar</b>bom</p> -> expected offset: 4, actual offset: 2
* <p>fo<b>bar</b>|bom</p> -> expected offset: 4, actual offset: 5 -> we are too far
*
* _findPositionIn( b, 4 - ( 5 - 3 ) ):
* <p>fo<b>|bar</b>bom</p> -> expected offset: 2, actual offset: 0
* <p>fo<b>bar|</b>bom</p> -> expected offset: 2, actual offset: 3 -> we are too far
*
* _findPositionIn( bar, 2 - ( 3 - 3 ) ):
* We are in the text node so we can simple find the offset.
* <p>fo<b>ba|r</b>bom</p> -> expected offset: 2, actual offset: 2 -> position found
*
* @private
* @param {module:engine/view/element~Element} viewParent Tree view element in which we are looking for the position.
* @param {Number} expectedOffset Expected offset.
* @returns {module:engine/view/position~Position} Found position.
*/
_findPositionIn( viewParent, expectedOffset ) {
// Last scanned view node.
let viewNode;
// Length of the last scanned view node.
let lastLength = 0;
let modelOffset = 0;
let viewOffset = 0;
// In the text node it is simple: offset in the model equals offset in the text.
if ( viewParent.is( 'text' ) ) {
return new ViewPosition( viewParent, expectedOffset );
}
// In other cases we add lengths of child nodes to find the proper offset.
// If it is smaller we add the length.
while ( modelOffset < expectedOffset ) {
viewNode = viewParent.getChild( viewOffset );
lastLength = this.getModelLength( viewNode );
modelOffset += lastLength;
viewOffset++;
}
// If it equals we found the position.
if ( modelOffset == expectedOffset ) {
return this._moveViewPositionToTextNode( new ViewPosition( viewParent, viewOffset ) );
}
// If it is higher we need to enter last child.
else {
// ( modelOffset - lastLength ) is the offset to the child we enter,
// so we subtract it from the expected offset to fine the offset in the child.
return this._findPositionIn( viewNode, expectedOffset - ( modelOffset - lastLength ) );
}
}
/**
* Because we prefer positions in text nodes over positions next to text node moves view position to the text node
* if it was next to it.
*
* <p>[]<b>foo</b></p> -> <p>[]<b>foo</b></p> // do not touch if position is not directly next to text
* <p>foo[]<b>foo</b></p> -> <p>foo{}<b>foo</b></p> // move to text node
* <p><b>[]foo</b></p> -> <p><b>{}foo</b></p> // move to text node
*
* @private
* @param {module:engine/view/position~Position} viewPosition Position potentially next to text node.
* @returns {module:engine/view/position~Position} Position in text node if possible.
*/
_moveViewPositionToTextNode( viewPosition ) {
// If the position is just after text node, put it at the end of that text node.
// If the position is just before text node, put it at the beginning of that text node.
const nodeBefore = viewPosition.nodeBefore;
const nodeAfter = viewPosition.nodeAfter;
if ( nodeBefore instanceof ViewText ) {
return new ViewPosition( nodeBefore, nodeBefore.data.length );
} else if ( nodeAfter instanceof ViewText ) {
return new ViewPosition( nodeAfter, 0 );
}
// Otherwise, just return the given position.
return viewPosition;
}
/**
* Fired for each model-to-view position mapping request. The purpose of this event is to enable custom model-to-view position
* mapping. Callbacks added to this event take {@link module:engine/model/position~Position model position} and are expected to
* calculate {@link module:engine/view/position~Position view position}. Calculated view position should be added as `viewPosition`
* value in `data` object that is passed as one of parameters to the event callback.
*
* // Assume that "captionedImage" model element is converted to <img> and following <span> elements in view,
* // and the model element is bound to <img> element. Force mapping model positions inside "captionedImage" to that
* // <span> element.
* mapper.on( 'modelToViewPosition', ( evt, data ) => {
* const positionParent = modelPosition.parent;
*
* if ( positionParent.name == 'captionedImage' ) {
* const viewImg = data.mapper.toViewElement( positionParent );
* const viewCaption = viewImg.nextSibling; // The <span> element.
*
* data.viewPosition = new ViewPosition( viewCaption, modelPosition.offset );
*
* // Stop the event if other callbacks should not modify calculated value.
* evt.stop();
* }
* } );
*
* **Note:** keep in mind that sometimes a "phantom" model position is being converted. "Phantom" model position is
* a position that points to a non-existing place in model. Such position might still be valid for conversion, though
* (it would point to a correct place in view when converted). One example of such situation is when a range is
* removed from model, there may be a need to map the range's end (which is no longer valid model position). To
* handle such situation, check `data.isPhantom` flag:
*
* // Assume that there is "customElement" model element and whenever position is before it, we want to move it
* // to the inside of the view element bound to "customElement".
* mapper.on( 'modelToViewPosition', ( evt, data ) => {
* if ( data.isPhantom ) {
* return;
* }
*
* // Below line might crash for phantom position that does not exist in model.
* const sibling = data.modelPosition.nodeBefore;
*
* // Check if this is the element we are interested in.
* if ( !sibling.is( 'customElement' ) ) {
* return;
* }
*
* const viewElement = data.mapper.toViewElement( sibling );
*
* data.viewPosition = new ViewPosition( sibling, 0 );
*
* evt.stop();
* } );
*
* **Note:** default mapping callback is provided with `low` priority setting and does not cancel the event, so it is possible to
* attach a custom callback after default callback and also use `data.viewPosition` calculated by default callback
* (for example to fix it).
*
* **Note:** default mapping callback will not fire if `data.viewPosition` is already set.
*
* **Note:** these callbacks are called **very often**. For efficiency reasons, it is advised to use them only when position
* mapping between given model and view elements is unsolvable using just elements mapping and default algorithm. Also,
* the condition that checks if special case scenario happened should be as simple as possible.
*
* @event modelToViewPosition
* @param {Object} data Data pipeline object that can store and pass data between callbacks. The callback should add
* `viewPosition` value to that object with calculated {@link module:engine/view/position~Position view position}.
* @param {module:engine/conversion/mapper~Mapper} data.mapper Mapper instance that fired the event.
*/
/**
* Fired for each view-to-model position mapping request. See {@link module:engine/conversion/mapper~Mapper#event:modelToViewPosition}.
*
* // See example in `modelToViewPosition` event description.
* // This custom mapping will map positions from <span> element next to <img> to the "captionedImage" element.
* mapper.on( 'viewToModelPosition', ( evt, data ) => {
* const positionParent = viewPosition.parent;
*
* if ( positionParent.hasClass( 'image-caption' ) ) {
* const viewImg = positionParent.previousSibling;
* const modelImg = data.mapper.toModelElement( viewImg );
*
* data.modelPosition = new ModelPosition( modelImg, viewPosition.offset );
* evt.stop();
* }
* } );
*
* **Note:** default mapping callback is provided with `low` priority setting and does not cancel the event, so it is possible to
* attach a custom callback after default callback and also use `data.modelPosition` calculated by default callback
* (for example to fix it).
*
* **Note:** default mapping callback will not fire if `data.modelPosition` is already set.
*
* **Note:** these callbacks are called **very often**. For efficiency reasons, it is advised to use them only when position
* mapping between given model and view elements is unsolvable using just elements mapping and default algorithm. Also,
* the condition that checks if special case scenario happened should be as simple as possible.
*
* @event viewToModelPosition
* @param {Object} data Data pipeline object that can store and pass data between callbacks. The callback should add
* `modelPosition` value to that object with calculated {@link module:engine/model/position~Position model position}.
* @param {module:engine/conversion/mapper~Mapper} data.mapper Mapper instance that fired the event.
*/
}
mix( Mapper, EmitterMixin );