mutationobserver.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: view, browser-only */
  6. import ViewDocument from '/ckeditor5/engine/view/document.js';
  7. import MutationObserver from '/ckeditor5/engine/view/observer/mutationobserver.js';
  8. import { parse } from '/tests/engine/_utils/view.js';
  9. describe( 'MutationObserver', () => {
  10. let domEditor, viewDocument, viewRoot, mutationObserver, lastMutations;
  11. beforeEach( () => {
  12. viewDocument = new ViewDocument();
  13. domEditor = document.getElementById( 'main' );
  14. lastMutations = null;
  15. viewDocument.createRoot( domEditor );
  16. viewDocument.selection.removeAllRanges();
  17. document.getSelection().removeAllRanges();
  18. mutationObserver = viewDocument.getObserver( MutationObserver );
  19. viewDocument.on( 'mutations', ( evt, mutations ) => lastMutations = mutations );
  20. viewRoot = viewDocument.getRoot();
  21. viewRoot.appendChildren( parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  22. viewDocument.render();
  23. } );
  24. afterEach( () => {
  25. mutationObserver.disable();
  26. } );
  27. it( 'should handle typing', () => {
  28. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  29. mutationObserver.flush();
  30. expectDomEditorNotToChange();
  31. expect( lastMutations.length ).to.equal( 1 );
  32. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  33. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  34. expect( lastMutations[ 0 ].newText ).to.equal( 'foom' );
  35. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  36. } );
  37. it( 'should not observe if disabled', () => {
  38. const additional = document.getElementById( 'additional' );
  39. mutationObserver.disable();
  40. viewDocument.createRoot( additional, 'additional' );
  41. additional.textContent = 'foobar';
  42. mutationObserver.flush();
  43. expect( lastMutations ).to.be.null;
  44. } );
  45. it( 'should handle bold', () => {
  46. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'f';
  47. const domB = document.createElement( 'b' );
  48. domB.appendChild( document.createTextNode( 'oo' ) );
  49. domEditor.childNodes[ 0 ].appendChild( domB );
  50. mutationObserver.flush();
  51. expectDomEditorNotToChange();
  52. expect( lastMutations.length ).to.equal( 1 );
  53. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  54. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  55. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  56. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'f' );
  57. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  58. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  59. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  60. } );
  61. it( 'should deduplicate text changes', () => {
  62. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  63. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  64. mutationObserver.flush();
  65. expectDomEditorNotToChange();
  66. expect( lastMutations.length ).to.equal( 1 );
  67. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  68. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  69. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  70. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  71. } );
  72. it( 'should ignore changes in elements not attached to tree view', () => {
  73. const domP = document.createElement( 'p' );
  74. const domB = document.createElement( 'b' );
  75. const domText = document.createTextNode( 'bom' );
  76. domEditor.appendChild( domP );
  77. domP.appendChild( domB );
  78. domB.appendChild( domText );
  79. mutationObserver.flush();
  80. expectDomEditorNotToChange();
  81. expect( lastMutations.length ).to.equal( 1 );
  82. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  83. expect( lastMutations[ 0 ].node ).to.equal( viewRoot );
  84. } );
  85. it( 'should be able to observe multiple roots', () => {
  86. const domAdditionalEditor = document.getElementById( 'additional' );
  87. // Prepare AdditionalEditor
  88. viewDocument.createRoot( domAdditionalEditor, 'additional' );
  89. viewDocument.getRoot( 'additional' ).appendChildren(
  90. parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  91. // Render AdditionalEditor (first editor has been rendered in the beforeEach function)
  92. viewDocument.render();
  93. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  94. domAdditionalEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  95. mutationObserver.flush();
  96. expect( lastMutations.length ).to.equal( 2 );
  97. } );
  98. it( 'should fire nothing if there were no mutations', () => {
  99. mutationObserver.flush();
  100. expectDomEditorNotToChange();
  101. expect( lastMutations ).to.be.null;
  102. } );
  103. it( 'should fire children mutation if the mutation occurred in the inline filler', () => {
  104. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  105. viewRoot.appendChildren( view );
  106. viewDocument.selection.setTo( selection );
  107. viewDocument.render();
  108. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  109. inlineFiller.data += 'x';
  110. mutationObserver.flush();
  111. expect( lastMutations.length ).to.equal( 1 );
  112. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  113. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  114. } );
  115. it( 'should have no inline filler in mutation', () => {
  116. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  117. viewRoot.appendChildren( view );
  118. viewDocument.selection.setTo( selection );
  119. viewDocument.render();
  120. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  121. inlineFiller.data += 'x';
  122. view.getChild( 1 ).appendChildren( parse( 'x' ) );
  123. mutationObserver.flush();
  124. viewDocument.render();
  125. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  126. inlineFiller.data += 'y';
  127. mutationObserver.flush();
  128. expect( lastMutations.length ).to.equal( 1 );
  129. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  130. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  131. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  132. } );
  133. it( 'should have no block filler in mutation', () => {
  134. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  135. viewDocument.render();
  136. const domP = domEditor.childNodes[ 2 ];
  137. domP.removeChild( domP.childNodes[ 0 ] );
  138. domP.appendChild( document.createTextNode( 'foo' ) );
  139. mutationObserver.flush();
  140. expect( lastMutations.length ).to.equal( 1 );
  141. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  142. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  143. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  144. } );
  145. function expectDomEditorNotToChange() {
  146. expect( domEditor.childNodes.length ).to.equal( 2 );
  147. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  148. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  149. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  150. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  151. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  152. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  153. }
  154. } );