document.js 16 KB

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