model.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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. 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.once( '_change', () => {
  155. changes += 'B';
  156. } );
  157. model.enqueueChange( () => {
  158. model.enqueueChange( () => {
  159. changes += 'C';
  160. } );
  161. changes += 'A';
  162. } );
  163. expect( changes ).to.equal( 'ABC' );
  164. } );
  165. it( 'should be possible to nest enqueueChange in changes event', () => {
  166. model.once( '_change', () => {
  167. changes += 'B';
  168. } );
  169. model.change( () => {
  170. model.enqueueChange( () => {
  171. changes += 'C';
  172. } );
  173. changes += 'A';
  174. } );
  175. expect( changes ).to.equal( 'ABC' );
  176. } );
  177. it( 'should be possible to nest changes in enqueueChange event', () => {
  178. model.once( '_change', () => {
  179. changes += 'C';
  180. } );
  181. model.enqueueChange( () => {
  182. model.change( () => {
  183. changes += 'A';
  184. } );
  185. changes += 'B';
  186. } );
  187. expect( changes ).to.equal( 'ABC' );
  188. } );
  189. it( 'should be possible to nest changes in changes event', () => {
  190. model.once( '_change', () => {
  191. changes += 'C';
  192. } );
  193. model.change( () => {
  194. model.change( () => {
  195. changes += 'A';
  196. } );
  197. changes += 'B';
  198. } );
  199. expect( changes ).to.equal( 'ABC' );
  200. } );
  201. it( 'should let mix blocks', () => {
  202. model.once( '_change', () => {
  203. model.change( () => {
  204. changes += 'B';
  205. nestedEnqueue();
  206. } );
  207. model.change( () => {
  208. changes += 'C';
  209. } );
  210. changes += 'D';
  211. } );
  212. model.change( () => {
  213. changes += 'A';
  214. } );
  215. expect( changes ).to.equal( 'ABCDE' );
  216. function nestedEnqueue() {
  217. model.enqueueChange( () => {
  218. changes += 'E';
  219. } );
  220. }
  221. } );
  222. it( 'should use the same writer in all change blocks (change & change)', () => {
  223. model.change( outerWriter => {
  224. model.change( innerWriter => {
  225. expect( innerWriter ).to.equal( outerWriter );
  226. } );
  227. } );
  228. } );
  229. it( 'should create new writer in enqueue block', () => {
  230. model.change( outerWriter => {
  231. model.enqueueChange( innerWriter => {
  232. expect( innerWriter ).to.not.equal( outerWriter );
  233. expect( innerWriter.batch ).to.not.equal( outerWriter.batch );
  234. } );
  235. } );
  236. } );
  237. it( 'should let you pass batch', () => {
  238. let outerBatch;
  239. model.change( outerWriter => {
  240. outerBatch = outerWriter.batch;
  241. model.enqueueChange( outerBatch, innerWriter => {
  242. expect( innerWriter.batch ).to.equal( outerBatch );
  243. } );
  244. } );
  245. } );
  246. it( 'should let you create transparent batch', () => {
  247. model.enqueueChange( 'transparent', writer => {
  248. expect( writer.batch.type ).to.equal( 'transparent' );
  249. } );
  250. } );
  251. } );
  252. describe( 'applyOperation()', () => {
  253. it( 'should execute provided operation', () => {
  254. const operation = {
  255. _execute: sinon.spy(),
  256. _validate: () => true
  257. };
  258. model.applyOperation( operation );
  259. sinon.assert.calledOnce( operation._execute );
  260. } );
  261. } );
  262. describe( 'insertContent()', () => {
  263. it( 'should be decorated', () => {
  264. schema.extend( '$text', { allowIn: '$root' } ); // To surpress warnings.
  265. const spy = sinon.spy();
  266. model.on( 'insertContent', spy );
  267. model.insertContent( new ModelText( 'a' ) );
  268. expect( spy.calledOnce ).to.be.true;
  269. } );
  270. it( 'should insert content (item)', () => {
  271. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  272. setData( model, '<paragraph>fo[]ar</paragraph>' );
  273. model.insertContent( new ModelText( 'ob' ) );
  274. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  275. } );
  276. it( 'should insert content (document fragment)', () => {
  277. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  278. setData( model, '<paragraph>fo[]ar</paragraph>' );
  279. model.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ) );
  280. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  281. } );
  282. it( 'should use current model selection if no selectable passed', () => {
  283. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  284. setData( model, '<paragraph>fo[]ar</paragraph>' );
  285. model.insertContent( new ModelText( 'ob' ) );
  286. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  287. } );
  288. it( 'should use parent batch', () => {
  289. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  290. setData( model, '<paragraph>[]</paragraph>' );
  291. model.change( writer => {
  292. model.insertContent( new ModelText( 'abc' ) );
  293. expect( writer.batch.operations ).to.length( 1 );
  294. } );
  295. } );
  296. } );
  297. describe( 'deleteContent()', () => {
  298. it( 'should be decorated', () => {
  299. const spy = sinon.spy();
  300. model.on( 'deleteContent', spy );
  301. model.deleteContent( model.document.selection );
  302. expect( spy.calledOnce ).to.be.true;
  303. } );
  304. it( 'should delete selected content', () => {
  305. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  306. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  307. model.deleteContent( model.document.selection );
  308. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  309. } );
  310. it( 'should use parent batch', () => {
  311. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  312. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  313. model.change( writer => {
  314. model.deleteContent( model.document.selection );
  315. expect( writer.batch.operations ).to.length( 1 );
  316. } );
  317. } );
  318. } );
  319. describe( 'modifySelection()', () => {
  320. it( 'should be decorated', () => {
  321. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  322. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  323. const spy = sinon.spy();
  324. model.on( 'modifySelection', spy );
  325. model.modifySelection( model.document.selection );
  326. expect( spy.calledOnce ).to.be.true;
  327. } );
  328. it( 'should modify a selection', () => {
  329. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  330. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  331. expect( getData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  332. model.modifySelection( model.document.selection, { direction: 'backward' } );
  333. expect( getData( model ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  334. } );
  335. } );
  336. describe( 'getSelectedContent()', () => {
  337. it( 'should be decorated', () => {
  338. const spy = sinon.spy();
  339. const sel = new ModelSelection();
  340. model.on( 'getSelectedContent', spy );
  341. model.getSelectedContent( sel );
  342. expect( spy.calledOnce ).to.be.true;
  343. } );
  344. it( 'should return selected content', () => {
  345. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  346. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  347. const content = model.getSelectedContent( model.document.selection );
  348. expect( stringify( content ) ).to.equal( 'ob' );
  349. } );
  350. it( 'should use parent batch', () => {
  351. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  352. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  353. model.change( writer => {
  354. model.getSelectedContent( model.document.selection );
  355. expect( writer.batch.operations ).to.length( 1 );
  356. } );
  357. } );
  358. } );
  359. describe( 'hasContent()', () => {
  360. let root;
  361. beforeEach( () => {
  362. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  363. schema.register( 'div', { inheritAllFrom: '$block' } );
  364. schema.extend( '$block', { allowIn: 'div' } );
  365. schema.register( 'image', {
  366. isObject: true
  367. } );
  368. schema.extend( 'image', { allowIn: 'div' } );
  369. schema.register( 'listItem', {
  370. inheritAllFrom: '$block'
  371. } );
  372. setData(
  373. model,
  374. '<div>' +
  375. '<paragraph></paragraph>' +
  376. '</div>' +
  377. '<paragraph>foo</paragraph>' +
  378. '<div>' +
  379. '<image></image>' +
  380. '</div>' +
  381. '<listItem></listItem>' +
  382. '<listItem></listItem>' +
  383. '<listItem></listItem>'
  384. );
  385. root = model.document.getRoot();
  386. } );
  387. it( 'should return true if given element has text node', () => {
  388. const pFoo = root.getChild( 1 );
  389. expect( model.hasContent( pFoo ) ).to.be.true;
  390. } );
  391. it( 'should return true if given element has text node (ignoreWhitespaces)', () => {
  392. const pFoo = root.getChild( 1 );
  393. expect( model.hasContent( pFoo, { ignoreWhitespaces: true } ) ).to.be.true;
  394. } );
  395. it( 'should return true if given element has text node containing spaces only', () => {
  396. const pEmpty = root.getChild( 0 ).getChild( 0 );
  397. model.enqueueChange( 'transparent', writer => {
  398. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  399. writer.insertText( ' ', pEmpty, 'end' );
  400. } );
  401. expect( model.hasContent( pEmpty ) ).to.be.true;
  402. } );
  403. it( 'should false true if given element has text node containing spaces only (ignoreWhitespaces)', () => {
  404. const pEmpty = root.getChild( 0 ).getChild( 0 );
  405. model.enqueueChange( 'transparent', writer => {
  406. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  407. writer.insertText( ' ', pEmpty, 'end' );
  408. } );
  409. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  410. } );
  411. it( 'should return true if given element has element that is an object', () => {
  412. const divImg = root.getChild( 2 );
  413. expect( model.hasContent( divImg ) ).to.be.true;
  414. } );
  415. it( 'should return false if given element has no elements', () => {
  416. const pEmpty = root.getChild( 0 ).getChild( 0 );
  417. expect( model.hasContent( pEmpty ) ).to.be.false;
  418. } );
  419. it( 'should return false if given element has only elements that are not objects', () => {
  420. const divP = root.getChild( 0 );
  421. expect( model.hasContent( divP ) ).to.be.false;
  422. } );
  423. it( 'should return true if there is a text node in given range', () => {
  424. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 2 ) );
  425. expect( model.hasContent( range ) ).to.be.true;
  426. } );
  427. it( 'should return true if there is a part of text node in given range', () => {
  428. const pFoo = root.getChild( 1 );
  429. const range = new ModelRange( ModelPosition._createAt( pFoo, 1 ), ModelPosition._createAt( pFoo, 2 ) );
  430. expect( model.hasContent( range ) ).to.be.true;
  431. } );
  432. it( 'should return true if there is element that is an object in given range', () => {
  433. const divImg = root.getChild( 2 );
  434. const range = new ModelRange( ModelPosition._createAt( divImg, 0 ), ModelPosition._createAt( divImg, 1 ) );
  435. expect( model.hasContent( range ) ).to.be.true;
  436. } );
  437. it( 'should return false if range is collapsed', () => {
  438. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 1 ) );
  439. expect( model.hasContent( range ) ).to.be.false;
  440. } );
  441. it( 'should return false if range has only elements that are not objects', () => {
  442. const range = new ModelRange( ModelPosition._createAt( root, 0 ), ModelPosition._createAt( root, 1 ) );
  443. expect( model.hasContent( range ) ).to.be.false;
  444. } );
  445. it( 'should return false for empty list items', () => {
  446. const range = new ModelRange( ModelPosition._createAt( root, 3 ), ModelPosition._createAt( root, 6 ) );
  447. expect( model.hasContent( range ) ).to.be.false;
  448. } );
  449. it( 'should return false for empty element with marker (usingOperation=false, 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: false, 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 for empty element with marker (usingOperation=true, affectsData=false)', () => {
  460. const pEmpty = root.getChild( 0 ).getChild( 0 );
  461. model.enqueueChange( 'transparent', writer => {
  462. // Insert marker.
  463. const range = ModelRange._createIn( pEmpty );
  464. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: false } );
  465. } );
  466. expect( model.hasContent( pEmpty ) ).to.be.false;
  467. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  468. } );
  469. it( 'should return false (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=false)', () => {
  470. const pEmpty = root.getChild( 0 ).getChild( 0 );
  471. model.enqueueChange( 'transparent', writer => {
  472. // Insert empty text.
  473. const text = writer.createText( ' ', { bold: true } );
  474. writer.append( text, pEmpty );
  475. // Insert marker.
  476. const range = ModelRange._createIn( pEmpty );
  477. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  478. } );
  479. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  480. } );
  481. it( 'should return true for empty text with marker (usingOperation=false, affectsData=false)', () => {
  482. const pEmpty = root.getChild( 0 ).getChild( 0 );
  483. model.enqueueChange( 'transparent', writer => {
  484. // Insert empty text.
  485. const text = writer.createText( ' ', { bold: true } );
  486. writer.append( text, pEmpty );
  487. // Insert marker.
  488. const range = ModelRange._createIn( pEmpty );
  489. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  490. } );
  491. expect( model.hasContent( pEmpty ) ).to.be.true;
  492. } );
  493. it( 'should return false for empty element with marker (usingOperation=false, 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: false, 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 false for empty element with marker (usingOperation=true, affectsData=true)', () => {
  504. const pEmpty = root.getChild( 0 ).getChild( 0 );
  505. model.enqueueChange( 'transparent', writer => {
  506. // Insert marker.
  507. const range = ModelRange._createIn( pEmpty );
  508. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  509. } );
  510. expect( model.hasContent( pEmpty ) ).to.be.false;
  511. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  512. } );
  513. it( 'should return true (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=true)', () => {
  514. const pEmpty = root.getChild( 0 ).getChild( 0 );
  515. model.enqueueChange( 'transparent', writer => {
  516. // Insert empty text.
  517. const text = writer.createText( ' ', { bold: true } );
  518. writer.append( text, pEmpty );
  519. // Insert marker.
  520. const range = ModelRange._createIn( pEmpty );
  521. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  522. } );
  523. expect( model.hasContent( pEmpty ) ).to.be.true;
  524. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.true;
  525. } );
  526. } );
  527. describe( 'createPositionFromPath()', () => {
  528. it( 'should return instance of Position', () => {
  529. expect( model.createPositionFromPath( model.document.getRoot(), [ 0 ] ) ).to.be.instanceof( ModelPosition );
  530. } );
  531. } );
  532. describe( 'createPositionAt()', () => {
  533. it( 'should return instance of Position', () => {
  534. expect( model.createPositionAt( model.document.getRoot(), 0 ) ).to.be.instanceof( ModelPosition );
  535. } );
  536. } );
  537. describe( 'createPositionAfter()', () => {
  538. it( 'should return instance of Position', () => {
  539. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  540. setData( model, '<paragraph>fo[]ar</paragraph>' );
  541. expect( model.createPositionAfter( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  542. } );
  543. } );
  544. describe( 'createPositionBefore()', () => {
  545. it( 'should return instance of Position', () => {
  546. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  547. setData( model, '<paragraph>fo[]ar</paragraph>' );
  548. expect( model.createPositionBefore( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  549. } );
  550. } );
  551. describe( 'createRange()', () => {
  552. it( 'should return instance of Range', () => {
  553. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  554. setData( model, '<paragraph>fo[]ar</paragraph>' );
  555. expect( model.createRange( model.createPositionAt( model.document.getRoot(), 0 ) ) ).to.be.instanceof( ModelRange );
  556. } );
  557. } );
  558. describe( 'createRangeIn()', () => {
  559. it( 'should return instance of Range', () => {
  560. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  561. setData( model, '<paragraph>fo[]ar</paragraph>' );
  562. expect( model.createRangeIn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  563. } );
  564. } );
  565. describe( 'createRangeOn()', () => {
  566. it( 'should return instance of Range', () => {
  567. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  568. setData( model, '<paragraph>fo[]ar</paragraph>' );
  569. expect( model.createRangeOn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  570. } );
  571. } );
  572. describe( 'createSelection()', () => {
  573. it( 'should return instance of Selection', () => {
  574. expect( model.createSelection() ).to.be.instanceof( ModelSelection );
  575. } );
  576. } );
  577. describe( 'createBatch()', () => {
  578. it( 'should return instance of Batch', () => {
  579. expect( model.createBatch() ).to.be.instanceof( Batch );
  580. } );
  581. } );
  582. describe( 'destroy()', () => {
  583. it( 'should destroy document', () => {
  584. sinon.spy( model.document, 'destroy' );
  585. model.destroy();
  586. sinon.assert.calledOnce( model.document.destroy );
  587. } );
  588. it( 'should stop listening', () => {
  589. const emitter = Object.create( EmitterMixin );
  590. const spy = sinon.spy();
  591. model.listenTo( emitter, 'event', spy );
  592. model.destroy();
  593. emitter.fire( 'event' );
  594. sinon.assert.notCalled( spy );
  595. } );
  596. } );
  597. } );