mutationobserver.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import View from '../../../src/view/view';
  7. import MutationObserver from '../../../src/view/observer/mutationobserver';
  8. import UIElement from '../../../src/view/uielement';
  9. import createViewRoot from '../_utils/createroot';
  10. import { parse } from '../../../src/dev-utils/view';
  11. describe( 'MutationObserver', () => {
  12. let view, domEditor, viewDocument, viewRoot, mutationObserver, lastMutations, domRoot;
  13. beforeEach( () => {
  14. domRoot = document.createElement( 'div' );
  15. domRoot.innerHTML = '<div contenteditable="true" id="main"></div><div contenteditable="true" id="additional"></div>';
  16. document.body.appendChild( domRoot );
  17. view = new View();
  18. viewDocument = view.document;
  19. domEditor = document.getElementById( 'main' );
  20. lastMutations = null;
  21. createViewRoot( viewDocument );
  22. view.attachDomRoot( domEditor );
  23. viewDocument.selection._setTo( null );
  24. document.getSelection().removeAllRanges();
  25. mutationObserver = view.getObserver( MutationObserver );
  26. viewDocument.on( 'mutations', ( evt, mutations ) => {
  27. lastMutations = mutations;
  28. } );
  29. viewRoot = viewDocument.getRoot();
  30. viewRoot.appendChildren( parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  31. view.render();
  32. } );
  33. afterEach( () => {
  34. mutationObserver.disable();
  35. domRoot.parentElement.removeChild( domRoot );
  36. view.destroy();
  37. } );
  38. it( 'should handle typing', () => {
  39. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  40. mutationObserver.flush();
  41. expectDomEditorNotToChange();
  42. expect( lastMutations.length ).to.equal( 1 );
  43. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  44. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  45. expect( lastMutations[ 0 ].newText ).to.equal( 'foom' );
  46. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  47. } );
  48. it( 'should not observe if disabled', () => {
  49. const additional = document.getElementById( 'additional' );
  50. mutationObserver.disable();
  51. createViewRoot( viewDocument, 'div', 'additional' );
  52. view.attachDomRoot( additional, 'additional' );
  53. additional.textContent = 'foobar';
  54. mutationObserver.flush();
  55. expect( lastMutations ).to.be.null;
  56. } );
  57. it( 'should handle bold', () => {
  58. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'f';
  59. const domB = document.createElement( 'b' );
  60. domB.appendChild( document.createTextNode( 'oo' ) );
  61. domEditor.childNodes[ 0 ].appendChild( domB );
  62. mutationObserver.flush();
  63. expectDomEditorNotToChange();
  64. expect( lastMutations.length ).to.equal( 1 );
  65. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  66. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  67. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  68. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'f' );
  69. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  70. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  71. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  72. } );
  73. it( 'should handle unbold', () => {
  74. viewRoot.removeChildren( 0, viewRoot.childCount );
  75. viewRoot.appendChildren( parse( '<container:p><attribute:b>foo</attribute:b></container:p>' ) );
  76. view.render();
  77. const domP = domEditor.childNodes[ 0 ];
  78. const domB = domP.childNodes[ 0 ];
  79. domP.removeChild( domB );
  80. domP.appendChild( document.createTextNode( 'foo' ) );
  81. mutationObserver.flush();
  82. // "expectDomEditorNotToChange()".
  83. expect( domEditor.childNodes.length ).to.equal( 1 );
  84. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  85. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  86. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].tagName ).to.equal( 'B' );
  87. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  88. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  89. // Check mutations.
  90. expect( lastMutations.length ).to.equal( 1 );
  91. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  92. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  93. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  94. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  95. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  96. expect( lastMutations[ 0 ].oldChildren[ 0 ].name ).to.equal( 'b' );
  97. } );
  98. it( 'should deduplicate text changes', () => {
  99. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  100. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  101. mutationObserver.flush();
  102. expectDomEditorNotToChange();
  103. expect( lastMutations.length ).to.equal( 1 );
  104. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  105. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  106. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  107. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  108. } );
  109. it( 'should ignore changes in elements not attached to tree view', () => {
  110. const domP = document.createElement( 'p' );
  111. const domB = document.createElement( 'b' );
  112. const domText = document.createTextNode( 'bom' );
  113. domEditor.appendChild( domP );
  114. domP.appendChild( domB );
  115. domB.appendChild( domText );
  116. mutationObserver.flush();
  117. expectDomEditorNotToChange();
  118. expect( lastMutations.length ).to.equal( 1 );
  119. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  120. expect( lastMutations[ 0 ].node ).to.equal( viewRoot );
  121. } );
  122. it( 'should fire mutations event with view selection instance, if dom selection can be mapped to view', done => {
  123. const textNode = domEditor.childNodes[ 0 ].childNodes[ 0 ];
  124. textNode.data = 'foom';
  125. const domSelection = document.getSelection();
  126. domSelection.collapse( textNode, 4 );
  127. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  128. expect( viewSelection.anchor.parent ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  129. expect( viewSelection.anchor.offset ).to.equal( 4 );
  130. done();
  131. } );
  132. mutationObserver.flush();
  133. expectDomEditorNotToChange();
  134. } );
  135. it( 'should fire mutations event with viewSelection param set to null, if dom selection cannot be mapped to view', done => {
  136. const textNode = domEditor.ownerDocument.createTextNode( 'foo' );
  137. domEditor.childNodes[ 0 ].appendChild( textNode );
  138. const domSelection = document.getSelection();
  139. domSelection.collapse( textNode, 3 );
  140. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  141. expect( viewSelection ).to.be.null;
  142. done();
  143. } );
  144. mutationObserver.flush();
  145. expectDomEditorNotToChange();
  146. } );
  147. it( 'should be able to observe multiple roots', () => {
  148. const domAdditionalEditor = document.getElementById( 'additional' );
  149. // Prepare AdditionalEditor
  150. createViewRoot( viewDocument, 'div', 'additional' );
  151. view.attachDomRoot( domAdditionalEditor, 'additional' );
  152. viewDocument.getRoot( 'additional' ).appendChildren(
  153. parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  154. // Render AdditionalEditor (first editor has been rendered in the beforeEach function)
  155. view.render();
  156. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  157. domAdditionalEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  158. mutationObserver.flush();
  159. expect( lastMutations.length ).to.equal( 2 );
  160. } );
  161. it( 'should fire nothing if there were no mutations', () => {
  162. mutationObserver.flush();
  163. expectDomEditorNotToChange();
  164. expect( lastMutations ).to.be.null;
  165. } );
  166. it( 'should fire children mutation if the mutation occurred in the inline filler', () => {
  167. const { view: viewContainer, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  168. view.change( writer => {
  169. viewRoot.appendChildren( viewContainer );
  170. writer.setSelection( selection );
  171. } );
  172. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  173. inlineFiller.data += 'x';
  174. mutationObserver.flush();
  175. expect( lastMutations.length ).to.equal( 1 );
  176. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  177. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  178. } );
  179. it( 'should have no inline filler in mutation', () => {
  180. const { view: viewContainer, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  181. view.change( writer => {
  182. viewRoot.appendChildren( viewContainer );
  183. writer.setSelection( selection );
  184. } );
  185. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  186. inlineFiller.data += 'x';
  187. view.change( () => {
  188. viewContainer.getChild( 1 ).appendChildren( parse( 'x' ) );
  189. mutationObserver.flush();
  190. } );
  191. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  192. inlineFiller.data += 'y';
  193. mutationObserver.flush();
  194. expect( lastMutations.length ).to.equal( 1 );
  195. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  196. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  197. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  198. } );
  199. it( 'should have no block filler in mutation', () => {
  200. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  201. view.render();
  202. const domP = domEditor.childNodes[ 2 ];
  203. domP.removeChild( domP.childNodes[ 0 ] );
  204. domP.appendChild( document.createTextNode( 'foo' ) );
  205. mutationObserver.flush();
  206. expect( lastMutations.length ).to.equal( 1 );
  207. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  208. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  209. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  210. } );
  211. it( 'should ignore mutation with bogus br inserted on the end of the empty paragraph', () => {
  212. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  213. view.render();
  214. const domP = domEditor.childNodes[ 2 ];
  215. domP.appendChild( document.createElement( 'br' ) );
  216. mutationObserver.flush();
  217. expect( lastMutations.length ).to.equal( 0 );
  218. } );
  219. it( 'should ignore mutation with bogus br inserted on the end of the paragraph with text', () => {
  220. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  221. view.render();
  222. const domP = domEditor.childNodes[ 2 ];
  223. domP.appendChild( document.createElement( 'br' ) );
  224. mutationObserver.flush();
  225. expect( lastMutations.length ).to.equal( 0 );
  226. } );
  227. it( 'should ignore mutation with bogus br inserted on the end of the paragraph while processing text mutations', () => {
  228. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  229. view.render();
  230. const domP = domEditor.childNodes[ 2 ];
  231. domP.childNodes[ 0 ].data = 'foo ';
  232. domP.appendChild( document.createElement( 'br' ) );
  233. mutationObserver.flush();
  234. expect( lastMutations.length ).to.equal( 1 );
  235. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  236. expect( lastMutations[ 0 ].newText ).to.equal( 'foo ' );
  237. } );
  238. it( 'should ignore child mutations which resulted in no changes – when element contains elements', () => {
  239. viewRoot.appendChildren( parse( '<container:p><container:x></container:x></container:p>' ) );
  240. view.render();
  241. const domP = domEditor.childNodes[ 2 ];
  242. const domY = document.createElement( 'y' );
  243. domP.appendChild( domY );
  244. domY.remove();
  245. mutationObserver.flush();
  246. expect( lastMutations.length ).to.equal( 0 );
  247. } );
  248. // This case is more tricky than the previous one because DOMConverter will return a different
  249. // instances of view text nodes every time it converts a DOM text node.
  250. it( 'should ignore child mutations which resulted in no changes – when element contains text nodes', () => {
  251. const domP = domEditor.childNodes[ 0 ];
  252. const domText = document.createTextNode( 'x' );
  253. domP.appendChild( domText );
  254. domText.remove();
  255. const domP2 = domEditor.childNodes[ 1 ];
  256. domP2.appendChild( document.createTextNode( 'x' ) );
  257. mutationObserver.flush();
  258. // There was onlu P2 change. P1 must be ignored.
  259. const viewP2 = viewRoot.getChild( 1 );
  260. expect( lastMutations.length ).to.equal( 1 );
  261. expect( lastMutations[ 0 ].node ).to.equal( viewP2 );
  262. } );
  263. it( 'should not ignore mutation with br inserted not on the end of the paragraph', () => {
  264. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  265. view.render();
  266. const domP = domEditor.childNodes[ 2 ];
  267. domP.insertBefore( document.createElement( 'br' ), domP.childNodes[ 0 ] );
  268. mutationObserver.flush();
  269. expect( lastMutations.length ).to.equal( 1 );
  270. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  271. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'br' );
  272. expect( lastMutations[ 0 ].newChildren[ 1 ].data ).to.equal( 'foo' );
  273. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  274. } );
  275. it( 'should not ignore mutation inserting element different than br on the end of the empty paragraph', () => {
  276. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  277. view.render();
  278. const domP = domEditor.childNodes[ 2 ];
  279. domP.appendChild( document.createElement( 'span' ) );
  280. mutationObserver.flush();
  281. expect( lastMutations.length ).to.equal( 1 );
  282. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  283. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'span' );
  284. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  285. } );
  286. it( 'should not ignore mutation inserting element different than br on the end of the paragraph with text', () => {
  287. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  288. view.render();
  289. const domP = domEditor.childNodes[ 2 ];
  290. domP.appendChild( document.createElement( 'span' ) );
  291. mutationObserver.flush();
  292. expect( lastMutations.length ).to.equal( 1 );
  293. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  294. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  295. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'span' );
  296. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  297. } );
  298. describe( 'UIElement integration', () => {
  299. function createUIElement( name ) {
  300. const element = new UIElement( name );
  301. element.render = function( domDocument ) {
  302. const root = this.toDomElement( domDocument );
  303. root.innerHTML = 'foo bar';
  304. return root;
  305. };
  306. return element;
  307. }
  308. beforeEach( () => {
  309. const uiElement = createUIElement( 'div' );
  310. viewRoot.appendChildren( uiElement );
  311. view.render();
  312. } );
  313. it( 'should not collect text mutations from UIElement', () => {
  314. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  315. mutationObserver.flush();
  316. expect( lastMutations.length ).to.equal( 0 );
  317. } );
  318. it( 'should not collect child mutations from UIElement', () => {
  319. const span = document.createElement( 'span' );
  320. domEditor.childNodes[ 2 ].appendChild( span );
  321. mutationObserver.flush();
  322. expect( lastMutations.length ).to.equal( 0 );
  323. } );
  324. } );
  325. function expectDomEditorNotToChange() {
  326. expect( domEditor.childNodes.length ).to.equal( 2 );
  327. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  328. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  329. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  330. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  331. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  332. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  333. }
  334. } );