8
0

document.js 7.3 KB

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