model.js 24 KB

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