document.js 17 KB

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