document.js 6.8 KB

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