mutationobserver.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 deduplicate text changes', () => {
  70. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  71. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  72. mutationObserver.flush();
  73. expectDomEditorNotToChange();
  74. expect( lastMutations.length ).to.equal( 1 );
  75. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  76. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  77. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  78. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  79. } );
  80. it( 'should ignore changes in elements not attached to tree view', () => {
  81. const domP = document.createElement( 'p' );
  82. const domB = document.createElement( 'b' );
  83. const domText = document.createTextNode( 'bom' );
  84. domEditor.appendChild( domP );
  85. domP.appendChild( domB );
  86. domB.appendChild( domText );
  87. mutationObserver.flush();
  88. expectDomEditorNotToChange();
  89. expect( lastMutations.length ).to.equal( 1 );
  90. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  91. expect( lastMutations[ 0 ].node ).to.equal( viewRoot );
  92. } );
  93. it( 'should fire mutations event with view selection instance, if dom selection can be mapped to view', done => {
  94. const textNode = domEditor.childNodes[ 0 ].childNodes[ 0 ];
  95. textNode.data = 'foom';
  96. const domSelection = document.getSelection();
  97. domSelection.collapse( textNode, 4 );
  98. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  99. expect( viewSelection.anchor.parent ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  100. expect( viewSelection.anchor.offset ).to.equal( 4 );
  101. done();
  102. } );
  103. mutationObserver.flush();
  104. expectDomEditorNotToChange();
  105. } );
  106. it( 'should fire mutations event with viewSelection param set to null, if dom selection cannot be mapped to view', done => {
  107. const textNode = domEditor.ownerDocument.createTextNode( 'foo' );
  108. domEditor.childNodes[ 0 ].appendChild( textNode );
  109. const domSelection = document.getSelection();
  110. domSelection.collapse( textNode, 3 );
  111. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  112. expect( viewSelection ).to.be.null;
  113. done();
  114. } );
  115. mutationObserver.flush();
  116. expectDomEditorNotToChange();
  117. } );
  118. it( 'should be able to observe multiple roots', () => {
  119. const domAdditionalEditor = document.getElementById( 'additional' );
  120. // Prepare AdditionalEditor
  121. viewDocument.createRoot( domAdditionalEditor, 'additional' );
  122. viewDocument.getRoot( 'additional' ).appendChildren(
  123. parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  124. // Render AdditionalEditor (first editor has been rendered in the beforeEach function)
  125. viewDocument.render();
  126. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  127. domAdditionalEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  128. mutationObserver.flush();
  129. expect( lastMutations.length ).to.equal( 2 );
  130. } );
  131. it( 'should fire nothing if there were no mutations', () => {
  132. mutationObserver.flush();
  133. expectDomEditorNotToChange();
  134. expect( lastMutations ).to.be.null;
  135. } );
  136. it( 'should fire children mutation if the mutation occurred in the inline filler', () => {
  137. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  138. viewRoot.appendChildren( view );
  139. viewDocument.selection.setTo( selection );
  140. viewDocument.render();
  141. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  142. inlineFiller.data += 'x';
  143. mutationObserver.flush();
  144. expect( lastMutations.length ).to.equal( 1 );
  145. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  146. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  147. } );
  148. it( 'should have no inline filler in mutation', () => {
  149. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  150. viewRoot.appendChildren( view );
  151. viewDocument.selection.setTo( selection );
  152. viewDocument.render();
  153. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  154. inlineFiller.data += 'x';
  155. view.getChild( 1 ).appendChildren( parse( 'x' ) );
  156. mutationObserver.flush();
  157. viewDocument.render();
  158. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  159. inlineFiller.data += 'y';
  160. mutationObserver.flush();
  161. expect( lastMutations.length ).to.equal( 1 );
  162. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  163. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  164. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  165. } );
  166. it( 'should have no block filler in mutation', () => {
  167. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  168. viewDocument.render();
  169. const domP = domEditor.childNodes[ 2 ];
  170. domP.removeChild( domP.childNodes[ 0 ] );
  171. domP.appendChild( document.createTextNode( 'foo' ) );
  172. mutationObserver.flush();
  173. expect( lastMutations.length ).to.equal( 1 );
  174. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  175. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  176. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  177. } );
  178. it( 'should ignore mutation with bogus br inserted on the end of the empty paragraph', () => {
  179. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  180. viewDocument.render();
  181. const domP = domEditor.childNodes[ 2 ];
  182. domP.appendChild( document.createElement( 'br' ) );
  183. mutationObserver.flush();
  184. expect( lastMutations.length ).to.equal( 0 );
  185. } );
  186. it( 'should ignore mutation with bogus br inserted on the end of the paragraph with text', () => {
  187. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  188. viewDocument.render();
  189. const domP = domEditor.childNodes[ 2 ];
  190. domP.appendChild( document.createElement( 'br' ) );
  191. mutationObserver.flush();
  192. expect( lastMutations.length ).to.equal( 0 );
  193. } );
  194. it( 'should ignore mutation with bogus br inserted on the end of the paragraph while processing text mutations', () => {
  195. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  196. viewDocument.render();
  197. const domP = domEditor.childNodes[ 2 ];
  198. domP.childNodes[ 0 ].data = 'foo ';
  199. domP.appendChild( document.createElement( 'br' ) );
  200. mutationObserver.flush();
  201. expect( lastMutations.length ).to.equal( 1 );
  202. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  203. expect( lastMutations[ 0 ].newText ).to.equal( 'foo ' );
  204. } );
  205. it( 'should not ignore mutation with br inserted not on the end of the paragraph', () => {
  206. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  207. viewDocument.render();
  208. const domP = domEditor.childNodes[ 2 ];
  209. domP.insertBefore( document.createElement( 'br' ), domP.childNodes[ 0 ] );
  210. mutationObserver.flush();
  211. expect( lastMutations.length ).to.equal( 1 );
  212. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  213. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'br' );
  214. expect( lastMutations[ 0 ].newChildren[ 1 ].data ).to.equal( 'foo' );
  215. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  216. } );
  217. it( 'should not ignore mutation inserting element different than br on the end of the empty paragraph', () => {
  218. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  219. viewDocument.render();
  220. const domP = domEditor.childNodes[ 2 ];
  221. domP.appendChild( document.createElement( 'span' ) );
  222. mutationObserver.flush();
  223. expect( lastMutations.length ).to.equal( 1 );
  224. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  225. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'span' );
  226. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  227. } );
  228. it( 'should not ignore mutation inserting element different than br on the end of the paragraph with text', () => {
  229. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  230. viewDocument.render();
  231. const domP = domEditor.childNodes[ 2 ];
  232. domP.appendChild( document.createElement( 'span' ) );
  233. mutationObserver.flush();
  234. expect( lastMutations.length ).to.equal( 1 );
  235. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  236. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  237. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'span' );
  238. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  239. } );
  240. describe( 'UIElement integration', () => {
  241. class MyUIElement extends UIElement {
  242. render( domDocument ) {
  243. const root = super.render( domDocument );
  244. root.innerHTML = 'foo bar';
  245. return root;
  246. }
  247. }
  248. beforeEach( () => {
  249. const uiElement = new MyUIElement( 'div' );
  250. viewRoot.appendChildren( uiElement );
  251. viewDocument.render();
  252. } );
  253. it( 'should not collect text mutations from UIElement', () => {
  254. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  255. mutationObserver.flush();
  256. expect( lastMutations.length ).to.equal( 0 );
  257. } );
  258. it( 'should not collect child mutations from UIElement', () => {
  259. const span = document.createElement( 'span' );
  260. domEditor.childNodes[ 2 ].appendChild( span );
  261. mutationObserver.flush();
  262. expect( lastMutations.length ).to.equal( 0 );
  263. } );
  264. } );
  265. function expectDomEditorNotToChange() {
  266. expect( domEditor.childNodes.length ).to.equal( 2 );
  267. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  268. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  269. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  270. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  271. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  272. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  273. }
  274. } );