document.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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 Range from '../../src/model/range';
  11. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  12. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  13. import count from '@ckeditor/ckeditor5-utils/src/count';
  14. describe( 'Document', () => {
  15. let model, doc;
  16. beforeEach( () => {
  17. model = new Model();
  18. doc = model.document;
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
  22. const doc = new Document( model );
  23. expect( doc ).to.have.property( 'model' ).to.equal( model );
  24. expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Collection );
  25. expect( doc.roots.length ).to.equal( 1 );
  26. expect( doc.graveyard ).to.be.instanceof( RootElement );
  27. expect( doc.graveyard.maxOffset ).to.equal( 0 );
  28. expect( count( doc.selection.getRanges() ) ).to.equal( 1 );
  29. } );
  30. } );
  31. describe( 'model#applyOperation listener', () => {
  32. let operation, data, batch;
  33. beforeEach( () => {
  34. data = { data: 'x' };
  35. operation = {
  36. type: 't',
  37. baseVersion: 0,
  38. isDocumentOperation: true,
  39. _execute: sinon.stub().returns( data ),
  40. _validate: () => {}
  41. };
  42. batch = new Batch();
  43. batch.addOperation( operation );
  44. } );
  45. it( 'for document operation: should increase document version and execute operation', () => {
  46. model.applyOperation( operation );
  47. expect( doc.version ).to.equal( 1 );
  48. expect( doc.history._operations.length ).to.equal( 1 );
  49. sinon.assert.calledOnce( operation._execute );
  50. } );
  51. it( 'for non-document operation: should only execute operation', () => {
  52. operation.isDocumentOperation = false;
  53. model.applyOperation( operation );
  54. expect( doc.version ).to.equal( 0 );
  55. expect( doc.history._operations.length ).to.equal( 0 );
  56. sinon.assert.calledOnce( operation._execute );
  57. } );
  58. it( 'should do nothing if operation event was cancelled', () => {
  59. model.on( 'applyOperation', evt => evt.stop(), { priority: 'highest' } );
  60. model.applyOperation( operation );
  61. expect( doc.version ).to.equal( 0 );
  62. expect( operation._execute.called ).to.be.false;
  63. } );
  64. it( 'should throw an error on the operation base version and the document version is different', () => {
  65. const operation = {
  66. baseVersion: 1,
  67. isDocumentOperation: true,
  68. _execute: () => {}
  69. };
  70. expect(
  71. () => {
  72. model.applyOperation( operation );
  73. }
  74. ).to.throw( CKEditorError, /^model-document-applyOperation-wrong-version/ );
  75. } );
  76. } );
  77. describe( 'getRootNames()', () => {
  78. it( 'should return empty iterator if no roots exist', () => {
  79. expect( count( doc.getRootNames() ) ).to.equal( 0 );
  80. } );
  81. it( 'should return an iterator of all roots without the graveyard', () => {
  82. doc.createRoot( '$root', 'a' );
  83. doc.createRoot( '$root', 'b' );
  84. expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
  85. } );
  86. } );
  87. describe( 'createRoot()', () => {
  88. it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
  89. const root = doc.createRoot();
  90. expect( doc.roots.length ).to.equal( 2 );
  91. expect( root ).to.be.instanceof( RootElement );
  92. expect( root.maxOffset ).to.equal( 0 );
  93. expect( root ).to.have.property( 'name', '$root' );
  94. expect( root ).to.have.property( 'rootName', 'main' );
  95. } );
  96. it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
  97. const root = doc.createRoot( 'customElementName', 'customRootName' );
  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', 'customElementName' );
  102. expect( root ).to.have.property( 'rootName', 'customRootName' );
  103. } );
  104. it( 'should throw an error when trying to create a second root with the same name', () => {
  105. doc.createRoot( '$root', 'rootName' );
  106. expect(
  107. () => {
  108. doc.createRoot( '$root', 'rootName' );
  109. }
  110. ).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
  111. } );
  112. } );
  113. describe( 'getRoot()', () => {
  114. it( 'should return a RootElement with default "main" name', () => {
  115. const newRoot = doc.createRoot( 'main' );
  116. expect( doc.getRoot() ).to.equal( newRoot );
  117. } );
  118. it( 'should return a RootElement with custom name', () => {
  119. const newRoot = doc.createRoot( 'custom', 'custom' );
  120. expect( doc.getRoot( 'custom' ) ).to.equal( newRoot );
  121. } );
  122. it( 'should return null when trying to get non-existent root', () => {
  123. expect( doc.getRoot( 'not-existing' ) ).to.null;
  124. } );
  125. } );
  126. describe( '_getDefaultRoot()', () => {
  127. it( 'should return graveyard root if there are no other roots in the document', () => {
  128. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  129. } );
  130. it( 'should return the first root added to the document', () => {
  131. const rootA = doc.createRoot( '$root', 'rootA' );
  132. doc.createRoot( '$root', 'rootB' );
  133. doc.createRoot( '$root', 'rootC' );
  134. expect( doc._getDefaultRoot() ).to.equal( rootA );
  135. } );
  136. } );
  137. describe( 'destroy()', () => {
  138. it( 'should destroy selection instance', () => {
  139. const spy = sinon.spy( doc.selection, 'destroy' );
  140. doc.destroy();
  141. sinon.assert.calledOnce( spy );
  142. } );
  143. it( 'should stop listening to events', () => {
  144. const spy = sinon.spy();
  145. doc.listenTo( model, 'something', spy );
  146. model.fire( 'something' );
  147. sinon.assert.calledOnce( spy );
  148. doc.destroy();
  149. model.fire( 'something' );
  150. // Still once.
  151. sinon.assert.calledOnce( spy );
  152. } );
  153. } );
  154. describe( 'differ', () => {
  155. beforeEach( () => {
  156. doc.createRoot();
  157. } );
  158. it( 'should buffer document operations in differ', () => {
  159. sinon.spy( doc.differ, 'bufferOperation' );
  160. model.change( writer => {
  161. writer.insertText( 'foo', doc.getRoot(), 0 );
  162. } );
  163. expect( doc.differ.bufferOperation.called ).to.be.true;
  164. } );
  165. it( 'should not buffer changes not done on document', () => {
  166. sinon.spy( doc.differ, 'bufferOperation' );
  167. model.change( writer => {
  168. const docFrag = writer.createDocumentFragment();
  169. writer.insertText( 'foo', docFrag, 0 );
  170. } );
  171. expect( doc.differ.bufferOperation.called ).to.be.false;
  172. } );
  173. it( 'should buffer marker changes in differ', () => {
  174. sinon.spy( doc.differ, 'bufferMarkerChange' );
  175. model.change( writer => {
  176. const range = Range.createCollapsedAt( doc.getRoot(), 0 );
  177. writer.addMarker( 'marker', { range, usingOperation: false } );
  178. } );
  179. expect( doc.differ.bufferMarkerChange.called ).to.be.true;
  180. } );
  181. it( 'should reset differ after change block is done', () => {
  182. model.change( writer => {
  183. writer.insertText( 'foo', doc.getRoot(), 0 );
  184. expect( doc.differ.getChanges().length > 0 ).to.be.true;
  185. } );
  186. expect( doc.differ.getChanges().length ).to.equal( 0 );
  187. } );
  188. } );
  189. describe( 'registerPostFixer()', () => {
  190. beforeEach( () => {
  191. doc.createRoot();
  192. } );
  193. it( 'should add a callback that is fired after changes are done', () => {
  194. const spy = sinon.spy();
  195. doc.registerPostFixer( spy );
  196. model.change( writer => {
  197. writer.insertText( 'foo', doc.getRoot(), 0 );
  198. } );
  199. expect( spy.calledOnce ).to.be.true;
  200. } );
  201. it( 'should not fire callbacks if no changes on document were done', () => {
  202. const spy = sinon.spy();
  203. doc.registerPostFixer( spy );
  204. model.change( writer => {
  205. const docFrag = writer.createDocumentFragment();
  206. writer.insertText( 'foo', docFrag, 0 );
  207. } );
  208. expect( spy.called ).to.be.false;
  209. } );
  210. it( 'should call all already processed callbacks again if a callback returned true', () => {
  211. const callA = sinon.spy();
  212. const callB = sinon.stub();
  213. callB.onFirstCall().returns( true ).onSecondCall().returns( false );
  214. const callC = sinon.spy();
  215. doc.registerPostFixer( callA );
  216. doc.registerPostFixer( callB );
  217. doc.registerPostFixer( callC );
  218. model.change( writer => {
  219. writer.insertText( 'foo', doc.getRoot(), 0 );
  220. } );
  221. sinon.assert.calledTwice( callA );
  222. sinon.assert.calledTwice( callB );
  223. sinon.assert.calledOnce( callC );
  224. } );
  225. } );
  226. describe( 'event change', () => {
  227. it( 'should be fired if there was a change in a document tree in a change block and have a batch as a param', () => {
  228. doc.createRoot();
  229. const spy = sinon.spy();
  230. doc.on( 'change', ( evt, batch ) => {
  231. spy();
  232. expect( batch ).to.be.instanceof( Batch );
  233. } );
  234. model.change( writer => {
  235. writer.insertText( 'foo', doc.getRoot(), 0 );
  236. } );
  237. sinon.assert.calledOnce( spy );
  238. } );
  239. it( 'should be fired if there was a selection change in an (enqueue)change block', () => {
  240. const root = doc.createRoot();
  241. const spy = sinon.spy();
  242. root._appendChild( new Text( 'foo' ) );
  243. doc.on( 'change', spy );
  244. model.change( writer => {
  245. writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 2 ) );
  246. } );
  247. sinon.assert.calledOnce( spy );
  248. } );
  249. it( 'should not be fired if writer was used on non-document tree', () => {
  250. const spy = sinon.spy();
  251. doc.on( 'change', ( evt, batch ) => {
  252. spy();
  253. expect( batch ).to.be.instanceof( Batch );
  254. } );
  255. model.change( writer => {
  256. const docFrag = writer.createDocumentFragment();
  257. writer.insertText( 'foo', docFrag, 0 );
  258. } );
  259. sinon.assert.notCalled( spy );
  260. } );
  261. } );
  262. describe( 'event change:data', () => {
  263. it( 'should be fired if there was a change in a document tree in a change block and have a batch as a param', () => {
  264. doc.createRoot();
  265. const spy = sinon.spy();
  266. doc.on( 'change:data', ( evt, batch ) => {
  267. spy();
  268. expect( batch ).to.be.instanceof( Batch );
  269. } );
  270. model.change( writer => {
  271. writer.insertText( 'foo', doc.getRoot(), 0 );
  272. } );
  273. sinon.assert.calledOnce( spy );
  274. } );
  275. it( 'should not be fired if only selection changes', () => {
  276. const root = doc.createRoot();
  277. const spy = sinon.spy();
  278. root._appendChild( new Text( 'foo' ) );
  279. doc.on( 'change:data', spy );
  280. model.change( writer => {
  281. writer.setSelection( Range.createFromParentsAndOffsets( root, 2, root, 2 ) );
  282. } );
  283. sinon.assert.notCalled( spy );
  284. } );
  285. it( 'should be fired if default marker operation is applied', () => {
  286. const root = doc.createRoot();
  287. const spy = sinon.spy();
  288. root._appendChild( new Text( 'foo' ) );
  289. doc.on( 'change:data', spy );
  290. model.change( writer => {
  291. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  292. writer.addMarker( 'name', { range, usingOperation: true, affectsData: true } );
  293. } );
  294. sinon.assert.calledOnce( spy );
  295. } );
  296. it( 'should not be fired if the marker operation is applied and marker does not affect data', () => {
  297. const root = doc.createRoot();
  298. const spy = sinon.spy();
  299. root._appendChild( new Text( 'foo' ) );
  300. doc.on( 'change:data', spy );
  301. model.change( writer => {
  302. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  303. writer.addMarker( 'name', { range, usingOperation: true } );
  304. } );
  305. sinon.assert.notCalled( spy );
  306. } );
  307. it( 'should be fired if the writer adds marker not managed by using operations', () => {
  308. const root = doc.createRoot();
  309. const spy = sinon.spy();
  310. root._appendChild( new Text( 'foo' ) );
  311. doc.on( 'change:data', spy );
  312. model.change( writer => {
  313. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  314. writer.addMarker( 'name', { range, usingOperation: false, affectsData: true } );
  315. } );
  316. sinon.assert.calledOnce( spy );
  317. } );
  318. it( 'should not be fired if the writer adds marker not managed by using operations with affectsData set to false', () => {
  319. const root = doc.createRoot();
  320. const spy = sinon.spy();
  321. root._appendChild( new Text( 'foo' ) );
  322. doc.on( 'change:data', spy );
  323. model.change( writer => {
  324. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  325. writer.addMarker( 'name', { range, usingOperation: false } );
  326. } );
  327. sinon.assert.notCalled( spy );
  328. } );
  329. it( 'should not be fired if writer was used on non-document tree', () => {
  330. const spy = sinon.spy();
  331. doc.on( 'change:data', ( evt, batch ) => {
  332. spy();
  333. expect( batch ).to.be.instanceof( Batch );
  334. } );
  335. model.change( writer => {
  336. const docFrag = writer.createDocumentFragment();
  337. writer.insertText( 'foo', docFrag, 0 );
  338. } );
  339. sinon.assert.notCalled( spy );
  340. } );
  341. it( 'should be fired when updated marker affects data', () => {
  342. const root = doc.createRoot();
  343. root._appendChild( new Text( 'foo' ) );
  344. const sandbox = sinon.createSandbox();
  345. const changeDataSpy = sandbox.spy();
  346. const changeSpy = sandbox.spy();
  347. doc.on( 'change:data', changeDataSpy );
  348. doc.on( 'change', changeSpy );
  349. model.change( writer => {
  350. const range = Range.createFromParentsAndOffsets( root, 2, root, 4 );
  351. writer.addMarker( 'name', { range, usingOperation: false } );
  352. } );
  353. sinon.assert.calledOnce( changeSpy );
  354. sinon.assert.notCalled( changeDataSpy );
  355. sandbox.resetHistory();
  356. model.change( writer => {
  357. writer.updateMarker( 'name', { affectsData: true } );
  358. } );
  359. sinon.assert.calledOnce( changeSpy );
  360. sinon.assert.calledOnce( changeDataSpy );
  361. sandbox.resetHistory();
  362. model.change( writer => {
  363. writer.updateMarker( 'name', { affectsData: false } );
  364. } );
  365. sinon.assert.calledOnce( changeSpy );
  366. sinon.assert.notCalled( changeDataSpy );
  367. } );
  368. } );
  369. it( 'should be correctly converted to json', () => {
  370. const serialized = doc.toJSON();
  371. expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' );
  372. expect( serialized.model ).to.equal( '[engine.model.Model]' );
  373. } );
  374. } );