document.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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. const range = Range.createCollapsedAt( doc.getRoot(), 0 );
  182. writer.addMarker( 'marker', { range, usingOperation: false } );
  183. } );
  184. expect( doc.differ.bufferMarkerChange.called ).to.be.true;
  185. } );
  186. it( 'should reset differ after change block is done', () => {
  187. model.change( writer => {
  188. writer.insertText( 'foo', doc.getRoot(), 0 );
  189. expect( doc.differ.getChanges().length > 0 ).to.be.true;
  190. } );
  191. expect( doc.differ.getChanges().length ).to.equal( 0 );
  192. } );
  193. } );
  194. describe( 'registerPostFixer()', () => {
  195. beforeEach( () => {
  196. doc.createRoot();
  197. } );
  198. it( 'should add a callback that is fired after changes are done', () => {
  199. const spy = sinon.spy();
  200. doc.registerPostFixer( spy );
  201. model.change( writer => {
  202. writer.insertText( 'foo', doc.getRoot(), 0 );
  203. } );
  204. expect( spy.calledOnce ).to.be.true;
  205. } );
  206. it( 'should not fire callbacks if no changes on document were done', () => {
  207. const spy = sinon.spy();
  208. doc.registerPostFixer( spy );
  209. model.change( writer => {
  210. const docFrag = writer.createDocumentFragment();
  211. writer.insertText( 'foo', docFrag, 0 );
  212. } );
  213. expect( spy.called ).to.be.false;
  214. } );
  215. it( 'should call all already processed callbacks again if a callback returned true', () => {
  216. const callA = sinon.spy();
  217. const callB = sinon.stub();
  218. callB.onFirstCall().returns( true ).onSecondCall().returns( false );
  219. const callC = sinon.spy();
  220. doc.registerPostFixer( callA );
  221. doc.registerPostFixer( callB );
  222. doc.registerPostFixer( callC );
  223. model.change( writer => {
  224. writer.insertText( 'foo', doc.getRoot(), 0 );
  225. } );
  226. sinon.assert.calledTwice( callA );
  227. sinon.assert.calledTwice( callB );
  228. sinon.assert.calledOnce( callC );
  229. } );
  230. } );
  231. describe( 'event change', () => {
  232. it( 'should be fired if there was a change in a document tree in a change block and have a batch as a param', () => {
  233. doc.createRoot();
  234. const spy = sinon.spy();
  235. doc.on( 'change', ( evt, batch ) => {
  236. spy();
  237. expect( batch ).to.be.instanceof( Batch );
  238. } );
  239. model.change( writer => {
  240. writer.insertText( 'foo', doc.getRoot(), 0 );
  241. } );
  242. expect( spy.calledOnce ).to.be.true;
  243. } );
  244. it( 'should be fired if there was a selection change in an (enqueue)change block', () => {
  245. const root = doc.createRoot();
  246. const spy = sinon.spy();
  247. root._appendChild( new Text( 'foo' ) );
  248. doc.on( 'change', spy );
  249. model.change( writer => {
  250. writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 2 ) );
  251. } );
  252. expect( spy.calledOnce ).to.be.true;
  253. } );
  254. it( 'should not be fired if writer was used on non-document tree', () => {
  255. const spy = sinon.spy();
  256. doc.on( 'change', ( evt, batch ) => {
  257. spy();
  258. expect( batch ).to.be.instanceof( Batch );
  259. } );
  260. model.change( writer => {
  261. const docFrag = writer.createDocumentFragment();
  262. writer.insertText( 'foo', docFrag, 0 );
  263. } );
  264. expect( spy.calledOnce ).to.be.false;
  265. } );
  266. } );
  267. describe( 'event change:data', () => {
  268. it( 'should be fired if there was a change in a document tree in a change block and have a batch as a param', () => {
  269. doc.createRoot();
  270. const spy = sinon.spy();
  271. doc.on( 'change:data', ( evt, batch ) => {
  272. spy();
  273. expect( batch ).to.be.instanceof( Batch );
  274. } );
  275. model.change( writer => {
  276. writer.insertText( 'foo', doc.getRoot(), 0 );
  277. } );
  278. expect( spy.calledOnce ).to.be.true;
  279. } );
  280. it( 'should be fired before the change event', () => {
  281. doc.createRoot();
  282. const callOrder = [];
  283. doc.on( 'change:data', () => {
  284. callOrder.push( 1 );
  285. } );
  286. doc.on( 'change', () => {
  287. callOrder.push( 2 );
  288. } );
  289. model.change( writer => {
  290. writer.insertText( 'foo', doc.getRoot(), 0 );
  291. } );
  292. expect( callOrder ).to.deep.equal( [ 1, 2 ] );
  293. } );
  294. it( 'should not be fired if only selection changes', () => {
  295. const root = doc.createRoot();
  296. const spy = sinon.spy();
  297. root._appendChild( new Text( 'foo' ) );
  298. doc.on( 'change:data', spy );
  299. model.change( writer => {
  300. writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 2 ) );
  301. } );
  302. sinon.assert.notCalled( spy );
  303. } );
  304. it( 'should be fired if default marker operation is applied', () => {
  305. const root = doc.createRoot();
  306. const spy = sinon.spy();
  307. root._appendChild( new Text( 'foo' ) );
  308. doc.on( 'change:data', spy );
  309. model.change( writer => {
  310. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  311. writer.addMarker( 'name', { range, usingOperation: true, affectsData: true } );
  312. } );
  313. sinon.assert.calledOnce( spy );
  314. } );
  315. it( 'should not be fired if the marker operation is applied and marker does not affect data', () => {
  316. const root = doc.createRoot();
  317. const spy = sinon.spy();
  318. root._appendChild( new Text( 'foo' ) );
  319. doc.on( 'change:data', spy );
  320. model.change( writer => {
  321. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  322. writer.addMarker( 'name', { range, usingOperation: true } );
  323. } );
  324. sinon.assert.notCalled( spy );
  325. } );
  326. it( 'should be fired if the writer adds marker not managed by using operations', () => {
  327. const root = doc.createRoot();
  328. const spy = sinon.spy();
  329. root._appendChild( new Text( 'foo' ) );
  330. doc.on( 'change:data', spy );
  331. model.change( writer => {
  332. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  333. writer.addMarker( 'name', { range, usingOperation: false, affectsData: true } );
  334. } );
  335. sinon.assert.calledOnce( spy );
  336. } );
  337. it( 'should not be fired if the writer adds marker not managed by using operations with affectsData set to false', () => {
  338. const root = doc.createRoot();
  339. const spy = sinon.spy();
  340. root._appendChild( new Text( 'foo' ) );
  341. doc.on( 'change:data', spy );
  342. model.change( writer => {
  343. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  344. writer.addMarker( 'name', { range, usingOperation: false } );
  345. } );
  346. sinon.assert.notCalled( spy );
  347. } );
  348. it( 'should not be fired if writer was used on non-document tree', () => {
  349. const spy = sinon.spy();
  350. doc.on( 'change:data', ( evt, batch ) => {
  351. spy();
  352. expect( batch ).to.be.instanceof( Batch );
  353. } );
  354. model.change( writer => {
  355. const docFrag = writer.createDocumentFragment();
  356. writer.insertText( 'foo', docFrag, 0 );
  357. } );
  358. expect( spy.calledOnce ).to.be.false;
  359. } );
  360. } );
  361. it( 'should be correctly converted to json', () => {
  362. const serialized = jsonParseStringify( doc );
  363. expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' );
  364. expect( serialized.model ).to.equal( '[engine.model.Model]' );
  365. } );
  366. } );