mutationobserver.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. // https://github.com/ckeditor/ckeditor5/issues/692 Scenario 1.
  200. it( 'should handle space after inline filler at the end of container', () => {
  201. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b></container:p>' );
  202. viewRoot.appendChildren( view );
  203. viewDocument.selection.setTo( selection );
  204. viewDocument.render();
  205. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  206. inlineFiller.data += ' ';
  207. mutationObserver.flush();
  208. expect( lastMutations.length ).to.equal( 1 );
  209. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  210. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  211. } );
  212. // https://github.com/ckeditor/ckeditor5/issues/692 Scenario 3.
  213. it( 'should handle space after inline filler at the end of container #2', () => {
  214. const { view, selection } = parse( '<container:p>foo<attribute:b>bar</attribute:b>[]</container:p>' );
  215. viewRoot.appendChildren( view );
  216. viewDocument.selection.setTo( selection );
  217. viewDocument.render();
  218. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 2 ];
  219. inlineFiller.data += ' ';
  220. mutationObserver.flush();
  221. expect( lastMutations.length ).to.equal( 1 );
  222. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  223. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  224. } );
  225. // https://github.com/ckeditor/ckeditor5/issues/692 Scenario 2.
  226. it( 'should handle space after inline filler at the beginning of container', () => {
  227. const { view, selection } = parse( '<container:p><attribute:b>[]</attribute:b>foo</container:p>' );
  228. viewRoot.appendChildren( view );
  229. viewDocument.selection.setTo( selection );
  230. viewDocument.render();
  231. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 0 ].childNodes[ 0 ];
  232. inlineFiller.data += ' ';
  233. mutationObserver.flush();
  234. expect( lastMutations.length ).to.equal( 1 );
  235. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  236. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  237. } );
  238. it( 'should have no block filler in mutation', () => {
  239. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  240. view.render();
  241. const domP = domEditor.childNodes[ 2 ];
  242. domP.removeChild( domP.childNodes[ 0 ] );
  243. domP.appendChild( document.createTextNode( 'foo' ) );
  244. mutationObserver.flush();
  245. expect( lastMutations.length ).to.equal( 1 );
  246. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  247. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  248. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  249. } );
  250. it( 'should ignore mutation with bogus br inserted on the end of the empty paragraph', () => {
  251. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  252. view.render();
  253. const domP = domEditor.childNodes[ 2 ];
  254. domP.appendChild( document.createElement( 'br' ) );
  255. mutationObserver.flush();
  256. expect( lastMutations.length ).to.equal( 0 );
  257. } );
  258. it( 'should ignore mutation with bogus br inserted on the end of the paragraph with text', () => {
  259. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  260. view.render();
  261. const domP = domEditor.childNodes[ 2 ];
  262. domP.appendChild( document.createElement( 'br' ) );
  263. mutationObserver.flush();
  264. expect( lastMutations.length ).to.equal( 0 );
  265. } );
  266. it( 'should ignore mutation with bogus br inserted on the end of the paragraph while processing text mutations', () => {
  267. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  268. view.render();
  269. const domP = domEditor.childNodes[ 2 ];
  270. domP.childNodes[ 0 ].data = 'foo ';
  271. domP.appendChild( document.createElement( 'br' ) );
  272. mutationObserver.flush();
  273. expect( lastMutations.length ).to.equal( 1 );
  274. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  275. expect( lastMutations[ 0 ].newText ).to.equal( 'foo ' );
  276. } );
  277. it( 'should ignore child mutations which resulted in no changes – when element contains elements', () => {
  278. viewRoot.appendChildren( parse( '<container:p><container:x></container:x></container:p>' ) );
  279. view.render();
  280. const domP = domEditor.childNodes[ 2 ];
  281. const domY = document.createElement( 'y' );
  282. domP.appendChild( domY );
  283. domY.remove();
  284. mutationObserver.flush();
  285. expect( lastMutations.length ).to.equal( 0 );
  286. } );
  287. // This case is more tricky than the previous one because DOMConverter will return a different
  288. // instances of view text nodes every time it converts a DOM text node.
  289. it( 'should ignore child mutations which resulted in no changes – when element contains text nodes', () => {
  290. const domP = domEditor.childNodes[ 0 ];
  291. const domText = document.createTextNode( 'x' );
  292. domP.appendChild( domText );
  293. domText.remove();
  294. const domP2 = domEditor.childNodes[ 1 ];
  295. domP2.appendChild( document.createTextNode( 'x' ) );
  296. mutationObserver.flush();
  297. // There was only P2 change. P1 must be ignored.
  298. const viewP2 = viewRoot.getChild( 1 );
  299. expect( lastMutations.length ).to.equal( 1 );
  300. expect( lastMutations[ 0 ].node ).to.equal( viewP2 );
  301. } );
  302. it( 'should not ignore mutation with br inserted not on the end of the paragraph', () => {
  303. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  304. view.render();
  305. const domP = domEditor.childNodes[ 2 ];
  306. domP.insertBefore( document.createElement( 'br' ), domP.childNodes[ 0 ] );
  307. mutationObserver.flush();
  308. expect( lastMutations.length ).to.equal( 1 );
  309. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  310. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'br' );
  311. expect( lastMutations[ 0 ].newChildren[ 1 ].data ).to.equal( 'foo' );
  312. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  313. } );
  314. it( 'should not ignore mutation inserting element different than br on the end of the empty paragraph', () => {
  315. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  316. view.render();
  317. const domP = domEditor.childNodes[ 2 ];
  318. domP.appendChild( document.createElement( 'span' ) );
  319. mutationObserver.flush();
  320. expect( lastMutations.length ).to.equal( 1 );
  321. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  322. expect( lastMutations[ 0 ].newChildren[ 0 ].name ).to.equal( 'span' );
  323. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  324. } );
  325. it( 'should not ignore mutation inserting element different than br on the end of the paragraph with text', () => {
  326. viewRoot.appendChildren( parse( '<container:p>foo</container:p>' ) );
  327. view.render();
  328. const domP = domEditor.childNodes[ 2 ];
  329. domP.appendChild( document.createElement( 'span' ) );
  330. mutationObserver.flush();
  331. expect( lastMutations.length ).to.equal( 1 );
  332. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  333. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  334. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'span' );
  335. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  336. } );
  337. describe( 'UIElement integration', () => {
  338. function createUIElement( name ) {
  339. const element = new UIElement( name );
  340. element.render = function( domDocument ) {
  341. const root = this.toDomElement( domDocument );
  342. root.innerHTML = 'foo bar';
  343. return root;
  344. };
  345. return element;
  346. }
  347. beforeEach( () => {
  348. const uiElement = createUIElement( 'div' );
  349. viewRoot.appendChildren( uiElement );
  350. view.render();
  351. } );
  352. it( 'should not collect text mutations from UIElement', () => {
  353. domEditor.childNodes[ 2 ].childNodes[ 0 ].data = 'foom';
  354. mutationObserver.flush();
  355. expect( lastMutations.length ).to.equal( 0 );
  356. } );
  357. it( 'should not collect child mutations from UIElement', () => {
  358. const span = document.createElement( 'span' );
  359. domEditor.childNodes[ 2 ].appendChild( span );
  360. mutationObserver.flush();
  361. expect( lastMutations.length ).to.equal( 0 );
  362. } );
  363. } );
  364. function expectDomEditorNotToChange() {
  365. expect( domEditor.childNodes.length ).to.equal( 2 );
  366. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  367. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  368. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  369. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  370. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  371. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  372. }
  373. } );