mutationobserver.js 6.8 KB

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