document.js 15 KB

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