8
0

document.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: treemodel */
  6. 'use strict';
  7. import Document from '/ckeditor5/core/treemodel/document.js';
  8. import RootElement from '/ckeditor5/core/treemodel/rootelement.js';
  9. import Batch from '/ckeditor5/core/treemodel/batch.js';
  10. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  11. import utils from '/ckeditor5/utils/utils.js';
  12. describe( 'Document', () => {
  13. let doc;
  14. beforeEach( () => {
  15. doc = new Document();
  16. } );
  17. describe( 'constructor', () => {
  18. it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
  19. expect( doc ).to.have.property( '_roots' ).that.is.instanceof( Map );
  20. expect( doc._roots.size ).to.equal( 1 );
  21. expect( doc.graveyard ).to.be.instanceof( RootElement );
  22. expect( doc.graveyard.getChildCount() ).to.equal( 0 );
  23. expect( doc.selection.getRanges().length ).to.equal( 1 );
  24. } );
  25. } );
  26. describe( 'rootNames', () => {
  27. it( 'should return empty iterator if no roots exist', () => {
  28. expect( utils.count( doc.rootNames ) ).to.equal( 0 );
  29. } );
  30. it( 'should return an iterator of all roots without the graveyard', () => {
  31. doc.createRoot( 'a' );
  32. doc.createRoot( 'b' );
  33. expect( Array.from( doc.rootNames ) ).to.deep.equal( [ 'a', 'b' ] );
  34. } );
  35. } );
  36. describe( 'createRoot', () => {
  37. it( 'should create a new RootElement, add it to roots map and return it', () => {
  38. let root = doc.createRoot( 'root', 'root' );
  39. expect( doc._roots.size ).to.equal( 2 );
  40. expect( root ).to.be.instanceof( RootElement );
  41. expect( root.getChildCount() ).to.equal( 0 );
  42. } );
  43. it( 'should throw an error when trying to create a second root with the same name', () => {
  44. doc.createRoot( 'root', 'root' );
  45. expect(
  46. () => {
  47. doc.createRoot( 'root', 'root' );
  48. }
  49. ).to.throw( CKEditorError, /document-createRoot-name-exists/ );
  50. } );
  51. } );
  52. describe( 'getRoot', () => {
  53. it( 'should return a RootElement previously created with given name', () => {
  54. let newRoot = doc.createRoot( 'root' );
  55. let getRoot = doc.getRoot( 'root' );
  56. expect( getRoot ).to.equal( newRoot );
  57. } );
  58. it( 'should throw an error when trying to get non-existent root', () => {
  59. expect(
  60. () => {
  61. doc.getRoot( 'root' );
  62. }
  63. ).to.throw( CKEditorError, /document-getRoot-root-not-exist/ );
  64. } );
  65. } );
  66. describe( 'applyOperation', () => {
  67. it( 'should increase document version, execute operation and fire event with proper data', () => {
  68. const changeCallback = sinon.spy();
  69. const type = 't';
  70. const data = { data: 'x' };
  71. const batch = 'batch';
  72. let operation = {
  73. type: type,
  74. delta: { batch: batch },
  75. baseVersion: 0,
  76. _execute: sinon.stub().returns( data )
  77. };
  78. doc.on( 'change', changeCallback );
  79. doc.applyOperation( operation );
  80. expect( doc.version ).to.equal( 1 );
  81. sinon.assert.calledOnce( operation._execute );
  82. sinon.assert.calledOnce( changeCallback );
  83. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  84. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  85. expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
  86. } );
  87. it( 'should throw an error on the operation base version and the document version is different', () => {
  88. let operation = {
  89. baseVersion: 1
  90. };
  91. expect(
  92. () => {
  93. doc.applyOperation( operation );
  94. }
  95. ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
  96. } );
  97. } );
  98. describe( 'batch', () => {
  99. it( 'should create a new batch with the document property', () => {
  100. const batch = doc.batch();
  101. expect( batch ).to.be.instanceof( Batch );
  102. expect( batch ).to.have.property( 'doc' ).that.equals( doc );
  103. } );
  104. } );
  105. describe( 'enqueue', () => {
  106. it( 'should be executed immediately and fire changesDone event', () => {
  107. let order = [];
  108. doc.on( 'changesDone', () => order.push( 'done' ) );
  109. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  110. expect( order ).to.have.length( 2 );
  111. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  112. expect( order[ 1 ] ).to.equal( 'done' );
  113. } );
  114. it( 'should fire done every time queue is empty', () => {
  115. let order = [];
  116. doc.on( 'changesDone', () => order.push( 'done' ) );
  117. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  118. doc.enqueueChanges( () => order.push( 'enqueue2' ) );
  119. expect( order ).to.have.length( 4 );
  120. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  121. expect( order[ 1 ] ).to.equal( 'done' );
  122. expect( order[ 2 ] ).to.equal( 'enqueue2' );
  123. expect( order[ 3 ] ).to.equal( 'done' );
  124. } );
  125. it( 'should put callbacks in the proper order', () => {
  126. let order = [];
  127. doc.on( 'changesDone', () => order.push( 'done' ) );
  128. doc.enqueueChanges( () => {
  129. order.push( 'enqueue1 start' );
  130. doc.enqueueChanges( () => {
  131. order.push( 'enqueue2 start' );
  132. doc.enqueueChanges( () => order.push( 'enqueue4' ) );
  133. order.push( 'enqueue2 end' );
  134. } );
  135. doc.enqueueChanges( () => order.push( 'enqueue3' ) );
  136. order.push( 'enqueue1 end' );
  137. } );
  138. expect( order ).to.have.length( 7 );
  139. expect( order[ 0 ] ).to.equal( 'enqueue1 start' );
  140. expect( order[ 1 ] ).to.equal( 'enqueue1 end' );
  141. expect( order[ 2 ] ).to.equal( 'enqueue2 start' );
  142. expect( order[ 3 ] ).to.equal( 'enqueue2 end' );
  143. expect( order[ 4 ] ).to.equal( 'enqueue3' );
  144. expect( order[ 5 ] ).to.equal( 'enqueue4' );
  145. expect( order[ 6 ] ).to.equal( 'done' );
  146. } );
  147. } );
  148. it( 'should update selection attributes whenever selection gets updated', () => {
  149. sinon.spy( doc.selection, '_updateAttributes' );
  150. doc.selection.fire( 'change:range' );
  151. expect( doc.selection._updateAttributes.called ).to.be.true;
  152. } );
  153. it( 'should update selection attributes whenever changes to the document are applied', () => {
  154. sinon.spy( doc.selection, '_updateAttributes' );
  155. doc.fire( 'changesDone' );
  156. expect( doc.selection._updateAttributes.called ).to.be.true;
  157. } );
  158. describe( '_getDefaultRoot', () => {
  159. it( 'should return graveyard root if there are no other roots in the document', () => {
  160. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  161. } );
  162. it( 'should return the first root added to the document', () => {
  163. let rootA = doc.createRoot( 'rootA' );
  164. doc.createRoot( 'rootB' );
  165. doc.createRoot( 'rootC' );
  166. expect( doc._getDefaultRoot() ).to.equal( rootA );
  167. } );
  168. } );
  169. } );