document.js 14 KB

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