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
/
renderer.js
766 lines (660 loc) · 24.2 KB
/
renderer.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
/**
* @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/**
* @module engine/view/renderer
*/
import ViewText from './text';
import ViewPosition from './position';
import { INLINE_FILLER, INLINE_FILLER_LENGTH, startsWithFiller, isInlineFiller, isBlockFiller } from './filler';
import mix from '@ckeditor/ckeditor5-utils/src/mix';
import diff from '@ckeditor/ckeditor5-utils/src/diff';
import insertAt from '@ckeditor/ckeditor5-utils/src/dom/insertat';
import remove from '@ckeditor/ckeditor5-utils/src/dom/remove';
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
import isText from '@ckeditor/ckeditor5-utils/src/dom/istext';
import fastDiff from '@ckeditor/ckeditor5-utils/src/fastdiff';
/**
* Renderer updates DOM structure and selection, to make them a reflection of the view structure and selection.
*
* View nodes which may need to be rendered needs to be {@link module:engine/view/renderer~Renderer#markToSync marked}.
* Then, on {@link module:engine/view/renderer~Renderer#render render}, renderer compares view nodes with DOM nodes
* in order to check which ones really need to be refreshed. Finally, it creates DOM nodes from these view nodes,
* {@link module:engine/view/domconverter~DomConverter#bindElements binds} them and inserts into the DOM tree.
*
* Every time {@link module:engine/view/renderer~Renderer#render render} is called, renderer additionally checks if
* {@link module:engine/view/renderer~Renderer#selection selection} needs update and updates it if so.
*
* Renderer uses {@link module:engine/view/domconverter~DomConverter} to transform and bind nodes.
*/
export default class Renderer {
/**
* Creates a renderer instance.
*
* @param {module:engine/view/domconverter~DomConverter} domConverter Converter instance.
* @param {module:engine/view/documentselection~DocumentSelection} selection View selection.
*/
constructor( domConverter, selection ) {
/**
* Set of DOM Documents instances.
*
* @readonly
* @member {Set.<Document>}
*/
this.domDocuments = new Set();
/**
* Converter instance.
*
* @readonly
* @member {module:engine/view/domconverter~DomConverter}
*/
this.domConverter = domConverter;
/**
* Set of nodes which attributes changed and may need to be rendered.
*
* @readonly
* @member {Set.<module:engine/view/node~Node>}
*/
this.markedAttributes = new Set();
/**
* Set of elements which child lists changed and may need to be rendered.
*
* @readonly
* @member {Set.<module:engine/view/node~Node>}
*/
this.markedChildren = new Set();
/**
* Set of text nodes which text data changed and may need to be rendered.
*
* @readonly
* @member {Set.<module:engine/view/node~Node>}
*/
this.markedTexts = new Set();
/**
* View selection. Renderer updates DOM selection based on the view selection.
*
* @readonly
* @member {module:engine/view/documentselection~DocumentSelection}
*/
this.selection = selection;
/**
* The text node in which the inline filler was rendered.
*
* @private
* @member {Text}
*/
this._inlineFiller = null;
/**
* Indicates if the view document is focused and selection can be rendered. Selection will not be rendered if
* this is set to `false`.
*
* @member {Boolean}
*/
this.isFocused = false;
/**
* DOM element containing fake selection.
*
* @private
* @type {null|HTMLElement}
*/
this._fakeSelectionContainer = null;
}
/**
* Mark node to be synchronized.
*
* Note that only view nodes which parents have corresponding DOM elements need to be marked to be synchronized.
*
* @see #markedAttributes
* @see #markedChildren
* @see #markedTexts
*
* @param {module:engine/view/document~ChangeType} type Type of the change.
* @param {module:engine/view/node~Node} node Node to be marked.
*/
markToSync( type, node ) {
if ( type === 'text' ) {
if ( this.domConverter.mapViewToDom( node.parent ) ) {
this.markedTexts.add( node );
}
} else {
// If the node has no DOM element it is not rendered yet,
// its children/attributes do not need to be marked to be sync.
if ( !this.domConverter.mapViewToDom( node ) ) {
return;
}
if ( type === 'attributes' ) {
this.markedAttributes.add( node );
} else if ( type === 'children' ) {
this.markedChildren.add( node );
} else {
/**
* Unknown type passed to Renderer.markToSync.
*
* @error renderer-unknown-type
*/
throw new CKEditorError( 'view-renderer-unknown-type: Unknown type passed to Renderer.markToSync.' );
}
}
}
/**
* Render method checks {@link #markedAttributes},
* {@link #markedChildren} and {@link #markedTexts} and updates all
* nodes which need to be updated. Then it clears all three sets. Also, every time render is called it compares and
* if needed updates the selection.
*
* Renderer tries not to break text composition (e.g. IME) and x-index of the selection,
* so it does as little as it is needed to update the DOM.
*
* For attributes it adds new attributes to DOM elements, updates values and removes
* attributes which do not exist in the view element.
*
* For text nodes it updates the text string if it is different. Note that if parent element is marked as an element
* which changed child list, text node update will not be done, because it may not be possible to
* {@link module:engine/view/domconverter~DomConverter#findCorrespondingDomText find a corresponding DOM text}.
* The change will be handled in the parent element.
*
* For elements, which child lists have changed, it calculates a {@link module:utils/diff~diff} and adds or removes children which have
* changed.
*
* Rendering also handles {@link module:engine/view/filler fillers}. Especially, it checks if the inline filler is needed
* at selection position and adds or removes it. To prevent breaking text composition inline filler will not be
* removed as long selection is in the text node which needed it at first.
*/
render() {
let inlineFillerPosition;
// There was inline filler rendered in the DOM but it's not
// at the selection position any more, so we can remove it
// (cause even if it's needed, it must be placed in another location).
if ( this._inlineFiller && !this._isSelectionInInlineFiller() ) {
this._removeInlineFiller();
}
// If we've got the filler, let's try to guess its position in the view.
if ( this._inlineFiller ) {
inlineFillerPosition = this._getInlineFillerPosition();
}
// Otherwise, if it's needed, create it at the selection position.
else if ( this._needsInlineFillerAtSelection() ) {
inlineFillerPosition = this.selection.getFirstPosition();
// Do not use `markToSync` so it will be added even if the parent is already added.
this.markedChildren.add( inlineFillerPosition.parent );
}
for ( const element of this.markedAttributes ) {
this._updateAttrs( element );
}
for ( const element of this.markedChildren ) {
this._updateChildren( element, { inlineFillerPosition } );
}
for ( const node of this.markedTexts ) {
if ( !this.markedChildren.has( node.parent ) && this.domConverter.mapViewToDom( node.parent ) ) {
this._updateText( node, { inlineFillerPosition } );
}
}
// Check whether the inline filler is required and where it really is in the DOM.
// At this point in most cases it will be in the DOM, but there are exceptions.
// For example, if the inline filler was deep in the created DOM structure, it will not be created.
// Similarly, if it was removed at the beginning of this function and then neither text nor children were updated,
// it will not be present.
// Fix those and similar scenarios.
if ( inlineFillerPosition ) {
const fillerDomPosition = this.domConverter.viewPositionToDom( inlineFillerPosition );
const domDocument = fillerDomPosition.parent.ownerDocument;
if ( !startsWithFiller( fillerDomPosition.parent ) ) {
// Filler has not been created at filler position. Create it now.
this._inlineFiller = this._addInlineFiller( domDocument, fillerDomPosition.parent, fillerDomPosition.offset );
} else {
// Filler has been found, save it.
this._inlineFiller = fillerDomPosition.parent;
}
} else {
// There is no filler needed.
this._inlineFiller = null;
}
this._updateSelection();
this._updateFocus();
this.markedTexts.clear();
this.markedAttributes.clear();
this.markedChildren.clear();
}
/**
* Adds inline filler at given position.
*
* The position can be given as an array of DOM nodes and an offset in that array,
* or a DOM parent element and offset in that element.
*
* @private
* @param {Document} domDocument
* @param {Element|Array.<Node>} domParentOrArray
* @param {Number} offset
* @returns {Text} The DOM text node that contains inline filler.
*/
_addInlineFiller( domDocument, domParentOrArray, offset ) {
const childNodes = domParentOrArray instanceof Array ? domParentOrArray : domParentOrArray.childNodes;
const nodeAfterFiller = childNodes[ offset ];
if ( isText( nodeAfterFiller ) ) {
nodeAfterFiller.data = INLINE_FILLER + nodeAfterFiller.data;
return nodeAfterFiller;
} else {
const fillerNode = domDocument.createTextNode( INLINE_FILLER );
if ( Array.isArray( domParentOrArray ) ) {
childNodes.splice( offset, 0, fillerNode );
} else {
insertAt( domParentOrArray, offset, fillerNode );
}
return fillerNode;
}
}
/**
* Gets the position of the inline filler based on the current selection.
* Here, we assume that we know that the filler is needed and
* {@link #_isSelectionInInlineFiller is at the selection position}, and, since it's needed,
* it's somewhere at the selection postion.
*
* Note: we cannot restore the filler position based on the filler's DOM text node, because
* when this method is called (before rendering) the bindings will often be broken. View to DOM
* bindings are only dependable after rendering.
*
* @private
* @returns {module:engine/view/position~Position}
*/
_getInlineFillerPosition() {
const firstPos = this.selection.getFirstPosition();
if ( firstPos.parent.is( 'text' ) ) {
return ViewPosition.createBefore( this.selection.getFirstPosition().parent );
} else {
return firstPos;
}
}
/**
* Returns `true` if the selection hasn't left the inline filler's text node.
* If it is `true` it means that the filler had been added for a reason and the selection does not
* left the filler's text node. E.g. the user can be in the middle of a composition so it should not be touched.
*
* @private
* @returns {Boolean} True if the inline filler and selection are in the same place.
*/
_isSelectionInInlineFiller() {
if ( this.selection.rangeCount != 1 || !this.selection.isCollapsed ) {
return false;
}
// Note, we can't check if selection's position equals position of the
// this._inlineFiller node, because of #663. We may not be able to calculate
// the filler's position in the view at this stage.
// Instead, we check it the other way – whether selection is anchored in
// that text node or next to it.
// Possible options are:
// "FILLER{}"
// "FILLERadded-text{}"
const selectionPosition = this.selection.getFirstPosition();
const position = this.domConverter.viewPositionToDom( selectionPosition );
if ( position && isText( position.parent ) && startsWithFiller( position.parent ) ) {
return true;
}
return false;
}
/**
* Removes the inline filler.
*
* @private
*/
_removeInlineFiller() {
const domFillerNode = this._inlineFiller;
// Something weird happened and the stored node doesn't contain the filler's text.
if ( !startsWithFiller( domFillerNode ) ) {
/**
* The inline filler node was lost. Most likely, something overwrote the filler text node
* in the DOM.
*
* @error view-renderer-filler-was-lost
*/
throw new CKEditorError( 'view-renderer-filler-was-lost: The inline filler node was lost.' );
}
if ( isInlineFiller( domFillerNode ) ) {
domFillerNode.parentNode.removeChild( domFillerNode );
} else {
domFillerNode.data = domFillerNode.data.substr( INLINE_FILLER_LENGTH );
}
this._inlineFiller = null;
}
/**
* Checks if the inline {@link module:engine/view/filler filler} should be added.
*
* @private
* @returns {Boolean} True if the inline fillers should be added.
*/
_needsInlineFillerAtSelection() {
if ( this.selection.rangeCount != 1 || !this.selection.isCollapsed ) {
return false;
}
const selectionPosition = this.selection.getFirstPosition();
const selectionParent = selectionPosition.parent;
const selectionOffset = selectionPosition.offset;
// If there is no DOM root we do not care about fillers.
if ( !this.domConverter.mapViewToDom( selectionParent.root ) ) {
return false;
}
if ( !( selectionParent.is( 'element' ) ) ) {
return false;
}
// Prevent adding inline filler inside elements with contenteditable=false.
// https://github.com/ckeditor/ckeditor5-engine/issues/1170
if ( !_isEditable( selectionParent ) ) {
return false;
}
// We have block filler, we do not need inline one.
if ( selectionOffset === selectionParent.getFillerOffset() ) {
return false;
}
const nodeBefore = selectionPosition.nodeBefore;
const nodeAfter = selectionPosition.nodeAfter;
if ( nodeBefore instanceof ViewText || nodeAfter instanceof ViewText ) {
return false;
}
return true;
}
/**
* Checks if text needs to be updated and possibly updates it.
*
* @private
* @param {module:engine/view/text~Text} viewText View text to update.
* @param {Object} options
* @param {module:engine/view/position~Position} options.inlineFillerPosition The position on which the inline
* filler should be rendered.
*/
_updateText( viewText, options ) {
const domText = this.domConverter.findCorrespondingDomText( viewText );
const newDomText = this.domConverter.viewToDom( viewText, domText.ownerDocument );
const actualText = domText.data;
let expectedText = newDomText.data;
const filler = options.inlineFillerPosition;
if ( filler && filler.parent == viewText.parent && filler.offset == viewText.index ) {
expectedText = INLINE_FILLER + expectedText;
}
if ( actualText != expectedText ) {
const actions = fastDiff( actualText, expectedText );
for ( const action of actions ) {
if ( action.type === 'insert' ) {
domText.insertData( action.index, action.values.join( '' ) );
} else { // 'delete'
domText.deleteData( action.index, action.howMany );
}
}
}
}
/**
* Checks if attributes list needs to be updated and possibly updates it.
*
* @private
* @param {module:engine/view/element~Element} viewElement View element to update.
*/
_updateAttrs( viewElement ) {
const domElement = this.domConverter.mapViewToDom( viewElement );
const domAttrKeys = Array.from( domElement.attributes ).map( attr => attr.name );
const viewAttrKeys = viewElement.getAttributeKeys();
// Add or overwrite attributes.
for ( const key of viewAttrKeys ) {
domElement.setAttribute( key, viewElement.getAttribute( key ) );
}
// Remove from DOM attributes which do not exists in the view.
for ( const key of domAttrKeys ) {
if ( !viewElement.hasAttribute( key ) ) {
domElement.removeAttribute( key );
}
}
}
/**
* Checks if elements child list needs to be updated and possibly updates it.
*
* @private
* @param {module:engine/view/element~Element} viewElement View element to update.
* @param {Object} options
* @param {module:engine/view/position~Position} options.inlineFillerPosition The position on which the inline
* filler should be rendered.
*/
_updateChildren( viewElement, options ) {
const domConverter = this.domConverter;
const domElement = domConverter.mapViewToDom( viewElement );
if ( !domElement ) {
// If there is no `domElement` it means that it was already removed from DOM.
// There is no need to update it. It will be updated when re-inserted.
return;
}
const domDocument = domElement.ownerDocument;
const filler = options.inlineFillerPosition;
const actualDomChildren = domElement.childNodes;
const expectedDomChildren = Array.from( domConverter.viewChildrenToDom( viewElement, domDocument, { bind: true } ) );
// Inline filler element has to be created during children update because we need it to diff actual dom
// elements with expected dom elements. We need inline filler in expected dom elements so we won't re-render
// text node if it is not necessary.
if ( filler && filler.parent == viewElement ) {
this._addInlineFiller( domDocument, expectedDomChildren, filler.offset );
}
const actions = diff( actualDomChildren, expectedDomChildren, sameNodes );
let i = 0;
const nodesToUnbind = new Set();
for ( const action of actions ) {
if ( action === 'insert' ) {
insertAt( domElement, i, expectedDomChildren[ i ] );
i++;
} else if ( action === 'delete' ) {
nodesToUnbind.add( actualDomChildren[ i ] );
remove( actualDomChildren[ i ] );
} else { // 'equal'
// Force updating text nodes inside elements which did not change and do not need to be re-rendered (#1125).
this._markDescendantTextToSync( domConverter.domToView( expectedDomChildren[ i ] ) );
i++;
}
}
// Unbind removed nodes. When node does not have a parent it means that it was removed from DOM tree during
// comparision with the expected DOM. We don't need to check child nodes, because if child node was reinserted,
// it was moved to DOM tree out of the removed node.
for ( const node of nodesToUnbind ) {
if ( !node.parentNode ) {
this.domConverter.unbindDomElement( node );
}
}
function sameNodes( actualDomChild, expectedDomChild ) {
// Elements.
if ( actualDomChild === expectedDomChild ) {
return true;
}
// Texts.
else if ( isText( actualDomChild ) && isText( expectedDomChild ) ) {
return actualDomChild.data === expectedDomChild.data;
}
// Block fillers.
else if ( isBlockFiller( actualDomChild, domConverter.blockFiller ) &&
isBlockFiller( expectedDomChild, domConverter.blockFiller ) ) {
return true;
}
// Not matching types.
return false;
}
}
/**
* Marks text nodes to be synced.
*
* If a text node is passed, it will be marked. If an element is passed, all descendant text nodes inside it will be marked.
*
* @private
* @param {module:engine/view/node~Node} viewNode View node to sync.
*/
_markDescendantTextToSync( viewNode ) {
if ( !viewNode ) {
return;
}
if ( viewNode.is( 'text' ) ) {
this.markedTexts.add( viewNode );
} else if ( viewNode.is( 'element' ) ) {
for ( const child of viewNode.getChildren() ) {
this._markDescendantTextToSync( child );
}
}
}
/**
* Checks if selection needs to be updated and possibly updates it.
*
* @private
*/
_updateSelection() {
// If there is no selection - remove DOM and fake selections.
if ( this.selection.rangeCount === 0 ) {
this._removeDomSelection();
this._removeFakeSelection();
return;
}
const domRoot = this.domConverter.mapViewToDom( this.selection.editableElement );
// Do nothing if there is no focus, or there is no DOM element corresponding to selection's editable element.
if ( !this.isFocused || !domRoot ) {
return;
}
// Render selection.
if ( this.selection.isFake ) {
this._updateFakeSelection( domRoot );
} else {
this._removeFakeSelection();
this._updateDomSelection( domRoot );
}
}
/**
* Updates fake selection.
*
* @private
* @param {HTMLElement} domRoot Valid DOM root where fake selection container should be added.
*/
_updateFakeSelection( domRoot ) {
const domDocument = domRoot.ownerDocument;
let container = this._fakeSelectionContainer;
// Create fake selection container if one does not exist.
if ( !container ) {
this._fakeSelectionContainer = container = domDocument.createElement( 'div' );
Object.assign( container.style, {
position: 'fixed',
top: 0,
left: '-9999px',
// See https://github.com/ckeditor/ckeditor5/issues/752.
width: '42px'
} );
// Fill it with a text node so we can update it later.
container.appendChild( domDocument.createTextNode( '\u00A0' ) );
}
// Add fake container if not already added.
if ( !container.parentElement ) {
domRoot.appendChild( container );
}
// Update contents.
container.firstChild.data = this.selection.fakeSelectionLabel || '\u00A0';
// Update selection.
const domSelection = domDocument.getSelection();
const domRange = domDocument.createRange();
domSelection.removeAllRanges();
domRange.selectNodeContents( container );
domSelection.addRange( domRange );
// Bind fake selection container with current selection.
this.domConverter.bindFakeSelection( container, this.selection );
}
/**
* Updates DOM selection.
*
* @private
* @param {HTMLElement} domRoot Valid DOM root where DOM selection should be rendered.
*/
_updateDomSelection( domRoot ) {
const domSelection = domRoot.ownerDocument.defaultView.getSelection();
// Let's check whether DOM selection needs updating at all.
if ( !this._domSelectionNeedsUpdate( domSelection ) ) {
return;
}
// Multi-range selection is not available in most browsers, and, at least in Chrome, trying to
// set such selection, that is not continuous, throws an error. Because of that, we will just use anchor
// and focus of view selection.
// Since we are not supporting multi-range selection, we also do not need to check if proper editable is
// selected. If there is any editable selected, it is okay (editable is taken from selection anchor).
const anchor = this.domConverter.viewPositionToDom( this.selection.anchor );
const focus = this.domConverter.viewPositionToDom( this.selection.focus );
// Focus the new editing host.
// Otherwise, FF may throw an error (https://github.com/ckeditor/ckeditor5/issues/721).
domRoot.focus();
domSelection.collapse( anchor.parent, anchor.offset );
domSelection.extend( focus.parent, focus.offset );
}
/**
* Checks whether given DOM selection needs to be updated.
*
* @private
* @param {Selection} domSelection DOM selection to check.
* @returns {Boolean}
*/
_domSelectionNeedsUpdate( domSelection ) {
if ( !this.domConverter.isDomSelectionCorrect( domSelection ) ) {
// Current DOM selection is in incorrect position. We need to update it.
return true;
}
const oldViewSelection = domSelection && this.domConverter.domSelectionToView( domSelection );
if ( oldViewSelection && this.selection.isEqual( oldViewSelection ) ) {
return false;
}
// If selection is not collapsed, it does not need to be updated if it is similar.
if ( !this.selection.isCollapsed && this.selection.isSimilar( oldViewSelection ) ) {
// Selection did not changed and is correct, do not update.
return false;
}
// Selections are not similar.
return true;
}
/**
* Removes DOM selection.
*
* @private
*/
_removeDomSelection() {
for ( const doc of this.domDocuments ) {
const domSelection = doc.getSelection();
if ( domSelection.rangeCount ) {
const activeDomElement = doc.activeElement;
const viewElement = this.domConverter.mapDomToView( activeDomElement );
if ( activeDomElement && viewElement ) {
doc.getSelection().removeAllRanges();
}
}
}
}
/**
* Removes fake selection.
*
* @private
*/
_removeFakeSelection() {
const container = this._fakeSelectionContainer;
if ( container ) {
container.remove();
}
}
/**
* Checks if focus needs to be updated and possibly updates it.
*
* @private
*/
_updateFocus() {
if ( this.isFocused ) {
const editable = this.selection.editableElement;
if ( editable ) {
this.domConverter.focus( editable );
}
}
}
}
mix( Renderer, ObservableMixin );
// Checks if provided element is editable.
//
// @private
// @param {module:engine/view/element~Element} element
// @returns {Boolean}
function _isEditable( element ) {
if ( element.getAttribute( 'contenteditable' ) == 'false' ) {
return false;
}
const parent = element.findAncestor( element => element.hasAttribute( 'contenteditable' ) );
return !parent || parent.getAttribute( 'contenteditable' ) == 'true';
}