model.js 15 KB

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