model.js 24 KB

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