model.js 15 KB

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