document.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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 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 model, doc;
  17. beforeEach( () => {
  18. model = new Model();
  19. doc = new Document( model );
  20. // Normally Model is the one who creates Document instance and keeps it as reference.
  21. // We have to be sure that Model uses the right Document instance.
  22. model.document = doc;
  23. } );
  24. describe( 'constructor()', () => {
  25. it( 'should create Document with no data, empty graveyard and selection set to default range', () => {
  26. const doc = new Document( model );
  27. expect( doc ).to.have.property( 'model' ).to.equal( model );
  28. expect( doc ).to.have.property( 'roots' ).that.is.instanceof( Map );
  29. expect( doc.roots.size ).to.equal( 1 );
  30. expect( doc.graveyard ).to.be.instanceof( RootElement );
  31. expect( doc.graveyard.maxOffset ).to.equal( 0 );
  32. expect( count( doc.selection.getRanges() ) ).to.equal( 1 );
  33. } );
  34. } );
  35. describe( 'model#applyOperation listener', () => {
  36. let operation, data, delta, batch;
  37. beforeEach( () => {
  38. data = { data: 'x' };
  39. operation = {
  40. type: 't',
  41. baseVersion: 0,
  42. isDocumentOperation: true,
  43. _execute: sinon.stub().returns( data ),
  44. _validate: () => {}
  45. };
  46. delta = new Delta();
  47. delta.addOperation( operation );
  48. delta.type = 'delta';
  49. batch = new Batch();
  50. batch.addDelta( delta );
  51. } );
  52. it( 'for document operation: should increase document version, execute operation and fire change event with proper data', () => {
  53. const changeCallback = sinon.spy();
  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( 't' );
  61. expect( changeCallback.args[ 0 ][ 2 ] ).to.equal( data );
  62. expect( changeCallback.args[ 0 ][ 3 ] ).to.equal( batch );
  63. expect( changeCallback.args[ 0 ][ 4 ] ).to.equal( delta.type );
  64. } );
  65. it( 'for non-document operation: should only execute operation', () => {
  66. const changeCallback = sinon.spy();
  67. operation.isDocumentOperation = false;
  68. doc.on( 'change', changeCallback );
  69. model.applyOperation( operation );
  70. expect( doc.version ).to.equal( 0 );
  71. expect( doc.history._deltas.length ).to.equal( 0 );
  72. sinon.assert.calledOnce( operation._execute );
  73. sinon.assert.notCalled( changeCallback );
  74. } );
  75. it( 'should do nothing if operation event was cancelled', () => {
  76. model.on( 'applyOperation', evt => evt.stop(), { priority: 'highest' } );
  77. model.applyOperation( operation );
  78. expect( doc.version ).to.equal( 0 );
  79. expect( operation._execute.called ).to.be.false;
  80. } );
  81. it( 'should throw an error on the operation base version and the document version is different', () => {
  82. const operation = {
  83. baseVersion: 1,
  84. isDocumentOperation: true,
  85. _execute: () => {}
  86. };
  87. expect(
  88. () => {
  89. model.applyOperation( operation );
  90. }
  91. ).to.throw( CKEditorError, /^model-document-applyOperation-wrong-version/ );
  92. } );
  93. } );
  94. describe( 'getRootNames()', () => {
  95. it( 'should return empty iterator if no roots exist', () => {
  96. expect( count( doc.getRootNames() ) ).to.equal( 0 );
  97. } );
  98. it( 'should return an iterator of all roots without the graveyard', () => {
  99. doc.createRoot( '$root', 'a' );
  100. doc.createRoot( '$root', 'b' );
  101. expect( Array.from( doc.getRootNames() ) ).to.deep.equal( [ 'a', 'b' ] );
  102. } );
  103. } );
  104. describe( 'createRoot()', () => {
  105. it( 'should create a new RootElement with default element and root names, add it to roots map and return it', () => {
  106. const root = doc.createRoot();
  107. expect( doc.roots.size ).to.equal( 2 );
  108. expect( root ).to.be.instanceof( RootElement );
  109. expect( root.maxOffset ).to.equal( 0 );
  110. expect( root ).to.have.property( 'name', '$root' );
  111. expect( root ).to.have.property( 'rootName', 'main' );
  112. } );
  113. it( 'should create a new RootElement with custom element and root names, add it to roots map and return it', () => {
  114. const root = doc.createRoot( 'customElementName', 'customRootName' );
  115. expect( doc.roots.size ).to.equal( 2 );
  116. expect( root ).to.be.instanceof( RootElement );
  117. expect( root.maxOffset ).to.equal( 0 );
  118. expect( root ).to.have.property( 'name', 'customElementName' );
  119. expect( root ).to.have.property( 'rootName', 'customRootName' );
  120. } );
  121. it( 'should throw an error when trying to create a second root with the same name', () => {
  122. doc.createRoot( '$root', 'rootName' );
  123. expect(
  124. () => {
  125. doc.createRoot( '$root', 'rootName' );
  126. }
  127. ).to.throw( CKEditorError, /model-document-createRoot-name-exists/ );
  128. } );
  129. } );
  130. describe( 'getRoot()', () => {
  131. it( 'should return a RootElement previously created with given name', () => {
  132. const newRoot = doc.createRoot();
  133. const getRoot = doc.getRoot();
  134. expect( getRoot ).to.equal( newRoot );
  135. } );
  136. it( 'should throw an error when trying to get non-existent root', () => {
  137. expect(
  138. () => {
  139. doc.getRoot( 'root' );
  140. }
  141. ).to.throw( CKEditorError, /model-document-getRoot-root-not-exist/ );
  142. } );
  143. } );
  144. describe( 'hasRoot()', () => {
  145. it( 'should return true when Document has RootElement with given name', () => {
  146. doc.createRoot();
  147. expect( doc.hasRoot( 'main' ) ).to.be.true;
  148. } );
  149. it( 'should return false when Document does not have RootElement with given name', () => {
  150. expect( doc.hasRoot( 'noroot' ) ).to.be.false;
  151. } );
  152. } );
  153. describe( 'selection', () => {
  154. it( 'should get updated attributes whenever attribute operation is applied', () => {
  155. sinon.spy( doc.selection, '_updateAttributes' );
  156. doc.fire( 'change', 'addAttribute' );
  157. expect( doc.selection._updateAttributes.called ).to.be.true;
  158. } );
  159. it( 'should throw if one of ranges starts or ends inside surrogate pair', () => {
  160. const root = doc.createRoot();
  161. root.appendChildren( '\uD83D\uDCA9' );
  162. expect( () => {
  163. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 0, root, 1 ) ] );
  164. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  165. expect( () => {
  166. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 2 ) ] );
  167. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  168. } );
  169. it( 'should throw if one of ranges starts or ends between base character and combining mark', () => {
  170. const root = doc.createRoot();
  171. root.appendChildren( 'foo̻̐ͩbar' );
  172. expect( () => {
  173. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 3, root, 9 ) ] );
  174. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  175. expect( () => {
  176. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 4, root, 9 ) ] );
  177. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  178. expect( () => {
  179. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 5, root, 9 ) ] );
  180. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  181. expect( () => {
  182. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 3 ) ] );
  183. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  184. expect( () => {
  185. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 4 ) ] );
  186. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  187. expect( () => {
  188. doc.selection.setRanges( [ Range.createFromParentsAndOffsets( root, 1, root, 5 ) ] );
  189. } ).to.throw( CKEditorError, /document-selection-wrong-position/ );
  190. } );
  191. } );
  192. describe( 'getNearestSelectionRange()', () => {
  193. let selection;
  194. beforeEach( () => {
  195. model.schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  196. model.schema.register( 'emptyBlock', { allowIn: '$root' } );
  197. model.schema.register( 'widget', {
  198. allowIn: '$root',
  199. isObject: true
  200. } );
  201. model.schema.register( 'blockWidget', {
  202. allowIn: '$root',
  203. allowContentOf: '$block',
  204. isObject: true
  205. } );
  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( '_getDefaultRoot()', () => {
  386. it( 'should return graveyard root if there are no other roots in the document', () => {
  387. expect( doc._getDefaultRoot() ).to.equal( doc.graveyard );
  388. } );
  389. it( 'should return the first root added to the document', () => {
  390. const rootA = doc.createRoot( '$root', 'rootA' );
  391. doc.createRoot( '$root', 'rootB' );
  392. doc.createRoot( '$root', 'rootC' );
  393. expect( doc._getDefaultRoot() ).to.equal( rootA );
  394. } );
  395. } );
  396. it( 'should be correctly converted to json', () => {
  397. const serialized = jsonParseStringify( doc );
  398. expect( serialized.selection ).to.equal( '[engine.model.DocumentSelection]' );
  399. expect( serialized.model ).to.equal( '[engine.model.Model]' );
  400. } );
  401. } );