8
0

model.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
  6. import Model from '../../src/model/model';
  7. import NoOperation from '../../src/model/operation/nooperation';
  8. import deltaTransform from '../../src/model/delta/transform';
  9. import Delta from '../../src/model/delta/delta';
  10. import ModelText from '../../src/model/text';
  11. import ModelRange from '../../src/model/range';
  12. import ModelSelection from '../../src/model/selection';
  13. import ModelDocumentFragment from '../../src/model/documentfragment';
  14. import { getData, setData, stringify } from '../../src/dev-utils/model';
  15. describe( 'Model', () => {
  16. let model, schema, changes;
  17. beforeEach( () => {
  18. model = new Model();
  19. model.document.createRoot();
  20. model.document.createRoot( '$root', 'title' );
  21. schema = model.schema;
  22. changes = '';
  23. } );
  24. describe( 'change & enqueueChange', () => {
  25. it( 'should execute changes immediately', () => {
  26. model.change( () => {
  27. changes += 'A';
  28. } );
  29. expect( changes ).to.equal( 'A' );
  30. } );
  31. it( 'should pass returned value', () => {
  32. const ret = model.change( () => {
  33. changes += 'A';
  34. return 'B';
  35. } );
  36. changes += ret;
  37. expect( changes ).to.equal( 'AB' );
  38. } );
  39. it( 'should not mixed the order when nested change is called', () => {
  40. const ret = model.change( () => {
  41. changes += 'A';
  42. nested();
  43. return 'D';
  44. } );
  45. changes += ret;
  46. expect( changes ).to.equal( 'ABCD' );
  47. function nested() {
  48. const ret = model.change( () => {
  49. changes += 'B';
  50. return 'C';
  51. } );
  52. changes += ret;
  53. }
  54. } );
  55. it( 'should execute enqueueChange immediately if its the first block', () => {
  56. model.enqueueChange( () => {
  57. changes += 'A';
  58. nested();
  59. } );
  60. expect( changes ).to.equal( 'ABC' );
  61. function nested() {
  62. const ret = model.change( () => {
  63. changes += 'B';
  64. return 'C';
  65. } );
  66. changes += ret;
  67. }
  68. } );
  69. it( 'should be possible to enqueueChange immediately if its the first block', () => {
  70. model.enqueueChange( () => {
  71. changes += 'A';
  72. nested();
  73. } );
  74. expect( changes ).to.equal( 'AB' );
  75. function nested() {
  76. model.change( () => {
  77. changes += 'B';
  78. } );
  79. }
  80. } );
  81. it( 'should be possible to nest change in enqueueChange', () => {
  82. model.enqueueChange( () => {
  83. changes += 'A';
  84. nested();
  85. changes += 'D';
  86. } );
  87. expect( changes ).to.equal( 'ABCD' );
  88. function nested() {
  89. const ret = model.change( () => {
  90. changes += 'B';
  91. return 'C';
  92. } );
  93. changes += ret;
  94. }
  95. } );
  96. it( 'should be possible to nest enqueueChange in enqueueChange', () => {
  97. model.enqueueChange( () => {
  98. changes += 'A';
  99. nestedEnqueue();
  100. changes += 'B';
  101. } );
  102. expect( changes ).to.equal( 'ABC' );
  103. function nestedEnqueue() {
  104. model.enqueueChange( () => {
  105. changes += 'C';
  106. } );
  107. }
  108. } );
  109. it( 'should be possible to nest enqueueChange in changes', () => {
  110. const ret = model.change( () => {
  111. changes += 'A';
  112. nestedEnqueue();
  113. changes += 'B';
  114. return 'D';
  115. } );
  116. changes += ret;
  117. expect( changes ).to.equal( 'ABCD' );
  118. function nestedEnqueue() {
  119. model.enqueueChange( () => {
  120. changes += 'C';
  121. } );
  122. }
  123. } );
  124. it( 'should be possible to nest enqueueChange in enqueueChange event', () => {
  125. model.once( 'change', () => {
  126. model.enqueueChange( () => {
  127. changes += 'C';
  128. } );
  129. changes += 'B';
  130. } );
  131. model.on( 'changesDone', () => {
  132. changes += 'D';
  133. } );
  134. model.enqueueChange( () => {
  135. changes += 'A';
  136. } );
  137. expect( changes ).to.equal( 'ABCD' );
  138. } );
  139. it( 'should be possible to nest enqueueChange in changes event', () => {
  140. model.once( 'change', () => {
  141. model.enqueueChange( () => {
  142. changes += 'C';
  143. } );
  144. changes += 'B';
  145. } );
  146. model.on( 'changesDone', () => {
  147. changes += 'D';
  148. } );
  149. model.change( () => {
  150. changes += 'A';
  151. } );
  152. expect( changes ).to.equal( 'ABCD' );
  153. } );
  154. it( 'should be possible to nest changes in enqueueChange event', () => {
  155. model.once( 'change', () => {
  156. model.change( () => {
  157. changes += 'B';
  158. } );
  159. changes += 'C';
  160. } );
  161. model.on( 'changesDone', () => {
  162. changes += 'D';
  163. } );
  164. model.enqueueChange( () => {
  165. changes += 'A';
  166. } );
  167. expect( changes ).to.equal( 'ABCD' );
  168. } );
  169. it( 'should be possible to nest changes in changes event', () => {
  170. model.once( 'change', () => {
  171. model.change( () => {
  172. changes += 'B';
  173. } );
  174. changes += 'C';
  175. } );
  176. model.on( 'changesDone', () => {
  177. changes += 'D';
  178. } );
  179. model.change( () => {
  180. changes += 'A';
  181. } );
  182. expect( changes ).to.equal( 'ABCD' );
  183. } );
  184. it( 'should let mix blocks', () => {
  185. model.once( 'change', () => {
  186. model.change( () => {
  187. changes += 'B';
  188. nestedEnqueue();
  189. } );
  190. model.change( () => {
  191. changes += 'C';
  192. } );
  193. changes += 'D';
  194. } );
  195. model.on( 'changesDone', () => {
  196. changes += 'F';
  197. } );
  198. model.change( () => {
  199. changes += 'A';
  200. } );
  201. expect( changes ).to.equal( 'ABCDEF' );
  202. function nestedEnqueue() {
  203. model.enqueueChange( () => {
  204. changes += 'E';
  205. } );
  206. }
  207. } );
  208. it( 'should use the same writer in all change blocks (change & change)', () => {
  209. model.change( outerWriter => {
  210. model.change( innerWriter => {
  211. expect( innerWriter ).to.equal( outerWriter );
  212. } );
  213. } );
  214. } );
  215. it( 'should create new writer in enqueue block', () => {
  216. model.change( outerWriter => {
  217. model.enqueueChange( innerWriter => {
  218. expect( innerWriter ).to.not.equal( outerWriter );
  219. expect( innerWriter.batch ).to.not.equal( outerWriter.batch );
  220. } );
  221. } );
  222. } );
  223. it( 'should let you pass batch', () => {
  224. let outerBatch;
  225. model.change( outerWriter => {
  226. outerBatch = outerWriter.batch;
  227. model.enqueueChange( outerBatch, innerWriter => {
  228. expect( innerWriter.batch ).to.equal( outerBatch );
  229. } );
  230. } );
  231. } );
  232. it( 'should let you create transparent batch', () => {
  233. model.enqueueChange( 'transparent', writer => {
  234. expect( writer.batch.type ).to.equal( 'transparent' );
  235. } );
  236. } );
  237. } );
  238. describe( 'applyOperation', () => {
  239. it( 'should execute provided operation end return the result of operation', () => {
  240. const returnValue = { foo: 'bar' };
  241. const operation = {
  242. _execute: sinon.stub().returns( returnValue ),
  243. _validate: () => true
  244. };
  245. model.applyOperation( operation );
  246. sinon.assert.calledOnce( operation._execute );
  247. expect( model.applyOperation( operation ) ).to.equal( returnValue );
  248. } );
  249. } );
  250. describe( 'transformDeltas', () => {
  251. it( 'should use deltaTransform.transformDeltaSets', () => {
  252. sinon.spy( deltaTransform, 'transformDeltaSets' );
  253. // Prepare some empty-ish deltas so the transformation won't throw an error.
  254. const deltasA = [ new Delta() ];
  255. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  256. const deltasB = [ new Delta() ];
  257. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  258. model.transformDeltas( deltasA, deltasB );
  259. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  260. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, null ) ).to.be.true;
  261. deltaTransform.transformDeltaSets.restore();
  262. } );
  263. it( 'should pass itself to transformDeltaSets if useContext was set to true', () => {
  264. sinon.spy( deltaTransform, 'transformDeltaSets' );
  265. // Prepare some empty-ish deltas so the transformation won't throw an error.
  266. const deltasA = [ new Delta() ];
  267. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  268. const deltasB = [ new Delta() ];
  269. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  270. model.transformDeltas( deltasA, deltasB, true );
  271. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  272. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, model.document ) ).to.be.true;
  273. deltaTransform.transformDeltaSets.restore();
  274. } );
  275. } );
  276. describe( 'insertContent()', () => {
  277. it( 'should be decorated', () => {
  278. schema.extend( '$text', { allowIn: '$root' } ); // To surpress warnings.
  279. const spy = sinon.spy();
  280. model.on( 'insertContent', spy );
  281. model.insertContent( new ModelText( 'a' ), model.document.selection );
  282. expect( spy.calledOnce ).to.be.true;
  283. } );
  284. it( 'should insert content (item)', () => {
  285. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  286. setData( model, '<paragraph>fo[]ar</paragraph>' );
  287. model.insertContent( new ModelText( 'ob' ), model.document.selection );
  288. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  289. } );
  290. it( 'should insert content (document fragment)', () => {
  291. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  292. setData( model, '<paragraph>fo[]ar</paragraph>' );
  293. model.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ), model.document.selection );
  294. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  295. } );
  296. it( 'should use parent batch', () => {
  297. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  298. setData( model, '<paragraph>[]</paragraph>' );
  299. model.change( writer => {
  300. model.insertContent( new ModelText( 'abc' ), model.document.selection );
  301. expect( writer.batch.deltas ).to.length( 1 );
  302. } );
  303. } );
  304. } );
  305. describe( 'deleteContent()', () => {
  306. it( 'should be decorated', () => {
  307. const spy = sinon.spy();
  308. model.on( 'deleteContent', spy );
  309. model.deleteContent( model.document.selection );
  310. expect( spy.calledOnce ).to.be.true;
  311. } );
  312. it( 'should delete selected content', () => {
  313. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  314. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  315. model.deleteContent( model.document.selection );
  316. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  317. } );
  318. it( 'should use parent batch', () => {
  319. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  320. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  321. model.change( writer => {
  322. model.deleteContent( model.document.selection );
  323. expect( writer.batch.deltas ).to.length( 1 );
  324. } );
  325. } );
  326. } );
  327. describe( 'modifySelection()', () => {
  328. it( 'should be decorated', () => {
  329. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  330. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  331. const spy = sinon.spy();
  332. model.on( 'modifySelection', spy );
  333. model.modifySelection( model.document.selection );
  334. expect( spy.calledOnce ).to.be.true;
  335. } );
  336. it( 'should modify a selection', () => {
  337. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  338. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  339. model.modifySelection( model.document.selection, { direction: 'backward' } );
  340. expect( getData( model ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  341. } );
  342. } );
  343. describe( 'getSelectedContent()', () => {
  344. it( 'should be decorated', () => {
  345. const spy = sinon.spy();
  346. const sel = new ModelSelection();
  347. model.on( 'getSelectedContent', spy );
  348. model.getSelectedContent( sel );
  349. expect( spy.calledOnce ).to.be.true;
  350. } );
  351. it( 'should return selected content', () => {
  352. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  353. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  354. const content = model.getSelectedContent( model.document.selection );
  355. expect( stringify( content ) ).to.equal( 'ob' );
  356. } );
  357. it( 'should use parent batch', () => {
  358. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  359. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  360. model.change( writer => {
  361. model.getSelectedContent( model.document.selection );
  362. expect( writer.batch.deltas ).to.length( 1 );
  363. } );
  364. } );
  365. } );
  366. describe( 'hasContent()', () => {
  367. let root;
  368. beforeEach( () => {
  369. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  370. schema.register( 'div', { inheritAllFrom: '$block' } );
  371. schema.extend( '$block', { allowIn: 'div' } );
  372. schema.register( 'image', {
  373. isObject: true
  374. } );
  375. schema.extend( 'image', { allowIn: 'div' } );
  376. setData(
  377. model,
  378. '<div>' +
  379. '<paragraph></paragraph>' +
  380. '</div>' +
  381. '<paragraph>foo</paragraph>' +
  382. '<div>' +
  383. '<image></image>' +
  384. '</div>'
  385. );
  386. root = model.document.getRoot();
  387. } );
  388. it( 'should return true if given element has text node', () => {
  389. const pFoo = root.getChild( 1 );
  390. expect( model.hasContent( pFoo ) ).to.be.true;
  391. } );
  392. it( 'should return true if given element has element that is an object', () => {
  393. const divImg = root.getChild( 2 );
  394. expect( model.hasContent( divImg ) ).to.be.true;
  395. } );
  396. it( 'should return false if given element has no elements', () => {
  397. const pEmpty = root.getChild( 0 ).getChild( 0 );
  398. expect( model.hasContent( pEmpty ) ).to.be.false;
  399. } );
  400. it( 'should return false if given element has only elements that are not objects', () => {
  401. const divP = root.getChild( 0 );
  402. expect( model.hasContent( divP ) ).to.be.false;
  403. } );
  404. it( 'should return true if there is a text node in given range', () => {
  405. const range = ModelRange.createFromParentsAndOffsets( root, 1, root, 2 );
  406. expect( model.hasContent( range ) ).to.be.true;
  407. } );
  408. it( 'should return true if there is a part of text node in given range', () => {
  409. const pFoo = root.getChild( 1 );
  410. const range = ModelRange.createFromParentsAndOffsets( pFoo, 1, pFoo, 2 );
  411. expect( model.hasContent( range ) ).to.be.true;
  412. } );
  413. it( 'should return true if there is element that is an object in given range', () => {
  414. const divImg = root.getChild( 2 );
  415. const range = ModelRange.createFromParentsAndOffsets( divImg, 0, divImg, 1 );
  416. expect( model.hasContent( range ) ).to.be.true;
  417. } );
  418. it( 'should return false if range is collapsed', () => {
  419. const range = ModelRange.createFromParentsAndOffsets( root, 1, root, 1 );
  420. expect( model.hasContent( range ) ).to.be.false;
  421. } );
  422. it( 'should return false if range has only elements that are not objects', () => {
  423. const range = ModelRange.createFromParentsAndOffsets( root, 0, root, 1 );
  424. expect( model.hasContent( range ) ).to.be.false;
  425. } );
  426. } );
  427. describe( 'destroy()', () => {
  428. it( 'should destroy document', () => {
  429. sinon.spy( model.document, 'destroy' );
  430. model.destroy();
  431. sinon.assert.calledOnce( model.document.destroy );
  432. } );
  433. it( 'should stop listening', () => {
  434. const emitter = Object.create( EmitterMixin );
  435. const spy = sinon.spy();
  436. model.listenTo( emitter, 'event', spy );
  437. model.destroy();
  438. emitter.fire( 'event' );
  439. sinon.assert.notCalled( spy );
  440. } );
  441. } );
  442. } );