8
0

model.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899
  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.register( 'content', {
  394. inheritAllFrom: '$block',
  395. isContent: true
  396. } );
  397. schema.extend( 'image', { allowIn: 'div' } );
  398. schema.register( 'listItem', {
  399. inheritAllFrom: '$block'
  400. } );
  401. setData(
  402. model,
  403. '<div>' +
  404. '<paragraph></paragraph>' +
  405. '</div>' +
  406. '<paragraph>foo</paragraph>' +
  407. '<div>' +
  408. '<image></image>' +
  409. '</div>' +
  410. '<listItem></listItem>' +
  411. '<listItem></listItem>' +
  412. '<listItem></listItem>' +
  413. '<content>foo</content>' +
  414. '<div>' +
  415. '<content></content>' +
  416. '</div>'
  417. );
  418. root = model.document.getRoot();
  419. } );
  420. it( 'should return true if given element has text node', () => {
  421. const pFoo = root.getChild( 1 );
  422. expect( model.hasContent( pFoo ) ).to.be.true;
  423. } );
  424. it( 'should return true if given element has text node (ignoreWhitespaces)', () => {
  425. const pFoo = root.getChild( 1 );
  426. expect( model.hasContent( pFoo, { ignoreWhitespaces: true } ) ).to.be.true;
  427. } );
  428. it( 'should return true if given element has text node containing spaces only', () => {
  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 ) ).to.be.true;
  435. } );
  436. it( 'should false true if given element has text node containing spaces only (ignoreWhitespaces)', () => {
  437. const pEmpty = root.getChild( 0 ).getChild( 0 );
  438. model.enqueueChange( 'transparent', writer => {
  439. // Model `setData()` method trims whitespaces so use writer here to insert whitespace only text.
  440. writer.insertText( ' ', pEmpty, 'end' );
  441. } );
  442. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  443. } );
  444. it( 'should return true if given element has element that is an object', () => {
  445. const divImg = root.getChild( 2 );
  446. expect( model.hasContent( divImg ) ).to.be.true;
  447. } );
  448. it( 'should return false if given element has no elements', () => {
  449. const pEmpty = root.getChild( 0 ).getChild( 0 );
  450. expect( model.hasContent( pEmpty ) ).to.be.false;
  451. } );
  452. it( 'should return false if given element has only elements that are not objects', () => {
  453. const divP = root.getChild( 0 );
  454. expect( model.hasContent( divP ) ).to.be.false;
  455. } );
  456. it( 'should return true if there is a text node in given range', () => {
  457. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 2 ) );
  458. expect( model.hasContent( range ) ).to.be.true;
  459. } );
  460. it( 'should return true if there is a part of text node in given range', () => {
  461. const pFoo = root.getChild( 1 );
  462. const range = new ModelRange( ModelPosition._createAt( pFoo, 1 ), ModelPosition._createAt( pFoo, 2 ) );
  463. expect( model.hasContent( range ) ).to.be.true;
  464. } );
  465. it( 'should return true if there is element that is an object in given range', () => {
  466. const divImg = root.getChild( 2 );
  467. const range = new ModelRange( ModelPosition._createAt( divImg, 0 ), ModelPosition._createAt( divImg, 1 ) );
  468. expect( model.hasContent( range ) ).to.be.true;
  469. } );
  470. it( 'should return false if range is collapsed', () => {
  471. const range = new ModelRange( ModelPosition._createAt( root, 1 ), ModelPosition._createAt( root, 1 ) );
  472. expect( model.hasContent( range ) ).to.be.false;
  473. } );
  474. it( 'should return false if range has only elements that are not objects', () => {
  475. const range = new ModelRange( ModelPosition._createAt( root, 0 ), ModelPosition._createAt( root, 1 ) );
  476. expect( model.hasContent( range ) ).to.be.false;
  477. } );
  478. it( 'should return false for empty list items', () => {
  479. const range = new ModelRange( ModelPosition._createAt( root, 3 ), ModelPosition._createAt( root, 6 ) );
  480. expect( model.hasContent( range ) ).to.be.false;
  481. } );
  482. it( 'should return false for empty element with marker (usingOperation=false, affectsData=false)', () => {
  483. const pEmpty = root.getChild( 0 ).getChild( 0 );
  484. model.enqueueChange( 'transparent', writer => {
  485. // Insert marker.
  486. const range = ModelRange._createIn( pEmpty );
  487. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  488. } );
  489. expect( model.hasContent( pEmpty ) ).to.be.false;
  490. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  491. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.false;
  492. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  493. } );
  494. it( 'should return false for empty element with marker (usingOperation=true, affectsData=false)', () => {
  495. const pEmpty = root.getChild( 0 ).getChild( 0 );
  496. model.enqueueChange( 'transparent', writer => {
  497. // Insert marker.
  498. const range = ModelRange._createIn( pEmpty );
  499. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: false } );
  500. } );
  501. expect( model.hasContent( pEmpty ) ).to.be.false;
  502. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  503. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.false;
  504. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  505. } );
  506. it( 'should return false (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=false)', () => {
  507. const pEmpty = root.getChild( 0 ).getChild( 0 );
  508. model.enqueueChange( 'transparent', writer => {
  509. // Insert empty text.
  510. const text = writer.createText( ' ', { bold: true } );
  511. writer.append( text, pEmpty );
  512. // Insert marker.
  513. const range = ModelRange._createIn( pEmpty );
  514. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  515. } );
  516. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  517. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true, ignoreMarkers: true } ) ).to.be.false;
  518. } );
  519. it( 'should return true for empty text with marker (usingOperation=false, affectsData=false)', () => {
  520. const pEmpty = root.getChild( 0 ).getChild( 0 );
  521. model.enqueueChange( 'transparent', writer => {
  522. // Insert empty text.
  523. const text = writer.createText( ' ', { bold: true } );
  524. writer.append( text, pEmpty );
  525. // Insert marker.
  526. const range = ModelRange._createIn( pEmpty );
  527. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: false } );
  528. } );
  529. expect( model.hasContent( pEmpty ) ).to.be.true;
  530. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.true;
  531. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  532. } );
  533. it( 'should return false for empty element with marker (usingOperation=false, affectsData=true)', () => {
  534. const pEmpty = root.getChild( 0 ).getChild( 0 );
  535. model.enqueueChange( 'transparent', writer => {
  536. // Insert marker.
  537. const range = ModelRange._createIn( pEmpty );
  538. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  539. } );
  540. expect( model.hasContent( pEmpty ) ).to.be.false;
  541. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  542. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.false;
  543. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  544. } );
  545. it( 'should return false for empty element with marker (usingOperation=true, affectsData=true)', () => {
  546. const pEmpty = root.getChild( 0 ).getChild( 0 );
  547. model.enqueueChange( 'transparent', writer => {
  548. // Insert marker.
  549. const range = ModelRange._createIn( pEmpty );
  550. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  551. } );
  552. expect( model.hasContent( pEmpty ) ).to.be.false;
  553. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.false;
  554. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.false;
  555. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  556. } );
  557. it( 'should return true (ignoreWhitespaces) for empty text with marker (usingOperation=false, affectsData=true)', () => {
  558. const pEmpty = root.getChild( 0 ).getChild( 0 );
  559. model.enqueueChange( 'transparent', writer => {
  560. // Insert empty text.
  561. const text = writer.createText( ' ', { bold: true } );
  562. writer.append( text, pEmpty );
  563. // Insert marker.
  564. const range = ModelRange._createIn( pEmpty );
  565. writer.addMarker( 'comment1', { range, usingOperation: false, affectsData: true } );
  566. } );
  567. expect( model.hasContent( pEmpty ) ).to.be.true;
  568. expect( model.hasContent( pEmpty, { ignoreWhitespaces: true } ) ).to.be.true;
  569. expect( model.hasContent( pEmpty, { ignoreMarkers: true } ) ).to.be.true;
  570. expect( model.hasContent( pEmpty, { ignoreMarkers: true, ignoreWhitespaces: true } ) ).to.be.false;
  571. } );
  572. it( 'should return true for an item registered as a content (isContent=true, isObject=false) in the schema', () => {
  573. const contentElement = root.getChild( 6 );
  574. expect( model.hasContent( contentElement ) ).to.be.true;
  575. } );
  576. it( 'should return true if a range contains an item registered as a content (isContent=true, isObject=false) in the schema', () => {
  577. // [<div><content></content></div>]
  578. const range = new ModelRange( ModelPosition._createAt( root, 6 ), ModelPosition._createAt( root, 7 ) );
  579. expect( model.hasContent( range ) ).to.be.true;
  580. } );
  581. } );
  582. describe( 'createPositionFromPath()', () => {
  583. it( 'should return instance of Position', () => {
  584. expect( model.createPositionFromPath( model.document.getRoot(), [ 0 ] ) ).to.be.instanceof( ModelPosition );
  585. } );
  586. } );
  587. describe( 'createPositionAt()', () => {
  588. it( 'should return instance of Position', () => {
  589. expect( model.createPositionAt( model.document.getRoot(), 0 ) ).to.be.instanceof( ModelPosition );
  590. } );
  591. } );
  592. describe( 'createPositionAfter()', () => {
  593. it( 'should return instance of Position', () => {
  594. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  595. setData( model, '<paragraph>fo[]ar</paragraph>' );
  596. expect( model.createPositionAfter( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  597. } );
  598. } );
  599. describe( 'createPositionBefore()', () => {
  600. it( 'should return instance of Position', () => {
  601. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  602. setData( model, '<paragraph>fo[]ar</paragraph>' );
  603. expect( model.createPositionBefore( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelPosition );
  604. } );
  605. } );
  606. describe( 'createRange()', () => {
  607. it( 'should return instance of Range', () => {
  608. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  609. setData( model, '<paragraph>fo[]ar</paragraph>' );
  610. expect( model.createRange( model.createPositionAt( model.document.getRoot(), 0 ) ) ).to.be.instanceof( ModelRange );
  611. } );
  612. } );
  613. describe( 'createRangeIn()', () => {
  614. it( 'should return instance of Range', () => {
  615. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  616. setData( model, '<paragraph>fo[]ar</paragraph>' );
  617. expect( model.createRangeIn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  618. } );
  619. } );
  620. describe( 'createRangeOn()', () => {
  621. it( 'should return instance of Range', () => {
  622. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  623. setData( model, '<paragraph>fo[]ar</paragraph>' );
  624. expect( model.createRangeOn( model.document.getRoot().getChild( 0 ) ) ).to.be.instanceof( ModelRange );
  625. } );
  626. } );
  627. describe( 'createSelection()', () => {
  628. it( 'should return instance of Selection', () => {
  629. expect( model.createSelection() ).to.be.instanceof( ModelSelection );
  630. } );
  631. } );
  632. describe( 'createBatch()', () => {
  633. it( 'should return instance of Batch', () => {
  634. const batch = model.createBatch();
  635. expect( batch ).to.be.instanceof( Batch );
  636. expect( batch.type ).to.equal( 'default' );
  637. } );
  638. it( 'should allow to define type of Batch', () => {
  639. const batch = model.createBatch( 'transparent' );
  640. expect( batch ).to.be.instanceof( Batch );
  641. expect( batch.type ).to.equal( 'transparent' );
  642. } );
  643. } );
  644. describe( 'createOperationFromJson()', () => {
  645. it( 'should create operation from JSON', () => {
  646. const operation = model.createOperationFromJSON( {
  647. __className: 'NoOperation',
  648. baseVersion: 0
  649. } );
  650. expect( operation ).to.instanceof( NoOperation );
  651. expect( operation.baseVersion ).to.equal( 0 );
  652. } );
  653. } );
  654. describe( 'destroy()', () => {
  655. it( 'should destroy document', () => {
  656. sinon.spy( model.document, 'destroy' );
  657. model.destroy();
  658. sinon.assert.calledOnce( model.document.destroy );
  659. } );
  660. it( 'should stop listening', () => {
  661. const emitter = Object.create( EmitterMixin );
  662. const spy = sinon.spy();
  663. model.listenTo( emitter, 'event', spy );
  664. model.destroy();
  665. emitter.fire( 'event' );
  666. sinon.assert.notCalled( spy );
  667. } );
  668. } );
  669. } );