model.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  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. // eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
  257. throw new CKEditorError( 'foo', null, { foo: 1 } );
  258. } );
  259. }, /foo/, null, { foo: 1 } );
  260. } );
  261. it( 'should rethrow native errors as they are in the dubug=true mode in the enqueueChange() block', () => {
  262. const error = new TypeError( 'foo' );
  263. expect( () => {
  264. model.enqueueChange( () => {
  265. throw error;
  266. } );
  267. } ).to.throw( TypeError, /foo/ );
  268. } );
  269. it( 'should throw the original CKEditorError error if it was thrown inside the `enqueueChange()` block', () => {
  270. // eslint-disable-next-line ckeditor5-rules/ckeditor-error-message
  271. const err = new CKEditorError( 'foo', null, { foo: 1 } );
  272. expectToThrowCKEditorError( () => {
  273. model.enqueueChange( () => {
  274. throw err;
  275. } );
  276. }, /foo/, null, { foo: 1 } );
  277. } );
  278. } );
  279. describe( 'applyOperation()', () => {
  280. it( 'should execute provided operation', () => {
  281. const operation = {
  282. _execute: sinon.spy(),
  283. _validate: () => true
  284. };
  285. model.applyOperation( operation );
  286. sinon.assert.calledOnce( operation._execute );
  287. } );
  288. } );
  289. describe( 'insertContent()', () => {
  290. it( 'should be decorated', () => {
  291. schema.extend( '$text', { allowIn: '$root' } ); // To surpress warnings.
  292. const spy = sinon.spy();
  293. model.on( 'insertContent', spy );
  294. model.insertContent( new ModelText( 'a' ) );
  295. expect( spy.calledOnce ).to.be.true;
  296. } );
  297. it( 'should insert content (item)', () => {
  298. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  299. setData( model, '<paragraph>fo[]ar</paragraph>' );
  300. model.insertContent( new ModelText( 'ob' ) );
  301. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  302. } );
  303. it( 'should insert content (document fragment)', () => {
  304. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  305. setData( model, '<paragraph>fo[]ar</paragraph>' );
  306. model.insertContent( new ModelDocumentFragment( [ new ModelText( 'ob' ) ] ) );
  307. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  308. } );
  309. it( 'should use current model selection if no selectable passed', () => {
  310. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  311. setData( model, '<paragraph>fo[]ar</paragraph>' );
  312. model.insertContent( new ModelText( 'ob' ) );
  313. expect( getData( model ) ).to.equal( '<paragraph>foob[]ar</paragraph>' );
  314. } );
  315. it( 'should use parent batch', () => {
  316. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  317. setData( model, '<paragraph>[]</paragraph>' );
  318. model.change( writer => {
  319. model.insertContent( new ModelText( 'abc' ) );
  320. expect( writer.batch.operations ).to.length( 1 );
  321. } );
  322. } );
  323. } );
  324. describe( 'deleteContent()', () => {
  325. it( 'should be decorated', () => {
  326. const spy = sinon.spy();
  327. model.on( 'deleteContent', spy );
  328. model.deleteContent( model.document.selection );
  329. expect( spy.calledOnce ).to.be.true;
  330. } );
  331. it( 'should delete selected content', () => {
  332. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  333. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  334. model.deleteContent( model.document.selection );
  335. expect( getData( model ) ).to.equal( '<paragraph>fo[]ar</paragraph>' );
  336. } );
  337. it( 'should use parent batch', () => {
  338. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  339. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  340. model.change( writer => {
  341. model.deleteContent( model.document.selection );
  342. expect( writer.batch.operations ).to.length( 1 );
  343. } );
  344. } );
  345. } );
  346. describe( 'modifySelection()', () => {
  347. it( 'should be decorated', () => {
  348. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  349. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  350. const spy = sinon.spy();
  351. model.on( 'modifySelection', spy );
  352. model.modifySelection( model.document.selection );
  353. expect( spy.calledOnce ).to.be.true;
  354. } );
  355. it( 'should modify a selection', () => {
  356. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  357. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  358. expect( getData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
  359. model.modifySelection( model.document.selection, { direction: 'backward' } );
  360. expect( getData( model ) ).to.equal( '<paragraph>fo[o]bar</paragraph>' );
  361. } );
  362. } );
  363. describe( 'getSelectedContent()', () => {
  364. it( 'should be decorated', () => {
  365. const spy = sinon.spy();
  366. const sel = new ModelSelection();
  367. model.on( 'getSelectedContent', spy );
  368. model.getSelectedContent( sel );
  369. expect( spy.calledOnce ).to.be.true;
  370. } );
  371. it( 'should return selected content', () => {
  372. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  373. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  374. const content = model.getSelectedContent( model.document.selection );
  375. expect( stringify( content ) ).to.equal( 'ob' );
  376. } );
  377. it( 'should use parent batch', () => {
  378. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  379. setData( model, '<paragraph>fo[ob]ar</paragraph>' );
  380. model.change( writer => {
  381. model.getSelectedContent( model.document.selection );
  382. expect( writer.batch.operations ).to.length( 1 );
  383. } );
  384. } );
  385. } );
  386. describe( 'hasContent()', () => {
  387. let root;
  388. beforeEach( () => {
  389. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  390. schema.register( 'div', { inheritAllFrom: '$block' } );
  391. schema.extend( '$block', { allowIn: 'div' } );
  392. schema.register( 'image', {
  393. isObject: true
  394. } );
  395. schema.register( 'content', {
  396. inheritAllFrom: '$block',
  397. isContent: true
  398. } );
  399. schema.extend( 'image', { allowIn: 'div' } );
  400. schema.register( 'listItem', {
  401. inheritAllFrom: '$block'
  402. } );
  403. setData(
  404. model,
  405. '<div>' +
  406. '<paragraph></paragraph>' +
  407. '</div>' +
  408. '<paragraph>foo</paragraph>' +
  409. '<div>' +
  410. '<image></image>' +
  411. '</div>' +
  412. '<listItem></listItem>' +
  413. '<listItem></listItem>' +
  414. '<listItem></listItem>' +
  415. '<content>foo</content>' +
  416. '<div>' +
  417. '<content></content>' +
  418. '</div>'
  419. );
  420. root = model.document.getRoot();
  421. } );
  422. it( 'should return true if given element has text node', () => {
  423. const pFoo = root.getChild( 1 );
  424. expect( model.hasContent( pFoo ) ).to.be.true;
  425. } );
  426. it( 'should return true if given element has text node (ignoreWhitespaces)', () => {
  427. const pFoo = root.getChild( 1 );
  428. expect( model.hasContent( pFoo, { ignoreWhitespaces: true } ) ).to.be.true;
  429. } );
  430. it( 'should return true if given element has text node containing spaces only', () => {
  431. const pEmpty = root.getChild( 0 ).getChild( 0 );
  432. model.enqueueChange( 'transparent', writer => {
  433. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  434. writer.insertText( ' ', pEmpty, 'end' );
  435. } );
  436. expect( model.hasContent( pEmpty ) ).to.be.true;
  437. } );
  438. it( 'should false true if given element has text node containing spaces only (ignoreWhitespaces)', () => {
  439. const pEmpty = root.getChild( 0 ).getChild( 0 );
  440. model.enqueueChange( 'transparent', writer => {
  441. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  442. writer.insertText( ' ', pEmpty, 'end' );
  443. } );
  444. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  445. } );
  446. it( 'should return true if given element has element that is an object', () => {
  447. const divImg = root.getChild( 2 );
  448. expect( model.hasContent( divImg ) ).to.be.true;
  449. } );
  450. it( 'should return false if given element has no elements', () => {
  451. const pEmpty = root.getChild( 0 ).getChild( 0 );
  452. expect( model.hasContent( pEmpty ) ).to.be.false;
  453. } );
  454. it( 'should return false if given element has only elements that are not objects', () => {
  455. const divP = root.getChild( 0 );
  456. expect( model.hasContent( divP ) ).to.be.false;
  457. } );
  458. it( 'should return true if there is a text node in given range', () => {
  459. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 2 ) );
  460. expect( model.hasContent( range ) ).to.be.true;
  461. } );
  462. it( 'should return true if there is a part of text node in given range', () => {
  463. const pFoo = root.getChild( 1 );
  464. const range = new ModelRange( ModelPosition._createAt( pFoo, 1 ), ModelPosition._createAt( pFoo, 2 ) );
  465. expect( model.hasContent( range ) ).to.be.true;
  466. } );
  467. it( 'should return true if there is element that is an object in given range', () => {
  468. const divImg = root.getChild( 2 );
  469. const range = new ModelRange( ModelPosition._createAt( divImg, 0 ), ModelPosition._createAt( divImg, 1 ) );
  470. expect( model.hasContent( range ) ).to.be.true;
  471. } );
  472. it( 'should return false if range is collapsed', () => {
  473. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 1 ) );
  474. expect( model.hasContent( range ) ).to.be.false;
  475. } );
  476. it( 'should return false if range has only elements that are not objects', () => {
  477. const range = new ModelRange( ModelPosition._createAt( root, 0 ), ModelPosition._createAt( root, 1 ) );
  478. expect( model.hasContent( range ) ).to.be.false;
  479. } );
  480. it( 'should return false for empty list items', () => {
  481. const range = new ModelRange( ModelPosition._createAt( root, 3 ), ModelPosition._createAt( root, 6 ) );
  482. expect( model.hasContent( range ) ).to.be.false;
  483. } );
  484. it( 'should return false for empty element with marker (usingOperation=false, affectsData=false)', () => {
  485. const pEmpty = root.getChild( 0 ).getChild( 0 );
  486. model.enqueueChange( 'transparent', writer => {
  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.false;
  492. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  493. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.false;
  494. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  495. } );
  496. it( 'should return false for empty element with marker (usingOperation=true, affectsData=false)', () => {
  497. const pEmpty = root.getChild( 0 ).getChild( 0 );
  498. model.enqueueChange( 'transparent', writer => {
  499. // Insert marker.
  500. const range = ModelRange._createIn( pEmpty );
  501. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: false } );
  502. } );
  503. expect( model.hasContent( pEmpty ) ).to.be.false;
  504. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  505. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.false;
  506. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  507. } );
  508. it( 'should return false (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=false)', () => {
  509. const pEmpty = root.getChild( 0 ).getChild( 0 );
  510. model.enqueueChange( 'transparent', writer => {
  511. // Insert empty text.
  512. const text = writer.createText( ' ', { bold: true } );
  513. writer.append( text, pEmpty );
  514. // Insert marker.
  515. const range = ModelRange._createIn( pEmpty );
  516. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  517. } );
  518. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  519. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true, ignoreMarkers: true } ) ).to.be.false;
  520. } );
  521. it( 'should return true for empty text with marker (usingOperation=false, affectsData=false)', () => {
  522. const pEmpty = root.getChild( 0 ).getChild( 0 );
  523. model.enqueueChange( 'transparent', writer => {
  524. // Insert empty text.
  525. const text = writer.createText( ' ', { bold: true } );
  526. writer.append( text, pEmpty );
  527. // Insert marker.
  528. const range = ModelRange._createIn( pEmpty );
  529. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  530. } );
  531. expect( model.hasContent( pEmpty ) ).to.be.true;
  532. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.true;
  533. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  534. } );
  535. it( 'should return false for empty element with marker (usingOperation=false, affectsData=true)', () => {
  536. const pEmpty = root.getChild( 0 ).getChild( 0 );
  537. model.enqueueChange( 'transparent', writer => {
  538. // Insert marker.
  539. const range = ModelRange._createIn( pEmpty );
  540. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  541. } );
  542. expect( model.hasContent( pEmpty ) ).to.be.false;
  543. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  544. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.false;
  545. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  546. } );
  547. it( 'should return false for empty element with marker (usingOperation=true, affectsData=true)', () => {
  548. const pEmpty = root.getChild( 0 ).getChild( 0 );
  549. model.enqueueChange( 'transparent', writer => {
  550. // Insert marker.
  551. const range = ModelRange._createIn( pEmpty );
  552. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  553. } );
  554. expect( model.hasContent( pEmpty ) ).to.be.false;
  555. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  556. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.false;
  557. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  558. } );
  559. it( 'should return true (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=true)', () => {
  560. const pEmpty = root.getChild( 0 ).getChild( 0 );
  561. model.enqueueChange( 'transparent', writer => {
  562. // Insert empty text.
  563. const text = writer.createText( ' ', { bold: true } );
  564. writer.append( text, pEmpty );
  565. // Insert marker.
  566. const range = ModelRange._createIn( pEmpty );
  567. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  568. } );
  569. expect( model.hasContent( pEmpty ) ).to.be.true;
  570. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.true;
  571. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.true;
  572. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  573. } );
  574. it( 'should return true for an item registered as a content (isContent=true, isObject=false) in the schema', () => {
  575. const contentElement = root.getChild( 6 );
  576. expect( model.hasContent( contentElement ) ).to.be.true;
  577. } );
  578. it( 'should return true if a range contains an item registered as a content (isContent=true, isObject=false) in the schema', () => {
  579. // [<div><content></content></div>]
  580. const range = new ModelRange( ModelPosition._createAt( root, 6 ), ModelPosition._createAt( root, 7 ) );
  581. expect( model.hasContent( range ) ).to.be.true;
  582. } );
  583. } );
  584. describe( 'createPositionFromPath()', () => {
  585. it( 'should return instance of Position', () => {
  586. expect( model.createPositionFromPath( model.document.getRoot(), [ 0 ] ) ).to.be.instanceof( ModelPosition );
  587. } );
  588. } );
  589. describe( 'createPositionAt()', () => {
  590. it( 'should return instance of Position', () => {
  591. expect( model.createPositionAt( model.document.getRoot(), 0 ) ).to.be.instanceof( ModelPosition );
  592. } );
  593. } );
  594. describe( 'createPositionAfter()', () => {
  595. it( 'should return instance of Position', () => {
  596. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  597. setData( model, '<paragraph>fo[]ar</paragraph>' );
  598. expect( model.createPositionAfter( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  599. } );
  600. } );
  601. describe( 'createPositionBefore()', () => {
  602. it( 'should return instance of Position', () => {
  603. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  604. setData( model, '<paragraph>fo[]ar</paragraph>' );
  605. expect( model.createPositionBefore( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  606. } );
  607. } );
  608. describe( 'createRange()', () => {
  609. it( 'should return instance of Range', () => {
  610. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  611. setData( model, '<paragraph>fo[]ar</paragraph>' );
  612. expect( model.createRange( model.createPositionAt( model.document.getRoot(), 0 ) ) ).to.be.instanceof( ModelRange );
  613. } );
  614. } );
  615. describe( 'createRangeIn()', () => {
  616. it( 'should return instance of Range', () => {
  617. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  618. setData( model, '<paragraph>fo[]ar</paragraph>' );
  619. expect( model.createRangeIn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  620. } );
  621. } );
  622. describe( 'createRangeOn()', () => {
  623. it( 'should return instance of Range', () => {
  624. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  625. setData( model, '<paragraph>fo[]ar</paragraph>' );
  626. expect( model.createRangeOn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  627. } );
  628. } );
  629. describe( 'createSelection()', () => {
  630. it( 'should return instance of Selection', () => {
  631. expect( model.createSelection() ).to.be.instanceof( ModelSelection );
  632. } );
  633. } );
  634. describe( 'createBatch()', () => {
  635. it( 'should return instance of Batch', () => {
  636. const batch = model.createBatch();
  637. expect( batch ).to.be.instanceof( Batch );
  638. expect( batch.type ).to.equal( 'default' );
  639. } );
  640. it( 'should allow to define type of Batch', () => {
  641. const batch = model.createBatch( 'transparent' );
  642. expect( batch ).to.be.instanceof( Batch );
  643. expect( batch.type ).to.equal( 'transparent' );
  644. } );
  645. } );
  646. describe( 'createOperationFromJson()', () => {
  647. it( 'should create operation from JSON', () => {
  648. const operation = model.createOperationFromJSON( {
  649. __className: 'NoOperation',
  650. baseVersion: 0
  651. } );
  652. expect( operation ).to.instanceof( NoOperation );
  653. expect( operation.baseVersion ).to.equal( 0 );
  654. } );
  655. } );
  656. describe( 'destroy()', () => {
  657. it( 'should destroy document', () => {
  658. sinon.spy( model.document, 'destroy' );
  659. model.destroy();
  660. sinon.assert.calledOnce( model.document.destroy );
  661. } );
  662. it( 'should stop listening', () => {
  663. const emitter = Object.create( EmitterMixin );
  664. const spy = sinon.spy();
  665. model.listenTo( emitter, 'event', spy );
  666. model.destroy();
  667. emitter.fire( 'event' );
  668. sinon.assert.notCalled( spy );
  669. } );
  670. } );
  671. } );