document.js 15 KB

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