model.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. model.document.createRoot( '$anywhere', 'anywhere' );
  45. schema.register( 'anything' );
  46. expect( schema.isRegistered( '$marker' ) ).to.be.true;
  47. expect( schema.checkChild( [ '$root' ], '$marker' ) ).to.be.true;
  48. expect( schema.checkChild( [ '$block' ], '$marker' ) ).to.be.true;
  49. expect( schema.checkChild( [ '$anywhere' ], '$marker' ) ).to.be.true;
  50. expect( schema.checkChild( [ 'anything' ], '$marker' ) ).to.be.true;
  51. } );
  52. } );
  53. describe( 'change() & enqueueChange()', () => {
  54. it( 'should execute changes immediately', () => {
  55. model.change( () => {
  56. changes += 'A';
  57. } );
  58. expect( changes ).to.equal( 'A' );
  59. } );
  60. it( 'should pass returned value', () => {
  61. const ret = model.change( () => {
  62. changes += 'A';
  63. return 'B';
  64. } );
  65. changes += ret;
  66. expect( changes ).to.equal( 'AB' );
  67. } );
  68. it( 'should not mixed the order when nested change is called', () => {
  69. const ret = model.change( () => {
  70. changes += 'A';
  71. nested();
  72. return 'D';
  73. } );
  74. changes += ret;
  75. expect( changes ).to.equal( 'ABCD' );
  76. function nested() {
  77. const ret = model.change( () => {
  78. changes += 'B';
  79. return 'C';
  80. } );
  81. changes += ret;
  82. }
  83. } );
  84. it( 'should execute enqueueChange immediately if its the first block', () => {
  85. model.enqueueChange( () => {
  86. changes += 'A';
  87. nested();
  88. } );
  89. expect( changes ).to.equal( 'ABC' );
  90. function nested() {
  91. const ret = model.change( () => {
  92. changes += 'B';
  93. return 'C';
  94. } );
  95. changes += ret;
  96. }
  97. } );
  98. it( 'should be possible to enqueueChange immediately if its the first block', () => {
  99. model.enqueueChange( () => {
  100. changes += 'A';
  101. nested();
  102. } );
  103. expect( changes ).to.equal( 'AB' );
  104. function nested() {
  105. model.change( () => {
  106. changes += 'B';
  107. } );
  108. }
  109. } );
  110. it( 'should be possible to nest change in enqueueChange', () => {
  111. model.enqueueChange( () => {
  112. changes += 'A';
  113. nested();
  114. changes += 'D';
  115. } );
  116. expect( changes ).to.equal( 'ABCD' );
  117. function nested() {
  118. const ret = model.change( () => {
  119. changes += 'B';
  120. return 'C';
  121. } );
  122. changes += ret;
  123. }
  124. } );
  125. it( 'should be possible to nest enqueueChange in enqueueChange', () => {
  126. model.enqueueChange( () => {
  127. changes += 'A';
  128. nestedEnqueue();
  129. changes += 'B';
  130. } );
  131. expect( changes ).to.equal( 'ABC' );
  132. function nestedEnqueue() {
  133. model.enqueueChange( () => {
  134. changes += 'C';
  135. } );
  136. }
  137. } );
  138. it( 'should be possible to nest enqueueChange in changes', () => {
  139. const ret = model.change( () => {
  140. changes += 'A';
  141. nestedEnqueue();
  142. changes += 'B';
  143. return 'D';
  144. } );
  145. changes += ret;
  146. expect( changes ).to.equal( 'ABCD' );
  147. function nestedEnqueue() {
  148. model.enqueueChange( () => {
  149. changes += 'C';
  150. } );
  151. }
  152. } );
  153. it( 'should be possible to nest enqueueChange in enqueueChange event', () => {
  154. model.enqueueChange( () => {
  155. changes += 'A';
  156. model.enqueueChange( () => {
  157. changes += 'C';
  158. } );
  159. changes += 'B';
  160. } );
  161. expect( changes ).to.equal( 'ABC' );
  162. } );
  163. it( 'should be possible to nest enqueueChange in changes event', () => {
  164. model.change( () => {
  165. changes += 'A';
  166. model.enqueueChange( () => {
  167. changes += 'C';
  168. } );
  169. changes += 'B';
  170. } );
  171. expect( changes ).to.equal( 'ABC' );
  172. } );
  173. it( 'should be possible to nest changes in enqueueChange event', () => {
  174. model.enqueueChange( () => {
  175. changes += 'A';
  176. model.change( () => {
  177. changes += 'B';
  178. } );
  179. changes += 'C';
  180. } );
  181. expect( changes ).to.equal( 'ABC' );
  182. } );
  183. it( 'should be possible to nest changes in changes event', () => {
  184. model.change( () => {
  185. changes += 'A';
  186. model.change( () => {
  187. changes += 'B';
  188. } );
  189. changes += 'C';
  190. } );
  191. expect( changes ).to.equal( 'ABC' );
  192. } );
  193. it( 'should let mix blocks', () => {
  194. model.change( () => {
  195. changes += 'A';
  196. model.enqueueChange( () => {
  197. changes += 'C';
  198. model.change( () => {
  199. changes += 'D';
  200. model.enqueueChange( () => {
  201. changes += 'F';
  202. } );
  203. } );
  204. model.change( () => {
  205. changes += 'E';
  206. } );
  207. } );
  208. changes += 'B';
  209. } );
  210. expect( changes ).to.equal( 'ABCDEF' );
  211. } );
  212. it( 'should use the same writer in all change blocks (change & change)', () => {
  213. model.change( outerWriter => {
  214. model.change( innerWriter => {
  215. expect( innerWriter ).to.equal( outerWriter );
  216. } );
  217. } );
  218. } );
  219. it( 'should create new writer in enqueue block', () => {
  220. model.change( outerWriter => {
  221. model.enqueueChange( innerWriter => {
  222. expect( innerWriter ).to.not.equal( outerWriter );
  223. expect( innerWriter.batch ).to.not.equal( outerWriter.batch );
  224. } );
  225. } );
  226. } );
  227. it( 'should let you pass batch', () => {
  228. let outerBatch;
  229. model.change( outerWriter => {
  230. outerBatch = outerWriter.batch;
  231. model.enqueueChange( outerBatch, innerWriter => {
  232. expect( innerWriter.batch ).to.equal( outerBatch );
  233. } );
  234. } );
  235. } );
  236. it( 'should let you create transparent batch', () => {
  237. model.enqueueChange( 'transparent', writer => {
  238. expect( writer.batch.type ).to.equal( 'transparent' );
  239. } );
  240. } );
  241. } );
  242. describe( 'applyOperation()', () => {
  243. it( 'should execute provided operation', () => {
  244. const operation = {
  245. _execute: sinon.spy(),
  246. _validate: () => true
  247. };
  248. model.applyOperation( operation );
  249. sinon.assert.calledOnce( operation._execute );
  250. } );
  251. } );
  252. describe( 'insertContent()', () => {
  253. it( 'should be decorated', () => {
  254. schema.extend( '$text', { allowIn: '$root' } ); // To surpress warnings.
  255. const spy = sinon.spy();
  256. model.on( 'insertContent', spy );
  257. model.insertContent( new ModelText( 'a' ) );
  258. expect( spy.calledOnce ).to.be.true;
  259. } );
  260. it( 'should insert content (item)', () => {
  261. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  262. setData( model, '<paragraph>fo[]ar</paragraph>' );
  263. model.insertContent( new ModelText( 'ob' ) );
  264. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  265. } );
  266. it( 'should insert content (document fragment)', () => {
  267. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  268. setData( model, '<paragraph>fo[]ar</paragraph>' );
  269. model.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ) );
  270. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  271. } );
  272. it( 'should use current model selection if no selectable passed', () => {
  273. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  274. setData( model, '<paragraph>fo[]ar</paragraph>' );
  275. model.insertContent( new ModelText( 'ob' ) );
  276. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  277. } );
  278. it( 'should use parent batch', () => {
  279. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  280. setData( model, '<paragraph>[]</paragraph>' );
  281. model.change( writer => {
  282. model.insertContent( new ModelText( 'abc' ) );
  283. expect( writer.batch.operations ).to.length( 1 );
  284. } );
  285. } );
  286. } );
  287. describe( 'deleteContent()', () => {
  288. it( 'should be decorated', () => {
  289. const spy = sinon.spy();
  290. model.on( 'deleteContent', spy );
  291. model.deleteContent( model.document.selection );
  292. expect( spy.calledOnce ).to.be.true;
  293. } );
  294. it( 'should delete selected content', () => {
  295. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  296. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  297. model.deleteContent( model.document.selection );
  298. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  299. } );
  300. it( 'should use parent batch', () => {
  301. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  302. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  303. model.change( writer => {
  304. model.deleteContent( model.document.selection );
  305. expect( writer.batch.operations ).to.length( 1 );
  306. } );
  307. } );
  308. } );
  309. describe( 'modifySelection()', () => {
  310. it( 'should be decorated', () => {
  311. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  312. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  313. const spy = sinon.spy();
  314. model.on( 'modifySelection', spy );
  315. model.modifySelection( model.document.selection );
  316. expect( spy.calledOnce ).to.be.true;
  317. } );
  318. it( 'should modify a selection', () => {
  319. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  320. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  321. expect( getData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  322. model.modifySelection( model.document.selection, { direction: 'backward' } );
  323. expect( getData( model ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  324. } );
  325. } );
  326. describe( 'getSelectedContent()', () => {
  327. it( 'should be decorated', () => {
  328. const spy = sinon.spy();
  329. const sel = new ModelSelection();
  330. model.on( 'getSelectedContent', spy );
  331. model.getSelectedContent( sel );
  332. expect( spy.calledOnce ).to.be.true;
  333. } );
  334. it( 'should return selected content', () => {
  335. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  336. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  337. const content = model.getSelectedContent( model.document.selection );
  338. expect( stringify( content ) ).to.equal( 'ob' );
  339. } );
  340. it( 'should use parent batch', () => {
  341. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  342. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  343. model.change( writer => {
  344. model.getSelectedContent( model.document.selection );
  345. expect( writer.batch.operations ).to.length( 1 );
  346. } );
  347. } );
  348. } );
  349. describe( 'hasContent()', () => {
  350. let root;
  351. beforeEach( () => {
  352. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  353. schema.register( 'div', { inheritAllFrom: '$block' } );
  354. schema.extend( '$block', { allowIn: 'div' } );
  355. schema.register( 'image', {
  356. isObject: true
  357. } );
  358. schema.extend( 'image', { allowIn: 'div' } );
  359. schema.register( 'listItem', {
  360. inheritAllFrom: '$block'
  361. } );
  362. setData(
  363. model,
  364. '<div>' +
  365. '<paragraph></paragraph>' +
  366. '</div>' +
  367. '<paragraph>foo</paragraph>' +
  368. '<div>' +
  369. '<image></image>' +
  370. '</div>' +
  371. '<listItem></listItem>' +
  372. '<listItem></listItem>' +
  373. '<listItem></listItem>'
  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 text node (ignoreWhitespaces)', () => {
  382. const pFoo = root.getChild( 1 );
  383. expect( model.hasContent( pFoo, { ignoreWhitespaces: true } ) ).to.be.true;
  384. } );
  385. it( 'should return true if given element has text node containing spaces only', () => {
  386. const pEmpty = root.getChild( 0 ).getChild( 0 );
  387. model.enqueueChange( 'transparent', writer => {
  388. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  389. writer.insertText( ' ', pEmpty, 'end' );
  390. } );
  391. expect( model.hasContent( pEmpty ) ).to.be.true;
  392. } );
  393. it( 'should false true if given element has text node containing spaces only (ignoreWhitespaces)', () => {
  394. const pEmpty = root.getChild( 0 ).getChild( 0 );
  395. model.enqueueChange( 'transparent', writer => {
  396. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  397. writer.insertText( ' ', pEmpty, 'end' );
  398. } );
  399. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  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 = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( 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 = new ModelRange( ModelPosition._createAt( pFoo, 1 ), ModelPosition._createAt( 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 = new ModelRange( ModelPosition._createAt( divImg, 0 ), ModelPosition._createAt( divImg, 1 ) );
  425. expect( model.hasContent( range ) ).to.be.true;
  426. } );
  427. it( 'should return false if range is collapsed', () => {
  428. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( 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 = new ModelRange( ModelPosition._createAt( root, 0 ), ModelPosition._createAt( root, 1 ) );
  433. expect( model.hasContent( range ) ).to.be.false;
  434. } );
  435. it( 'should return false for empty list items', () => {
  436. const range = new ModelRange( ModelPosition._createAt( root, 3 ), ModelPosition._createAt( root, 6 ) );
  437. expect( model.hasContent( range ) ).to.be.false;
  438. } );
  439. it( 'should return false for empty element with marker (usingOperation=false, affectsData=false)', () => {
  440. const pEmpty = root.getChild( 0 ).getChild( 0 );
  441. model.enqueueChange( 'transparent', writer => {
  442. // Insert marker.
  443. const range = ModelRange._createIn( pEmpty );
  444. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  445. } );
  446. expect( model.hasContent( pEmpty ) ).to.be.false;
  447. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  448. } );
  449. it( 'should return false for empty element with marker (usingOperation=true, affectsData=false)', () => {
  450. const pEmpty = root.getChild( 0 ).getChild( 0 );
  451. model.enqueueChange( 'transparent', writer => {
  452. // Insert marker.
  453. const range = ModelRange._createIn( pEmpty );
  454. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: false } );
  455. } );
  456. expect( model.hasContent( pEmpty ) ).to.be.false;
  457. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  458. } );
  459. it( 'should return false (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=false)', () => {
  460. const pEmpty = root.getChild( 0 ).getChild( 0 );
  461. model.enqueueChange( 'transparent', writer => {
  462. // Insert empty text.
  463. const text = writer.createText( ' ', { bold: true } );
  464. writer.append( text, pEmpty );
  465. // Insert marker.
  466. const range = ModelRange._createIn( pEmpty );
  467. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  468. } );
  469. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  470. } );
  471. it( 'should return true for empty text with marker (usingOperation=false, affectsData=false)', () => {
  472. const pEmpty = root.getChild( 0 ).getChild( 0 );
  473. model.enqueueChange( 'transparent', writer => {
  474. // Insert empty text.
  475. const text = writer.createText( ' ', { bold: true } );
  476. writer.append( text, pEmpty );
  477. // Insert marker.
  478. const range = ModelRange._createIn( pEmpty );
  479. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  480. } );
  481. expect( model.hasContent( pEmpty ) ).to.be.true;
  482. } );
  483. it( 'should return false for empty element with marker (usingOperation=false, affectsData=true)', () => {
  484. const pEmpty = root.getChild( 0 ).getChild( 0 );
  485. model.enqueueChange( 'transparent', writer => {
  486. // Insert marker.
  487. const range = ModelRange._createIn( pEmpty );
  488. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  489. } );
  490. expect( model.hasContent( pEmpty ) ).to.be.false;
  491. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  492. } );
  493. it( 'should return false for empty element with marker (usingOperation=true, affectsData=true)', () => {
  494. const pEmpty = root.getChild( 0 ).getChild( 0 );
  495. model.enqueueChange( 'transparent', writer => {
  496. // Insert marker.
  497. const range = ModelRange._createIn( pEmpty );
  498. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  499. } );
  500. expect( model.hasContent( pEmpty ) ).to.be.false;
  501. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  502. } );
  503. it( 'should return true (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=true)', () => {
  504. const pEmpty = root.getChild( 0 ).getChild( 0 );
  505. model.enqueueChange( 'transparent', writer => {
  506. // Insert empty text.
  507. const text = writer.createText( ' ', { bold: true } );
  508. writer.append( text, pEmpty );
  509. // Insert marker.
  510. const range = ModelRange._createIn( pEmpty );
  511. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  512. } );
  513. expect( model.hasContent( pEmpty ) ).to.be.true;
  514. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.true;
  515. } );
  516. } );
  517. describe( 'createPositionFromPath()', () => {
  518. it( 'should return instance of Position', () => {
  519. expect( model.createPositionFromPath( model.document.getRoot(), [ 0 ] ) ).to.be.instanceof( ModelPosition );
  520. } );
  521. } );
  522. describe( 'createPositionAt()', () => {
  523. it( 'should return instance of Position', () => {
  524. expect( model.createPositionAt( model.document.getRoot(), 0 ) ).to.be.instanceof( ModelPosition );
  525. } );
  526. } );
  527. describe( 'createPositionAfter()', () => {
  528. it( 'should return instance of Position', () => {
  529. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  530. setData( model, '<paragraph>fo[]ar</paragraph>' );
  531. expect( model.createPositionAfter( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  532. } );
  533. } );
  534. describe( 'createPositionBefore()', () => {
  535. it( 'should return instance of Position', () => {
  536. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  537. setData( model, '<paragraph>fo[]ar</paragraph>' );
  538. expect( model.createPositionBefore( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  539. } );
  540. } );
  541. describe( 'createRange()', () => {
  542. it( 'should return instance of Range', () => {
  543. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  544. setData( model, '<paragraph>fo[]ar</paragraph>' );
  545. expect( model.createRange( model.createPositionAt( model.document.getRoot(), 0 ) ) ).to.be.instanceof( ModelRange );
  546. } );
  547. } );
  548. describe( 'createRangeIn()', () => {
  549. it( 'should return instance of Range', () => {
  550. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  551. setData( model, '<paragraph>fo[]ar</paragraph>' );
  552. expect( model.createRangeIn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  553. } );
  554. } );
  555. describe( 'createRangeOn()', () => {
  556. it( 'should return instance of Range', () => {
  557. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  558. setData( model, '<paragraph>fo[]ar</paragraph>' );
  559. expect( model.createRangeOn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  560. } );
  561. } );
  562. describe( 'createSelection()', () => {
  563. it( 'should return instance of Selection', () => {
  564. expect( model.createSelection() ).to.be.instanceof( ModelSelection );
  565. } );
  566. } );
  567. describe( 'createBatch()', () => {
  568. it( 'should return instance of Batch', () => {
  569. const batch = model.createBatch();
  570. expect( batch ).to.be.instanceof( Batch );
  571. expect( batch.type ).to.equal( 'default' );
  572. } );
  573. it( 'should allow to define type of Batch', () => {
  574. const batch = model.createBatch( 'transparent' );
  575. expect( batch ).to.be.instanceof( Batch );
  576. expect( batch.type ).to.equal( 'transparent' );
  577. } );
  578. } );
  579. describe( 'destroy()', () => {
  580. it( 'should destroy document', () => {
  581. sinon.spy( model.document, 'destroy' );
  582. model.destroy();
  583. sinon.assert.calledOnce( model.document.destroy );
  584. } );
  585. it( 'should stop listening', () => {
  586. const emitter = Object.create( EmitterMixin );
  587. const spy = sinon.spy();
  588. model.listenTo( emitter, 'event', spy );
  589. model.destroy();
  590. emitter.fire( 'event' );
  591. sinon.assert.notCalled( spy );
  592. } );
  593. } );
  594. } );