mutationobserver.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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, mutationObserver, lastMutations;
  13. beforeEach( () => {
  14. domEditor = document.getElementById( 'editor' );
  15. treeView = new TreeView( domEditor );
  16. mutationObserver = new MutationObserver();
  17. lastMutations = null;
  18. treeView.addObserver( mutationObserver );
  19. treeView.on( 'mutations', ( evt, mutations ) => lastMutations = mutations );
  20. treeView.viewRoot.insertChildren( 0, [
  21. new Element( 'p', [], [ new Text( 'foo' ) ] ),
  22. new Element( 'p', [], [ new Text( 'bar' ) ] )
  23. ] );
  24. treeView.render();
  25. } );
  26. afterEach( () => {
  27. mutationObserver.detach();
  28. } );
  29. it( 'should handle typing', () => {
  30. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foom';
  31. handleMutation();
  32. expectDomEditorNotToChange();
  33. expect( lastMutations.length ).to.equal( 1 );
  34. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  35. expect( lastMutations[ 0 ].node ).to.equal( treeView.viewRoot.getChild( 0 ).getChild( 0 ) );
  36. expect( lastMutations[ 0 ].newText ).to.equal( 'foom' );
  37. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  38. } );
  39. it( 'should handle bold', () => {
  40. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'f';
  41. const domB = document.createElement( 'b' );
  42. domB.appendChild( document.createTextNode( 'oo' ) );
  43. domEditor.childNodes[ 0 ].appendChild( domB );
  44. handleMutation();
  45. expectDomEditorNotToChange();
  46. expect( lastMutations.length ).to.equal( 1 );
  47. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  48. expect( lastMutations[ 0 ].node ).to.equal( treeView.viewRoot.getChild( 0 ) );
  49. expect( lastMutations[ 0 ].newChildren.length ).to.equal( 2 );
  50. expect( lastMutations[ 0 ].newChildren[ 0 ].data ).to.equal( 'f' );
  51. expect( lastMutations[ 0 ].newChildren[ 1 ].name ).to.equal( 'b' );
  52. expect( lastMutations[ 0 ].oldChildren.length ).to.equal( 1 );
  53. expect( lastMutations[ 0 ].oldChildren[ 0 ].data ).to.equal( 'foo' );
  54. } );
  55. it( 'should deduplicate text changes', () => {
  56. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'foox';
  57. domEditor.childNodes[ 0 ].childNodes[ 0 ].data = 'fooxy';
  58. handleMutation();
  59. expectDomEditorNotToChange();
  60. expect( lastMutations.length ).to.equal( 1 );
  61. expect( lastMutations[ 0 ].type ).to.equal( 'text' );
  62. expect( lastMutations[ 0 ].node ).to.equal( treeView.viewRoot.getChild( 0 ).getChild( 0 ) );
  63. expect( lastMutations[ 0 ].newText ).to.equal( 'fooxy' );
  64. expect( lastMutations[ 0 ].oldText ).to.equal( 'foo' );
  65. } );
  66. it( 'should ignore changes in elements not attached to tree view', () => {
  67. const domP = document.createElement( 'p' );
  68. const domB = document.createElement( 'b' );
  69. const domText = document.createTextNode( 'bom' );
  70. domEditor.appendChild( domP );
  71. domP.appendChild( domB );
  72. domB.appendChild( domText );
  73. handleMutation();
  74. expectDomEditorNotToChange();
  75. expect( lastMutations.length ).to.equal( 1 );
  76. expect( lastMutations[ 0 ].type ).to.equal( 'children' );
  77. expect( lastMutations[ 0 ].node ).to.equal( treeView.viewRoot );
  78. } );
  79. function handleMutation() {
  80. mutationObserver._onMutations( mutationObserver._mutationObserver.takeRecords() );
  81. }
  82. function expectDomEditorNotToChange() {
  83. expect( domEditor.childNodes.length ).to.equal( 2 );
  84. expect( domEditor.childNodes[ 0 ].tagName ).to.equal( 'P' );
  85. expect( domEditor.childNodes[ 1 ].tagName ).to.equal( 'P' );
  86. expect( domEditor.childNodes[ 0 ].childNodes.length ).to.equal( 1 );
  87. expect( domEditor.childNodes[ 0 ].childNodes[ 0 ].data ).to.equal( 'foo' );
  88. expect( domEditor.childNodes[ 1 ].childNodes.length ).to.equal( 1 );
  89. expect( domEditor.childNodes[ 1 ].childNodes[ 0 ].data ).to.equal( 'bar' );
  90. }
  91. } );