8
0

document.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 Batch from '../../src/model/batch';
  9. import Delta from '../../src/model/delta/delta';
  10. import NoOperation from '../../src/model/operation/nooperation';
  11. import deltaTransform from '../../src/model/delta/transform';
  12. import Range from '../../src/model/range';
  13. import Collection from '@ckeditor/ckeditor5-utils/src/collection';
  14. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  15. import count from '@ckeditor/ckeditor5-utils/src/count';
  16. import { jsonParseStringify } from './_utils/utils';
  17. import { setData, getData } from '../../src/dev-utils/model';
  18. describe( 'Document', () => {
  19. let model, doc;
  20. beforeEach( () => {
  21. model = new Model();
  22. doc = new Document( model );
  23. // Normally Model is the one who creates Document instance and keeps it as reference.
  24. // We have to be sure that Model uses the right Document instance.
  25. model.document = doc;
  26. } );
  27. describe( 'constructor()', () => {
  28. it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
  29. const doc = new Document( model );
  30. expect( doc ).to.have.property( 'model' ).to.equal( model );
  31. expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Collection );
  32. expect( doc.roots.length ).to.equal( 1 );
  33. expect( doc.graveyard ).to.be.instanceof( RootElement );
  34. expect( doc.graveyard.maxOffset ).to.equal( 0 );
  35. expect( count( doc.selection.getRanges() ) ).to.equal( 1 );
  36. } );
  37. } );
  38. describe( 'model#applyOperation listener', () => {
  39. let operation, data, delta, batch;
  40. beforeEach( () => {
  41. data = { data: 'x' };
  42. operation = {
  43. type: 't',
  44. baseVersion: 0,
  45. isDocumentOperation: true,
  46. _execute: sinon.stub().returns( data ),
  47. _validate: () => {}
  48. };
  49. delta = new Delta();
  50. delta.addOperation( operation );
  51. delta.type = 'delta';
  52. batch = new Batch();
  53. batch.addDelta( delta );
  54. } );
  55. it( 'for document operation: should increase document version and execute operation', () => {
  56. model.applyOperation( operation );
  57. expect( doc.version ).to.equal( 1 );
  58. expect( doc.history._deltas.length ).to.equal( 1 );
  59. sinon.assert.calledOnce( operation._execute );
  60. } );
  61. it( 'for non-document operation: should only execute operation', () => {
  62. operation.isDocumentOperation = false;
  63. model.applyOperation( operation );
  64. expect( doc.version ).to.equal( 0 );
  65. expect( doc.history._deltas.length ).to.equal( 0 );
  66. sinon.assert.calledOnce( operation._execute );
  67. } );
  68. it( 'should do nothing if operation event was cancelled', () => {
  69. model.on( 'applyOperation', evt => evt.stop(), { priority: 'highest' } );
  70. model.applyOperation( operation );
  71. expect( doc.version ).to.equal( 0 );
  72. expect( operation._execute.called ).to.be.false;
  73. } );
  74. it( 'should throw an error on the operation base version and the document version is different', () => {
  75. const operation = {
  76. baseVersion: 1,
  77. isDocumentOperation: true,
  78. _execute: () => {}
  79. };
  80. expect(
  81. () => {
  82. model.applyOperation( operation );
  83. }
  84. ).to.throw( CKEditorError, /^model-document-applyOperation-wrong-version/ );
  85. } );
  86. } );
  87. describe( 'getRootNames()', () => {
  88. it( 'should return empty iterator if no roots exist', () => {
  89. expect( count( doc.getRootNames() ) ).to.equal( 0 );
  90. } );
  91. it( 'should return an iterator of all roots without the graveyard', () => {
  92. doc.createRoot( '$root', 'a' );
  93. doc.createRoot( '$root', 'b' );
  94. expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
  95. } );
  96. } );
  97. describe( 'createRoot()', () => {
  98. it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
  99. const root = doc.createRoot();
  100. expect( doc.roots.length ).to.equal( 2 );
  101. expect( root ).to.be.instanceof( RootElement );
  102. expect( root.maxOffset ).to.equal( 0 );
  103. expect( root ).to.have.property( 'name', '$root' );
  104. expect( root ).to.have.property( 'rootName', 'main' );
  105. } );
  106. it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
  107. const root = doc.createRoot( 'customElementName', 'customRootName' );
  108. expect( doc.roots.length ).to.equal( 2 );
  109. expect( root ).to.be.instanceof( RootElement );
  110. expect( root.maxOffset ).to.equal( 0 );
  111. expect( root ).to.have.property( 'name', 'customElementName' );
  112. expect( root ).to.have.property( 'rootName', 'customRootName' );
  113. } );
  114. it( 'should throw an error when trying to create a second root with the same name', () => {
  115. doc.createRoot( '$root', 'rootName' );
  116. expect(
  117. () => {
  118. doc.createRoot( '$root', 'rootName' );
  119. }
  120. ).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
  121. } );
  122. } );
  123. describe( 'getRoot()', () => {
  124. it( 'should return a RootElement with default "main" name', () => {
  125. const newRoot = doc.createRoot( 'main' );
  126. expect( doc.getRoot() ).to.equal( newRoot );
  127. } );
  128. it( 'should return a RootElement with custom name', () => {
  129. const newRoot = doc.createRoot( 'custom', 'custom' );
  130. expect( doc.getRoot( 'custom' ) ).to.equal( newRoot );
  131. } );
  132. it( 'should return null when trying to get non-existent root', () => {
  133. expect( doc.getRoot( 'not-existing' ) ).to.null;
  134. } );
  135. } );
  136. describe( 'getNearestSelectionRange()', () => {
  137. let selection;
  138. beforeEach( () => {
  139. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  140. model.schema.register( 'emptyBlock', { allowIn: '$root' } );
  141. model.schema.register( 'widget', {
  142. allowIn: '$root',
  143. isObject: true
  144. } );
  145. model.schema.register( 'blockWidget', {
  146. allowIn: '$root',
  147. allowContentOf: '$block',
  148. isObject: true
  149. } );
  150. doc.createRoot();
  151. selection = doc.selection;
  152. } );
  153. test(
  154. 'should return collapsed range if text node can be placed at that position - both',
  155. '<paragraph>[]</paragraph>',
  156. 'both',
  157. '<paragraph>[]</paragraph>'
  158. );
  159. test(
  160. 'should return collapsed range if text node can be placed at that position - forward',
  161. '<paragraph>[]</paragraph>',
  162. 'forward',
  163. '<paragraph>[]</paragraph>'
  164. );
  165. test(
  166. 'should return collapsed range if text node can be placed at that position - backward',
  167. '<paragraph>[]</paragraph>',
  168. 'backward',
  169. '<paragraph>[]</paragraph>'
  170. );
  171. test( 'should return null in empty document - both', '', 'both', null );
  172. test( 'should return null in empty document - backward', '', 'backward', null );
  173. test( 'should return null in empty document - forward', '', 'forward', null );
  174. test(
  175. 'should find range before when searching both ways',
  176. '<paragraph></paragraph>[]<paragraph></paragraph>',
  177. 'both',
  178. '<paragraph>[]</paragraph><paragraph></paragraph>'
  179. );
  180. test(
  181. 'should find range before when searching backward',
  182. '<paragraph></paragraph>[]<paragraph></paragraph>',
  183. 'backward',
  184. '<paragraph>[]</paragraph><paragraph></paragraph>'
  185. );
  186. test(
  187. 'should find range after when searching forward',
  188. '<paragraph></paragraph>[]<paragraph></paragraph>',
  189. 'forward',
  190. '<paragraph></paragraph><paragraph>[]</paragraph>'
  191. );
  192. test(
  193. 'should find range after when searching both ways when it is closer',
  194. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  195. 'both',
  196. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  197. );
  198. test(
  199. 'should find range before when searching both ways when it is closer',
  200. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  201. 'both',
  202. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  203. );
  204. test(
  205. 'should return null if there is no valid range',
  206. '[]<emptyBlock></emptyBlock>',
  207. 'both',
  208. null
  209. );
  210. test(
  211. 'should return null if there is no valid range in given direction - backward',
  212. '[]<paragraph></paragraph>',
  213. 'backward',
  214. null
  215. );
  216. test(
  217. 'should return null if there is no valid range in given direction - forward',
  218. '<paragraph></paragraph>[]',
  219. 'forward',
  220. null
  221. );
  222. test(
  223. 'should move forward when placed at root start',
  224. '[]<paragraph></paragraph><paragraph></paragraph>',
  225. 'both',
  226. '<paragraph>[]</paragraph><paragraph></paragraph>'
  227. );
  228. test(
  229. 'should move backward when placed at root end',
  230. '<paragraph></paragraph><paragraph></paragraph>[]',
  231. 'both',
  232. '<paragraph></paragraph><paragraph>[]</paragraph>'
  233. );
  234. describe( 'in case of objects which do not allow text inside', () => {
  235. test(
  236. 'should select nearest object (o[]o) - both',
  237. '<widget></widget>[]<widget></widget>',
  238. 'both',
  239. '[<widget></widget>]<widget></widget>'
  240. );
  241. test(
  242. 'should select nearest object (o[]o) - forward',
  243. '<widget></widget>[]<widget></widget>',
  244. 'forward',
  245. '<widget></widget>[<widget></widget>]'
  246. );
  247. test(
  248. 'should select nearest object (o[]o) - backward',
  249. '<widget></widget>[]<widget></widget>',
  250. 'both',
  251. '[<widget></widget>]<widget></widget>'
  252. );
  253. test(
  254. 'should select nearest object (p[]o) - forward',
  255. '<paragraph></paragraph>[]<widget></widget>',
  256. 'forward',
  257. '<paragraph></paragraph>[<widget></widget>]'
  258. );
  259. test(
  260. 'should select nearest object (o[]p) - both',
  261. '<widget></widget>[]<paragraph></paragraph>',
  262. 'both',
  263. '[<widget></widget>]<paragraph></paragraph>'
  264. );
  265. test(
  266. 'should select nearest object (o[]p) - backward',
  267. '<widget></widget>[]<paragraph></paragraph>',
  268. 'backward',
  269. '[<widget></widget>]<paragraph></paragraph>'
  270. );
  271. test(
  272. 'should select nearest object ([]o) - both',
  273. '[]<widget></widget><paragraph></paragraph>',
  274. 'both',
  275. '[<widget></widget>]<paragraph></paragraph>'
  276. );
  277. test(
  278. 'should select nearest object ([]o) - forward',
  279. '[]<widget></widget><paragraph></paragraph>',
  280. 'forward',
  281. '[<widget></widget>]<paragraph></paragraph>'
  282. );
  283. test(
  284. 'should select nearest object (o[]) - both',
  285. '<paragraph></paragraph><widget></widget>[]',
  286. 'both',
  287. '<paragraph></paragraph>[<widget></widget>]'
  288. );
  289. test(
  290. 'should select nearest object (o[]) - backward',
  291. '<paragraph></paragraph><widget></widget>[]',
  292. 'both',
  293. '<paragraph></paragraph>[<widget></widget>]'
  294. );
  295. } );
  296. describe( 'in case of objects which allow text inside', () => {
  297. test(
  298. 'should select nearest object which allows text (o[]o) - both',
  299. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  300. 'both',
  301. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  302. );
  303. test(
  304. 'should select nearest object (o[]p) - both',
  305. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  306. 'both',
  307. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  308. );
  309. test(
  310. 'should select nearest object which allows text ([]o) - both',
  311. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  312. 'both',
  313. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  314. );
  315. } );
  316. function test( testName, data, direction, expected ) {
  317. it( testName, () => {
  318. setData( model, data );
  319. const range = doc.getNearestSelectionRange( selection.anchor, direction );
  320. if ( expected === null ) {
  321. expect( range ).to.be.null;
  322. } else {
  323. selection.setRanges( [ range ] );
  324. expect( getData( model ) ).to.equal( expected );
  325. }
  326. } );
  327. }
  328. } );
  329. describe( '_getDefaultRoot()', () => {
  330. it( 'should return graveyard root if there are no other roots in the document', () => {
  331. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  332. } );
  333. it( 'should return the first root added to the document', () => {
  334. const rootA = doc.createRoot( '$root', 'rootA' );
  335. doc.createRoot( '$root', 'rootB' );
  336. doc.createRoot( '$root', 'rootC' );
  337. expect( doc._getDefaultRoot() ).to.equal( rootA );
  338. } );
  339. } );
  340. describe( 'destroy()', () => {
  341. it( 'should destroy selection instance', () => {
  342. const spy = sinon.spy( doc.selection, 'destroy' );
  343. doc.destroy();
  344. sinon.assert.calledOnce( spy );
  345. } );
  346. it( 'should stop listening to events', () => {
  347. const spy = sinon.spy();
  348. doc.listenTo( model, 'something', spy );
  349. model.fire( 'something' );
  350. sinon.assert.calledOnce( spy );
  351. doc.destroy();
  352. model.fire( 'something' );
  353. // Still once.
  354. sinon.assert.calledOnce( spy );
  355. } );
  356. } );
  357. describe( 'differ', () => {
  358. beforeEach( () => {
  359. doc.createRoot();
  360. } );
  361. it( 'should buffer document operations in differ', () => {
  362. sinon.spy( doc.differ, 'bufferOperation' );
  363. model.change( writer => {
  364. writer.insertText( 'foo', doc.getRoot(), 0 );
  365. } );
  366. expect( doc.differ.bufferOperation.called ).to.be.true;
  367. } );
  368. it( 'should not buffer changes not done on document', () => {
  369. sinon.spy( doc.differ, 'bufferOperation' );
  370. model.change( writer => {
  371. const docFrag = writer.createDocumentFragment();
  372. writer.insertText( 'foo', docFrag, 0 );
  373. } );
  374. expect( doc.differ.bufferOperation.called ).to.be.false;
  375. } );
  376. it( 'should buffer marker changes in differ', () => {
  377. sinon.spy( doc.differ, 'bufferMarkerChange' );
  378. model.change( () => {
  379. model.markers.set( 'marker', Range.createCollapsedAt( doc.getRoot(), 0 ) );
  380. } );
  381. expect( doc.differ.bufferMarkerChange.called ).to.be.true;
  382. } );
  383. it( 'should reset differ after change block is done', () => {
  384. model.change( writer => {
  385. writer.insertText( 'foo', doc.getRoot(), 0 );
  386. expect( doc.differ.getChanges().length > 0 ).to.be.true;
  387. } );
  388. expect( doc.differ.getChanges().length ).to.equal( 0 );
  389. } );
  390. } );
  391. describe( 'registerPostFixer()', () => {
  392. beforeEach( () => {
  393. doc.createRoot();
  394. } );
  395. it( 'should add a callback that is fired after changes are done', () => {
  396. const spy = sinon.spy();
  397. doc.registerPostFixer( spy );
  398. model.change( writer => {
  399. writer.insertText( 'foo', doc.getRoot(), 0 );
  400. } );
  401. expect( spy.calledOnce ).to.be.true;
  402. } );
  403. it( 'should not fire callbacks if no changes on document were done', () => {
  404. const spy = sinon.spy();
  405. doc.registerPostFixer( spy );
  406. model.change( writer => {
  407. const docFrag = writer.createDocumentFragment();
  408. writer.insertText( 'foo', docFrag, 0 );
  409. } );
  410. expect( spy.called ).to.be.false;
  411. } );
  412. it( 'should call all already processed callbacks again if a callback returned true', () => {
  413. const callA = sinon.spy();
  414. const callB = sinon.stub().onFirstCall().returns( true ).onSecondCall().returns( false );
  415. const callC = sinon.spy();
  416. doc.registerPostFixer( callA );
  417. doc.registerPostFixer( callB );
  418. doc.registerPostFixer( callC );
  419. model.change( writer => {
  420. writer.insertText( 'foo', doc.getRoot(), 0 );
  421. } );
  422. expect( callA.calledTwice ).to.be.true;
  423. expect( callB.calledTwice ).to.be.true;
  424. expect( callC.calledOnce ).to.be.true;
  425. } );
  426. } );
  427. it( 'should be correctly converted to json', () => {
  428. const serialized = jsonParseStringify( doc );
  429. expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' );
  430. expect( serialized.model ).to.equal( '[engine.model.Model]' );
  431. } );
  432. } );