document.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../../src/model/document';
  6. import Schema from '../../../src/model/schema';
  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 CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  14. import count from '@ckeditor/ckeditor5-utils/src/count';
  15. import { jsonParseStringify } from '../../../tests/model/_utils/utils';
  16. import { setData, getData } from '../../../src/dev-utils/model';
  17. describe( 'Document', () => {
  18. let doc;
  19. beforeEach( () => {
  20. doc = new Document();
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
  24. expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Map );
  25. expect( doc.roots.size ).to.equal( 1 );
  26. expect( doc.graveyard ).to.be.instanceof( RootElement );
  27. expect( doc.graveyard.maxOffset ).to.equal( 0 );
  28. expect( count( doc.selection.getRanges() ) ).to.equal( 1 );
  29. expect( doc.schema ).to.be.instanceof( Schema );
  30. } );
  31. } );
  32. describe( 'getRootNames()', () => {
  33. it( 'should return empty iterator if no roots exist', () => {
  34. expect( count( doc.getRootNames() ) ).to.equal( 0 );
  35. } );
  36. it( 'should return an iterator of all roots without the graveyard', () => {
  37. doc.createRoot( '$root', 'a' );
  38. doc.createRoot( '$root', 'b' );
  39. expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
  40. } );
  41. } );
  42. describe( 'createRoot()', () => {
  43. it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
  44. const root = doc.createRoot();
  45. expect( doc.roots.size ).to.equal( 2 );
  46. expect( root ).to.be.instanceof( RootElement );
  47. expect( root.maxOffset ).to.equal( 0 );
  48. expect( root ).to.have.property( 'name', '$root' );
  49. expect( root ).to.have.property( 'rootName', 'main' );
  50. } );
  51. it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
  52. const root = doc.createRoot( 'customElementName', 'customRootName' );
  53. expect( doc.roots.size ).to.equal( 2 );
  54. expect( root ).to.be.instanceof( RootElement );
  55. expect( root.maxOffset ).to.equal( 0 );
  56. expect( root ).to.have.property( 'name', 'customElementName' );
  57. expect( root ).to.have.property( 'rootName', 'customRootName' );
  58. } );
  59. it( 'should throw an error when trying to create a second root with the same name', () => {
  60. doc.createRoot( '$root', 'rootName' );
  61. expect(
  62. () => {
  63. doc.createRoot( '$root', 'rootName' );
  64. }
  65. ).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
  66. } );
  67. } );
  68. describe( 'getRoot()', () => {
  69. it( 'should return a RootElement previously created with given name', () => {
  70. const newRoot = doc.createRoot();
  71. const getRoot = doc.getRoot();
  72. expect( getRoot ).to.equal( newRoot );
  73. } );
  74. it( 'should throw an error when trying to get non-existent root', () => {
  75. expect(
  76. () => {
  77. doc.getRoot( 'root' );
  78. }
  79. ).to.throw( CKEditorError, /model-document-getRoot-root-not-exist/ );
  80. } );
  81. } );
  82. describe( 'hasRoot()', () => {
  83. it( 'should return true when Document has RootElement with given name', () => {
  84. doc.createRoot();
  85. expect( doc.hasRoot( 'main' ) ).to.be.true;
  86. } );
  87. it( 'should return false when Document does not have RootElement with given name', () => {
  88. expect( doc.hasRoot( 'noroot' ) ).to.be.false;
  89. } );
  90. } );
  91. describe( 'applyOperation()', () => {
  92. it( 'should increase document version, execute operation and fire event with proper data ' +
  93. 'when operation is a document operation', () => {
  94. const changeCallback = sinon.spy();
  95. const type = 't';
  96. const data = { data: 'x' };
  97. const batch = new Batch();
  98. const delta = new Delta();
  99. delta.type = 'type';
  100. const operation = {
  101. type,
  102. baseVersion: 0,
  103. isDocumentOperation: true,
  104. _execute: sinon.stub().returns( data )
  105. };
  106. delta.addOperation( operation );
  107. batch.addDelta( delta );
  108. doc.on( 'change', changeCallback );
  109. doc.applyOperation( operation );
  110. expect( doc.version ).to.equal( 1 );
  111. expect( doc.history._deltas.length ).to.equal( 1 );
  112. sinon.assert.calledOnce( operation._execute );
  113. sinon.assert.calledOnce( changeCallback );
  114. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  115. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  116. expect( changeCallback.args[ 0 ][ 3 ] ).to.deep.equal( batch );
  117. expect( changeCallback.args[ 0 ][ 4 ] ).to.equal( delta.type );
  118. } );
  119. it( 'should execute operation, fire event with proper data and not increase document version ' +
  120. 'when operation is not a document operation', () => {
  121. const changeCallback = sinon.spy();
  122. const type = 't';
  123. const data = { data: 'x' };
  124. const batch = new Batch();
  125. const delta = new Delta();
  126. delta.type = 'type';
  127. const operation = {
  128. type,
  129. baseVersion: 0,
  130. isDocumentOperation: false,
  131. _execute: sinon.stub().returns( data )
  132. };
  133. delta.addOperation( operation );
  134. batch.addDelta( delta );
  135. doc.on( 'change', changeCallback );
  136. doc.applyOperation( operation );
  137. expect( doc.version ).to.equal( 0 );
  138. expect( doc.history._deltas.length ).to.equal( 0 );
  139. sinon.assert.calledOnce( operation._execute );
  140. sinon.assert.calledOnce( changeCallback );
  141. expect( changeCallback.args[ 0 ][ 1 ] ).to.equal( type );
  142. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  143. expect( changeCallback.args[ 0 ][ 3 ] ).to.deep.equal( batch );
  144. expect( changeCallback.args[ 0 ][ 4 ] ).to.equal( delta.type );
  145. } );
  146. it( 'should throw an error on the operation base version and the document version is different', () => {
  147. const operation = {
  148. baseVersion: 1
  149. };
  150. expect(
  151. () => {
  152. doc.applyOperation( operation );
  153. }
  154. ).to.throw( CKEditorError, /^model-document-applyOperation-wrong-version/ );
  155. } );
  156. } );
  157. describe( 'batch()', () => {
  158. it( 'should create a new batch with the document property', () => {
  159. const batch = doc.batch();
  160. expect( batch ).to.be.instanceof( Batch );
  161. expect( batch ).to.have.property( 'document' ).that.equals( doc );
  162. } );
  163. it( 'should set given batch type', () => {
  164. const batch = doc.batch( 'ignore' );
  165. expect( batch ).to.have.property( 'type' ).that.equals( 'ignore' );
  166. } );
  167. } );
  168. describe( 'enqueue()', () => {
  169. it( 'should be executed immediately and fire changesDone event', () => {
  170. const order = [];
  171. doc.on( 'changesDone', () => order.push( 'done' ) );
  172. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  173. expect( order ).to.have.length( 2 );
  174. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  175. expect( order[ 1 ] ).to.equal( 'done' );
  176. } );
  177. it( 'should fire done every time queue is empty', () => {
  178. const order = [];
  179. doc.on( 'changesDone', () => order.push( 'done' ) );
  180. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  181. doc.enqueueChanges( () => order.push( 'enqueue2' ) );
  182. expect( order ).to.have.length( 4 );
  183. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  184. expect( order[ 1 ] ).to.equal( 'done' );
  185. expect( order[ 2 ] ).to.equal( 'enqueue2' );
  186. expect( order[ 3 ] ).to.equal( 'done' );
  187. } );
  188. it( 'should put callbacks in the proper order', () => {
  189. const order = [];
  190. doc.on( 'changesDone', () => order.push( 'done' ) );
  191. doc.enqueueChanges( () => {
  192. order.push( 'enqueue1 start' );
  193. doc.enqueueChanges( () => {
  194. order.push( 'enqueue2 start' );
  195. doc.enqueueChanges( () => order.push( 'enqueue4' ) );
  196. order.push( 'enqueue2 end' );
  197. } );
  198. doc.enqueueChanges( () => order.push( 'enqueue3' ) );
  199. order.push( 'enqueue1 end' );
  200. } );
  201. expect( order ).to.have.length( 7 );
  202. expect( order[ 0 ] ).to.equal( 'enqueue1 start' );
  203. expect( order[ 1 ] ).to.equal( 'enqueue1 end' );
  204. expect( order[ 2 ] ).to.equal( 'enqueue2 start' );
  205. expect( order[ 3 ] ).to.equal( 'enqueue2 end' );
  206. expect( order[ 4 ] ).to.equal( 'enqueue3' );
  207. expect( order[ 5 ] ).to.equal( 'enqueue4' );
  208. expect( order[ 6 ] ).to.equal( 'done' );
  209. } );
  210. } );
  211. describe( 'selection', () => {
  212. it( 'should get updated attributes whenever attribute operation is applied', () => {
  213. sinon.spy( doc.selection, '_updateAttributes' );
  214. doc.fire( 'change', 'addAttribute' );
  215. expect( doc.selection._updateAttributes.called ).to.be.true;
  216. } );
  217. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  218. const root = doc.createRoot();
  219. root.appendChildren( '\uD83D\uDCA9' );
  220. expect( () => {
  221. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 0, root, 1 ) ] );
  222. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  223. expect( () => {
  224. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 2 ) ] );
  225. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  226. } );
  227. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  228. const root = doc.createRoot();
  229. root.appendChildren( 'foo̻̐ͩbar' );
  230. expect( () => {
  231. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 3, root, 9 ) ] );
  232. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  233. expect( () => {
  234. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 4, root, 9 ) ] );
  235. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  236. expect( () => {
  237. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 5, root, 9 ) ] );
  238. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  239. expect( () => {
  240. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 3 ) ] );
  241. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  242. expect( () => {
  243. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 4 ) ] );
  244. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  245. expect( () => {
  246. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 5 ) ] );
  247. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  248. } );
  249. } );
  250. describe( 'getNearestSelectionRange()', () => {
  251. let selection;
  252. beforeEach( () => {
  253. doc.schema.registerItem( 'paragraph', '$block' );
  254. doc.schema.registerItem( 'emptyBlock' );
  255. doc.schema.allow( { name: 'emptyBlock', inside: '$root' } );
  256. doc.schema.registerItem( 'widget' );
  257. doc.schema.allow( { name: 'widget', inside: '$root' } );
  258. doc.schema.objects.add( 'widget' );
  259. doc.schema.registerItem( 'blockWidget', '$block' );
  260. doc.schema.allow( { name: 'blockWidget', inside: '$root' } );
  261. doc.schema.objects.add( 'blockWidget' );
  262. doc.createRoot();
  263. selection = doc.selection;
  264. } );
  265. test(
  266. 'should return collapsed range if text node can be placed at that position - both',
  267. '<paragraph>[]</paragraph>',
  268. 'both',
  269. '<paragraph>[]</paragraph>'
  270. );
  271. test(
  272. 'should return collapsed range if text node can be placed at that position - forward',
  273. '<paragraph>[]</paragraph>',
  274. 'forward',
  275. '<paragraph>[]</paragraph>'
  276. );
  277. test(
  278. 'should return collapsed range if text node can be placed at that position - backward',
  279. '<paragraph>[]</paragraph>',
  280. 'backward',
  281. '<paragraph>[]</paragraph>'
  282. );
  283. test( 'should return null in empty document - both', '', 'both', null );
  284. test( 'should return null in empty document - backward', '', 'backward', null );
  285. test( 'should return null in empty document - forward', '', 'forward', null );
  286. test(
  287. 'should find range before when searching both ways',
  288. '<paragraph></paragraph>[]<paragraph></paragraph>',
  289. 'both',
  290. '<paragraph>[]</paragraph><paragraph></paragraph>'
  291. );
  292. test(
  293. 'should find range before when searching backward',
  294. '<paragraph></paragraph>[]<paragraph></paragraph>',
  295. 'backward',
  296. '<paragraph>[]</paragraph><paragraph></paragraph>'
  297. );
  298. test(
  299. 'should find range after when searching forward',
  300. '<paragraph></paragraph>[]<paragraph></paragraph>',
  301. 'forward',
  302. '<paragraph></paragraph><paragraph>[]</paragraph>'
  303. );
  304. test(
  305. 'should find range after when searching both ways when it is closer',
  306. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  307. 'both',
  308. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  309. );
  310. test(
  311. 'should find range before when searching both ways when it is closer',
  312. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  313. 'both',
  314. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  315. );
  316. test(
  317. 'should return null if there is no valid range',
  318. '[]<emptyBlock></emptyBlock>',
  319. 'both',
  320. null
  321. );
  322. test(
  323. 'should return null if there is no valid range in given direction - backward',
  324. '[]<paragraph></paragraph>',
  325. 'backward',
  326. null
  327. );
  328. test(
  329. 'should return null if there is no valid range in given direction - forward',
  330. '<paragraph></paragraph>[]',
  331. 'forward',
  332. null
  333. );
  334. test(
  335. 'should move forward when placed at root start',
  336. '[]<paragraph></paragraph><paragraph></paragraph>',
  337. 'both',
  338. '<paragraph>[]</paragraph><paragraph></paragraph>'
  339. );
  340. test(
  341. 'should move backward when placed at root end',
  342. '<paragraph></paragraph><paragraph></paragraph>[]',
  343. 'both',
  344. '<paragraph></paragraph><paragraph>[]</paragraph>'
  345. );
  346. describe( 'in case of objects which do not allow text inside', () => {
  347. test(
  348. 'should select nearest object (o[]o) - both',
  349. '<widget></widget>[]<widget></widget>',
  350. 'both',
  351. '[<widget></widget>]<widget></widget>'
  352. );
  353. test(
  354. 'should select nearest object (o[]o) - forward',
  355. '<widget></widget>[]<widget></widget>',
  356. 'forward',
  357. '<widget></widget>[<widget></widget>]'
  358. );
  359. test(
  360. 'should select nearest object (o[]o) - backward',
  361. '<widget></widget>[]<widget></widget>',
  362. 'both',
  363. '[<widget></widget>]<widget></widget>'
  364. );
  365. test(
  366. 'should select nearest object (p[]o) - forward',
  367. '<paragraph></paragraph>[]<widget></widget>',
  368. 'forward',
  369. '<paragraph></paragraph>[<widget></widget>]'
  370. );
  371. test(
  372. 'should select nearest object (o[]p) - both',
  373. '<widget></widget>[]<paragraph></paragraph>',
  374. 'both',
  375. '[<widget></widget>]<paragraph></paragraph>'
  376. );
  377. test(
  378. 'should select nearest object (o[]p) - backward',
  379. '<widget></widget>[]<paragraph></paragraph>',
  380. 'backward',
  381. '[<widget></widget>]<paragraph></paragraph>'
  382. );
  383. test(
  384. 'should select nearest object ([]o) - both',
  385. '[]<widget></widget><paragraph></paragraph>',
  386. 'both',
  387. '[<widget></widget>]<paragraph></paragraph>'
  388. );
  389. test(
  390. 'should select nearest object ([]o) - forward',
  391. '[]<widget></widget><paragraph></paragraph>',
  392. 'forward',
  393. '[<widget></widget>]<paragraph></paragraph>'
  394. );
  395. test(
  396. 'should select nearest object (o[]) - both',
  397. '<paragraph></paragraph><widget></widget>[]',
  398. 'both',
  399. '<paragraph></paragraph>[<widget></widget>]'
  400. );
  401. test(
  402. 'should select nearest object (o[]) - backward',
  403. '<paragraph></paragraph><widget></widget>[]',
  404. 'both',
  405. '<paragraph></paragraph>[<widget></widget>]'
  406. );
  407. } );
  408. describe( 'in case of objects which allow text inside', () => {
  409. test(
  410. 'should select nearest object which allows text (o[]o) - both',
  411. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  412. 'both',
  413. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  414. );
  415. test(
  416. 'should select nearest object (o[]p) - both',
  417. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  418. 'both',
  419. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  420. );
  421. test(
  422. 'should select nearest object which allows text ([]o) - both',
  423. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  424. 'both',
  425. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  426. );
  427. } );
  428. function test( testName, data, direction, expected ) {
  429. it( testName, () => {
  430. setData( doc, data );
  431. const range = doc.getNearestSelectionRange( selection.anchor, direction );
  432. if ( expected === null ) {
  433. expect( range ).to.be.null;
  434. } else {
  435. selection.setRanges( [ range ] );
  436. expect( getData( doc ) ).to.equal( expected );
  437. }
  438. } );
  439. }
  440. } );
  441. describe( 'transformDeltas', () => {
  442. it( 'should use deltaTransform.transformDeltaSets', () => {
  443. sinon.spy( deltaTransform, 'transformDeltaSets' );
  444. // Prepare some empty-ish deltas so the transformation won't throw an error.
  445. const deltasA = [ new Delta() ];
  446. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  447. const deltasB = [ new Delta() ];
  448. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  449. doc.transformDeltas( deltasA, deltasB );
  450. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  451. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, null ) ).to.be.true;
  452. deltaTransform.transformDeltaSets.restore();
  453. } );
  454. it( 'should pass itself to transformDeltaSets if useContext was set to true', () => {
  455. sinon.spy( deltaTransform, 'transformDeltaSets' );
  456. // Prepare some empty-ish deltas so the transformation won't throw an error.
  457. const deltasA = [ new Delta() ];
  458. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  459. const deltasB = [ new Delta() ];
  460. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  461. doc.transformDeltas( deltasA, deltasB, true );
  462. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  463. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, doc ) ).to.be.true;
  464. deltaTransform.transformDeltaSets.restore();
  465. } );
  466. } );
  467. describe( '_getDefaultRoot()', () => {
  468. it( 'should return graveyard root if there are no other roots in the document', () => {
  469. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  470. } );
  471. it( 'should return the first root added to the document', () => {
  472. const rootA = doc.createRoot( '$root', 'rootA' );
  473. doc.createRoot( '$root', 'rootB' );
  474. doc.createRoot( '$root', 'rootC' );
  475. expect( doc._getDefaultRoot() ).to.equal( rootA );
  476. } );
  477. } );
  478. it( 'should be correctly converted to json', () => {
  479. expect( jsonParseStringify( doc ).selection ).to.equal( '[engine.model.DocumentSelection]' );
  480. } );
  481. } );