model.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /**
  2. * @license Copyright (c) 2003-2020, 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 NoOperation from '../../src/model/operation/nooperation';
  14. import { getData, setData, stringify } from '../../src/dev-utils/model';
  15. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  16. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  17. describe( 'Model', () => {
  18. let model, schema, changes;
  19. beforeEach( () => {
  20. model = new Model();
  21. model.document.createRoot();
  22. model.document.createRoot( '$root', 'title' );
  23. schema = model.schema;
  24. changes = '';
  25. } );
  26. describe( 'constructor()', () => {
  27. it( 'registers $root to the schema', () => {
  28. expect( schema.isRegistered( '$root' ) ).to.be.true;
  29. expect( schema.isLimit( '$root' ) ).to.be.true;
  30. } );
  31. it( 'registers $block to the schema', () => {
  32. expect( schema.isRegistered( '$block' ) ).to.be.true;
  33. expect( schema.isBlock( '$block' ) ).to.be.true;
  34. expect( schema.checkChild( [ '$root' ], '$block' ) ).to.be.true;
  35. } );
  36. it( 'registers $text to the schema', () => {
  37. expect( schema.isRegistered( '$text' ) ).to.be.true;
  38. expect( schema.checkChild( [ '$block' ], '$text' ) ).to.be.true;
  39. } );
  40. it( 'registers $clipboardHolder to the schema', () => {
  41. expect( schema.isRegistered( '$clipboardHolder' ) ).to.be.true;
  42. expect( schema.isLimit( '$clipboardHolder' ) ).to.be.true;
  43. expect( schema.checkChild( [ '$clipboardHolder' ], '$text' ) ).to.be.true;
  44. expect( schema.checkChild( [ '$clipboardHolder' ], '$block' ) ).to.be.true;
  45. } );
  46. it( 'registers $marker to the schema', () => {
  47. model.document.createRoot( '$anywhere', 'anywhere' );
  48. schema.register( 'anything' );
  49. expect( schema.isRegistered( '$marker' ) ).to.be.true;
  50. expect( schema.checkChild( [ '$root' ], '$marker' ) ).to.be.true;
  51. expect( schema.checkChild( [ '$block' ], '$marker' ) ).to.be.true;
  52. expect( schema.checkChild( [ '$anywhere' ], '$marker' ) ).to.be.true;
  53. expect( schema.checkChild( [ 'anything' ], '$marker' ) ).to.be.true;
  54. } );
  55. } );
  56. describe( 'change() & enqueueChange()', () => {
  57. it( 'should execute changes immediately', () => {
  58. model.change( () => {
  59. changes += 'A';
  60. } );
  61. expect( changes ).to.equal( 'A' );
  62. } );
  63. it( 'should pass returned value', () => {
  64. const ret = model.change( () => {
  65. changes += 'A';
  66. return 'B';
  67. } );
  68. changes += ret;
  69. expect( changes ).to.equal( 'AB' );
  70. } );
  71. it( 'should not mixed the order when nested change is called', () => {
  72. const ret = model.change( () => {
  73. changes += 'A';
  74. nested();
  75. return 'D';
  76. } );
  77. changes += ret;
  78. expect( changes ).to.equal( 'ABCD' );
  79. function nested() {
  80. const ret = model.change( () => {
  81. changes += 'B';
  82. return 'C';
  83. } );
  84. changes += ret;
  85. }
  86. } );
  87. it( 'should execute enqueueChange immediately if its the first block', () => {
  88. model.enqueueChange( () => {
  89. changes += 'A';
  90. nested();
  91. } );
  92. expect( changes ).to.equal( 'ABC' );
  93. function nested() {
  94. const ret = model.change( () => {
  95. changes += 'B';
  96. return 'C';
  97. } );
  98. changes += ret;
  99. }
  100. } );
  101. it( 'should be possible to enqueueChange immediately if its the first block', () => {
  102. model.enqueueChange( () => {
  103. changes += 'A';
  104. nested();
  105. } );
  106. expect( changes ).to.equal( 'AB' );
  107. function nested() {
  108. model.change( () => {
  109. changes += 'B';
  110. } );
  111. }
  112. } );
  113. it( 'should be possible to nest change in enqueueChange', () => {
  114. model.enqueueChange( () => {
  115. changes += 'A';
  116. nested();
  117. changes += 'D';
  118. } );
  119. expect( changes ).to.equal( 'ABCD' );
  120. function nested() {
  121. const ret = model.change( () => {
  122. changes += 'B';
  123. return 'C';
  124. } );
  125. changes += ret;
  126. }
  127. } );
  128. it( 'should be possible to nest enqueueChange in enqueueChange', () => {
  129. model.enqueueChange( () => {
  130. changes += 'A';
  131. nestedEnqueue();
  132. changes += 'B';
  133. } );
  134. expect( changes ).to.equal( 'ABC' );
  135. function nestedEnqueue() {
  136. model.enqueueChange( () => {
  137. changes += 'C';
  138. } );
  139. }
  140. } );
  141. it( 'should be possible to nest enqueueChange in changes', () => {
  142. const ret = model.change( () => {
  143. changes += 'A';
  144. nestedEnqueue();
  145. changes += 'B';
  146. return 'D';
  147. } );
  148. changes += ret;
  149. expect( changes ).to.equal( 'ABCD' );
  150. function nestedEnqueue() {
  151. model.enqueueChange( () => {
  152. changes += 'C';
  153. } );
  154. }
  155. } );
  156. it( 'should be possible to nest enqueueChange in enqueueChange event', () => {
  157. model.enqueueChange( () => {
  158. changes += 'A';
  159. model.enqueueChange( () => {
  160. changes += 'C';
  161. } );
  162. changes += 'B';
  163. } );
  164. expect( changes ).to.equal( 'ABC' );
  165. } );
  166. it( 'should be possible to nest enqueueChange in changes event', () => {
  167. model.change( () => {
  168. changes += 'A';
  169. model.enqueueChange( () => {
  170. changes += 'C';
  171. } );
  172. changes += 'B';
  173. } );
  174. expect( changes ).to.equal( 'ABC' );
  175. } );
  176. it( 'should be possible to nest changes in enqueueChange event', () => {
  177. model.enqueueChange( () => {
  178. changes += 'A';
  179. model.change( () => {
  180. changes += 'B';
  181. } );
  182. changes += 'C';
  183. } );
  184. expect( changes ).to.equal( 'ABC' );
  185. } );
  186. it( 'should be possible to nest changes in changes event', () => {
  187. model.change( () => {
  188. changes += 'A';
  189. model.change( () => {
  190. changes += 'B';
  191. } );
  192. changes += 'C';
  193. } );
  194. expect( changes ).to.equal( 'ABC' );
  195. } );
  196. it( 'should let mix blocks', () => {
  197. model.change( () => {
  198. changes += 'A';
  199. model.enqueueChange( () => {
  200. changes += 'C';
  201. model.change( () => {
  202. changes += 'D';
  203. model.enqueueChange( () => {
  204. changes += 'F';
  205. } );
  206. } );
  207. model.change( () => {
  208. changes += 'E';
  209. } );
  210. } );
  211. changes += 'B';
  212. } );
  213. expect( changes ).to.equal( 'ABCDEF' );
  214. } );
  215. it( 'should use the same writer in all change blocks (change & change)', () => {
  216. model.change( outerWriter => {
  217. model.change( innerWriter => {
  218. expect( innerWriter ).to.equal( outerWriter );
  219. } );
  220. } );
  221. } );
  222. it( 'should create new writer in enqueue block', () => {
  223. model.change( outerWriter => {
  224. model.enqueueChange( innerWriter => {
  225. expect( innerWriter ).to.not.equal( outerWriter );
  226. expect( innerWriter.batch ).to.not.equal( outerWriter.batch );
  227. } );
  228. } );
  229. } );
  230. it( 'should let you pass batch', () => {
  231. let outerBatch;
  232. model.change( outerWriter => {
  233. outerBatch = outerWriter.batch;
  234. model.enqueueChange( outerBatch, innerWriter => {
  235. expect( innerWriter.batch ).to.equal( outerBatch );
  236. } );
  237. } );
  238. } );
  239. it( 'should let you create transparent batch', () => {
  240. model.enqueueChange( 'transparent', writer => {
  241. expect( writer.batch.type ).to.equal( 'transparent' );
  242. } );
  243. } );
  244. it( 'should rethrow native errors as they are in the dubug=true mode in the model.change() block', () => {
  245. const error = new TypeError( 'foo' );
  246. expect( () => {
  247. model.change( () => {
  248. throw error;
  249. } );
  250. } ).to.throw( TypeError, /foo/ );
  251. } );
  252. it( 'should throw the original CKEditorError error if it was thrown inside the `change()` block', () => {
  253. expectToThrowCKEditorError( () => {
  254. model.change( () => {
  255. throw new CKEditorError( 'foo', null, { foo: 1 } );
  256. } );
  257. }, /foo/, null, { foo: 1 } );
  258. } );
  259. it( 'should rethrow native errors as they are in the dubug=true mode in the enqueueChange() block', () => {
  260. const error = new TypeError( 'foo' );
  261. expect( () => {
  262. model.enqueueChange( () => {
  263. throw error;
  264. } );
  265. } ).to.throw( TypeError, /foo/ );
  266. } );
  267. it( 'should throw the original CKEditorError error if it was thrown inside the `enqueueChange()` block', () => {
  268. const err = new CKEditorError( 'foo', null, { foo: 1 } );
  269. expectToThrowCKEditorError( () => {
  270. model.enqueueChange( () => {
  271. throw err;
  272. } );
  273. }, /foo/, null, { foo: 1 } );
  274. } );
  275. } );
  276. describe( 'applyOperation()', () => {
  277. it( 'should execute provided operation', () => {
  278. const operation = {
  279. _execute: sinon.spy(),
  280. _validate: () => true
  281. };
  282. model.applyOperation( operation );
  283. sinon.assert.calledOnce( operation._execute );
  284. } );
  285. } );
  286. describe( 'insertContent()', () => {
  287. it( 'should be decorated', () => {
  288. schema.extend( '$text', { allowIn: '$root' } ); // To surpress warnings.
  289. const spy = sinon.spy();
  290. model.on( 'insertContent', spy );
  291. model.insertContent( new ModelText( 'a' ) );
  292. expect( spy.calledOnce ).to.be.true;
  293. } );
  294. it( 'should insert content (item)', () => {
  295. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  296. setData( model, '<paragraph>fo[]ar</paragraph>' );
  297. model.insertContent( new ModelText( 'ob' ) );
  298. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  299. } );
  300. it( 'should insert content (document fragment)', () => {
  301. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  302. setData( model, '<paragraph>fo[]ar</paragraph>' );
  303. model.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ) );
  304. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  305. } );
  306. it( 'should use current model selection if no selectable passed', () => {
  307. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  308. setData( model, '<paragraph>fo[]ar</paragraph>' );
  309. model.insertContent( new ModelText( 'ob' ) );
  310. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  311. } );
  312. it( 'should use parent batch', () => {
  313. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  314. setData( model, '<paragraph>[]</paragraph>' );
  315. model.change( writer => {
  316. model.insertContent( new ModelText( 'abc' ) );
  317. expect( writer.batch.operations ).to.length( 1 );
  318. } );
  319. } );
  320. } );
  321. describe( 'deleteContent()', () => {
  322. it( 'should be decorated', () => {
  323. const spy = sinon.spy();
  324. model.on( 'deleteContent', spy );
  325. model.deleteContent( model.document.selection );
  326. expect( spy.calledOnce ).to.be.true;
  327. } );
  328. it( 'should delete selected content', () => {
  329. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  330. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  331. model.deleteContent( model.document.selection );
  332. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  333. } );
  334. it( 'should use parent batch', () => {
  335. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  336. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  337. model.change( writer => {
  338. model.deleteContent( model.document.selection );
  339. expect( writer.batch.operations ).to.length( 1 );
  340. } );
  341. } );
  342. } );
  343. describe( 'modifySelection()', () => {
  344. it( 'should be decorated', () => {
  345. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  346. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  347. const spy = sinon.spy();
  348. model.on( 'modifySelection', spy );
  349. model.modifySelection( model.document.selection );
  350. expect( spy.calledOnce ).to.be.true;
  351. } );
  352. it( 'should modify a selection', () => {
  353. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  354. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  355. expect( getData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  356. model.modifySelection( model.document.selection, { direction: 'backward' } );
  357. expect( getData( model ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  358. } );
  359. } );
  360. describe( 'getSelectedContent()', () => {
  361. it( 'should be decorated', () => {
  362. const spy = sinon.spy();
  363. const sel = new ModelSelection();
  364. model.on( 'getSelectedContent', spy );
  365. model.getSelectedContent( sel );
  366. expect( spy.calledOnce ).to.be.true;
  367. } );
  368. it( 'should return selected content', () => {
  369. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  370. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  371. const content = model.getSelectedContent( model.document.selection );
  372. expect( stringify( content ) ).to.equal( 'ob' );
  373. } );
  374. it( 'should use parent batch', () => {
  375. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  376. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  377. model.change( writer => {
  378. model.getSelectedContent( model.document.selection );
  379. expect( writer.batch.operations ).to.length( 1 );
  380. } );
  381. } );
  382. } );
  383. describe( 'hasContent()', () => {
  384. let root;
  385. beforeEach( () => {
  386. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  387. schema.register( 'div', { inheritAllFrom: '$block' } );
  388. schema.extend( '$block', { allowIn: 'div' } );
  389. schema.register( 'image', {
  390. isObject: true
  391. } );
  392. schema.extend( 'image', { allowIn: 'div' } );
  393. schema.register( 'listItem', {
  394. inheritAllFrom: '$block'
  395. } );
  396. setData(
  397. model,
  398. '<div>' +
  399. '<paragraph></paragraph>' +
  400. '</div>' +
  401. '<paragraph>foo</paragraph>' +
  402. '<div>' +
  403. '<image></image>' +
  404. '</div>' +
  405. '<listItem></listItem>' +
  406. '<listItem></listItem>' +
  407. '<listItem></listItem>'
  408. );
  409. root = model.document.getRoot();
  410. } );
  411. it( 'should return true if given element has text node', () => {
  412. const pFoo = root.getChild( 1 );
  413. expect( model.hasContent( pFoo ) ).to.be.true;
  414. } );
  415. it( 'should return true if given element has text node (ignoreWhitespaces)', () => {
  416. const pFoo = root.getChild( 1 );
  417. expect( model.hasContent( pFoo, { ignoreWhitespaces: true } ) ).to.be.true;
  418. } );
  419. it( 'should return true if given element has text node containing spaces only', () => {
  420. const pEmpty = root.getChild( 0 ).getChild( 0 );
  421. model.enqueueChange( 'transparent', writer => {
  422. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  423. writer.insertText( ' ', pEmpty, 'end' );
  424. } );
  425. expect( model.hasContent( pEmpty ) ).to.be.true;
  426. } );
  427. it( 'should false true if given element has text node containing spaces only (ignoreWhitespaces)', () => {
  428. const pEmpty = root.getChild( 0 ).getChild( 0 );
  429. model.enqueueChange( 'transparent', writer => {
  430. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  431. writer.insertText( ' ', pEmpty, 'end' );
  432. } );
  433. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  434. } );
  435. it( 'should return true if given element has element that is an object', () => {
  436. const divImg = root.getChild( 2 );
  437. expect( model.hasContent( divImg ) ).to.be.true;
  438. } );
  439. it( 'should return false if given element has no elements', () => {
  440. const pEmpty = root.getChild( 0 ).getChild( 0 );
  441. expect( model.hasContent( pEmpty ) ).to.be.false;
  442. } );
  443. it( 'should return false if given element has only elements that are not objects', () => {
  444. const divP = root.getChild( 0 );
  445. expect( model.hasContent( divP ) ).to.be.false;
  446. } );
  447. it( 'should return true if there is a text node in given range', () => {
  448. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 2 ) );
  449. expect( model.hasContent( range ) ).to.be.true;
  450. } );
  451. it( 'should return true if there is a part of text node in given range', () => {
  452. const pFoo = root.getChild( 1 );
  453. const range = new ModelRange( ModelPosition._createAt( pFoo, 1 ), ModelPosition._createAt( pFoo, 2 ) );
  454. expect( model.hasContent( range ) ).to.be.true;
  455. } );
  456. it( 'should return true if there is element that is an object in given range', () => {
  457. const divImg = root.getChild( 2 );
  458. const range = new ModelRange( ModelPosition._createAt( divImg, 0 ), ModelPosition._createAt( divImg, 1 ) );
  459. expect( model.hasContent( range ) ).to.be.true;
  460. } );
  461. it( 'should return false if range is collapsed', () => {
  462. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 1 ) );
  463. expect( model.hasContent( range ) ).to.be.false;
  464. } );
  465. it( 'should return false if range has only elements that are not objects', () => {
  466. const range = new ModelRange( ModelPosition._createAt( root, 0 ), ModelPosition._createAt( root, 1 ) );
  467. expect( model.hasContent( range ) ).to.be.false;
  468. } );
  469. it( 'should return false for empty list items', () => {
  470. const range = new ModelRange( ModelPosition._createAt( root, 3 ), ModelPosition._createAt( root, 6 ) );
  471. expect( model.hasContent( range ) ).to.be.false;
  472. } );
  473. it( 'should return false for empty element with marker (usingOperation=false, affectsData=false)', () => {
  474. const pEmpty = root.getChild( 0 ).getChild( 0 );
  475. model.enqueueChange( 'transparent', writer => {
  476. // Insert marker.
  477. const range = ModelRange._createIn( pEmpty );
  478. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  479. } );
  480. expect( model.hasContent( pEmpty ) ).to.be.false;
  481. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  482. } );
  483. it( 'should return false for empty element with marker (usingOperation=true, affectsData=false)', () => {
  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: true, affectsData: false } );
  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 (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=false)', () => {
  494. const pEmpty = root.getChild( 0 ).getChild( 0 );
  495. model.enqueueChange( 'transparent', writer => {
  496. // Insert empty text.
  497. const text = writer.createText( ' ', { bold: true } );
  498. writer.append( text, pEmpty );
  499. // Insert marker.
  500. const range = ModelRange._createIn( pEmpty );
  501. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  502. } );
  503. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  504. } );
  505. it( 'should return true for empty text with marker (usingOperation=false, affectsData=false)', () => {
  506. const pEmpty = root.getChild( 0 ).getChild( 0 );
  507. model.enqueueChange( 'transparent', writer => {
  508. // Insert empty text.
  509. const text = writer.createText( ' ', { bold: true } );
  510. writer.append( text, pEmpty );
  511. // Insert marker.
  512. const range = ModelRange._createIn( pEmpty );
  513. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  514. } );
  515. expect( model.hasContent( pEmpty ) ).to.be.true;
  516. } );
  517. it( 'should return false for empty element with marker (usingOperation=false, affectsData=true)', () => {
  518. const pEmpty = root.getChild( 0 ).getChild( 0 );
  519. model.enqueueChange( 'transparent', writer => {
  520. // Insert marker.
  521. const range = ModelRange._createIn( pEmpty );
  522. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  523. } );
  524. expect( model.hasContent( pEmpty ) ).to.be.false;
  525. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  526. } );
  527. it( 'should return false for empty element with marker (usingOperation=true, affectsData=true)', () => {
  528. const pEmpty = root.getChild( 0 ).getChild( 0 );
  529. model.enqueueChange( 'transparent', writer => {
  530. // Insert marker.
  531. const range = ModelRange._createIn( pEmpty );
  532. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  533. } );
  534. expect( model.hasContent( pEmpty ) ).to.be.false;
  535. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  536. } );
  537. it( 'should return true (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=true)', () => {
  538. const pEmpty = root.getChild( 0 ).getChild( 0 );
  539. model.enqueueChange( 'transparent', writer => {
  540. // Insert empty text.
  541. const text = writer.createText( ' ', { bold: true } );
  542. writer.append( text, pEmpty );
  543. // Insert marker.
  544. const range = ModelRange._createIn( pEmpty );
  545. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  546. } );
  547. expect( model.hasContent( pEmpty ) ).to.be.true;
  548. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.true;
  549. } );
  550. } );
  551. describe( 'createPositionFromPath()', () => {
  552. it( 'should return instance of Position', () => {
  553. expect( model.createPositionFromPath( model.document.getRoot(), [ 0 ] ) ).to.be.instanceof( ModelPosition );
  554. } );
  555. } );
  556. describe( 'createPositionAt()', () => {
  557. it( 'should return instance of Position', () => {
  558. expect( model.createPositionAt( model.document.getRoot(), 0 ) ).to.be.instanceof( ModelPosition );
  559. } );
  560. } );
  561. describe( 'createPositionAfter()', () => {
  562. it( 'should return instance of Position', () => {
  563. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  564. setData( model, '<paragraph>fo[]ar</paragraph>' );
  565. expect( model.createPositionAfter( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  566. } );
  567. } );
  568. describe( 'createPositionBefore()', () => {
  569. it( 'should return instance of Position', () => {
  570. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  571. setData( model, '<paragraph>fo[]ar</paragraph>' );
  572. expect( model.createPositionBefore( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  573. } );
  574. } );
  575. describe( 'createRange()', () => {
  576. it( 'should return instance of Range', () => {
  577. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  578. setData( model, '<paragraph>fo[]ar</paragraph>' );
  579. expect( model.createRange( model.createPositionAt( model.document.getRoot(), 0 ) ) ).to.be.instanceof( ModelRange );
  580. } );
  581. } );
  582. describe( 'createRangeIn()', () => {
  583. it( 'should return instance of Range', () => {
  584. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  585. setData( model, '<paragraph>fo[]ar</paragraph>' );
  586. expect( model.createRangeIn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  587. } );
  588. } );
  589. describe( 'createRangeOn()', () => {
  590. it( 'should return instance of Range', () => {
  591. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  592. setData( model, '<paragraph>fo[]ar</paragraph>' );
  593. expect( model.createRangeOn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  594. } );
  595. } );
  596. describe( 'createSelection()', () => {
  597. it( 'should return instance of Selection', () => {
  598. expect( model.createSelection() ).to.be.instanceof( ModelSelection );
  599. } );
  600. } );
  601. describe( 'createBatch()', () => {
  602. it( 'should return instance of Batch', () => {
  603. const batch = model.createBatch();
  604. expect( batch ).to.be.instanceof( Batch );
  605. expect( batch.type ).to.equal( 'default' );
  606. } );
  607. it( 'should allow to define type of Batch', () => {
  608. const batch = model.createBatch( 'transparent' );
  609. expect( batch ).to.be.instanceof( Batch );
  610. expect( batch.type ).to.equal( 'transparent' );
  611. } );
  612. } );
  613. describe( 'createOperationFromJson()', () => {
  614. it( 'should create operation from JSON', () => {
  615. const operation = model.createOperationFromJSON( {
  616. __className: 'NoOperation',
  617. baseVersion: 0
  618. } );
  619. expect( operation ).to.instanceof( NoOperation );
  620. expect( operation.baseVersion ).to.equal( 0 );
  621. } );
  622. } );
  623. describe( 'destroy()', () => {
  624. it( 'should destroy document', () => {
  625. sinon.spy( model.document, 'destroy' );
  626. model.destroy();
  627. sinon.assert.calledOnce( model.document.destroy );
  628. } );
  629. it( 'should stop listening', () => {
  630. const emitter = Object.create( EmitterMixin );
  631. const spy = sinon.spy();
  632. model.listenTo( emitter, 'event', spy );
  633. model.destroy();
  634. emitter.fire( 'event' );
  635. sinon.assert.notCalled( spy );
  636. } );
  637. } );
  638. } );