document.js 14 KB

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