model.js 17 KB

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