model.js 14 KB

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