8
0

document.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 with default "main" name', () => {
  133. const newRoot = doc.createRoot( 'main' );
  134. expect( doc.getRoot() ).to.equal( newRoot );
  135. } );
  136. it( 'should return a RootElement with custom name', () => {
  137. const newRoot = doc.createRoot( 'custom', 'custom' );
  138. expect( doc.getRoot( 'custom' ) ).to.equal( newRoot );
  139. } );
  140. it( 'should return null when trying to get non-existent root', () => {
  141. expect( doc.getRoot( 'not-existing' ) ).to.null;
  142. } );
  143. } );
  144. describe( 'selection', () => {
  145. it( 'should get updated attributes whenever attribute operation is applied', () => {
  146. sinon.spy( doc.selection, '_updateAttributes' );
  147. doc.fire( 'change', 'addAttribute' );
  148. expect( doc.selection._updateAttributes.called ).to.be.true;
  149. } );
  150. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  151. const root = doc.createRoot();
  152. root.appendChildren( '\uD83D\uDCA9' );
  153. expect( () => {
  154. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 0, root, 1 ) ] );
  155. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  156. expect( () => {
  157. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 2 ) ] );
  158. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  159. } );
  160. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  161. const root = doc.createRoot();
  162. root.appendChildren( 'foo̻̐ͩbar' );
  163. expect( () => {
  164. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 3, root, 9 ) ] );
  165. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  166. expect( () => {
  167. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 4, root, 9 ) ] );
  168. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  169. expect( () => {
  170. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 5, root, 9 ) ] );
  171. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  172. expect( () => {
  173. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 3 ) ] );
  174. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  175. expect( () => {
  176. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 4 ) ] );
  177. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  178. expect( () => {
  179. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 5 ) ] );
  180. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  181. } );
  182. } );
  183. describe( 'getNearestSelectionRange()', () => {
  184. let selection;
  185. beforeEach( () => {
  186. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  187. model.schema.register( 'emptyBlock', { allowIn: '$root' } );
  188. model.schema.register( 'widget', {
  189. allowIn: '$root',
  190. isObject: true
  191. } );
  192. model.schema.register( 'blockWidget', {
  193. allowIn: '$root',
  194. allowContentOf: '$block',
  195. isObject: true
  196. } );
  197. doc.createRoot();
  198. selection = doc.selection;
  199. } );
  200. test(
  201. 'should return collapsed range if text node can be placed at that position - both',
  202. '<paragraph>[]</paragraph>',
  203. 'both',
  204. '<paragraph>[]</paragraph>'
  205. );
  206. test(
  207. 'should return collapsed range if text node can be placed at that position - forward',
  208. '<paragraph>[]</paragraph>',
  209. 'forward',
  210. '<paragraph>[]</paragraph>'
  211. );
  212. test(
  213. 'should return collapsed range if text node can be placed at that position - backward',
  214. '<paragraph>[]</paragraph>',
  215. 'backward',
  216. '<paragraph>[]</paragraph>'
  217. );
  218. test( 'should return null in empty document - both', '', 'both', null );
  219. test( 'should return null in empty document - backward', '', 'backward', null );
  220. test( 'should return null in empty document - forward', '', 'forward', null );
  221. test(
  222. 'should find range before when searching both ways',
  223. '<paragraph></paragraph>[]<paragraph></paragraph>',
  224. 'both',
  225. '<paragraph>[]</paragraph><paragraph></paragraph>'
  226. );
  227. test(
  228. 'should find range before when searching backward',
  229. '<paragraph></paragraph>[]<paragraph></paragraph>',
  230. 'backward',
  231. '<paragraph>[]</paragraph><paragraph></paragraph>'
  232. );
  233. test(
  234. 'should find range after when searching forward',
  235. '<paragraph></paragraph>[]<paragraph></paragraph>',
  236. 'forward',
  237. '<paragraph></paragraph><paragraph>[]</paragraph>'
  238. );
  239. test(
  240. 'should find range after when searching both ways when it is closer',
  241. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<paragraph></paragraph>',
  242. 'both',
  243. '<paragraph></paragraph><emptyBlock></emptyBlock><paragraph>[]</paragraph>'
  244. );
  245. test(
  246. 'should find range before when searching both ways when it is closer',
  247. '<paragraph></paragraph><emptyBlock></emptyBlock>[]<emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>',
  248. 'both',
  249. '<paragraph>[]</paragraph><emptyBlock></emptyBlock><emptyBlock></emptyBlock><emptyBlock></emptyBlock><paragraph></paragraph>'
  250. );
  251. test(
  252. 'should return null if there is no valid range',
  253. '[]<emptyBlock></emptyBlock>',
  254. 'both',
  255. null
  256. );
  257. test(
  258. 'should return null if there is no valid range in given direction - backward',
  259. '[]<paragraph></paragraph>',
  260. 'backward',
  261. null
  262. );
  263. test(
  264. 'should return null if there is no valid range in given direction - forward',
  265. '<paragraph></paragraph>[]',
  266. 'forward',
  267. null
  268. );
  269. test(
  270. 'should move forward when placed at root start',
  271. '[]<paragraph></paragraph><paragraph></paragraph>',
  272. 'both',
  273. '<paragraph>[]</paragraph><paragraph></paragraph>'
  274. );
  275. test(
  276. 'should move backward when placed at root end',
  277. '<paragraph></paragraph><paragraph></paragraph>[]',
  278. 'both',
  279. '<paragraph></paragraph><paragraph>[]</paragraph>'
  280. );
  281. describe( 'in case of objects which do not allow text inside', () => {
  282. test(
  283. 'should select nearest object (o[]o) - both',
  284. '<widget></widget>[]<widget></widget>',
  285. 'both',
  286. '[<widget></widget>]<widget></widget>'
  287. );
  288. test(
  289. 'should select nearest object (o[]o) - forward',
  290. '<widget></widget>[]<widget></widget>',
  291. 'forward',
  292. '<widget></widget>[<widget></widget>]'
  293. );
  294. test(
  295. 'should select nearest object (o[]o) - backward',
  296. '<widget></widget>[]<widget></widget>',
  297. 'both',
  298. '[<widget></widget>]<widget></widget>'
  299. );
  300. test(
  301. 'should select nearest object (p[]o) - forward',
  302. '<paragraph></paragraph>[]<widget></widget>',
  303. 'forward',
  304. '<paragraph></paragraph>[<widget></widget>]'
  305. );
  306. test(
  307. 'should select nearest object (o[]p) - both',
  308. '<widget></widget>[]<paragraph></paragraph>',
  309. 'both',
  310. '[<widget></widget>]<paragraph></paragraph>'
  311. );
  312. test(
  313. 'should select nearest object (o[]p) - backward',
  314. '<widget></widget>[]<paragraph></paragraph>',
  315. 'backward',
  316. '[<widget></widget>]<paragraph></paragraph>'
  317. );
  318. test(
  319. 'should select nearest object ([]o) - both',
  320. '[]<widget></widget><paragraph></paragraph>',
  321. 'both',
  322. '[<widget></widget>]<paragraph></paragraph>'
  323. );
  324. test(
  325. 'should select nearest object ([]o) - forward',
  326. '[]<widget></widget><paragraph></paragraph>',
  327. 'forward',
  328. '[<widget></widget>]<paragraph></paragraph>'
  329. );
  330. test(
  331. 'should select nearest object (o[]) - both',
  332. '<paragraph></paragraph><widget></widget>[]',
  333. 'both',
  334. '<paragraph></paragraph>[<widget></widget>]'
  335. );
  336. test(
  337. 'should select nearest object (o[]) - backward',
  338. '<paragraph></paragraph><widget></widget>[]',
  339. 'both',
  340. '<paragraph></paragraph>[<widget></widget>]'
  341. );
  342. } );
  343. describe( 'in case of objects which allow text inside', () => {
  344. test(
  345. 'should select nearest object which allows text (o[]o) - both',
  346. '<blockWidget></blockWidget>[]<blockWidget></blockWidget>',
  347. 'both',
  348. '[<blockWidget></blockWidget>]<blockWidget></blockWidget>'
  349. );
  350. test(
  351. 'should select nearest object (o[]p) - both',
  352. '<blockWidget></blockWidget>[]<paragraph></paragraph>',
  353. 'both',
  354. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  355. );
  356. test(
  357. 'should select nearest object which allows text ([]o) - both',
  358. '[]<blockWidget></blockWidget><paragraph></paragraph>',
  359. 'both',
  360. '[<blockWidget></blockWidget>]<paragraph></paragraph>'
  361. );
  362. } );
  363. function test( testName, data, direction, expected ) {
  364. it( testName, () => {
  365. setData( model, data );
  366. const range = doc.getNearestSelectionRange( selection.anchor, direction );
  367. if ( expected === null ) {
  368. expect( range ).to.be.null;
  369. } else {
  370. selection.setRanges( [ range ] );
  371. expect( getData( model ) ).to.equal( expected );
  372. }
  373. } );
  374. }
  375. } );
  376. describe( '_getDefaultRoot()', () => {
  377. it( 'should return graveyard root if there are no other roots in the document', () => {
  378. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  379. } );
  380. it( 'should return the first root added to the document', () => {
  381. const rootA = doc.createRoot( '$root', 'rootA' );
  382. doc.createRoot( '$root', 'rootB' );
  383. doc.createRoot( '$root', 'rootC' );
  384. expect( doc._getDefaultRoot() ).to.equal( rootA );
  385. } );
  386. } );
  387. describe( 'destroy()', () => {
  388. it( 'should destroy selection instance', () => {
  389. const spy = sinon.spy( doc.selection, 'destroy' );
  390. doc.destroy();
  391. sinon.assert.calledOnce( spy );
  392. } );
  393. it( 'should stop listening to events', () => {
  394. const spy = sinon.spy();
  395. doc.listenTo( model, 'something', spy );
  396. model.fire( 'something' );
  397. sinon.assert.calledOnce( spy );
  398. doc.destroy();
  399. model.fire( 'something' );
  400. // Still once.
  401. sinon.assert.calledOnce( spy );
  402. } );
  403. } );
  404. it( 'should be correctly converted to json', () => {
  405. const serialized = jsonParseStringify( doc );
  406. expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' );
  407. expect( serialized.model ).to.equal( '[engine.model.Model]' );
  408. } );
  409. } );