renderer.js 23 KB

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