document.js 16 KB

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