8
0

document.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: model */
  6. 'use strict';
  7. import Document from '/ckeditor5/engine/model/document.js';
  8. import Schema from '/ckeditor5/engine/model/schema.js';
  9. import Composer from '/ckeditor5/engine/model/composer/composer.js';
  10. import RootElement from '/ckeditor5/engine/model/rootelement.js';
  11. import Batch from '/ckeditor5/engine/model/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( '$root', 'a' );
  36. doc.createRoot( '$root', 'b' );
  37. expect( Array.from( doc.rootNames ) ).to.deep.equal( [ 'a', 'b' ] );
  38. } );
  39. } );
  40. describe( 'createRoot', () => {
  41. it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
  42. let root = doc.createRoot();
  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. expect( root ).to.have.property( 'rootName', 'main' );
  48. } );
  49. it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
  50. let root = doc.createRoot( 'customElementName', 'customRootName' );
  51. expect( doc._roots.size ).to.equal( 2 );
  52. expect( root ).to.be.instanceof( RootElement );
  53. expect( root.getChildCount() ).to.equal( 0 );
  54. expect( root ).to.have.property( 'name', 'customElementName' );
  55. expect( root ).to.have.property( 'rootName', 'customRootName' );
  56. } );
  57. it( 'should throw an error when trying to create a second root with the same name', () => {
  58. doc.createRoot( '$root', 'rootName' );
  59. expect(
  60. () => {
  61. doc.createRoot( '$root', 'rootName' );
  62. }
  63. ).to.throw( CKEditorError, /document-createRoot-name-exists/ );
  64. } );
  65. } );
  66. describe( 'getRoot', () => {
  67. it( 'should return a RootElement previously created with given name', () => {
  68. let newRoot = doc.createRoot();
  69. let getRoot = doc.getRoot();
  70. expect( getRoot ).to.equal( newRoot );
  71. } );
  72. it( 'should throw an error when trying to get non-existent root', () => {
  73. expect(
  74. () => {
  75. doc.getRoot( 'root' );
  76. }
  77. ).to.throw( CKEditorError, /document-getRoot-root-not-exist/ );
  78. } );
  79. } );
  80. describe( 'hasRoot', () => {
  81. it( 'should return true when Document has RootElement with given name', () => {
  82. doc.createRoot();
  83. expect( doc.hasRoot( 'main' ) ).to.be.true;
  84. } );
  85. it( 'should return false when Document does not have RootElement with given name', () => {
  86. expect( doc.hasRoot( 'noroot' ) ).to.be.false;
  87. } );
  88. } );
  89. describe( 'applyOperation', () => {
  90. it( 'should increase document version, execute operation and fire event with proper data', () => {
  91. const changeCallback = sinon.spy();
  92. const type = 't';
  93. const data = { data: 'x' };
  94. const batch = 'batch';
  95. let operation = {
  96. type: type,
  97. delta: { batch: batch },
  98. baseVersion: 0,
  99. _execute: sinon.stub().returns( data )
  100. };
  101. doc.on( 'change', changeCallback );
  102. doc.applyOperation( operation );
  103. expect( doc.version ).to.equal( 1 );
  104. sinon.assert.calledOnce( operation._execute );
  105. sinon.assert.calledOnce( changeCallback );
  106. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  107. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  108. expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
  109. } );
  110. it( 'should throw an error on the operation base version and the document version is different', () => {
  111. let operation = {
  112. baseVersion: 1
  113. };
  114. expect(
  115. () => {
  116. doc.applyOperation( operation );
  117. }
  118. ).to.throw( CKEditorError, /document-applyOperation-wrong-version/ );
  119. } );
  120. } );
  121. describe( 'batch', () => {
  122. it( 'should create a new batch with the document property', () => {
  123. const batch = doc.batch();
  124. expect( batch ).to.be.instanceof( Batch );
  125. expect( batch ).to.have.property( 'doc' ).that.equals( doc );
  126. } );
  127. it( 'should set given batch type', () => {
  128. const batch = doc.batch( 'ignore' );
  129. expect( batch ).to.have.property( 'type' ).that.equals( 'ignore' );
  130. } );
  131. } );
  132. describe( 'enqueue', () => {
  133. it( 'should be executed immediately and fire changesDone event', () => {
  134. let order = [];
  135. doc.on( 'changesDone', () => order.push( 'done' ) );
  136. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  137. expect( order ).to.have.length( 2 );
  138. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  139. expect( order[ 1 ] ).to.equal( 'done' );
  140. } );
  141. it( 'should fire done every time queue is empty', () => {
  142. let order = [];
  143. doc.on( 'changesDone', () => order.push( 'done' ) );
  144. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  145. doc.enqueueChanges( () => order.push( 'enqueue2' ) );
  146. expect( order ).to.have.length( 4 );
  147. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  148. expect( order[ 1 ] ).to.equal( 'done' );
  149. expect( order[ 2 ] ).to.equal( 'enqueue2' );
  150. expect( order[ 3 ] ).to.equal( 'done' );
  151. } );
  152. it( 'should put callbacks in the proper order', () => {
  153. let order = [];
  154. doc.on( 'changesDone', () => order.push( 'done' ) );
  155. doc.enqueueChanges( () => {
  156. order.push( 'enqueue1 start' );
  157. doc.enqueueChanges( () => {
  158. order.push( 'enqueue2 start' );
  159. doc.enqueueChanges( () => order.push( 'enqueue4' ) );
  160. order.push( 'enqueue2 end' );
  161. } );
  162. doc.enqueueChanges( () => order.push( 'enqueue3' ) );
  163. order.push( 'enqueue1 end' );
  164. } );
  165. expect( order ).to.have.length( 7 );
  166. expect( order[ 0 ] ).to.equal( 'enqueue1 start' );
  167. expect( order[ 1 ] ).to.equal( 'enqueue1 end' );
  168. expect( order[ 2 ] ).to.equal( 'enqueue2 start' );
  169. expect( order[ 3 ] ).to.equal( 'enqueue2 end' );
  170. expect( order[ 4 ] ).to.equal( 'enqueue3' );
  171. expect( order[ 5 ] ).to.equal( 'enqueue4' );
  172. expect( order[ 6 ] ).to.equal( 'done' );
  173. } );
  174. } );
  175. it( 'should update selection attributes whenever selection gets updated', () => {
  176. sinon.spy( doc.selection, '_updateAttributes' );
  177. doc.selection.fire( 'change:range' );
  178. expect( doc.selection._updateAttributes.called ).to.be.true;
  179. } );
  180. it( 'should update selection attributes whenever changes to the document are applied', () => {
  181. sinon.spy( doc.selection, '_updateAttributes' );
  182. doc.fire( 'changesDone' );
  183. expect( doc.selection._updateAttributes.called ).to.be.true;
  184. } );
  185. describe( '_getDefaultRoot', () => {
  186. it( 'should return graveyard root if there are no other roots in the document', () => {
  187. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  188. } );
  189. it( 'should return the first root added to the document', () => {
  190. let rootA = doc.createRoot( '$root', 'rootA' );
  191. doc.createRoot( '$root', 'rootB' );
  192. doc.createRoot( '$root', 'rootC' );
  193. expect( doc._getDefaultRoot() ).to.equal( rootA );
  194. } );
  195. } );
  196. } );