model.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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( '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. model.enqueueChange( () => {
  148. changes += 'C';
  149. } );
  150. changes += 'B';
  151. } );
  152. model.on( 'changesDone', () => {
  153. changes += 'D';
  154. } );
  155. model.enqueueChange( () => {
  156. changes += 'A';
  157. } );
  158. expect( changes ).to.equal( 'ABCD' );
  159. } );
  160. it( 'should be possible to nest enqueueChange in changes event', () => {
  161. model.once( 'change', () => {
  162. model.enqueueChange( () => {
  163. changes += 'C';
  164. } );
  165. changes += 'B';
  166. } );
  167. model.on( 'changesDone', () => {
  168. changes += 'D';
  169. } );
  170. model.change( () => {
  171. changes += 'A';
  172. } );
  173. expect( changes ).to.equal( 'ABCD' );
  174. } );
  175. it( 'should be possible to nest changes in enqueueChange event', () => {
  176. model.once( 'change', () => {
  177. model.change( () => {
  178. changes += 'B';
  179. } );
  180. changes += 'C';
  181. } );
  182. model.on( 'changesDone', () => {
  183. changes += 'D';
  184. } );
  185. model.enqueueChange( () => {
  186. changes += 'A';
  187. } );
  188. expect( changes ).to.equal( 'ABCD' );
  189. } );
  190. it( 'should be possible to nest changes in changes event', () => {
  191. model.once( 'change', () => {
  192. model.change( () => {
  193. changes += 'B';
  194. } );
  195. changes += 'C';
  196. } );
  197. model.on( 'changesDone', () => {
  198. changes += 'D';
  199. } );
  200. model.change( () => {
  201. changes += 'A';
  202. } );
  203. expect( changes ).to.equal( 'ABCD' );
  204. } );
  205. it( 'should let mix blocks', () => {
  206. model.once( 'change', () => {
  207. model.change( () => {
  208. changes += 'B';
  209. nestedEnqueue();
  210. } );
  211. model.change( () => {
  212. changes += 'C';
  213. } );
  214. changes += 'D';
  215. } );
  216. model.on( 'changesDone', () => {
  217. changes += 'F';
  218. } );
  219. model.change( () => {
  220. changes += 'A';
  221. } );
  222. expect( changes ).to.equal( 'ABCDEF' );
  223. function nestedEnqueue() {
  224. model.enqueueChange( () => {
  225. changes += 'E';
  226. } );
  227. }
  228. } );
  229. it( 'should use the same writer in all change blocks (change & change)', () => {
  230. model.change( outerWriter => {
  231. model.change( innerWriter => {
  232. expect( innerWriter ).to.equal( outerWriter );
  233. } );
  234. } );
  235. } );
  236. it( 'should create new writer in enqueue block', () => {
  237. model.change( outerWriter => {
  238. model.enqueueChange( innerWriter => {
  239. expect( innerWriter ).to.not.equal( outerWriter );
  240. expect( innerWriter.batch ).to.not.equal( outerWriter.batch );
  241. } );
  242. } );
  243. } );
  244. it( 'should let you pass batch', () => {
  245. let outerBatch;
  246. model.change( outerWriter => {
  247. outerBatch = outerWriter.batch;
  248. model.enqueueChange( outerBatch, innerWriter => {
  249. expect( innerWriter.batch ).to.equal( outerBatch );
  250. } );
  251. } );
  252. } );
  253. it( 'should let you create transparent batch', () => {
  254. model.enqueueChange( 'transparent', writer => {
  255. expect( writer.batch.type ).to.equal( 'transparent' );
  256. } );
  257. } );
  258. } );
  259. describe( 'applyOperation()', () => {
  260. it( 'should execute provided operation end return the result of operation', () => {
  261. const returnValue = { foo: 'bar' };
  262. const operation = {
  263. _execute: sinon.stub().returns( returnValue ),
  264. _validate: () => true
  265. };
  266. model.applyOperation( operation );
  267. sinon.assert.calledOnce( operation._execute );
  268. expect( model.applyOperation( operation ) ).to.equal( returnValue );
  269. } );
  270. } );
  271. describe( 'transformDeltas()', () => {
  272. it( 'should use deltaTransform.transformDeltaSets', () => {
  273. sinon.spy( deltaTransform, 'transformDeltaSets' );
  274. // Prepare some empty-ish deltas so the transformation won't throw an error.
  275. const deltasA = [ new Delta() ];
  276. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  277. const deltasB = [ new Delta() ];
  278. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  279. model.transformDeltas( deltasA, deltasB );
  280. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  281. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, null ) ).to.be.true;
  282. deltaTransform.transformDeltaSets.restore();
  283. } );
  284. it( 'should pass itself to transformDeltaSets if useContext was set to true', () => {
  285. sinon.spy( deltaTransform, 'transformDeltaSets' );
  286. // Prepare some empty-ish deltas so the transformation won't throw an error.
  287. const deltasA = [ new Delta() ];
  288. deltasA[ 0 ].addOperation( new NoOperation( 0 ) );
  289. const deltasB = [ new Delta() ];
  290. deltasB[ 0 ].addOperation( new NoOperation( 0 ) );
  291. model.transformDeltas( deltasA, deltasB, true );
  292. expect( deltaTransform.transformDeltaSets.calledOnce ).to.be.true;
  293. expect( deltaTransform.transformDeltaSets.calledWith( deltasA, deltasB, model.document ) ).to.be.true;
  294. deltaTransform.transformDeltaSets.restore();
  295. } );
  296. } );
  297. describe( 'insertContent()', () => {
  298. it( 'should be decorated', () => {
  299. schema.extend( '$text', { allowIn: '$root' } ); // To surpress warnings.
  300. const spy = sinon.spy();
  301. model.on( 'insertContent', spy );
  302. model.insertContent( new ModelText( 'a' ), model.document.selection );
  303. expect( spy.calledOnce ).to.be.true;
  304. } );
  305. it( 'should insert content (item)', () => {
  306. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  307. setData( model, '<paragraph>fo[]ar</paragraph>' );
  308. model.insertContent( new ModelText( 'ob' ), model.document.selection );
  309. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  310. } );
  311. it( 'should insert content (document fragment)', () => {
  312. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  313. setData( model, '<paragraph>fo[]ar</paragraph>' );
  314. model.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ), model.document.selection );
  315. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  316. } );
  317. it( 'should use parent batch', () => {
  318. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  319. setData( model, '<paragraph>[]</paragraph>' );
  320. model.change( writer => {
  321. model.insertContent( new ModelText( 'abc' ), model.document.selection );
  322. expect( writer.batch.deltas ).to.length( 1 );
  323. } );
  324. } );
  325. } );
  326. describe( 'deleteContent()', () => {
  327. it( 'should be decorated', () => {
  328. const spy = sinon.spy();
  329. model.on( 'deleteContent', spy );
  330. model.deleteContent( model.document.selection );
  331. expect( spy.calledOnce ).to.be.true;
  332. } );
  333. it( 'should delete selected content', () => {
  334. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  335. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  336. model.deleteContent( model.document.selection );
  337. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  338. } );
  339. it( 'should use parent batch', () => {
  340. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  341. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  342. model.change( writer => {
  343. model.deleteContent( model.document.selection );
  344. expect( writer.batch.deltas ).to.length( 1 );
  345. } );
  346. } );
  347. } );
  348. describe( 'modifySelection()', () => {
  349. it( 'should be decorated', () => {
  350. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  351. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  352. const spy = sinon.spy();
  353. model.on( 'modifySelection', spy );
  354. model.modifySelection( model.document.selection );
  355. expect( spy.calledOnce ).to.be.true;
  356. } );
  357. it( 'should modify a selection', () => {
  358. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  359. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  360. model.modifySelection( model.document.selection, { direction: 'backward' } );
  361. expect( getData( model ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  362. } );
  363. } );
  364. describe( 'getSelectedContent()', () => {
  365. it( 'should be decorated', () => {
  366. const spy = sinon.spy();
  367. const sel = new ModelSelection();
  368. model.on( 'getSelectedContent', spy );
  369. model.getSelectedContent( sel );
  370. expect( spy.calledOnce ).to.be.true;
  371. } );
  372. it( 'should return selected content', () => {
  373. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  374. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  375. const content = model.getSelectedContent( model.document.selection );
  376. expect( stringify( content ) ).to.equal( 'ob' );
  377. } );
  378. it( 'should use parent batch', () => {
  379. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  380. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  381. model.change( writer => {
  382. model.getSelectedContent( model.document.selection );
  383. expect( writer.batch.deltas ).to.length( 1 );
  384. } );
  385. } );
  386. } );
  387. describe( 'hasContent()', () => {
  388. let root;
  389. beforeEach( () => {
  390. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  391. schema.register( 'div', { inheritAllFrom: '$block' } );
  392. schema.extend( '$block', { allowIn: 'div' } );
  393. schema.register( 'image', {
  394. isObject: true
  395. } );
  396. schema.extend( 'image', { allowIn: 'div' } );
  397. setData(
  398. model,
  399. '<div>' +
  400. '<paragraph></paragraph>' +
  401. '</div>' +
  402. '<paragraph>foo</paragraph>' +
  403. '<div>' +
  404. '<image></image>' +
  405. '</div>'
  406. );
  407. root = model.document.getRoot();
  408. } );
  409. it( 'should return true if given element has text node', () => {
  410. const pFoo = root.getChild( 1 );
  411. expect( model.hasContent( pFoo ) ).to.be.true;
  412. } );
  413. it( 'should return true if given element has element that is an object', () => {
  414. const divImg = root.getChild( 2 );
  415. expect( model.hasContent( divImg ) ).to.be.true;
  416. } );
  417. it( 'should return false if given element has no elements', () => {
  418. const pEmpty = root.getChild( 0 ).getChild( 0 );
  419. expect( model.hasContent( pEmpty ) ).to.be.false;
  420. } );
  421. it( 'should return false if given element has only elements that are not objects', () => {
  422. const divP = root.getChild( 0 );
  423. expect( model.hasContent( divP ) ).to.be.false;
  424. } );
  425. it( 'should return true if there is a text node in given range', () => {
  426. const range = ModelRange.createFromParentsAndOffsets( root, 1, root, 2 );
  427. expect( model.hasContent( range ) ).to.be.true;
  428. } );
  429. it( 'should return true if there is a part of text node in given range', () => {
  430. const pFoo = root.getChild( 1 );
  431. const range = ModelRange.createFromParentsAndOffsets( pFoo, 1, pFoo, 2 );
  432. expect( model.hasContent( range ) ).to.be.true;
  433. } );
  434. it( 'should return true if there is element that is an object in given range', () => {
  435. const divImg = root.getChild( 2 );
  436. const range = ModelRange.createFromParentsAndOffsets( divImg, 0, divImg, 1 );
  437. expect( model.hasContent( range ) ).to.be.true;
  438. } );
  439. it( 'should return false if range is collapsed', () => {
  440. const range = ModelRange.createFromParentsAndOffsets( root, 1, root, 1 );
  441. expect( model.hasContent( range ) ).to.be.false;
  442. } );
  443. it( 'should return false if range has only elements that are not objects', () => {
  444. const range = ModelRange.createFromParentsAndOffsets( root, 0, root, 1 );
  445. expect( model.hasContent( range ) ).to.be.false;
  446. } );
  447. } );
  448. describe( 'destroy()', () => {
  449. it( 'should destroy document', () => {
  450. sinon.spy( model.document, 'destroy' );
  451. model.destroy();
  452. sinon.assert.calledOnce( model.document.destroy );
  453. } );
  454. it( 'should stop listening', () => {
  455. const emitter = Object.create( EmitterMixin );
  456. const spy = sinon.spy();
  457. model.listenTo( emitter, 'event', spy );
  458. model.destroy();
  459. emitter.fire( 'event' );
  460. sinon.assert.notCalled( spy );
  461. } );
  462. } );
  463. } );