8
0

mutationobserver.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treeview */
  6. 'use strict';
  7. import TreeView from '/ckeditor5/core/treeview/treeview.js';
  8. import Element from '/ckeditor5/core/treeview/element.js';
  9. import Text from '/ckeditor5/core/treeview/text.js';
  10. import MutationObserver from '/ckeditor5/core/treeview/observer/mutationobserver.js';
  11. describe( 'MutationObserver', () => {
  12. let domEditor, treeView, viewRoot, mutationObserver, lastMutations;
  13. beforeEach( () => {
  14. treeView = new TreeView();
  15. domEditor = document.getElementById( 'editor' );
  16. lastMutations = null;
  17. treeView.createRoot( domEditor, 'editor' );
  18. treeView.addObserver( MutationObserver );
  19. mutationObserver = Array.from( treeView._observers )[ 0 ];
  20. treeView.on( 'mutations', ( evt, mutations ) => lastMutations = mutations );
  21. viewRoot = treeView.viewRoots.get( 'editor' );
  22. viewRoot.insertChildren( 0, [
  23. new Element( 'p', [], [ new Text( 'foo' ) ] ),
  24. new Element( 'p', [], [ new Text( 'bar' ) ] )
  25. ] );
  26. treeView.render();
  27. } );
  28. afterEach( () => {
  29. mutationObserver.disable();
  30. } );
  31. it( 'should handle typing', () => {
  32. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  33. handleMutation();
  34. expectDomEditorNotToChange();
  35. expect( lastMutations.length ).to.equal( 1 );
  36. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  37. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  38. expect( lastMutations[ 0 ].newText ).to.equal( 'foom' );
  39. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  40. } );
  41. it( 'should handle bold', () => {
  42. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'f';
  43. const domB = document.createElement( 'b' );
  44. domB.appendChild( document.createTextNode( 'oo' ) );
  45. domEditor.childNodes[ 0 ].appendChild( domB );
  46. handleMutation();
  47. expectDomEditorNotToChange();
  48. expect( lastMutations.length ).to.equal( 1 );
  49. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  50. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ) );
  51. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  52. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'f' );
  53. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  54. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  55. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  56. } );
  57. it( 'should deduplicate text changes', () => {
  58. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  59. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  60. handleMutation();
  61. expectDomEditorNotToChange();
  62. expect( lastMutations.length ).to.equal( 1 );
  63. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  64. expect( lastMutations[ 0 ].node ).to.equal( viewRoot.getChild( 0 ).getChild( 0 ) );
  65. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  66. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  67. } );
  68. it( 'should ignore changes in elements not attached to tree view', () => {
  69. const domP = document.createElement( 'p' );
  70. const domB = document.createElement( 'b' );
  71. const domText = document.createTextNode( 'bom' );
  72. domEditor.appendChild( domP );
  73. domP.appendChild( domB );
  74. domB.appendChild( domText );
  75. handleMutation();
  76. expectDomEditorNotToChange();
  77. expect( lastMutations.length ).to.equal( 1 );
  78. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  79. expect( lastMutations[ 0 ].node ).to.equal( viewRoot );
  80. } );
  81. it( 'should be able to observe multiple roots', () => {
  82. const domEditor2 = document.getElementById( 'editor2' );
  83. // Prepare editor2
  84. treeView.createRoot( domEditor2, 'editor2' );
  85. treeView.viewRoots.get( 'editor2' ).insertChildren( 0, [
  86. new Element( 'p', [], [ new Text( 'foo' ) ] ),
  87. new Element( 'p', [], [ new Text( 'bar' ) ] )
  88. ] );
  89. // Render editor2 (first editor has been rendered in the beforeEach function)
  90. treeView.render();
  91. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  92. domEditor2.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  93. handleMutation();
  94. expect( lastMutations.length ).to.equal( 2 );
  95. } );
  96. function handleMutation() {
  97. mutationObserver._onMutations( mutationObserver._mutationObserver.takeRecords() );
  98. }
  99. function expectDomEditorNotToChange() {
  100. expect( domEditor.childNodes.length ).to.equal( 2 );
  101. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  102. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  103. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  104. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  105. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  106. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  107. }
  108. } );