model.js 24 KB

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