8
0

document.js 10 KB

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