document.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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, not fire event 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.notCalled( changeCallback );
  141. } );
  142. it( 'should throw an error on the operation base version and the document version is different', () => {
  143. const operation = {
  144. baseVersion: 1
  145. };
  146. expect(
  147. () => {
  148. doc.applyOperation( operation );
  149. }
  150. ).to.throw( CKEditorError, /^model-document-applyOperation-wrong-version/ );
  151. } );
  152. } );
  153. describe( 'batch()', () => {
  154. it( 'should create a new batch with the document property', () => {
  155. const batch = doc.batch();
  156. expect( batch ).to.be.instanceof( Batch );
  157. expect( batch ).to.have.property( 'document' ).that.equals( doc );
  158. } );
  159. it( 'should set given batch type', () => {
  160. const batch = doc.batch( 'ignore' );
  161. expect( batch ).to.have.property( 'type' ).that.equals( 'ignore' );
  162. } );
  163. } );
  164. describe( 'enqueue()', () => {
  165. it( 'should be executed immediately and fire changesDone event', () => {
  166. const order = [];
  167. doc.on( 'changesDone', () => order.push( 'done' ) );
  168. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  169. expect( order ).to.have.length( 2 );
  170. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  171. expect( order[ 1 ] ).to.equal( 'done' );
  172. } );
  173. it( 'should fire done every time queue is empty', () => {
  174. const order = [];
  175. doc.on( 'changesDone', () => order.push( 'done' ) );
  176. doc.enqueueChanges( () => order.push( 'enqueue1' ) );
  177. doc.enqueueChanges( () => order.push( 'enqueue2' ) );
  178. expect( order ).to.have.length( 4 );
  179. expect( order[ 0 ] ).to.equal( 'enqueue1' );
  180. expect( order[ 1 ] ).to.equal( 'done' );
  181. expect( order[ 2 ] ).to.equal( 'enqueue2' );
  182. expect( order[ 3 ] ).to.equal( 'done' );
  183. } );
  184. it( 'should put callbacks in the proper order', () => {
  185. const order = [];
  186. doc.on( 'changesDone', () => order.push( 'done' ) );
  187. doc.enqueueChanges( () => {
  188. order.push( 'enqueue1 start' );
  189. doc.enqueueChanges( () => {
  190. order.push( 'enqueue2 start' );
  191. doc.enqueueChanges( () => order.push( 'enqueue4' ) );
  192. order.push( 'enqueue2 end' );
  193. } );
  194. doc.enqueueChanges( () => order.push( 'enqueue3' ) );
  195. order.push( 'enqueue1 end' );
  196. } );
  197. expect( order ).to.have.length( 7 );
  198. expect( order[ 0 ] ).to.equal( 'enqueue1 start' );
  199. expect( order[ 1 ] ).to.equal( 'enqueue1 end' );
  200. expect( order[ 2 ] ).to.equal( 'enqueue2 start' );
  201. expect( order[ 3 ] ).to.equal( 'enqueue2 end' );
  202. expect( order[ 4 ] ).to.equal( 'enqueue3' );
  203. expect( order[ 5 ] ).to.equal( 'enqueue4' );
  204. expect( order[ 6 ] ).to.equal( 'done' );
  205. } );
  206. } );
  207. describe( 'selection', () => {
  208. it( 'should get updated attributes whenever attribute operation is applied', () => {
  209. sinon.spy( doc.selection, '_updateAttributes' );
  210. doc.fire( 'change', 'addAttribute' );
  211. expect( doc.selection._updateAttributes.called ).to.be.true;
  212. } );
  213. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  214. const root = doc.createRoot();
  215. root.appendChildren( '\uD83D\uDCA9' );
  216. expect( () => {
  217. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 0, root, 1 ) ] );
  218. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  219. expect( () => {
  220. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 2 ) ] );
  221. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  222. } );
  223. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  224. const root = doc.createRoot();
  225. root.appendChildren( 'foo̻̐ͩbar' );
  226. expect( () => {
  227. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 3, root, 9 ) ] );
  228. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  229. expect( () => {
  230. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 4, root, 9 ) ] );
  231. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  232. expect( () => {
  233. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 5, root, 9 ) ] );
  234. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  235. expect( () => {
  236. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 3 ) ] );
  237. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  238. expect( () => {
  239. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 4 ) ] );
  240. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  241. expect( () => {
  242. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 5 ) ] );
  243. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  244. } );
  245. } );
  246. describe( 'getNearestSelectionRange()', () => {
  247. let selection;
  248. beforeEach( () => {
  249. doc.schema.registerItem( 'paragraph', '$block' );
  250. doc.schema.registerItem( 'emptyBlock' );
  251. doc.schema.allow( { name: 'emptyBlock', inside: '$root' } );
  252. doc.schema.registerItem( 'widget' );
  253. doc.schema.allow( { name: 'widget', inside: '$root' } );
  254. doc.schema.objects.add( 'widget' );
  255. doc.schema.registerItem( 'blockWidget', '$block' );
  256. doc.schema.allow( { name: 'blockWidget', inside: '$root' } );
  257. doc.schema.objects.add( 'blockWidget' );
  258. doc.createRoot();
  259. selection = doc.selection;
  260. } );
  261. test(
  262. 'should return collapsed range if text node can be placed at that position - both',
  263. '<paragraph>[]</paragraph>',
  264. 'both',
  265. '<paragraph>[]</paragraph>'
  266. );
  267. test(
  268. 'should return collapsed range if text node can be placed at that position - forward',
  269. '<paragraph>[]</paragraph>',
  270. 'forward',
  271. '<paragraph>[]</paragraph>'
  272. );
  273. test(
  274. 'should return collapsed range if text node can be placed at that position - backward',
  275. '<paragraph>[]</paragraph>',
  276. 'backward',
  277. '<paragraph>[]</paragraph>'
  278. );
  279. test( 'should return null in empty document - both', '', 'both', null );
  280. test( 'should return null in empty document - backward', '', 'backward', null );
  281. test( 'should return null in empty document - forward', '', 'forward', null );
  282. test(
  283. 'should find range before when searching both ways',
  284. '<paragraph></paragraph>[]<paragraph></paragraph>',
  285. 'both',
  286. '<paragraph>[]</paragraph><paragraph></paragraph>'
  287. );
  288. test(
  289. 'should find range before when searching backward',
  290. '<paragraph></paragraph>[]<paragraph></paragraph>',
  291. 'backward',
  292. '<paragraph>[]</paragraph><paragraph></paragraph>'
  293. );
  294. test(
  295. 'should find range after when searching forward',
  296. '<paragraph></paragraph>[]<paragraph></paragraph>',
  297. 'forward',
  298. '<paragraph></paragraph><paragraph>[]</paragraph>'
  299. );
  300. test(
  301. 'should find range after when searching both ways when it is closer',
  302. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  303. 'both',
  304. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  305. );
  306. test(
  307. 'should find range before when searching both ways when it is closer',
  308. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  309. 'both',
  310. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  311. );
  312. test(
  313. 'should return null if there is no valid range',
  314. '[]<emptyBlock></emptyBlock>',
  315. 'both',
  316. null
  317. );
  318. test(
  319. 'should return null if there is no valid range in given direction - backward',
  320. '[]<paragraph></paragraph>',
  321. 'backward',
  322. null
  323. );
  324. test(
  325. 'should return null if there is no valid range in given direction - forward',
  326. '<paragraph></paragraph>[]',
  327. 'forward',
  328. null
  329. );
  330. test(
  331. 'should move forward when placed at root start',
  332. '[]<paragraph></paragraph><paragraph></paragraph>',
  333. 'both',
  334. '<paragraph>[]</paragraph><paragraph></paragraph>'
  335. );
  336. test(
  337. 'should move backward when placed at root end',
  338. '<paragraph></paragraph><paragraph></paragraph>[]',
  339. 'both',
  340. '<paragraph></paragraph><paragraph>[]</paragraph>'
  341. );
  342. describe( 'in case of objects which do not allow text inside', () => {
  343. test(
  344. 'should select nearest object (o[]o) - both',
  345. '<widget></widget>[]<widget></widget>',
  346. 'both',
  347. '[<widget></widget>]<widget></widget>'
  348. );
  349. test(
  350. 'should select nearest object (o[]o) - forward',
  351. '<widget></widget>[]<widget></widget>',
  352. 'forward',
  353. '<widget></widget>[<widget></widget>]'
  354. );
  355. test(
  356. 'should select nearest object (o[]o) - backward',
  357. '<widget></widget>[]<widget></widget>',
  358. 'both',
  359. '[<widget></widget>]<widget></widget>'
  360. );
  361. test(
  362. 'should select nearest object (p[]o) - forward',
  363. '<paragraph></paragraph>[]<widget></widget>',
  364. 'forward',
  365. '<paragraph></paragraph>[<widget></widget>]'
  366. );
  367. test(
  368. 'should select nearest object (o[]p) - both',
  369. '<widget></widget>[]<paragraph></paragraph>',
  370. 'both',
  371. '[<widget></widget>]<paragraph></paragraph>'
  372. );
  373. test(
  374. 'should select nearest object (o[]p) - backward',
  375. '<widget></widget>[]<paragraph></paragraph>',
  376. 'backward',
  377. '[<widget></widget>]<paragraph></paragraph>'
  378. );
  379. test(
  380. 'should select nearest object ([]o) - both',
  381. '[]<widget></widget><paragraph></paragraph>',
  382. 'both',
  383. '[<widget></widget>]<paragraph></paragraph>'
  384. );
  385. test(
  386. 'should select nearest object ([]o) - forward',
  387. '[]<widget></widget><paragraph></paragraph>',
  388. 'forward',
  389. '[<widget></widget>]<paragraph></paragraph>'
  390. );
  391. test(
  392. 'should select nearest object (o[]) - both',
  393. '<paragraph></paragraph><widget></widget>[]',
  394. 'both',
  395. '<paragraph></paragraph>[<widget></widget>]'
  396. );
  397. test(
  398. 'should select nearest object (o[]) - backward',
  399. '<paragraph></paragraph><widget></widget>[]',
  400. 'both',
  401. '<paragraph></paragraph>[<widget></widget>]'
  402. );
  403. } );
  404. describe( 'in case of objects which allow text inside', () => {
  405. test(
  406. 'should select nearest object which allows text (o[]o) - both',
  407. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  408. 'both',
  409. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  410. );
  411. test(
  412. 'should select nearest object (o[]p) - both',
  413. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  414. 'both',
  415. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  416. );
  417. test(
  418. 'should select nearest object which allows text ([]o) - both',
  419. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  420. 'both',
  421. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  422. );
  423. } );
  424. function test( testName, data, direction, expected ) {
  425. it( testName, () => {
  426. setData( doc, data );
  427. const range = doc.getNearestSelectionRange( selection.anchor, direction );
  428. if ( expected === null ) {
  429. expect( range ).to.be.null;
  430. } else {
  431. selection.setRanges( [ range ] );
  432. expect( getData( doc ) ).to.equal( expected );
  433. }
  434. } );
  435. }
  436. } );
  437. describe( 'transformDeltas', () => {
  438. it( 'should use deltaTransform.transformDeltaSets', () => {
  439. sinon.spy( deltaTransform, 'transformDeltaSets' );
  440. // Prepare some empty-ish deltas so the transformation won't throw an error.
  441. const deltasA = [ new Delta() ];
  442. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  443. const deltasB = [ new Delta() ];
  444. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  445. doc.transformDeltas( deltasA, deltasB );
  446. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  447. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, null ) ).to.be.true;
  448. deltaTransform.transformDeltaSets.restore();
  449. } );
  450. it( 'should pass itself to transformDeltaSets if useContext was set to true', () => {
  451. sinon.spy( deltaTransform, 'transformDeltaSets' );
  452. // Prepare some empty-ish deltas so the transformation won't throw an error.
  453. const deltasA = [ new Delta() ];
  454. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  455. const deltasB = [ new Delta() ];
  456. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  457. doc.transformDeltas( deltasA, deltasB, true );
  458. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  459. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, doc ) ).to.be.true;
  460. deltaTransform.transformDeltaSets.restore();
  461. } );
  462. } );
  463. describe( '_getDefaultRoot()', () => {
  464. it( 'should return graveyard root if there are no other roots in the document', () => {
  465. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  466. } );
  467. it( 'should return the first root added to the document', () => {
  468. const rootA = doc.createRoot( '$root', 'rootA' );
  469. doc.createRoot( '$root', 'rootB' );
  470. doc.createRoot( '$root', 'rootC' );
  471. expect( doc._getDefaultRoot() ).to.equal( rootA );
  472. } );
  473. } );
  474. it( 'should be correctly converted to json', () => {
  475. expect( jsonParseStringify( doc ).selection ).to.equal( '[engine.model.DocumentSelection]' );
  476. } );
  477. } );