mutationobserver.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 { parse } from '../../../src/dev-utils/view';
  9. describe( 'MutationObserver', () => {
  10. let domEditor, viewDocument, viewRoot, mutationObserver, lastMutations, domRoot;
  11. beforeEach( () => {
  12. domRoot = document.createElement( 'div' );
  13. domRoot.innerHTML = '<div contenteditable="true" id="main"></div><div contenteditable="true" id="additional"></div>';
  14. document.body.appendChild( domRoot );
  15. viewDocument = new ViewDocument();
  16. domEditor = document.getElementById( 'main' );
  17. lastMutations = null;
  18. viewDocument.createRoot( domEditor );
  19. viewDocument.selection.removeAllRanges();
  20. document.getSelection().removeAllRanges();
  21. mutationObserver = viewDocument.getObserver( MutationObserver );
  22. viewDocument.on( 'mutations', ( evt, mutations ) => {
  23. lastMutations = mutations;
  24. } );
  25. viewRoot = viewDocument.getRoot();
  26. viewRoot.appendChildren( parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  27. viewDocument.render();
  28. } );
  29. afterEach( () => {
  30. mutationObserver.disable();
  31. domRoot.parentElement.removeChild( domRoot );
  32. viewDocument.destroy();
  33. } );
  34. it( 'should handle typing', () => {
  35. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  36. mutationObserver.flush();
  37. expectDomEditorNotToChange();
  38. expect( lastMutations.length ).to.equal( 1 );
  39. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  40. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  41. expect( lastMutations[ 0 ].newText ).to.equal( 'foom' );
  42. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  43. } );
  44. it( 'should not observe if disabled', () => {
  45. const additional = document.getElementById( 'additional' );
  46. mutationObserver.disable();
  47. viewDocument.createRoot( additional, 'additional' );
  48. additional.textContent = 'foobar';
  49. mutationObserver.flush();
  50. expect( lastMutations ).to.be.null;
  51. } );
  52. it( 'should handle bold', () => {
  53. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'f';
  54. const domB = document.createElement( 'b' );
  55. domB.appendChild( document.createTextNode( 'oo' ) );
  56. domEditor.childNodes[ 0 ].appendChild( domB );
  57. mutationObserver.flush();
  58. expectDomEditorNotToChange();
  59. expect( lastMutations.length ).to.equal( 1 );
  60. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  61. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  62. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  63. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'f' );
  64. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  65. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  66. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  67. } );
  68. it( 'should deduplicate text changes', () => {
  69. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  70. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  71. mutationObserver.flush();
  72. expectDomEditorNotToChange();
  73. expect( lastMutations.length ).to.equal( 1 );
  74. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  75. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  76. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  77. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  78. } );
  79. it( 'should ignore changes in elements not attached to tree view', () => {
  80. const domP = document.createElement( 'p' );
  81. const domB = document.createElement( 'b' );
  82. const domText = document.createTextNode( 'bom' );
  83. domEditor.appendChild( domP );
  84. domP.appendChild( domB );
  85. domB.appendChild( domText );
  86. mutationObserver.flush();
  87. expectDomEditorNotToChange();
  88. expect( lastMutations.length ).to.equal( 1 );
  89. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  90. expect( lastMutations[ 0 ].node ).to.equal( viewRoot );
  91. } );
  92. it( 'should fire mutations event with view selection instance, if dom selection can be mapped to view', done => {
  93. const textNode = domEditor.childNodes[ 0 ].childNodes[ 0 ];
  94. textNode.data = 'foom';
  95. const domSelection = document.getSelection();
  96. domSelection.collapse( textNode, 4 );
  97. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  98. expect( viewSelection.anchor.parent ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  99. expect( viewSelection.anchor.offset ).to.equal( 4 );
  100. done();
  101. } );
  102. mutationObserver.flush();
  103. expectDomEditorNotToChange();
  104. } );
  105. it( 'should fire mutations event with viewSelection param set to null, if dom selection cannot be mapped to view', done => {
  106. const textNode = domEditor.ownerDocument.createTextNode( 'foo' );
  107. domEditor.childNodes[ 0 ].appendChild( textNode );
  108. const domSelection = document.getSelection();
  109. domSelection.collapse( textNode, 3 );
  110. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  111. expect( viewSelection ).to.be.null;
  112. done();
  113. } );
  114. mutationObserver.flush();
  115. expectDomEditorNotToChange();
  116. } );
  117. it( 'should be able to observe multiple roots', () => {
  118. const domAdditionalEditor = document.getElementById( 'additional' );
  119. // Prepare AdditionalEditor
  120. viewDocument.createRoot( domAdditionalEditor, 'additional' );
  121. viewDocument.getRoot( 'additional' ).appendChildren(
  122. parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  123. // Render AdditionalEditor (first editor has been rendered in the beforeEach function)
  124. viewDocument.render();
  125. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  126. domAdditionalEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  127. mutationObserver.flush();
  128. expect( lastMutations.length ).to.equal( 2 );
  129. } );
  130. it( 'should fire nothing if there were no mutations', () => {
  131. mutationObserver.flush();
  132. expectDomEditorNotToChange();
  133. expect( lastMutations ).to.be.null;
  134. } );
  135. it( 'should fire children mutation if the mutation occurred in the inline filler', () => {
  136. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  137. viewRoot.appendChildren( view );
  138. viewDocument.selection.setTo( selection );
  139. viewDocument.render();
  140. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  141. inlineFiller.data += 'x';
  142. mutationObserver.flush();
  143. expect( lastMutations.length ).to.equal( 1 );
  144. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  145. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  146. } );
  147. it( 'should have no inline filler in mutation', () => {
  148. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  149. viewRoot.appendChildren( view );
  150. viewDocument.selection.setTo( selection );
  151. viewDocument.render();
  152. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  153. inlineFiller.data += 'x';
  154. view.getChild( 1 ).appendChildren( parse( 'x' ) );
  155. mutationObserver.flush();
  156. viewDocument.render();
  157. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  158. inlineFiller.data += 'y';
  159. mutationObserver.flush();
  160. expect( lastMutations.length ).to.equal( 1 );
  161. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  162. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  163. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  164. } );
  165. it( 'should have no block filler in mutation', () => {
  166. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  167. viewDocument.render();
  168. const domP = domEditor.childNodes[ 2 ];
  169. domP.removeChild( domP.childNodes[ 0 ] );
  170. domP.appendChild( document.createTextNode( 'foo' ) );
  171. mutationObserver.flush();
  172. expect( lastMutations.length ).to.equal( 1 );
  173. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  174. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  175. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  176. } );
  177. it( 'should ignore mutation with bogus br inserted on the end of the empty paragraph', () => {
  178. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  179. viewDocument.render();
  180. const domP = domEditor.childNodes[ 2 ];
  181. domP.appendChild( document.createElement( 'br' ) );
  182. mutationObserver.flush();
  183. expect( lastMutations.length ).to.equal( 0 );
  184. } );
  185. it( 'should ignore mutation with bogus br inserted on the end of the paragraph with text', () => {
  186. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  187. viewDocument.render();
  188. const domP = domEditor.childNodes[ 2 ];
  189. domP.appendChild( document.createElement( 'br' ) );
  190. mutationObserver.flush();
  191. expect( lastMutations.length ).to.equal( 0 );
  192. } );
  193. it( 'should ignore mutation with bogus br inserted on the end of the paragraph while processing text mutations', () => {
  194. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  195. viewDocument.render();
  196. const domP = domEditor.childNodes[ 2 ];
  197. domP.childNodes[ 0 ].data = 'foo ';
  198. domP.appendChild( document.createElement( 'br' ) );
  199. mutationObserver.flush();
  200. expect( lastMutations.length ).to.equal( 1 );
  201. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  202. expect( lastMutations[ 0 ].newText ).to.equal( 'foo ' );
  203. } );
  204. it( 'should not ignore mutation with br inserted not on the end of the paragraph', () => {
  205. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  206. viewDocument.render();
  207. const domP = domEditor.childNodes[ 2 ];
  208. domP.insertBefore( document.createElement( 'br' ), domP.childNodes[ 0 ] );
  209. mutationObserver.flush();
  210. expect( lastMutations.length ).to.equal( 1 );
  211. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  212. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'br' );
  213. expect( lastMutations[ 0 ].newChildren[ 1 ].data ).to.equal( 'foo' );
  214. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  215. } );
  216. it( 'should not ignore mutation inserting element different than br on the end of the empty paragraph', () => {
  217. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  218. viewDocument.render();
  219. const domP = domEditor.childNodes[ 2 ];
  220. domP.appendChild( document.createElement( 'span' ) );
  221. mutationObserver.flush();
  222. expect( lastMutations.length ).to.equal( 1 );
  223. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  224. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'span' );
  225. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  226. } );
  227. it( 'should not ignore mutation inserting element different than br on the end of the paragraph with text', () => {
  228. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  229. viewDocument.render();
  230. const domP = domEditor.childNodes[ 2 ];
  231. domP.appendChild( document.createElement( 'span' ) );
  232. mutationObserver.flush();
  233. expect( lastMutations.length ).to.equal( 1 );
  234. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  235. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  236. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'span' );
  237. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  238. } );
  239. function expectDomEditorNotToChange() {
  240. expect( domEditor.childNodes.length ).to.equal( 2 );
  241. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  242. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  243. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  244. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  245. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  246. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  247. }
  248. } );