renderer.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /**
  6. * @module engine/view/renderer
  7. */
  8. import ViewText from './text';
  9. import ViewPosition from './position';
  10. import { INLINE_FILLER, INLINE_FILLER_LENGTH, startsWithFiller, isInlineFiller, isBlockFiller } from './filler';
  11. import mix from '@ckeditor/ckeditor5-utils/src/mix';
  12. import diff from '@ckeditor/ckeditor5-utils/src/diff';
  13. import insertAt from '@ckeditor/ckeditor5-utils/src/dom/insertat';
  14. import remove from '@ckeditor/ckeditor5-utils/src/dom/remove';
  15. import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
  16. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  17. /**
  18. * Renderer updates DOM structure and selection, to make them a reflection of the view structure and selection.
  19. *
  20. * View nodes which may need to be rendered needs to be {@link module:engine/view/renderer~Renderer#markToSync marked}.
  21. * Then, on {@link module:engine/view/renderer~Renderer#render render}, renderer compares view nodes with DOM nodes
  22. * in order to check which ones really need to be refreshed. Finally, it creates DOM nodes from these view nodes,
  23. * {@link module:engine/view/domconverter~DomConverter#bindElements binds} them and inserts into the DOM tree.
  24. *
  25. * Every time {@link module:engine/view/renderer~Renderer#render render} is called, renderer additionally checks if
  26. * {@link module:engine/view/renderer~Renderer#selection selection} needs update and updates it if so.
  27. *
  28. * Renderer uses {@link module:engine/view/domconverter~DomConverter} to transform and bind nodes.
  29. */
  30. export default class Renderer {
  31. /**
  32. * Creates a renderer instance.
  33. *
  34. * @param {module:engine/view/domconverter~DomConverter} domConverter Converter instance.
  35. * @param {module:engine/view/selection~Selection} selection View selection.
  36. */
  37. constructor( domConverter, selection ) {
  38. /**
  39. * Set of DOM Documents instances.
  40. *
  41. * @member {Set.<Document>}
  42. */
  43. this.domDocuments = new Set();
  44. /**
  45. * Converter instance.
  46. *
  47. * @readonly
  48. * @member {module:engine/view/domconverter~DomConverter}
  49. */
  50. this.domConverter = domConverter;
  51. /**
  52. * Set of nodes which attributes changed and may need to be rendered.
  53. *
  54. * @readonly
  55. * @member {Set.<module:engine/view/node~Node>}
  56. */
  57. this.markedAttributes = new Set();
  58. /**
  59. * Set of elements which child lists changed and may need to be rendered.
  60. *
  61. * @readonly
  62. * @member {Set.<module:engine/view/node~Node>}
  63. */
  64. this.markedChildren = new Set();
  65. /**
  66. * Set of text nodes which text data changed and may need to be rendered.
  67. *
  68. * @readonly
  69. * @member {Set.<module:engine/view/node~Node>}
  70. */
  71. this.markedTexts = new Set();
  72. /**
  73. * View selection. Renderer updates DOM selection based on the view selection.
  74. *
  75. * @readonly
  76. * @member {module:engine/view/selection~Selection}
  77. */
  78. this.selection = selection;
  79. /**
  80. * The text node in which the inline filler was rendered.
  81. *
  82. * @private
  83. * @member {Text}
  84. */
  85. this._inlineFiller = null;
  86. /**
  87. * Indicates if the view document is focused and selection can be rendered. Selection will not be rendered if
  88. * this is set to `false`.
  89. *
  90. * @member {Boolean}
  91. */
  92. this.isFocused = false;
  93. /**
  94. * DOM element containing fake selection.
  95. *
  96. * @private
  97. * @type {null|HTMLElement}
  98. */
  99. this._fakeSelectionContainer = null;
  100. }
  101. /**
  102. * Mark node to be synchronized.
  103. *
  104. * Note that only view nodes which parents have corresponding DOM elements need to be marked to be synchronized.
  105. *
  106. * @see #markedAttributes
  107. * @see #markedChildren
  108. * @see #markedTexts
  109. *
  110. * @param {module:engine/view/document~ChangeType} type Type of the change.
  111. * @param {module:engine/view/node~Node} node Node to be marked.
  112. */
  113. markToSync( type, node ) {
  114. if ( type === 'text' ) {
  115. if ( this.domConverter.mapViewToDom( node.parent ) ) {
  116. this.markedTexts.add( node );
  117. }
  118. } else {
  119. // If the node has no DOM element it is not rendered yet,
  120. // its children/attributes do not need to be marked to be sync.
  121. if ( !this.domConverter.mapViewToDom( node ) ) {
  122. return;
  123. }
  124. if ( type === 'attributes' ) {
  125. this.markedAttributes.add( node );
  126. } else if ( type === 'children' ) {
  127. this.markedChildren.add( node );
  128. } else {
  129. /**
  130. * Unknown type passed to Renderer.markToSync.
  131. *
  132. * @error renderer-unknown-type
  133. */
  134. throw new CKEditorError( 'view-renderer-unknown-type: Unknown type passed to Renderer.markToSync.' );
  135. }
  136. }
  137. }
  138. /**
  139. * Render method checks {@link #markedAttributes},
  140. * {@link #markedChildren} and {@link #markedTexts} and updates all
  141. * nodes which need to be updated. Then it clears all three sets. Also, every time render is called it compares and
  142. * if needed updates the selection.
  143. *
  144. * Renderer tries not to break text composition (e.g. IME) and x-index of the selection,
  145. * so it does as little as it is needed to update the DOM.
  146. *
  147. * For attributes it adds new attributes to DOM elements, updates values and removes
  148. * attributes which do not exist in the view element.
  149. *
  150. * For text nodes it updates the text string if it is different. Note that if parent element is marked as an element
  151. * which changed child list, text node update will not be done, because it may not be possible to
  152. * {@link module:engine/view/domconverter~DomConverter#findCorrespondingDomText find a corresponding DOM text}.
  153. * The change will be handled in the parent element.
  154. *
  155. * For elements, which child lists have changed, it calculates a {@link module:utils/diff~diff} and adds or removes children which have
  156. * changed.
  157. *
  158. * Rendering also handles {@link module:engine/view/filler fillers}. Especially, it checks if the inline filler is needed
  159. * at selection position and adds or removes it. To prevent breaking text composition inline filler will not be
  160. * removed as long selection is in the text node which needed it at first.
  161. */
  162. render() {
  163. let inlineFillerPosition;
  164. // There was inline filler rendered in the DOM but it's not
  165. // at the selection position any more, so we can remove it
  166. // (cause even if it's needed, it must be placed in another location).
  167. if ( this._inlineFiller && !this._isSelectionInInlineFiller() ) {
  168. this._removeInlineFiller();
  169. }
  170. // If we've got the filler, let's try to guess its position in the view.
  171. if ( this._inlineFiller ) {
  172. inlineFillerPosition = this._getInlineFillerPosition();
  173. }
  174. // Otherwise, if it's needed, create it at the selection position.
  175. else if ( this._needsInlineFillerAtSelection() ) {
  176. inlineFillerPosition = this.selection.getFirstPosition();
  177. // Do not use `markToSync` so it will be added even if the parent is already added.
  178. this.markedChildren.add( inlineFillerPosition.parent );
  179. }
  180. for ( const node of this.markedTexts ) {
  181. if ( !this.markedChildren.has( node.parent ) && this.domConverter.mapViewToDom( node.parent ) ) {
  182. this._updateText( node, { inlineFillerPosition } );
  183. }
  184. }
  185. for ( const element of this.markedAttributes ) {
  186. this._updateAttrs( element );
  187. }
  188. for ( const element of this.markedChildren ) {
  189. this._updateChildren( element, { inlineFillerPosition } );
  190. }
  191. // Check whether the inline filler is required and where it really is in the DOM.
  192. // At this point in most cases it will be in the DOM, but there are exceptions.
  193. // For example, if the inline filler was deep in the created DOM structure, it will not be created.
  194. // Similarly, if it was removed at the beginning of this function and then neither text nor children were updated,
  195. // it will not be present.
  196. // Fix those and similar scenarios.
  197. if ( inlineFillerPosition ) {
  198. const fillerDomPosition = this.domConverter.viewPositionToDom( inlineFillerPosition );
  199. const domDocument = fillerDomPosition.parent.ownerDocument;
  200. if ( !startsWithFiller( fillerDomPosition.parent ) ) {
  201. // Filler has not been created at filler position. Create it now.
  202. this._inlineFiller = this._addInlineFiller( domDocument, fillerDomPosition.parent, fillerDomPosition.offset );
  203. } else {
  204. // Filler has been found, save it.
  205. this._inlineFiller = fillerDomPosition.parent;
  206. }
  207. } else {
  208. // There is no filler needed.
  209. this._inlineFiller = null;
  210. }
  211. this._updateSelection();
  212. this._updateFocus();
  213. this.markedTexts.clear();
  214. this.markedAttributes.clear();
  215. this.markedChildren.clear();
  216. }
  217. /**
  218. * Adds inline filler at given position.
  219. *
  220. * The position can be given as an array of DOM nodes and an offset in that array,
  221. * or a DOM parent element and offset in that element.
  222. *
  223. * @private
  224. * @param {Document} domDocument
  225. * @param {Element|Array.<Node>} domParentOrArray
  226. * @param {Number} offset
  227. * @returns {Text} The DOM text node that contains inline filler.
  228. */
  229. _addInlineFiller( domDocument, domParentOrArray, offset ) {
  230. const childNodes = domParentOrArray instanceof Array ? domParentOrArray : domParentOrArray.childNodes;
  231. const nodeAfterFiller = childNodes[ offset ];
  232. if ( this.domConverter.isText( nodeAfterFiller ) ) {
  233. nodeAfterFiller.data = INLINE_FILLER + nodeAfterFiller.data;
  234. return nodeAfterFiller;
  235. } else {
  236. const fillerNode = domDocument.createTextNode( INLINE_FILLER );
  237. if ( Array.isArray( domParentOrArray ) ) {
  238. childNodes.splice( offset, 0, fillerNode );
  239. } else {
  240. insertAt( domParentOrArray, offset, fillerNode );
  241. }
  242. return fillerNode;
  243. }
  244. }
  245. /**
  246. * Gets the position of the inline filler based on the current selection.
  247. * Here, we assume that we know that the filler is needed and
  248. * {@link #_isSelectionInInlineFiller is at the selection position}, and, since it's needed,
  249. * it's somewhere at the selection postion.
  250. *
  251. * Note: we cannot restore the filler position based on the filler's DOM text node, because
  252. * when this method is called (before rendering) the bindings will often be broken. View to DOM
  253. * bindings are only dependable after rendering.
  254. *
  255. * @private
  256. * @returns {module:engine/view/position~Position}
  257. */
  258. _getInlineFillerPosition() {
  259. const firstPos = this.selection.getFirstPosition();
  260. if ( firstPos.parent.is( 'text' ) ) {
  261. return ViewPosition.createBefore( this.selection.getFirstPosition().parent );
  262. } else {
  263. return firstPos;
  264. }
  265. }
  266. /**
  267. * Returns `true` if the selection hasn't left the inline filler's text node.
  268. * If it is `true` it means that the filler had been added for a reason and the selection does not
  269. * left the filler's text node. E.g. the user can be in the middle of a composition so it should not be touched.
  270. *
  271. * @private
  272. * @returns {Boolean} True if the inline filler and selection are in the same place.
  273. */
  274. _isSelectionInInlineFiller() {
  275. if ( this.selection.rangeCount != 1 || !this.selection.isCollapsed ) {
  276. return false;
  277. }
  278. // Note, we can't check if selection's position equals position of the
  279. // this._inlineFiller node, because of #663. We may not be able to calculate
  280. // the filler's position in the view at this stage.
  281. // Instead, we check it the other way – whether selection is anchored in
  282. // that text node or next to it.
  283. // Possible options are:
  284. // "FILLER{}"
  285. // "FILLERadded-text{}"
  286. const selectionPosition = this.selection.getFirstPosition();
  287. const position = this.domConverter.viewPositionToDom( selectionPosition );
  288. if ( position && this.domConverter.isText( position.parent ) && startsWithFiller( position.parent ) ) {
  289. return true;
  290. }
  291. return false;
  292. }
  293. /**
  294. * Removes the inline filler.
  295. *
  296. * @private
  297. */
  298. _removeInlineFiller() {
  299. const domFillerNode = this._inlineFiller;
  300. // Something weird happened and the stored node doesn't contain the filler's text.
  301. if ( !startsWithFiller( domFillerNode ) ) {
  302. /**
  303. * The inline filler node was lost. Most likely, something overwrote the filler text node
  304. * in the DOM.
  305. *
  306. * @error view-renderer-filler-was-lost
  307. */
  308. throw new CKEditorError( 'view-renderer-filler-was-lost: The inline filler node was lost.' );
  309. }
  310. if ( isInlineFiller( domFillerNode ) ) {
  311. domFillerNode.parentNode.removeChild( domFillerNode );
  312. } else {
  313. domFillerNode.data = domFillerNode.data.substr( INLINE_FILLER_LENGTH );
  314. }
  315. this._inlineFiller = null;
  316. }
  317. /**
  318. * Checks if the inline {@link module:engine/view/filler filler} should be added.
  319. *
  320. * @private
  321. * @returns {Boolean} True if the inline fillers should be added.
  322. */
  323. _needsInlineFillerAtSelection() {
  324. if ( this.selection.rangeCount != 1 || !this.selection.isCollapsed ) {
  325. return false;
  326. }
  327. const selectionPosition = this.selection.getFirstPosition();
  328. const selectionParent = selectionPosition.parent;
  329. const selectionOffset = selectionPosition.offset;
  330. // If there is no DOM root we do not care about fillers.
  331. if ( !this.domConverter.mapViewToDom( selectionParent.root ) ) {
  332. return false;
  333. }
  334. if ( !( selectionParent.is( 'element' ) ) ) {
  335. return false;
  336. }
  337. // Prevent adding inline filler inside elements with contenteditable=false.
  338. // https://github.com/ckeditor/ckeditor5-engine/issues/1170
  339. if ( !_isEditable( selectionParent ) ) {
  340. return false;
  341. }
  342. // We have block filler, we do not need inline one.
  343. if ( selectionOffset === selectionParent.getFillerOffset() ) {
  344. return false;
  345. }
  346. const nodeBefore = selectionPosition.nodeBefore;
  347. const nodeAfter = selectionPosition.nodeAfter;
  348. if ( nodeBefore instanceof ViewText || nodeAfter instanceof ViewText ) {
  349. return false;
  350. }
  351. return true;
  352. }
  353. /**
  354. * Checks if text needs to be updated and possibly updates it.
  355. *
  356. * @private
  357. * @param {module:engine/view/text~Text} viewText View text to update.
  358. * @param {Object} options
  359. * @param {module:engine/view/position~Position} options.inlineFillerPosition The position on which the inline
  360. * filler should be rendered.
  361. */
  362. _updateText( viewText, options ) {
  363. const domText = this.domConverter.findCorrespondingDomText( viewText );
  364. const newDomText = this.domConverter.viewToDom( viewText, domText.ownerDocument );
  365. const actualText = domText.data;
  366. let expectedText = newDomText.data;
  367. const filler = options.inlineFillerPosition;
  368. if ( filler && filler.parent == viewText.parent && filler.offset == viewText.index ) {
  369. expectedText = INLINE_FILLER + expectedText;
  370. }
  371. if ( actualText != expectedText ) {
  372. domText.data = expectedText;
  373. }
  374. }
  375. /**
  376. * Checks if attributes list needs to be updated and possibly updates it.
  377. *
  378. * @private
  379. * @param {module:engine/view/element~Element} viewElement View element to update.
  380. */
  381. _updateAttrs( viewElement ) {
  382. const domElement = this.domConverter.mapViewToDom( viewElement );
  383. const domAttrKeys = Array.from( domElement.attributes ).map( attr => attr.name );
  384. const viewAttrKeys = viewElement.getAttributeKeys();
  385. // Add or overwrite attributes.
  386. for ( const key of viewAttrKeys ) {
  387. domElement.setAttribute( key, viewElement.getAttribute( key ) );
  388. }
  389. // Remove from DOM attributes which do not exists in the view.
  390. for ( const key of domAttrKeys ) {
  391. if ( !viewElement.hasAttribute( key ) ) {
  392. domElement.removeAttribute( key );
  393. }
  394. }
  395. }
  396. /**
  397. * Checks if elements child list needs to be updated and possibly updates it.
  398. *
  399. * @private
  400. * @param {module:engine/view/element~Element} viewElement View element to update.
  401. * @param {Object} options
  402. * @param {module:engine/view/position~Position} options.inlineFillerPosition The position on which the inline
  403. * filler should be rendered.
  404. */
  405. _updateChildren( viewElement, options ) {
  406. const domConverter = this.domConverter;
  407. const domElement = domConverter.mapViewToDom( viewElement );
  408. if ( !domElement ) {
  409. // If there is no `domElement` it means that it was already removed from DOM.
  410. // There is no need to update it. It will be updated when re-inserted.
  411. return;
  412. }
  413. const domDocument = domElement.ownerDocument;
  414. const filler = options.inlineFillerPosition;
  415. const actualDomChildren = domElement.childNodes;
  416. const expectedDomChildren = Array.from( domConverter.viewChildrenToDom( viewElement, domDocument, { bind: true } ) );
  417. // Inline filler element has to be created during children update because we need it to diff actual dom
  418. // elements with expected dom elements. We need inline filler in expected dom elements so we won't re-render
  419. // text node if it is not necessary.
  420. if ( filler && filler.parent == viewElement ) {
  421. this._addInlineFiller( domDocument, expectedDomChildren, filler.offset );
  422. }
  423. const actions = diff( actualDomChildren, expectedDomChildren, sameNodes );
  424. let i = 0;
  425. const nodesToUnbind = new Set();
  426. for ( const action of actions ) {
  427. if ( action === 'insert' ) {
  428. insertAt( domElement, i, expectedDomChildren[ i ] );
  429. i++;
  430. } else if ( action === 'delete' ) {
  431. nodesToUnbind.add( actualDomChildren[ i ] );
  432. remove( actualDomChildren[ i ] );
  433. } else { // 'equal'
  434. i++;
  435. }
  436. }
  437. // Unbind removed nodes. When node does not have a parent it means that it was removed from DOM tree during
  438. // comparision with the expected DOM. We don't need to check child nodes, because if child node was reinserted,
  439. // it was moved to DOM tree out of the removed node.
  440. for ( const node of nodesToUnbind ) {
  441. if ( !node.parentNode ) {
  442. this.domConverter.unbindDomElement( node );
  443. }
  444. }
  445. function sameNodes( actualDomChild, expectedDomChild ) {
  446. // Elements.
  447. if ( actualDomChild === expectedDomChild ) {
  448. return true;
  449. }
  450. // Texts.
  451. else if ( domConverter.isText( actualDomChild ) && domConverter.isText( expectedDomChild ) ) {
  452. return actualDomChild.data === expectedDomChild.data;
  453. }
  454. // Block fillers.
  455. else if ( isBlockFiller( actualDomChild, domConverter.blockFiller ) &&
  456. isBlockFiller( expectedDomChild, domConverter.blockFiller ) ) {
  457. return true;
  458. }
  459. // Not matching types.
  460. return false;
  461. }
  462. }
  463. /**
  464. * Checks if selection needs to be updated and possibly updates it.
  465. *
  466. * @private
  467. */
  468. _updateSelection() {
  469. // If there is no selection - remove DOM and fake selections.
  470. if ( this.selection.rangeCount === 0 ) {
  471. this._removeDomSelection();
  472. this._removeFakeSelection();
  473. return;
  474. }
  475. const domRoot = this.domConverter.mapViewToDom( this.selection.editableElement );
  476. // Do nothing if there is no focus, or there is no DOM element corresponding to selection's editable element.
  477. if ( !this.isFocused || !domRoot ) {
  478. return;
  479. }
  480. // Render selection.
  481. if ( this.selection.isFake ) {
  482. this._updateFakeSelection( domRoot );
  483. } else {
  484. this._removeFakeSelection();
  485. this._updateDomSelection( domRoot );
  486. }
  487. }
  488. /**
  489. * Updates fake selection.
  490. *
  491. * @private
  492. * @param {HTMLElement} domRoot Valid DOM root where fake selection container should be added.
  493. */
  494. _updateFakeSelection( domRoot ) {
  495. const domDocument = domRoot.ownerDocument;
  496. // Create fake selection container if one does not exist.
  497. if ( !this._fakeSelectionContainer ) {
  498. this._fakeSelectionContainer = domDocument.createElement( 'div' );
  499. this._fakeSelectionContainer.style.position = 'fixed';
  500. this._fakeSelectionContainer.style.top = 0;
  501. this._fakeSelectionContainer.style.left = '-9999px';
  502. this._fakeSelectionContainer.appendChild( domDocument.createTextNode( '\u00A0' ) );
  503. }
  504. // Add fake container if not already added.
  505. if ( !this._fakeSelectionContainer.parentElement ) {
  506. domRoot.appendChild( this._fakeSelectionContainer );
  507. }
  508. // Update contents.
  509. const content = this.selection.fakeSelectionLabel || '\u00A0';
  510. this._fakeSelectionContainer.firstChild.data = content;
  511. // Update selection.
  512. const domSelection = domDocument.getSelection();
  513. domSelection.removeAllRanges();
  514. const domRange = domDocument.createRange();
  515. domRange.selectNodeContents( this._fakeSelectionContainer );
  516. domSelection.addRange( domRange );
  517. // Bind fake selection container with current selection.
  518. this.domConverter.bindFakeSelection( this._fakeSelectionContainer, this.selection );
  519. }
  520. /**
  521. * Updates DOM selection.
  522. *
  523. * @private
  524. * @param {HTMLElement} domRoot Valid DOM root where DOM selection should be rendered.
  525. */
  526. _updateDomSelection( domRoot ) {
  527. const domSelection = domRoot.ownerDocument.defaultView.getSelection();
  528. // Let's check whether DOM selection needs updating at all.
  529. if ( !this._domSelectionNeedsUpdate( domSelection ) ) {
  530. return;
  531. }
  532. // Multi-range selection is not available in most browsers, and, at least in Chrome, trying to
  533. // set such selection, that is not continuous, throws an error. Because of that, we will just use anchor
  534. // and focus of view selection.
  535. // Since we are not supporting multi-range selection, we also do not need to check if proper editable is
  536. // selected. If there is any editable selected, it is okay (editable is taken from selection anchor).
  537. const anchor = this.domConverter.viewPositionToDom( this.selection.anchor );
  538. const focus = this.domConverter.viewPositionToDom( this.selection.focus );
  539. domSelection.collapse( anchor.parent, anchor.offset );
  540. domSelection.extend( focus.parent, focus.offset );
  541. }
  542. /**
  543. * Checks whether given DOM selection needs to be updated.
  544. *
  545. * @private
  546. * @param {Selection} domSelection DOM selection to check.
  547. * @returns {Boolean}
  548. */
  549. _domSelectionNeedsUpdate( domSelection ) {
  550. if ( !this.domConverter.isDomSelectionCorrect( domSelection ) ) {
  551. // Current DOM selection is in incorrect position. We need to update it.
  552. return true;
  553. }
  554. const oldViewSelection = domSelection && this.domConverter.domSelectionToView( domSelection );
  555. if ( oldViewSelection && this.selection.isEqual( oldViewSelection ) ) {
  556. return false;
  557. }
  558. // If selection is not collapsed, it does not need to be updated if it is similar.
  559. if ( !this.selection.isCollapsed && this.selection.isSimilar( oldViewSelection ) ) {
  560. // Selection did not changed and is correct, do not update.
  561. return false;
  562. }
  563. // Selections are not similar.
  564. return true;
  565. }
  566. /**
  567. * Removes DOM selection.
  568. *
  569. * @private
  570. */
  571. _removeDomSelection() {
  572. for ( const doc of this.domDocuments ) {
  573. const domSelection = doc.getSelection();
  574. if ( domSelection.rangeCount ) {
  575. const activeDomElement = doc.activeElement;
  576. const viewElement = this.domConverter.mapDomToView( activeDomElement );
  577. if ( activeDomElement && viewElement ) {
  578. doc.getSelection().removeAllRanges();
  579. }
  580. }
  581. }
  582. }
  583. /**
  584. * Removes fake selection.
  585. *
  586. * @private
  587. */
  588. _removeFakeSelection() {
  589. const container = this._fakeSelectionContainer;
  590. if ( container ) {
  591. container.remove();
  592. }
  593. }
  594. /**
  595. * Checks if focus needs to be updated and possibly updates it.
  596. *
  597. * @private
  598. */
  599. _updateFocus() {
  600. if ( this.isFocused ) {
  601. const editable = this.selection.editableElement;
  602. if ( editable ) {
  603. this.domConverter.focus( editable );
  604. }
  605. }
  606. }
  607. }
  608. mix( Renderer, ObservableMixin );
  609. // Checks if provided element is editable.
  610. //
  611. // @private
  612. // @param {module:engine/view/element~Element} element
  613. // @returns {Boolean}
  614. function _isEditable( element ) {
  615. if ( element.getAttribute( 'contenteditable' ) == 'false' ) {
  616. return false;
  617. }
  618. const parent = element.findAncestor( element => element.hasAttribute( 'contenteditable' ) );
  619. return !parent || parent.getAttribute( 'contenteditable' ) == 'true';
  620. }