document.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../src/model/model';
  6. import Document from '../../src/model/document';
  7. import RootElement from '../../src/model/rootelement';
  8. import Text from '../../src/model/text';
  9. import Batch from '../../src/model/batch';
  10. import Delta from '../../src/model/delta/delta';
  11. import Range from '../../src/model/range';
  12. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  13. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. import { jsonParseStringify } from './_utils/utils';
  16. describe( 'Document', () => {
  17. let model, doc;
  18. beforeEach( () => {
  19. model = new Model();
  20. doc = new Document( model );
  21. // Normally Model is the one who creates Document instance and keeps it as reference.
  22. // We have to be sure that Model uses the right Document instance.
  23. model.document = doc;
  24. } );
  25. describe( 'constructor()', () => {
  26. it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
  27. const doc = new Document( model );
  28. expect( doc ).to.have.property( 'model' ).to.equal( model );
  29. expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Collection );
  30. expect( doc.roots.length ).to.equal( 1 );
  31. expect( doc.graveyard ).to.be.instanceof( RootElement );
  32. expect( doc.graveyard.maxOffset ).to.equal( 0 );
  33. expect( count( doc.selection.getRanges() ) ).to.equal( 1 );
  34. } );
  35. } );
  36. describe( 'model#applyOperation listener', () => {
  37. let operation, data, delta, batch;
  38. beforeEach( () => {
  39. data = { data: 'x' };
  40. operation = {
  41. type: 't',
  42. baseVersion: 0,
  43. isDocumentOperation: true,
  44. _execute: sinon.stub().returns( data ),
  45. _validate: () => {}
  46. };
  47. delta = new Delta();
  48. delta.addOperation( operation );
  49. delta.type = 'delta';
  50. batch = new Batch();
  51. batch.addDelta( delta );
  52. } );
  53. it( 'for document operation: should increase document version and execute operation', () => {
  54. model.applyOperation( operation );
  55. expect( doc.version ).to.equal( 1 );
  56. expect( doc.history._deltas.length ).to.equal( 1 );
  57. sinon.assert.calledOnce( operation._execute );
  58. } );
  59. it( 'for non-document operation: should only execute operation', () => {
  60. operation.isDocumentOperation = false;
  61. model.applyOperation( operation );
  62. expect( doc.version ).to.equal( 0 );
  63. expect( doc.history._deltas.length ).to.equal( 0 );
  64. sinon.assert.calledOnce( operation._execute );
  65. } );
  66. it( 'should do nothing if operation event was cancelled', () => {
  67. model.on( 'applyOperation', evt => evt.stop(), { priority: 'highest' } );
  68. model.applyOperation( operation );
  69. expect( doc.version ).to.equal( 0 );
  70. expect( operation._execute.called ).to.be.false;
  71. } );
  72. it( 'should throw an error on the operation base version and the document version is different', () => {
  73. const operation = {
  74. baseVersion: 1,
  75. isDocumentOperation: true,
  76. _execute: () => {}
  77. };
  78. expect(
  79. () => {
  80. model.applyOperation( operation );
  81. }
  82. ).to.throw( CKEditorError, /^model-document-applyOperation-wrong-version/ );
  83. } );
  84. } );
  85. describe( 'getRootNames()', () => {
  86. it( 'should return empty iterator if no roots exist', () => {
  87. expect( count( doc.getRootNames() ) ).to.equal( 0 );
  88. } );
  89. it( 'should return an iterator of all roots without the graveyard', () => {
  90. doc.createRoot( '$root', 'a' );
  91. doc.createRoot( '$root', 'b' );
  92. expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
  93. } );
  94. } );
  95. describe( 'createRoot()', () => {
  96. it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
  97. const root = doc.createRoot();
  98. expect( doc.roots.length ).to.equal( 2 );
  99. expect( root ).to.be.instanceof( RootElement );
  100. expect( root.maxOffset ).to.equal( 0 );
  101. expect( root ).to.have.property( 'name', '$root' );
  102. expect( root ).to.have.property( 'rootName', 'main' );
  103. } );
  104. it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
  105. const root = doc.createRoot( 'customElementName', 'customRootName' );
  106. expect( doc.roots.length ).to.equal( 2 );
  107. expect( root ).to.be.instanceof( RootElement );
  108. expect( root.maxOffset ).to.equal( 0 );
  109. expect( root ).to.have.property( 'name', 'customElementName' );
  110. expect( root ).to.have.property( 'rootName', 'customRootName' );
  111. } );
  112. it( 'should throw an error when trying to create a second root with the same name', () => {
  113. doc.createRoot( '$root', 'rootName' );
  114. expect(
  115. () => {
  116. doc.createRoot( '$root', 'rootName' );
  117. }
  118. ).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
  119. } );
  120. } );
  121. describe( 'getRoot()', () => {
  122. it( 'should return a RootElement with default "main" name', () => {
  123. const newRoot = doc.createRoot( 'main' );
  124. expect( doc.getRoot() ).to.equal( newRoot );
  125. } );
  126. it( 'should return a RootElement with custom name', () => {
  127. const newRoot = doc.createRoot( 'custom', 'custom' );
  128. expect( doc.getRoot( 'custom' ) ).to.equal( newRoot );
  129. } );
  130. it( 'should return null when trying to get non-existent root', () => {
  131. expect( doc.getRoot( 'not-existing' ) ).to.null;
  132. } );
  133. } );
  134. describe( '_getDefaultRoot()', () => {
  135. it( 'should return graveyard root if there are no other roots in the document', () => {
  136. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  137. } );
  138. it( 'should return the first root added to the document', () => {
  139. const rootA = doc.createRoot( '$root', 'rootA' );
  140. doc.createRoot( '$root', 'rootB' );
  141. doc.createRoot( '$root', 'rootC' );
  142. expect( doc._getDefaultRoot() ).to.equal( rootA );
  143. } );
  144. } );
  145. describe( 'destroy()', () => {
  146. it( 'should destroy selection instance', () => {
  147. const spy = sinon.spy( doc.selection, 'destroy' );
  148. doc.destroy();
  149. sinon.assert.calledOnce( spy );
  150. } );
  151. it( 'should stop listening to events', () => {
  152. const spy = sinon.spy();
  153. doc.listenTo( model, 'something', spy );
  154. model.fire( 'something' );
  155. sinon.assert.calledOnce( spy );
  156. doc.destroy();
  157. model.fire( 'something' );
  158. // Still once.
  159. sinon.assert.calledOnce( spy );
  160. } );
  161. } );
  162. describe( 'differ', () => {
  163. beforeEach( () => {
  164. doc.createRoot();
  165. } );
  166. it( 'should buffer document operations in differ', () => {
  167. sinon.spy( doc.differ, 'bufferOperation' );
  168. model.change( writer => {
  169. writer.insertText( 'foo', doc.getRoot(), 0 );
  170. } );
  171. expect( doc.differ.bufferOperation.called ).to.be.true;
  172. } );
  173. it( 'should not buffer changes not done on document', () => {
  174. sinon.spy( doc.differ, 'bufferOperation' );
  175. model.change( writer => {
  176. const docFrag = writer.createDocumentFragment();
  177. writer.insertText( 'foo', docFrag, 0 );
  178. } );
  179. expect( doc.differ.bufferOperation.called ).to.be.false;
  180. } );
  181. it( 'should buffer marker changes in differ', () => {
  182. sinon.spy( doc.differ, 'bufferMarkerChange' );
  183. model.change( () => {
  184. model.markers.set( 'marker', Range.createCollapsedAt( doc.getRoot(), 0 ) );
  185. } );
  186. expect( doc.differ.bufferMarkerChange.called ).to.be.true;
  187. } );
  188. it( 'should reset differ after change block is done', () => {
  189. model.change( writer => {
  190. writer.insertText( 'foo', doc.getRoot(), 0 );
  191. expect( doc.differ.getChanges().length > 0 ).to.be.true;
  192. } );
  193. expect( doc.differ.getChanges().length ).to.equal( 0 );
  194. } );
  195. } );
  196. describe( 'registerPostFixer()', () => {
  197. beforeEach( () => {
  198. doc.createRoot();
  199. } );
  200. it( 'should add a callback that is fired after changes are done', () => {
  201. const spy = sinon.spy();
  202. doc.registerPostFixer( spy );
  203. model.change( writer => {
  204. writer.insertText( 'foo', doc.getRoot(), 0 );
  205. } );
  206. expect( spy.calledOnce ).to.be.true;
  207. } );
  208. it( 'should not fire callbacks if no changes on document were done', () => {
  209. const spy = sinon.spy();
  210. doc.registerPostFixer( spy );
  211. model.change( writer => {
  212. const docFrag = writer.createDocumentFragment();
  213. writer.insertText( 'foo', docFrag, 0 );
  214. } );
  215. expect( spy.called ).to.be.false;
  216. } );
  217. it( 'should call all already processed callbacks again if a callback returned true', () => {
  218. const callA = sinon.spy();
  219. const callB = sinon.stub().onFirstCall().returns( true ).onSecondCall().returns( false );
  220. const callC = sinon.spy();
  221. doc.registerPostFixer( callA );
  222. doc.registerPostFixer( callB );
  223. doc.registerPostFixer( callC );
  224. model.change( writer => {
  225. writer.insertText( 'foo', doc.getRoot(), 0 );
  226. } );
  227. expect( callA.calledTwice ).to.be.true;
  228. expect( callB.calledTwice ).to.be.true;
  229. expect( callC.calledOnce ).to.be.true;
  230. } );
  231. } );
  232. describe( 'event change', () => {
  233. it( 'should be fired if there was a change in a document tree in a change block and have a batch as a param', () => {
  234. doc.createRoot();
  235. const spy = sinon.spy();
  236. doc.on( 'change', ( evt, batch ) => {
  237. spy();
  238. expect( batch ).to.be.instanceof( Batch );
  239. } );
  240. model.change( writer => {
  241. writer.insertText( 'foo', doc.getRoot(), 0 );
  242. } );
  243. expect( spy.calledOnce ).to.be.true;
  244. } );
  245. it( 'should be fired if there was a change in a document tree in a change block and have a batch as param', () => {
  246. doc.createRoot();
  247. const spy = sinon.spy();
  248. doc.on( 'change', ( evt, batch ) => {
  249. spy();
  250. expect( batch ).to.be.instanceof( Batch );
  251. } );
  252. model.enqueueChange( writer => {
  253. writer.insertText( 'foo', doc.getRoot(), 0 );
  254. } );
  255. expect( spy.calledOnce ).to.be.true;
  256. } );
  257. it( 'should be fired if there was a selection change in an (enqueue)change block', () => {
  258. doc.createRoot();
  259. const spy = sinon.spy();
  260. const root = doc.getRoot();
  261. root.appendChildren( new Text( 'foo' ) );
  262. doc.on( 'change', spy );
  263. model.change( writer => {
  264. writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 2 ) );
  265. } );
  266. expect( spy.calledOnce ).to.be.true;
  267. } );
  268. it( 'should not be fired if writer was used on non-document tree', () => {
  269. const spy = sinon.spy();
  270. doc.on( 'change', ( evt, batch ) => {
  271. spy();
  272. expect( batch ).to.be.instanceof( Batch );
  273. } );
  274. model.change( writer => {
  275. const docFrag = writer.createDocumentFragment();
  276. writer.insertText( 'foo', docFrag, 0 );
  277. } );
  278. expect( spy.calledOnce ).to.be.false;
  279. } );
  280. } );
  281. it( 'should be correctly converted to json', () => {
  282. const serialized = jsonParseStringify( doc );
  283. expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' );
  284. expect( serialized.model ).to.equal( '[engine.model.Model]' );
  285. } );
  286. } );