8
0

document.js 14 KB

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