document.js 16 KB

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