document.js 17 KB

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