model.js 25 KB

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