mutationobserver.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 '/tests/engine/_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 be able to observe multiple roots', () => {
  87. const domAdditionalEditor = document.getElementById( 'additional' );
  88. // Prepare AdditionalEditor
  89. viewDocument.createRoot( domAdditionalEditor, 'additional' );
  90. viewDocument.getRoot( 'additional' ).appendChildren(
  91. parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  92. // Render AdditionalEditor (first editor has been rendered in the beforeEach function)
  93. viewDocument.render();
  94. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  95. domAdditionalEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  96. mutationObserver.flush();
  97. expect( lastMutations.length ).to.equal( 2 );
  98. } );
  99. it( 'should fire nothing if there were no mutations', () => {
  100. mutationObserver.flush();
  101. expectDomEditorNotToChange();
  102. expect( lastMutations ).to.be.null;
  103. } );
  104. it( 'should fire children mutation if the mutation occurred in the inline filler', () => {
  105. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  106. viewRoot.appendChildren( view );
  107. viewDocument.selection.setTo( selection );
  108. viewDocument.render();
  109. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  110. inlineFiller.data += 'x';
  111. mutationObserver.flush();
  112. expect( lastMutations.length ).to.equal( 1 );
  113. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  114. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  115. } );
  116. it( 'should have no inline filler in mutation', () => {
  117. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  118. viewRoot.appendChildren( view );
  119. viewDocument.selection.setTo( selection );
  120. viewDocument.render();
  121. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  122. inlineFiller.data += 'x';
  123. view.getChild( 1 ).appendChildren( parse( 'x' ) );
  124. mutationObserver.flush();
  125. viewDocument.render();
  126. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  127. inlineFiller.data += 'y';
  128. mutationObserver.flush();
  129. expect( lastMutations.length ).to.equal( 1 );
  130. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  131. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  132. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  133. } );
  134. it( 'should have no block filler in mutation', () => {
  135. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  136. viewDocument.render();
  137. const domP = domEditor.childNodes[ 2 ];
  138. domP.removeChild( domP.childNodes[ 0 ] );
  139. domP.appendChild( document.createTextNode( 'foo' ) );
  140. mutationObserver.flush();
  141. expect( lastMutations.length ).to.equal( 1 );
  142. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  143. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  144. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  145. } );
  146. function expectDomEditorNotToChange() {
  147. expect( domEditor.childNodes.length ).to.equal( 2 );
  148. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  149. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  150. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  151. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  152. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  153. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  154. }
  155. } );