document.js 17 KB

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