model.js 23 KB

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