model.js 14 KB

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