8
0

model.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. /**
  2. * @license Copyright (c) 2003-2019, 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. schema.register( 'listItem', {
  366. inheritAllFrom: '$block'
  367. } );
  368. setData(
  369. model,
  370. '<div>' +
  371. '<paragraph></paragraph>' +
  372. '</div>' +
  373. '<paragraph>foo</paragraph>' +
  374. '<div>' +
  375. '<image></image>' +
  376. '</div>' +
  377. '<listItem></listItem>' +
  378. '<listItem></listItem>' +
  379. '<listItem></listItem>'
  380. );
  381. root = model.document.getRoot();
  382. } );
  383. it( 'should return true if given element has text node', () => {
  384. const pFoo = root.getChild( 1 );
  385. expect( model.hasContent( pFoo ) ).to.be.true;
  386. } );
  387. it( 'should return true if given element has text node (trimWhitespaces)', () => {
  388. const pFoo = root.getChild( 1 );
  389. expect( model.hasContent( pFoo, { trimWhitespaces: true } ) ).to.be.true;
  390. } );
  391. it( 'should return true if given element has text node containing spaces only', () => {
  392. const pEmpty = root.getChild( 0 ).getChild( 0 );
  393. model.enqueueChange( 'transparent', writer => {
  394. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  395. writer.insertText( ' ', pEmpty, 'end' );
  396. } );
  397. expect( model.hasContent( pEmpty ) ).to.be.true;
  398. } );
  399. it( 'should false true if given element has text node containing spaces only (trimWhitespaces)', () => {
  400. const pEmpty = root.getChild( 0 ).getChild( 0 );
  401. model.enqueueChange( 'transparent', writer => {
  402. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  403. writer.insertText( ' ', pEmpty, 'end' );
  404. } );
  405. expect( model.hasContent( pEmpty, { trimWhitespaces: true } ) ).to.be.false;
  406. } );
  407. it( 'should return true if given element has element that is an object', () => {
  408. const divImg = root.getChild( 2 );
  409. expect( model.hasContent( divImg ) ).to.be.true;
  410. } );
  411. it( 'should return false if given element has no elements', () => {
  412. const pEmpty = root.getChild( 0 ).getChild( 0 );
  413. expect( model.hasContent( pEmpty ) ).to.be.false;
  414. } );
  415. it( 'should return false if given element has only elements that are not objects', () => {
  416. const divP = root.getChild( 0 );
  417. expect( model.hasContent( divP ) ).to.be.false;
  418. } );
  419. it( 'should return true if there is a text node in given range', () => {
  420. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 2 ) );
  421. expect( model.hasContent( range ) ).to.be.true;
  422. } );
  423. it( 'should return true if there is a part of text node in given range', () => {
  424. const pFoo = root.getChild( 1 );
  425. const range = new ModelRange( ModelPosition._createAt( pFoo, 1 ), ModelPosition._createAt( pFoo, 2 ) );
  426. expect( model.hasContent( range ) ).to.be.true;
  427. } );
  428. it( 'should return true if there is element that is an object in given range', () => {
  429. const divImg = root.getChild( 2 );
  430. const range = new ModelRange( ModelPosition._createAt( divImg, 0 ), ModelPosition._createAt( divImg, 1 ) );
  431. expect( model.hasContent( range ) ).to.be.true;
  432. } );
  433. it( 'should return false if range is collapsed', () => {
  434. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 1 ) );
  435. expect( model.hasContent( range ) ).to.be.false;
  436. } );
  437. it( 'should return false if range has only elements that are not objects', () => {
  438. const range = new ModelRange( ModelPosition._createAt( root, 0 ), ModelPosition._createAt( root, 1 ) );
  439. expect( model.hasContent( range ) ).to.be.false;
  440. } );
  441. it( 'should return false for empty list items', () => {
  442. const range = new ModelRange( ModelPosition._createAt( root, 3 ), ModelPosition._createAt( root, 6 ) );
  443. expect( model.hasContent( range ) ).to.be.false;
  444. } );
  445. it( 'should return false for empty element with marker (usingOperation=false, affectsData=false)', () => {
  446. const pEmpty = root.getChild( 0 ).getChild( 0 );
  447. model.enqueueChange( 'transparent', writer => {
  448. // Insert marker.
  449. const range = ModelRange._createIn( pEmpty );
  450. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  451. } );
  452. expect( model.hasContent( pEmpty ) ).to.be.false;
  453. expect( model.hasContent( pEmpty, { trimWhitespaces: true } ) ).to.be.false;
  454. } );
  455. it( 'should return false for empty element with marker (usingOperation=true, affectsData=false)', () => {
  456. const pEmpty = root.getChild( 0 ).getChild( 0 );
  457. model.enqueueChange( 'transparent', writer => {
  458. // Insert marker.
  459. const range = ModelRange._createIn( pEmpty );
  460. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: false } );
  461. } );
  462. expect( model.hasContent( pEmpty ) ).to.be.false;
  463. expect( model.hasContent( pEmpty, { trimWhitespaces: true } ) ).to.be.false;
  464. } );
  465. it( 'should return false (trimWhitespaces) for empty text with marker (usingOperation=false, affectsData=false)', () => {
  466. const pEmpty = root.getChild( 0 ).getChild( 0 );
  467. model.enqueueChange( 'transparent', writer => {
  468. // Insert empty text.
  469. const text = writer.createText( ' ', { bold: true } );
  470. writer.append( text, pEmpty );
  471. // Insert marker.
  472. const range = ModelRange._createIn( pEmpty );
  473. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  474. } );
  475. expect( model.hasContent( pEmpty, { trimWhitespaces: true } ) ).to.be.false;
  476. } );
  477. it( 'should return true for empty text with marker (usingOperation=false, affectsData=false)', () => {
  478. const pEmpty = root.getChild( 0 ).getChild( 0 );
  479. model.enqueueChange( 'transparent', writer => {
  480. // Insert empty text.
  481. const text = writer.createText( ' ', { bold: true } );
  482. writer.append( text, pEmpty );
  483. // Insert marker.
  484. const range = ModelRange._createIn( pEmpty );
  485. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  486. } );
  487. expect( model.hasContent( pEmpty ) ).to.be.true;
  488. } );
  489. it( 'should return false for empty element with marker (usingOperation=false, affectsData=true)', () => {
  490. const pEmpty = root.getChild( 0 ).getChild( 0 );
  491. model.enqueueChange( 'transparent', writer => {
  492. // Insert marker.
  493. const range = ModelRange._createIn( pEmpty );
  494. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  495. } );
  496. expect( model.hasContent( pEmpty ) ).to.be.false;
  497. expect( model.hasContent( pEmpty, { trimWhitespaces: true } ) ).to.be.false;
  498. } );
  499. it( 'should return false for empty element with marker (usingOperation=true, affectsData=true)', () => {
  500. const pEmpty = root.getChild( 0 ).getChild( 0 );
  501. model.enqueueChange( 'transparent', writer => {
  502. // Insert marker.
  503. const range = ModelRange._createIn( pEmpty );
  504. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  505. } );
  506. expect( model.hasContent( pEmpty ) ).to.be.false;
  507. expect( model.hasContent( pEmpty, { trimWhitespaces: true } ) ).to.be.false;
  508. } );
  509. it( 'should return true (trimWhitespaces) for empty text with marker (usingOperation=false, affectsData=true)', () => {
  510. const pEmpty = root.getChild( 0 ).getChild( 0 );
  511. model.enqueueChange( 'transparent', writer => {
  512. // Insert empty text.
  513. const text = writer.createText( ' ', { bold: true } );
  514. writer.append( text, pEmpty );
  515. // Insert marker.
  516. const range = ModelRange._createIn( pEmpty );
  517. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  518. } );
  519. expect( model.hasContent( pEmpty ) ).to.be.true;
  520. expect( model.hasContent( pEmpty, { trimWhitespaces: true } ) ).to.be.true;
  521. } );
  522. } );
  523. describe( 'createPositionFromPath()', () => {
  524. it( 'should return instance of Position', () => {
  525. expect( model.createPositionFromPath( model.document.getRoot(), [ 0 ] ) ).to.be.instanceof( ModelPosition );
  526. } );
  527. } );
  528. describe( 'createPositionAt()', () => {
  529. it( 'should return instance of Position', () => {
  530. expect( model.createPositionAt( model.document.getRoot(), 0 ) ).to.be.instanceof( ModelPosition );
  531. } );
  532. } );
  533. describe( 'createPositionAfter()', () => {
  534. it( 'should return instance of Position', () => {
  535. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  536. setData( model, '<paragraph>fo[]ar</paragraph>' );
  537. expect( model.createPositionAfter( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  538. } );
  539. } );
  540. describe( 'createPositionBefore()', () => {
  541. it( 'should return instance of Position', () => {
  542. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  543. setData( model, '<paragraph>fo[]ar</paragraph>' );
  544. expect( model.createPositionBefore( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  545. } );
  546. } );
  547. describe( 'createRange()', () => {
  548. it( 'should return instance of Range', () => {
  549. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  550. setData( model, '<paragraph>fo[]ar</paragraph>' );
  551. expect( model.createRange( model.createPositionAt( model.document.getRoot(), 0 ) ) ).to.be.instanceof( ModelRange );
  552. } );
  553. } );
  554. describe( 'createRangeIn()', () => {
  555. it( 'should return instance of Range', () => {
  556. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  557. setData( model, '<paragraph>fo[]ar</paragraph>' );
  558. expect( model.createRangeIn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  559. } );
  560. } );
  561. describe( 'createRangeOn()', () => {
  562. it( 'should return instance of Range', () => {
  563. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  564. setData( model, '<paragraph>fo[]ar</paragraph>' );
  565. expect( model.createRangeOn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  566. } );
  567. } );
  568. describe( 'createSelection()', () => {
  569. it( 'should return instance of Selection', () => {
  570. expect( model.createSelection() ).to.be.instanceof( ModelSelection );
  571. } );
  572. } );
  573. describe( 'createBatch()', () => {
  574. it( 'should return instance of Batch', () => {
  575. expect( model.createBatch() ).to.be.instanceof( Batch );
  576. } );
  577. } );
  578. describe( 'destroy()', () => {
  579. it( 'should destroy document', () => {
  580. sinon.spy( model.document, 'destroy' );
  581. model.destroy();
  582. sinon.assert.calledOnce( model.document.destroy );
  583. } );
  584. it( 'should stop listening', () => {
  585. const emitter = Object.create( EmitterMixin );
  586. const spy = sinon.spy();
  587. model.listenTo( emitter, 'event', spy );
  588. model.destroy();
  589. emitter.fire( 'event' );
  590. sinon.assert.notCalled( spy );
  591. } );
  592. } );
  593. } );