mutationobserver.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: view, browser-only */
  7. import ViewDocument from 'ckeditor5/engine/view/document.js';
  8. import MutationObserver from 'ckeditor5/engine/view/observer/mutationobserver.js';
  9. import { parse } from 'ckeditor5/engine/dev-utils/view.js';
  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 ) => lastMutations = mutations );
  24. viewRoot = viewDocument.getRoot();
  25. viewRoot.appendChildren( parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  26. viewDocument.render();
  27. } );
  28. afterEach( () => {
  29. mutationObserver.disable();
  30. domRoot.parentElement.removeChild( domRoot );
  31. viewDocument.destroy();
  32. } );
  33. it( 'should handle typing', () => {
  34. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  35. mutationObserver.flush();
  36. expectDomEditorNotToChange();
  37. expect( lastMutations.length ).to.equal( 1 );
  38. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  39. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  40. expect( lastMutations[ 0 ].newText ).to.equal( 'foom' );
  41. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  42. } );
  43. it( 'should not observe if disabled', () => {
  44. const additional = document.getElementById( 'additional' );
  45. mutationObserver.disable();
  46. viewDocument.createRoot( additional, 'additional' );
  47. additional.textContent = 'foobar';
  48. mutationObserver.flush();
  49. expect( lastMutations ).to.be.null;
  50. } );
  51. it( 'should handle bold', () => {
  52. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'f';
  53. const domB = document.createElement( 'b' );
  54. domB.appendChild( document.createTextNode( 'oo' ) );
  55. domEditor.childNodes[ 0 ].appendChild( domB );
  56. mutationObserver.flush();
  57. expectDomEditorNotToChange();
  58. expect( lastMutations.length ).to.equal( 1 );
  59. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  60. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  61. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  62. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'f' );
  63. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  64. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  65. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  66. } );
  67. it( 'should deduplicate text changes', () => {
  68. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  69. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  70. mutationObserver.flush();
  71. expectDomEditorNotToChange();
  72. expect( lastMutations.length ).to.equal( 1 );
  73. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  74. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  75. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  76. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  77. } );
  78. it( 'should ignore changes in elements not attached to tree view', () => {
  79. const domP = document.createElement( 'p' );
  80. const domB = document.createElement( 'b' );
  81. const domText = document.createTextNode( 'bom' );
  82. domEditor.appendChild( domP );
  83. domP.appendChild( domB );
  84. domB.appendChild( domText );
  85. mutationObserver.flush();
  86. expectDomEditorNotToChange();
  87. expect( lastMutations.length ).to.equal( 1 );
  88. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  89. expect( lastMutations[ 0 ].node ).to.equal( viewRoot );
  90. } );
  91. it( 'should fire mutations event with view selection instance, if dom selection can be mapped to view', ( done ) => {
  92. const textNode = domEditor.childNodes[ 0 ].childNodes[ 0 ];
  93. textNode.data = 'foom';
  94. const domSelection = document.getSelection();
  95. domSelection.collapse( textNode, 4 );
  96. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  97. expect( viewSelection.anchor.parent ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  98. expect( viewSelection.anchor.offset ).to.equal( 4 );
  99. done();
  100. } );
  101. mutationObserver.flush();
  102. expectDomEditorNotToChange();
  103. } );
  104. it( 'should fire mutations event with viewSelection param set to null, if dom selection cannot be mapped to view', ( done ) => {
  105. const textNode = domEditor.ownerDocument.createTextNode( 'foo' );
  106. domEditor.childNodes[ 0 ].appendChild( textNode );
  107. const domSelection = document.getSelection();
  108. domSelection.collapse( textNode, 3 );
  109. viewDocument.on( 'mutations', ( evt, viewMutations, viewSelection ) => {
  110. expect( viewSelection ).to.be.null;
  111. done();
  112. } );
  113. mutationObserver.flush();
  114. expectDomEditorNotToChange();
  115. } );
  116. it( 'should be able to observe multiple roots', () => {
  117. const domAdditionalEditor = document.getElementById( 'additional' );
  118. // Prepare AdditionalEditor
  119. viewDocument.createRoot( domAdditionalEditor, 'additional' );
  120. viewDocument.getRoot( 'additional' ).appendChildren(
  121. parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  122. // Render AdditionalEditor (first editor has been rendered in the beforeEach function)
  123. viewDocument.render();
  124. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  125. domAdditionalEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  126. mutationObserver.flush();
  127. expect( lastMutations.length ).to.equal( 2 );
  128. } );
  129. it( 'should fire nothing if there were no mutations', () => {
  130. mutationObserver.flush();
  131. expectDomEditorNotToChange();
  132. expect( lastMutations ).to.be.null;
  133. } );
  134. it( 'should fire children mutation if the mutation occurred in the inline filler', () => {
  135. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  136. viewRoot.appendChildren( view );
  137. viewDocument.selection.setTo( selection );
  138. viewDocument.render();
  139. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  140. inlineFiller.data += 'x';
  141. mutationObserver.flush();
  142. expect( lastMutations.length ).to.equal( 1 );
  143. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  144. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  145. } );
  146. it( 'should have no inline filler in mutation', () => {
  147. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  148. viewRoot.appendChildren( view );
  149. viewDocument.selection.setTo( selection );
  150. viewDocument.render();
  151. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  152. inlineFiller.data += 'x';
  153. view.getChild( 1 ).appendChildren( parse( 'x' ) );
  154. mutationObserver.flush();
  155. viewDocument.render();
  156. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  157. inlineFiller.data += 'y';
  158. mutationObserver.flush();
  159. expect( lastMutations.length ).to.equal( 1 );
  160. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  161. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  162. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  163. } );
  164. it( 'should have no block filler in mutation', () => {
  165. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  166. viewDocument.render();
  167. const domP = domEditor.childNodes[ 2 ];
  168. domP.removeChild( domP.childNodes[ 0 ] );
  169. domP.appendChild( document.createTextNode( 'foo' ) );
  170. mutationObserver.flush();
  171. expect( lastMutations.length ).to.equal( 1 );
  172. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  173. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  174. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  175. } );
  176. function expectDomEditorNotToChange() {
  177. expect( domEditor.childNodes.length ).to.equal( 2 );
  178. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  179. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  180. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  181. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  182. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  183. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  184. }
  185. } );