8
0

model.js 28 KB

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