mutationobserver.js 8.5 KB

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