model.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  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', () => {
  246. const operation = {
  247. _execute: sinon.spy(),
  248. _validate: () => true
  249. };
  250. model.applyOperation( operation );
  251. sinon.assert.calledOnce( operation._execute );
  252. } );
  253. } );
  254. describe( 'transformDeltas()', () => {
  255. it( 'should use deltaTransform.transformDeltaSets', () => {
  256. sinon.spy( deltaTransform, 'transformDeltaSets' );
  257. // Prepare some empty-ish deltas so the transformation won't throw an error.
  258. const deltasA = [ new Delta() ];
  259. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  260. const deltasB = [ new Delta() ];
  261. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  262. model.transformDeltas( deltasA, deltasB );
  263. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  264. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, null ) ).to.be.true;
  265. deltaTransform.transformDeltaSets.restore();
  266. } );
  267. it( 'should pass itself to transformDeltaSets if useContext was set to true', () => {
  268. sinon.spy( deltaTransform, 'transformDeltaSets' );
  269. // Prepare some empty-ish deltas so the transformation won't throw an error.
  270. const deltasA = [ new Delta() ];
  271. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  272. const deltasB = [ new Delta() ];
  273. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  274. model.transformDeltas( deltasA, deltasB, true );
  275. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  276. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, model.document ) ).to.be.true;
  277. deltaTransform.transformDeltaSets.restore();
  278. } );
  279. } );
  280. describe( 'insertContent()', () => {
  281. it( 'should be decorated', () => {
  282. schema.extend( '$text', { allowIn: '$root' } ); // To surpress warnings.
  283. const spy = sinon.spy();
  284. model.on( 'insertContent', spy );
  285. model.insertContent( new ModelText( 'a' ), model.document.selection );
  286. expect( spy.calledOnce ).to.be.true;
  287. } );
  288. it( 'should insert content (item)', () => {
  289. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  290. setData( model, '<paragraph>fo[]ar</paragraph>' );
  291. model.insertContent( new ModelText( 'ob' ), model.document.selection );
  292. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  293. } );
  294. it( 'should insert content (document fragment)', () => {
  295. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  296. setData( model, '<paragraph>fo[]ar</paragraph>' );
  297. model.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ), model.document.selection );
  298. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  299. } );
  300. it( 'should use parent batch', () => {
  301. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  302. setData( model, '<paragraph>[]</paragraph>' );
  303. model.change( writer => {
  304. model.insertContent( new ModelText( 'abc' ), model.document.selection );
  305. expect( writer.batch.deltas ).to.length( 1 );
  306. } );
  307. } );
  308. } );
  309. describe( 'deleteContent()', () => {
  310. it( 'should be decorated', () => {
  311. const spy = sinon.spy();
  312. model.on( 'deleteContent', spy );
  313. model.deleteContent( model.document.selection );
  314. expect( spy.calledOnce ).to.be.true;
  315. } );
  316. it( 'should delete selected content', () => {
  317. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  318. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  319. model.deleteContent( model.document.selection );
  320. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  321. } );
  322. it( 'should use parent batch', () => {
  323. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  324. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  325. model.change( writer => {
  326. model.deleteContent( model.document.selection );
  327. expect( writer.batch.deltas ).to.length( 1 );
  328. } );
  329. } );
  330. } );
  331. describe( 'modifySelection()', () => {
  332. it( 'should be decorated', () => {
  333. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  334. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  335. const spy = sinon.spy();
  336. model.on( 'modifySelection', spy );
  337. model.modifySelection( model.document.selection );
  338. expect( spy.calledOnce ).to.be.true;
  339. } );
  340. it( 'should modify a selection', () => {
  341. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  342. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  343. model.modifySelection( model.document.selection, { direction: 'backward' } );
  344. expect( getData( model ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  345. } );
  346. } );
  347. describe( 'getSelectedContent()', () => {
  348. it( 'should be decorated', () => {
  349. const spy = sinon.spy();
  350. const sel = new ModelSelection();
  351. model.on( 'getSelectedContent', spy );
  352. model.getSelectedContent( sel );
  353. expect( spy.calledOnce ).to.be.true;
  354. } );
  355. it( 'should return selected content', () => {
  356. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  357. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  358. const content = model.getSelectedContent( model.document.selection );
  359. expect( stringify( content ) ).to.equal( 'ob' );
  360. } );
  361. it( 'should use parent batch', () => {
  362. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  363. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  364. model.change( writer => {
  365. model.getSelectedContent( model.document.selection );
  366. expect( writer.batch.deltas ).to.length( 1 );
  367. } );
  368. } );
  369. } );
  370. describe( 'hasContent()', () => {
  371. let root;
  372. beforeEach( () => {
  373. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  374. schema.register( 'div', { inheritAllFrom: '$block' } );
  375. schema.extend( '$block', { allowIn: 'div' } );
  376. schema.register( 'image', {
  377. isObject: true
  378. } );
  379. schema.extend( 'image', { allowIn: 'div' } );
  380. setData(
  381. model,
  382. '<div>' +
  383. '<paragraph></paragraph>' +
  384. '</div>' +
  385. '<paragraph>foo</paragraph>' +
  386. '<div>' +
  387. '<image></image>' +
  388. '</div>'
  389. );
  390. root = model.document.getRoot();
  391. } );
  392. it( 'should return true if given element has text node', () => {
  393. const pFoo = root.getChild( 1 );
  394. expect( model.hasContent( pFoo ) ).to.be.true;
  395. } );
  396. it( 'should return true if given element has element that is an object', () => {
  397. const divImg = root.getChild( 2 );
  398. expect( model.hasContent( divImg ) ).to.be.true;
  399. } );
  400. it( 'should return false if given element has no elements', () => {
  401. const pEmpty = root.getChild( 0 ).getChild( 0 );
  402. expect( model.hasContent( pEmpty ) ).to.be.false;
  403. } );
  404. it( 'should return false if given element has only elements that are not objects', () => {
  405. const divP = root.getChild( 0 );
  406. expect( model.hasContent( divP ) ).to.be.false;
  407. } );
  408. it( 'should return true if there is a text node in given range', () => {
  409. const range = ModelRange.createFromParentsAndOffsets( root, 1, root, 2 );
  410. expect( model.hasContent( range ) ).to.be.true;
  411. } );
  412. it( 'should return true if there is a part of text node in given range', () => {
  413. const pFoo = root.getChild( 1 );
  414. const range = ModelRange.createFromParentsAndOffsets( pFoo, 1, pFoo, 2 );
  415. expect( model.hasContent( range ) ).to.be.true;
  416. } );
  417. it( 'should return true if there is element that is an object in given range', () => {
  418. const divImg = root.getChild( 2 );
  419. const range = ModelRange.createFromParentsAndOffsets( divImg, 0, divImg, 1 );
  420. expect( model.hasContent( range ) ).to.be.true;
  421. } );
  422. it( 'should return false if range is collapsed', () => {
  423. const range = ModelRange.createFromParentsAndOffsets( root, 1, root, 1 );
  424. expect( model.hasContent( range ) ).to.be.false;
  425. } );
  426. it( 'should return false if range has only elements that are not objects', () => {
  427. const range = ModelRange.createFromParentsAndOffsets( root, 0, root, 1 );
  428. expect( model.hasContent( range ) ).to.be.false;
  429. } );
  430. } );
  431. describe( 'destroy()', () => {
  432. it( 'should destroy document', () => {
  433. sinon.spy( model.document, 'destroy' );
  434. model.destroy();
  435. sinon.assert.calledOnce( model.document.destroy );
  436. } );
  437. it( 'should stop listening', () => {
  438. const emitter = Object.create( EmitterMixin );
  439. const spy = sinon.spy();
  440. model.listenTo( emitter, 'event', spy );
  441. model.destroy();
  442. emitter.fire( 'event' );
  443. sinon.assert.notCalled( spy );
  444. } );
  445. } );
  446. } );