renderer.js 23 KB

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