8
0

mutationobserver.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. viewRoot.appendChildren( viewContainer );
  169. viewDocument.selection._setTo( selection );
  170. view.render();
  171. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  172. inlineFiller.data += 'x';
  173. mutationObserver.flush();
  174. expect( lastMutations.length ).to.equal( 1 );
  175. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  176. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  177. } );
  178. it( 'should have no inline filler in mutation', () => {
  179. const { view: viewContainer, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  180. viewRoot.appendChildren( viewContainer );
  181. viewDocument.selection._setTo( selection );
  182. view.render();
  183. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  184. inlineFiller.data += 'x';
  185. viewContainer.getChild( 1 ).appendChildren( parse( 'x' ) );
  186. mutationObserver.flush();
  187. view.render();
  188. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  189. inlineFiller.data += 'y';
  190. mutationObserver.flush();
  191. expect( lastMutations.length ).to.equal( 1 );
  192. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  193. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  194. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  195. } );
  196. it( 'should have no block filler in mutation', () => {
  197. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  198. view.render();
  199. const domP = domEditor.childNodes[ 2 ];
  200. domP.removeChild( domP.childNodes[ 0 ] );
  201. domP.appendChild( document.createTextNode( 'foo' ) );
  202. mutationObserver.flush();
  203. expect( lastMutations.length ).to.equal( 1 );
  204. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  205. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  206. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  207. } );
  208. it( 'should ignore mutation with bogus br inserted on the end of the empty paragraph', () => {
  209. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  210. view.render();
  211. const domP = domEditor.childNodes[ 2 ];
  212. domP.appendChild( document.createElement( 'br' ) );
  213. mutationObserver.flush();
  214. expect( lastMutations.length ).to.equal( 0 );
  215. } );
  216. it( 'should ignore mutation with bogus br inserted on the end of the paragraph with text', () => {
  217. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  218. view.render();
  219. const domP = domEditor.childNodes[ 2 ];
  220. domP.appendChild( document.createElement( 'br' ) );
  221. mutationObserver.flush();
  222. expect( lastMutations.length ).to.equal( 0 );
  223. } );
  224. it( 'should ignore mutation with bogus br inserted on the end of the paragraph while processing text mutations', () => {
  225. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  226. view.render();
  227. const domP = domEditor.childNodes[ 2 ];
  228. domP.childNodes[ 0 ].data = 'foo ';
  229. domP.appendChild( document.createElement( 'br' ) );
  230. mutationObserver.flush();
  231. expect( lastMutations.length ).to.equal( 1 );
  232. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  233. expect( lastMutations[ 0 ].newText ).to.equal( 'foo ' );
  234. } );
  235. it( 'should ignore child mutations which resulted in no changes – when element contains elements', () => {
  236. viewRoot.appendChildren( parse( '<container:p><container:x></container:x></container:p>' ) );
  237. view.render();
  238. const domP = domEditor.childNodes[ 2 ];
  239. const domY = document.createElement( 'y' );
  240. domP.appendChild( domY );
  241. domY.remove();
  242. mutationObserver.flush();
  243. expect( lastMutations.length ).to.equal( 0 );
  244. } );
  245. // This case is more tricky than the previous one because DOMConverter will return a different
  246. // instances of view text nodes every time it converts a DOM text node.
  247. it( 'should ignore child mutations which resulted in no changes – when element contains text nodes', () => {
  248. const domP = domEditor.childNodes[ 0 ];
  249. const domText = document.createTextNode( 'x' );
  250. domP.appendChild( domText );
  251. domText.remove();
  252. const domP2 = domEditor.childNodes[ 1 ];
  253. domP2.appendChild( document.createTextNode( 'x' ) );
  254. mutationObserver.flush();
  255. // There was onlu P2 change. P1 must be ignored.
  256. const viewP2 = viewRoot.getChild( 1 );
  257. expect( lastMutations.length ).to.equal( 1 );
  258. expect( lastMutations[ 0 ].node ).to.equal( viewP2 );
  259. } );
  260. it( 'should not ignore mutation with br inserted not on the end of the paragraph', () => {
  261. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  262. view.render();
  263. const domP = domEditor.childNodes[ 2 ];
  264. domP.insertBefore( document.createElement( 'br' ), domP.childNodes[ 0 ] );
  265. mutationObserver.flush();
  266. expect( lastMutations.length ).to.equal( 1 );
  267. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  268. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'br' );
  269. expect( lastMutations[ 0 ].newChildren[ 1 ].data ).to.equal( 'foo' );
  270. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  271. } );
  272. it( 'should not ignore mutation inserting element different than br on the end of the empty paragraph', () => {
  273. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  274. view.render();
  275. const domP = domEditor.childNodes[ 2 ];
  276. domP.appendChild( document.createElement( 'span' ) );
  277. mutationObserver.flush();
  278. expect( lastMutations.length ).to.equal( 1 );
  279. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  280. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'span' );
  281. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  282. } );
  283. it( 'should not ignore mutation inserting element different than br on the end of the paragraph with text', () => {
  284. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  285. view.render();
  286. const domP = domEditor.childNodes[ 2 ];
  287. domP.appendChild( document.createElement( 'span' ) );
  288. mutationObserver.flush();
  289. expect( lastMutations.length ).to.equal( 1 );
  290. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  291. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  292. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'span' );
  293. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  294. } );
  295. describe( 'UIElement integration', () => {
  296. function createUIElement( name ) {
  297. const element = new UIElement( name );
  298. element.render = function( domDocument ) {
  299. const root = this.toDomElement( domDocument );
  300. root.innerHTML = 'foo bar';
  301. return root;
  302. };
  303. return element;
  304. }
  305. beforeEach( () => {
  306. const uiElement = createUIElement( 'div' );
  307. viewRoot.appendChildren( uiElement );
  308. view.render();
  309. } );
  310. it( 'should not collect text mutations from UIElement', () => {
  311. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  312. mutationObserver.flush();
  313. expect( lastMutations.length ).to.equal( 0 );
  314. } );
  315. it( 'should not collect child mutations from UIElement', () => {
  316. const span = document.createElement( 'span' );
  317. domEditor.childNodes[ 2 ].appendChild( span );
  318. mutationObserver.flush();
  319. expect( lastMutations.length ).to.equal( 0 );
  320. } );
  321. } );
  322. function expectDomEditorNotToChange() {
  323. expect( domEditor.childNodes.length ).to.equal( 2 );
  324. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  325. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  326. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  327. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  328. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  329. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  330. }
  331. } );