mutationobserver.js 15 KB

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