mutationobserver.js 8.3 KB

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