8
0

mutationobserver.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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.addObserver( 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 handle bold', () => {
  38. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'f';
  39. const domB = document.createElement( 'b' );
  40. domB.appendChild( document.createTextNode( 'oo' ) );
  41. domEditor.childNodes[ 0 ].appendChild( domB );
  42. mutationObserver.flush();
  43. expectDomEditorNotToChange();
  44. expect( lastMutations.length ).to.equal( 1 );
  45. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  46. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  47. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  48. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'f' );
  49. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  50. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  51. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  52. } );
  53. it( 'should deduplicate text changes', () => {
  54. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  55. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  56. mutationObserver.flush();
  57. expectDomEditorNotToChange();
  58. expect( lastMutations.length ).to.equal( 1 );
  59. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  60. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  61. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  62. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  63. } );
  64. it( 'should ignore changes in elements not attached to tree view', () => {
  65. const domP = document.createElement( 'p' );
  66. const domB = document.createElement( 'b' );
  67. const domText = document.createTextNode( 'bom' );
  68. domEditor.appendChild( domP );
  69. domP.appendChild( domB );
  70. domB.appendChild( domText );
  71. mutationObserver.flush();
  72. expectDomEditorNotToChange();
  73. expect( lastMutations.length ).to.equal( 1 );
  74. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  75. expect( lastMutations[ 0 ].node ).to.equal( viewRoot );
  76. } );
  77. it( 'should be able to observe multiple roots', () => {
  78. const domAdditionalEditor = document.getElementById( 'additional' );
  79. // Prepare AdditionalEditor
  80. viewDocument.createRoot( domAdditionalEditor, 'additional' );
  81. viewDocument.getRoot( 'additional' ).appendChildren(
  82. parse( '<container:p>foo</container:p><container:p>bar</container:p>' ) );
  83. // Render AdditionalEditor (first editor has been rendered in the beforeEach function)
  84. viewDocument.render();
  85. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  86. domAdditionalEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  87. mutationObserver.flush();
  88. expect( lastMutations.length ).to.equal( 2 );
  89. } );
  90. it( 'should fire nothing if there were no mutations', () => {
  91. mutationObserver.flush();
  92. expectDomEditorNotToChange();
  93. expect( lastMutations ).to.be.null;
  94. } );
  95. it( 'should fire children mutation if the mutation occurred in the inline filler', () => {
  96. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  97. viewRoot.appendChildren( view );
  98. viewDocument.selection.setTo( selection );
  99. viewDocument.render();
  100. const inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  101. inlineFiller.data += 'x';
  102. mutationObserver.flush();
  103. expect( lastMutations.length ).to.equal( 1 );
  104. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  105. expect( lastMutations[ 0 ].node ).to.equal( selection.getFirstPosition().parent );
  106. } );
  107. it( 'should have no inline filler in mutation', () => {
  108. const { view, selection } = parse( '<container:p>foo<attribute:b>[]</attribute:b>bar</container:p>' );
  109. viewRoot.appendChildren( view );
  110. viewDocument.selection.setTo( selection );
  111. viewDocument.render();
  112. let inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  113. inlineFiller.data += 'x';
  114. view.getChild( 1 ).appendChildren( parse( 'x' ) );
  115. mutationObserver.flush();
  116. viewDocument.render();
  117. inlineFiller = domEditor.childNodes[ 2 ].childNodes[ 1 ].childNodes[ 0 ];
  118. inlineFiller.data += 'y';
  119. mutationObserver.flush();
  120. expect( lastMutations.length ).to.equal( 1 );
  121. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  122. expect( lastMutations[ 0 ].oldText ).to.equal( 'x' );
  123. expect( lastMutations[ 0 ].newText ).to.equal( 'xy' );
  124. } );
  125. it( 'should have no block filler in mutation', () => {
  126. viewRoot.appendChildren( parse( '<container:p></container:p>' ) );
  127. viewDocument.render();
  128. const domP = domEditor.childNodes[ 2 ];
  129. domP.removeChild( domP.childNodes[ 0 ] );
  130. domP.appendChild( document.createTextNode( 'foo' ) );
  131. mutationObserver.flush();
  132. expect( lastMutations.length ).to.equal( 1 );
  133. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 1 );
  134. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'foo' );
  135. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 0 );
  136. } );
  137. function expectDomEditorNotToChange() {
  138. expect( domEditor.childNodes.length ).to.equal( 2 );
  139. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  140. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  141. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  142. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  143. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  144. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  145. }
  146. } );