mutationobserver.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import Observer from './observer.js';
  7. import { startsWithFiller, getDataWithoutFiller } from '../filler.js';
  8. /**
  9. * Mutation observer class observes changes in the DOM, fires {@link engine.view.Document#mutations} event, mark view elements
  10. * as changed and call {@link engine.view.render}. Because all mutated nodes are marked as "to be rendered" and the
  11. * {@link engine.view.render} is called, all changes will be reverted, unless the mutation will be handled by the
  12. * {@link engine.view.Document#mutations} event listener. It means user will see only handled changes, and the editor will
  13. * block all changes which are not handled.
  14. *
  15. * Mutation Observer also take care of reducing number of mutations which are fired. It removes duplicates and
  16. * mutations on elements which do not have corresponding view elements. Also
  17. * {@link engine.view.Document.MutatatedText text mutation} is fired only if parent element do not change child list.
  18. *
  19. * @memberOf engine.view.observer
  20. * @extends engine.view.observer.Observer
  21. */
  22. export default class MutationObserver extends Observer {
  23. constructor( document ) {
  24. super( document );
  25. /**
  26. * Native mutation observer config.
  27. *
  28. * @private
  29. * @member {Object} engine.view.observer.MutationObserver#_config
  30. */
  31. this._config = {
  32. childList: true,
  33. characterData: true,
  34. characterDataOldValue: true,
  35. subtree: true
  36. };
  37. /**
  38. * Reference to the {@link engine.view.Document#domConverter}.
  39. *
  40. * @member {engine.view.DomConverter} engine.view.observer.MutationObserver#domConverter
  41. */
  42. this.domConverter = document.domConverter;
  43. /**
  44. * Reference to the {@link engine.view.Document#renderer}.
  45. *
  46. * @member {engine.view.Renderer} engine.view.observer.MutationObserver#renderer
  47. */
  48. this.renderer = document.renderer;
  49. /**
  50. * Observed DOM elements.
  51. *
  52. * @private
  53. * @member {Array.<HTMLElement>} engine.view.observer.MutationObserver#_domElements
  54. */
  55. this._domElements = [];
  56. /**
  57. * Native mutation observer.
  58. *
  59. * @private
  60. * @member {MutationObserver} engine.view.observer.MutationObserver#_mutationObserver
  61. */
  62. this._mutationObserver = new window.MutationObserver( this._onMutations.bind( this ) );
  63. }
  64. /**
  65. * Synchronously fires {@link engine.view.Document#mutations} event with all mutations in record queue.
  66. * At the same time empties the queue so mutations will not be fired twice.
  67. *
  68. * @returns {[type]} [description]
  69. */
  70. flush() {
  71. this._onMutations( this._mutationObserver.takeRecords() );
  72. }
  73. /**
  74. * @inheritDoc
  75. */
  76. observe( domElement ) {
  77. this._domElements.push( domElement );
  78. if ( this.isEnabled ) {
  79. this._mutationObserver.observe( domElement, this._config );
  80. }
  81. }
  82. /**
  83. * @inheritDoc
  84. */
  85. enable() {
  86. super.enable();
  87. for ( let domElement of this._domElements ) {
  88. this._mutationObserver.observe( domElement, this._config );
  89. }
  90. }
  91. /**
  92. * @inheritDoc
  93. */
  94. disable() {
  95. super.disable();
  96. this._mutationObserver.disconnect();
  97. }
  98. /**
  99. * Handles mutations. Deduplicates, mark view elements to sync, fire event and call render.
  100. *
  101. * @private
  102. * @method engine.view.observer.MutationObserver#_onMutations
  103. * @param {Array.<Object>} domMutations Array of native mutations.
  104. */
  105. _onMutations( domMutations ) {
  106. // As a result of this.flush() we can have an empty collection.
  107. if ( domMutations.length === 0 ) {
  108. return;
  109. }
  110. const domConverter = this.domConverter;
  111. // Use map and set for deduplication.
  112. const mutatedTexts = new Map();
  113. const mutatedElements = new Set();
  114. // Handle `childList` mutations first, so we will be able to check if the `characterData` mutation is in the
  115. // element with changed structure anyway.
  116. for ( let mutation of domMutations ) {
  117. if ( mutation.type === 'childList' ) {
  118. const element = domConverter.getCorrespondingViewElement( mutation.target );
  119. if ( element ) {
  120. mutatedElements.add( element );
  121. }
  122. }
  123. }
  124. // Handle `characterData` mutations later, when we have the full list of nodes which changed structure.
  125. for ( let mutation of domMutations ) {
  126. if ( mutation.type === 'characterData' ) {
  127. const text = domConverter.getCorrespondingViewText( mutation.target );
  128. if ( text && !mutatedElements.has( text.parent ) ) {
  129. // Use text as a key, for deduplication. If there will be another mutation on the same text element
  130. // we will have only one in the map.
  131. mutatedTexts.set( text, {
  132. type: 'text',
  133. oldText: text.data,
  134. newText: getDataWithoutFiller( mutation.target ),
  135. node: text
  136. } );
  137. }
  138. // When we added first letter to the text node which had only inline filler, for the DOM it is mutation
  139. // on text, but for the view, where filler text node did not existed, new text node was created, so we
  140. // need to fire 'children' mutation instead of 'text'.
  141. else if ( !text && startsWithFiller( mutation.target ) ) {
  142. mutatedElements.add( domConverter.getCorrespondingViewElement( mutation.target.parentNode ) );
  143. }
  144. }
  145. }
  146. // Now we build the list of mutations to fire and mark elements. We did not do it earlier to avoid marking the
  147. // same node multiple times in case of duplication.
  148. // List of mutations we will fire.
  149. const viewMutations = [];
  150. for ( let mutatedText of mutatedTexts.values() ) {
  151. this.renderer.markToSync( 'TEXT', mutatedText.node );
  152. viewMutations.push( mutatedText );
  153. }
  154. for ( let viewElement of mutatedElements ) {
  155. const domElement = domConverter.getCorrespondingDomElement( viewElement );
  156. const viewChildren = viewElement.getChildren();
  157. const newViewChildren = domConverter.domChildrenToView( domElement );
  158. this.renderer.markToSync( 'CHILDREN', viewElement );
  159. viewMutations.push( {
  160. type: 'children',
  161. oldChildren: Array.from( viewChildren ),
  162. newChildren: Array.from( newViewChildren ),
  163. node: viewElement
  164. } );
  165. }
  166. this.document.fire( 'mutations', viewMutations );
  167. this.document.render();
  168. }
  169. }
  170. /**
  171. * Fired when mutation occurred. If tree view is not changed on this event, DOM will be reverter to the state before
  172. * mutation, so all changes which should be applied, should be handled on this event.
  173. *
  174. * @event engine.view.Document#mutations
  175. * @param {Array.<engine.view.Document~MutatatedText|engine.view.Document~MutatatedChildren>} viewMutations
  176. * Array of mutations.
  177. * For mutated texts it will be {@link engine.view.Document~MutatatedText} and for mutated elements it will be
  178. * {@link engine.view.Document~MutatatedElement}. You can recognize the type based on the `type` property.
  179. */
  180. /**
  181. * Mutation item for text.
  182. *
  183. * @see engine.view.Document#mutations
  184. * @see engine.view.MutatatedChildren
  185. *
  186. * @typedef {Object} engine.view.MutatatedText
  187. *
  188. * @property {String} type For text mutations it is always 'text'.
  189. * @property {engine.view.Text} node Mutated text node.
  190. * @property {String} oldText Old text.
  191. * @property {String} newText New text.
  192. */
  193. /**
  194. * Mutation item for child nodes.
  195. *
  196. * @see engine.view.Document#mutations
  197. * @see engine.view.MutatatedText
  198. *
  199. * @typedef {Object} engine.view.MutatatedChildren
  200. *
  201. * @property {String} type For child nodes mutations it is always 'children'.
  202. * @property {engine.view.Element} node Parent of the mutated children.
  203. * @property {Array.<engine.view.Node>} oldChildren Old child nodes.
  204. * @property {Array.<engine.view.Node>} newChildren New child nodes.
  205. */