document.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. import Document from '/ckeditor5/engine/model/document.js';
  7. import Schema from '/ckeditor5/engine/model/schema.js';
  8. import Composer from '/ckeditor5/engine/model/composer/composer.js';
  9. import RootElement from '/ckeditor5/engine/model/rootelement.js';
  10. import Batch from '/ckeditor5/engine/model/batch.js';
  11. import Delta from '/ckeditor5/engine/model/delta/delta.js';
  12. import Range from '/ckeditor5/engine/model/range.js';
  13. import count from '/ckeditor5/utils/count.js';
  14. import { jsonParseStringify } from '/tests/engine/model/_utils/utils.js';
  15. describe( 'Document', () => {
  16. let doc;
  17. beforeEach( () => {
  18. doc = new Document();
  19. } );
  20. describe( 'constructor', () => {
  21. it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
  22. expect( doc ).to.have.property( '_roots' ).that.is.instanceof( Map );
  23. expect( doc._roots.size ).to.equal( 1 );
  24. expect( doc.graveyard ).to.be.instanceof( RootElement );
  25. expect( doc.graveyard.maxOffset ).to.equal( 0 );
  26. expect( count( doc.selection.getRanges() ) ).to.equal( 1 );
  27. expect( doc.composer ).to.be.instanceof( Composer );
  28. expect( doc.schema ).to.be.instanceof( Schema );
  29. } );
  30. } );
  31. describe( 'getRootNames()', () => {
  32. it( 'should return empty iterator if no roots exist', () => {
  33. expect( count( doc.getRootNames() ) ).to.equal( 0 );
  34. } );
  35. it( 'should return an iterator of all roots without the graveyard', () => {
  36. doc.createRoot( '$root', 'a' );
  37. doc.createRoot( '$root', 'b' );
  38. expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
  39. } );
  40. } );
  41. describe( 'createRoot', () => {
  42. it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
  43. let root = doc.createRoot();
  44. expect( doc._roots.size ).to.equal( 2 );
  45. expect( root ).to.be.instanceof( RootElement );
  46. expect( root.maxOffset ).to.equal( 0 );
  47. expect( root ).to.have.property( 'name', '$root' );
  48. expect( root ).to.have.property( 'rootName', 'main' );
  49. } );
  50. it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
  51. let root = doc.createRoot( 'customElementName', 'customRootName' );
  52. expect( doc._roots.size ).to.equal( 2 );
  53. expect( root ).to.be.instanceof( RootElement );
  54. expect( root.maxOffset ).to.equal( 0 );
  55. expect( root ).to.have.property( 'name', 'customElementName' );
  56. expect( root ).to.have.property( 'rootName', 'customRootName' );
  57. } );
  58. it( 'should throw an error when trying to create a second root with the same name', () => {
  59. doc.createRoot( '$root', 'rootName' );
  60. expect(
  61. () => {
  62. doc.createRoot( '$root', 'rootName' );
  63. }
  64. ).to.throwCKEditorError( /model-document-createRoot-name-exists/ );
  65. } );
  66. } );
  67. describe( 'getRoot', () => {
  68. it( 'should return a RootElement previously created with given name', () => {
  69. let newRoot = doc.createRoot();
  70. let getRoot = doc.getRoot();
  71. expect( getRoot ).to.equal( newRoot );
  72. } );
  73. it( 'should throw an error when trying to get non-existent root', () => {
  74. expect(
  75. () => {
  76. doc.getRoot( 'root' );
  77. }
  78. ).to.throwCKEditorError( /model-document-getRoot-root-not-exist/ );
  79. } );
  80. } );
  81. describe( 'hasRoot', () => {
  82. it( 'should return true when Document has RootElement with given name', () => {
  83. doc.createRoot();
  84. expect( doc.hasRoot( 'main' ) ).to.be.true;
  85. } );
  86. it( 'should return false when Document does not have RootElement with given name', () => {
  87. expect( doc.hasRoot( 'noroot' ) ).to.be.false;
  88. } );
  89. } );
  90. describe( 'applyOperation', () => {
  91. it( 'should increase document version, execute operation and fire event with proper data', () => {
  92. const changeCallback = sinon.spy();
  93. const type = 't';
  94. const data = { data: 'x' };
  95. const batch = new Batch();
  96. const delta = new Delta();
  97. let operation = {
  98. type: type,
  99. baseVersion: 0,
  100. _execute: sinon.stub().returns( data )
  101. };
  102. delta.addOperation( operation );
  103. batch.addDelta( delta );
  104. doc.on( 'change', changeCallback );
  105. doc.applyOperation( operation );
  106. expect( doc.version ).to.equal( 1 );
  107. sinon.assert.calledOnce( operation._execute );
  108. sinon.assert.calledOnce( changeCallback );
  109. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  110. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  111. expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
  112. } );
  113. it( 'should throw an error on the operation base version and the document version is different', () => {
  114. let operation = {
  115. baseVersion: 1
  116. };
  117. expect(
  118. () => {
  119. doc.applyOperation( operation );
  120. }
  121. ).to.throwCKEditorError( /model-document-applyOperation-wrong-version/ );
  122. } );
  123. } );
  124. describe( 'batch', () => {
  125. it( 'should create a new batch with the document property', () => {
  126. const batch = doc.batch();
  127. expect( batch ).to.be.instanceof( Batch );
  128. expect( batch ).to.have.property( 'document' ).that.equals( doc );
  129. } );
  130. it( 'should set given batch type', () => {
  131. const batch = doc.batch( 'ignore' );
  132. expect( batch ).to.have.property( 'type' ).that.equals( 'ignore' );
  133. } );
  134. } );
  135. describe( 'enqueue', () => {
  136. it( 'should be executed immediately and fire changesDone event', () => {
  137. let order = [];
  138. doc.on( 'changesDone', () => order.push( 'done' ) );
  139. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  140. expect( order ).to.have.length( 2 );
  141. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  142. expect( order[ 1 ] ).to.equal( 'done' );
  143. } );
  144. it( 'should fire done every time queue is empty', () => {
  145. let order = [];
  146. doc.on( 'changesDone', () => order.push( 'done' ) );
  147. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  148. doc.enqueueChanges( () => order.push( 'enqueue2' ) );
  149. expect( order ).to.have.length( 4 );
  150. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  151. expect( order[ 1 ] ).to.equal( 'done' );
  152. expect( order[ 2 ] ).to.equal( 'enqueue2' );
  153. expect( order[ 3 ] ).to.equal( 'done' );
  154. } );
  155. it( 'should put callbacks in the proper order', () => {
  156. let order = [];
  157. doc.on( 'changesDone', () => order.push( 'done' ) );
  158. doc.enqueueChanges( () => {
  159. order.push( 'enqueue1 start' );
  160. doc.enqueueChanges( () => {
  161. order.push( 'enqueue2 start' );
  162. doc.enqueueChanges( () => order.push( 'enqueue4' ) );
  163. order.push( 'enqueue2 end' );
  164. } );
  165. doc.enqueueChanges( () => order.push( 'enqueue3' ) );
  166. order.push( 'enqueue1 end' );
  167. } );
  168. expect( order ).to.have.length( 7 );
  169. expect( order[ 0 ] ).to.equal( 'enqueue1 start' );
  170. expect( order[ 1 ] ).to.equal( 'enqueue1 end' );
  171. expect( order[ 2 ] ).to.equal( 'enqueue2 start' );
  172. expect( order[ 3 ] ).to.equal( 'enqueue2 end' );
  173. expect( order[ 4 ] ).to.equal( 'enqueue3' );
  174. expect( order[ 5 ] ).to.equal( 'enqueue4' );
  175. expect( order[ 6 ] ).to.equal( 'done' );
  176. } );
  177. } );
  178. describe( 'selection', () => {
  179. it( 'should get updated attributes whenever selection gets updated', () => {
  180. sinon.spy( doc.selection, '_updateAttributes' );
  181. doc.selection.fire( 'change:range' );
  182. expect( doc.selection._updateAttributes.called ).to.be.true;
  183. } );
  184. it( 'should get updated attributes whenever changes to the document are applied', () => {
  185. sinon.spy( doc.selection, '_updateAttributes' );
  186. doc.fire( 'changesDone' );
  187. expect( doc.selection._updateAttributes.called ).to.be.true;
  188. } );
  189. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  190. const root = doc.createRoot();
  191. root.appendChildren( '\uD83D\uDCA9' );
  192. expect( () => {
  193. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 0, root, 1 ) ] );
  194. } ).to.throwCKEditorError( /document-selection-wrong-position/ );
  195. expect( () => {
  196. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 2 ) ] );
  197. } ).to.throwCKEditorError( /document-selection-wrong-position/ );
  198. } );
  199. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  200. const root = doc.createRoot();
  201. root.appendChildren( 'foo̻̐ͩbar' );
  202. expect( () => {
  203. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 3, root, 9 ) ] );
  204. } ).to.throwCKEditorError( /document-selection-wrong-position/ );
  205. expect( () => {
  206. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 4, root, 9 ) ] );
  207. } ).to.throwCKEditorError( /document-selection-wrong-position/ );
  208. expect( () => {
  209. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 5, root, 9 ) ] );
  210. } ).to.throwCKEditorError( /document-selection-wrong-position/ );
  211. expect( () => {
  212. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 3 ) ] );
  213. } ).to.throwCKEditorError( /document-selection-wrong-position/ );
  214. expect( () => {
  215. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 4 ) ] );
  216. } ).to.throwCKEditorError( /document-selection-wrong-position/ );
  217. expect( () => {
  218. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 5 ) ] );
  219. } ).to.throwCKEditorError( /document-selection-wrong-position/ );
  220. } );
  221. } );
  222. describe( '_getDefaultRoot', () => {
  223. it( 'should return graveyard root if there are no other roots in the document', () => {
  224. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  225. } );
  226. it( 'should return the first root added to the document', () => {
  227. let rootA = doc.createRoot( '$root', 'rootA' );
  228. doc.createRoot( '$root', 'rootB' );
  229. doc.createRoot( '$root', 'rootC' );
  230. expect( doc._getDefaultRoot() ).to.equal( rootA );
  231. } );
  232. } );
  233. it( 'should be correctly converted to json', () => {
  234. expect( jsonParseStringify( doc ).selection ).to.equal( '[engine.model.LiveSelection]' );
  235. } );
  236. } );