deletecontent.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  6. import Position from '../../../src/model/position';
  7. import Range from '../../../src/model/range';
  8. import Element from '../../../src/model/element';
  9. import deleteContent from '../../../src/model/utils/deletecontent';
  10. import { setData, getData } from '../../../src/dev-utils/model';
  11. describe( 'DataController utils', () => {
  12. let model, doc;
  13. describe( 'deleteContent', () => {
  14. it( 'should use parent batch', () => {
  15. model = new Model();
  16. doc = model.document;
  17. doc.createRoot();
  18. model.schema.extend( '$text', { allowIn: '$root' } );
  19. setData( model, 'x[abc]x' );
  20. model.change( writer => {
  21. deleteContent( model, doc.selection );
  22. expect( writer.batch.deltas ).to.length( 1 );
  23. } );
  24. } );
  25. describe( 'in simple scenarios', () => {
  26. beforeEach( () => {
  27. model = new Model();
  28. doc = model.document;
  29. doc.createRoot();
  30. const schema = model.schema;
  31. schema.register( 'image', { allowWhere: '$text' } );
  32. schema.extend( '$text', { allowIn: '$root' } );
  33. } );
  34. test(
  35. 'does nothing on collapsed selection',
  36. 'f[]oo',
  37. 'f[]oo'
  38. );
  39. test(
  40. 'deletes single character',
  41. 'f[o]o',
  42. 'f[]o'
  43. );
  44. it( 'deletes single character (backward selection)', () => {
  45. setData( model, 'f[o]o', { lastRangeBackward: true } );
  46. deleteContent( model, doc.selection );
  47. expect( getData( model ) ).to.equal( 'f[]o' );
  48. } );
  49. test(
  50. 'deletes whole text',
  51. '[foo]',
  52. '[]'
  53. );
  54. test(
  55. 'deletes whole text between nodes',
  56. '<image></image>[foo]<image></image>',
  57. '<image></image>[]<image></image>'
  58. );
  59. test(
  60. 'deletes an element',
  61. 'x[<image></image>]y',
  62. 'x[]y'
  63. );
  64. test(
  65. 'deletes a bunch of nodes',
  66. 'w[x<image></image>y]z',
  67. 'w[]z'
  68. );
  69. test(
  70. 'does not break things when option.merge passed',
  71. 'w[x<image></image>y]z',
  72. 'w[]z'
  73. );
  74. } );
  75. describe( 'with text attributes', () => {
  76. beforeEach( () => {
  77. model = new Model();
  78. doc = model.document;
  79. doc.createRoot();
  80. const schema = model.schema;
  81. schema.register( 'image', { allowWhere: '$text' } );
  82. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  83. schema.extend( '$text', {
  84. allowIn: '$root',
  85. allowAttributes: [ 'bold', 'italic' ]
  86. } );
  87. } );
  88. it( 'deletes characters (first half has attrs)', () => {
  89. setData( model, '<$text bold="true">fo[o</$text>b]ar' );
  90. deleteContent( model, doc.selection );
  91. expect( getData( model ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  92. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  93. } );
  94. it( 'deletes characters (2nd half has attrs)', () => {
  95. setData( model, 'fo[o<$text bold="true">b]ar</$text>' );
  96. deleteContent( model, doc.selection );
  97. expect( getData( model ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  98. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  99. } );
  100. it( 'clears selection attrs when emptied content', () => {
  101. setData( model, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  102. deleteContent( model, doc.selection );
  103. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  104. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  105. } );
  106. it( 'leaves selection attributes when text contains them', () => {
  107. setData(
  108. model,
  109. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  110. {
  111. selectionAttributes: {
  112. bold: true
  113. }
  114. }
  115. );
  116. deleteContent( model, doc.selection );
  117. expect( getData( model ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  118. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  119. } );
  120. } );
  121. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  122. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  123. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  124. // should not happen unless we're talking about lists or tables, but these features will need to cover
  125. // their scenarios themselves. In all generic scenarios elements are never nested.
  126. //
  127. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  128. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  129. // Those case should, again, be handled by their specific implementations.
  130. describe( 'in multi-element scenarios', () => {
  131. beforeEach( () => {
  132. model = new Model();
  133. doc = model.document;
  134. doc.createRoot();
  135. const schema = model.schema;
  136. schema.register( 'paragraph', {
  137. inheritAllFrom: '$block',
  138. allowIn: 'pparent',
  139. allowAttributes: 'align'
  140. } );
  141. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  142. schema.register( 'image', { inheritAllFrom: '$text' } );
  143. schema.register( 'pchild', { allowIn: 'paragraph' } );
  144. schema.register( 'pparent', { allowIn: '$root' } );
  145. schema.extend( '$text', { allowIn: [ 'pchild', 'pparent' ] } );
  146. } );
  147. test(
  148. 'do not merge when no need to',
  149. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  150. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>'
  151. );
  152. test(
  153. 'merges second element into the first one (same name)',
  154. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  155. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
  156. );
  157. test(
  158. 'does not merge second element into the first one (same name, !option.merge)',
  159. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  160. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  161. { leaveUnmerged: true }
  162. );
  163. test(
  164. 'merges second element into the first one (different name)',
  165. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  166. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
  167. );
  168. // Note: in all these cases we ignore the direction of merge.
  169. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  170. // forward and backward delete.
  171. it( 'merges second element into the first one (different name, backward selection)', () => {
  172. setData(
  173. model,
  174. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  175. { lastRangeBackward: true }
  176. );
  177. deleteContent( model, doc.selection );
  178. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  179. } );
  180. test(
  181. 'merges second element into the first one (different attrs)',
  182. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  183. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
  184. );
  185. test(
  186. 'merges second element to an empty first element',
  187. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  188. '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>'
  189. );
  190. test(
  191. 'merges empty element into the first element',
  192. '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
  193. '<heading1>f[]</heading1><paragraph>x</paragraph>'
  194. );
  195. test(
  196. 'leaves just one element when all selected',
  197. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  198. '<heading1>[]bar</heading1>'
  199. );
  200. it( 'uses merge delta even if merged element is empty', () => {
  201. let mergeSpy;
  202. setData( model, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  203. model.change( writer => {
  204. mergeSpy = sinon.spy( writer, 'merge' );
  205. deleteContent( model, doc.selection );
  206. } );
  207. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  208. expect( mergeSpy.called ).to.be.true;
  209. } );
  210. it( 'uses merge delta even if merged element is empty #2', () => {
  211. let mergeSpy;
  212. setData( model, '<paragraph>ab[</paragraph><paragraph>]</paragraph>' );
  213. model.change( writer => {
  214. mergeSpy = sinon.spy( writer, 'merge' );
  215. deleteContent( model, doc.selection );
  216. } );
  217. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  218. expect( mergeSpy.called ).to.be.true;
  219. } );
  220. it( 'does not try to move the second block if not needed', () => {
  221. let mergeSpy, moveSpy;
  222. setData( model, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
  223. model.change( writer => {
  224. mergeSpy = sinon.spy( writer, 'merge' );
  225. moveSpy = sinon.spy( writer, 'move' );
  226. deleteContent( model, doc.selection );
  227. } );
  228. expect( getData( model ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
  229. expect( moveSpy.called ).to.be.false;
  230. expect( mergeSpy.called ).to.be.true;
  231. } );
  232. // Note: in all these cases we ignore the direction of merge.
  233. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  234. // forward and backward delete.
  235. describe( 'with nested elements', () => {
  236. test(
  237. 'merges elements when deep nested',
  238. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  239. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
  240. );
  241. it( 'merges elements when deep nested (3rd level)', () => {
  242. const root = doc.getRoot();
  243. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  244. // <pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>
  245. // <pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>
  246. root.appendChildren(
  247. new Element( 'pparent', null, [
  248. 'x',
  249. new Element( 'paragraph', null, [
  250. 'x',
  251. new Element( 'pchild', null, 'foo' )
  252. ] )
  253. ] )
  254. );
  255. root.appendChildren(
  256. new Element( 'pparent', null, [
  257. new Element( 'paragraph', null, [
  258. new Element( 'pchild', null, 'bar' ),
  259. 'y'
  260. ] ),
  261. 'y'
  262. ] )
  263. );
  264. const range = new Range(
  265. new Position( doc.getRoot(), [ 0, 1, 1, 2 ] ), // fo[o
  266. new Position( doc.getRoot(), [ 1, 0, 0, 1 ] ) // b]ar
  267. );
  268. model.change( writer => {
  269. writer.setSelection( range );
  270. } );
  271. deleteContent( model, doc.selection );
  272. expect( getData( model ) )
  273. .to.equal( '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>' );
  274. } );
  275. test(
  276. 'merges elements when left end deep nested',
  277. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
  278. '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
  279. );
  280. test(
  281. 'merges elements when right end deep nested',
  282. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
  283. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
  284. );
  285. it( 'merges elements when left end deep nested (3rd level)', () => {
  286. const root = doc.getRoot();
  287. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  288. // <pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent><paragraph>b]om</paragraph>
  289. root.appendChildren(
  290. new Element( 'pparent', null, [
  291. 'x',
  292. new Element( 'paragraph', null, [
  293. 'foo',
  294. new Element( 'pchild', null, 'bar' )
  295. ] )
  296. ] )
  297. );
  298. root.appendChildren(
  299. new Element( 'paragraph', null, 'bom' )
  300. );
  301. const range = new Range(
  302. new Position( doc.getRoot(), [ 0, 1, 3, 2 ] ), // ba[r
  303. new Position( doc.getRoot(), [ 1, 1 ] ) // b]om
  304. );
  305. model.change( writer => {
  306. writer.setSelection( range );
  307. } );
  308. deleteContent( model, doc.selection );
  309. expect( getData( model ) )
  310. .to.equal( '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>' );
  311. } );
  312. test(
  313. 'merges elements when right end deep nested (in an empty container)',
  314. '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
  315. '<paragraph>fo[]</paragraph>'
  316. );
  317. test(
  318. 'merges elements when left end deep nested (in an empty container)',
  319. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  320. '<paragraph><pchild>[]ar</pchild></paragraph><paragraph>x</paragraph>'
  321. );
  322. it( 'merges elements when left end deep nested (3rd level)', () => {
  323. const root = doc.getRoot();
  324. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  325. // <paragraph>fo[o</paragraph><pparent><paragraph><pchild>bar]</pchild></paragraph></pparent>
  326. root.appendChildren(
  327. new Element( 'paragraph', null, 'foo' )
  328. );
  329. root.appendChildren(
  330. new Element( 'pparent', null, [
  331. new Element( 'paragraph', null, [
  332. new Element( 'pchild', null, 'bar' )
  333. ] )
  334. ] )
  335. );
  336. const range = new Range(
  337. new Position( doc.getRoot(), [ 0, 2 ] ), // f[oo
  338. new Position( doc.getRoot(), [ 1, 0, 0, 3 ] ) // bar]
  339. );
  340. model.change( writer => {
  341. writer.setSelection( range );
  342. } );
  343. deleteContent( model, doc.selection );
  344. expect( getData( model ) )
  345. .to.equal( '<paragraph>fo[]</paragraph>' );
  346. } );
  347. } );
  348. describe( 'with object elements', () => {
  349. beforeEach( () => {
  350. const schema = model.schema;
  351. schema.register( 'blockWidget', {
  352. isObject: true
  353. } );
  354. schema.register( 'nestedEditable', {
  355. isLimit: true
  356. } );
  357. schema.extend( 'blockWidget', { allowIn: '$root' } );
  358. schema.extend( 'nestedEditable', { allowIn: 'blockWidget' } );
  359. schema.extend( '$text', { allowIn: 'nestedEditable' } );
  360. } );
  361. test(
  362. 'does not merge an object element (if it is first)',
  363. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  364. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
  365. );
  366. test(
  367. 'does not merge an object element (if it is second)',
  368. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  369. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
  370. );
  371. } );
  372. describe( 'filtering out', () => {
  373. beforeEach( () => {
  374. const schema = model.schema;
  375. schema.addAttributeCheck( ( ctx, attributeName ) => {
  376. // Disallow 'c' on pchild>pchild>$text.
  377. if ( ctx.endsWith( 'pchild pchild $text' ) && attributeName == 'c' ) {
  378. return false;
  379. }
  380. // Allow 'a' and 'b' on paragraph>$text.
  381. if ( ctx.endsWith( 'paragraph $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
  382. return true;
  383. }
  384. // Allow 'b' and 'c' in pchild>$text.
  385. if ( ctx.endsWith( 'pchild $text' ) && [ 'b', 'c' ].includes( attributeName ) ) {
  386. return true;
  387. }
  388. } );
  389. schema.extend( 'pchild', { allowIn: 'pchild' } );
  390. } );
  391. test(
  392. 'filters out disallowed attributes after left merge',
  393. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text a="1" b="1">z</$text></paragraph>',
  394. '<paragraph>x<pchild>fo[]<$text b="1">z</$text></pchild></paragraph>'
  395. );
  396. test(
  397. 'filters out disallowed attributes from nested nodes after left merge',
  398. '<paragraph>' +
  399. 'x' +
  400. '<pchild>fo[o</pchild>' +
  401. '</paragraph>' +
  402. '<paragraph>' +
  403. 'b]a<$text a="1" b="1">r</$text>' +
  404. '<pchild>b<$text b="1" c="1">i</$text>z</pchild>' +
  405. 'y' +
  406. '</paragraph>',
  407. '<paragraph>' +
  408. 'x' +
  409. '<pchild>' +
  410. 'fo[]a<$text b="1">r</$text>' +
  411. '<pchild>b<$text b="1">i</$text>z</pchild>' +
  412. 'y' +
  413. '</pchild>' +
  414. '</paragraph>'
  415. );
  416. test(
  417. 'filters out disallowed attributes after right merge',
  418. '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text b="1" c="1">y]z</$text></pchild></paragraph>',
  419. '<paragraph>fo[]<$text b="1">z</$text></paragraph>'
  420. );
  421. } );
  422. } );
  423. describe( 'in element selections scenarios', () => {
  424. beforeEach( () => {
  425. model = new Model();
  426. doc = model.document;
  427. // <p> like root.
  428. doc.createRoot( 'paragraph', 'paragraphRoot' );
  429. // <body> like root.
  430. doc.createRoot( '$root', 'bodyRoot' );
  431. // Special root which allows only blockWidgets inside itself.
  432. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  433. const schema = model.schema;
  434. schema.register( 'image', { allowWhere: '$text' } );
  435. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  436. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  437. schema.register( 'blockWidget' );
  438. schema.register( 'restrictedRoot', {
  439. isLimit: true
  440. } );
  441. schema.extend( '$block', { allowIn: '$root' } );
  442. schema.extend( 'blockWidget', { allowIn: '$root' } );
  443. schema.extend( 'blockWidget', { allowIn: 'restrictedRoot' } );
  444. } );
  445. // See also "in simple scenarios => deletes an element".
  446. it( 'deletes two inline elements', () => {
  447. model.schema.extend( 'paragraph', {
  448. isLimit: true
  449. } );
  450. setData(
  451. model,
  452. 'x[<image></image><image></image>]z',
  453. { rootName: 'paragraphRoot' }
  454. );
  455. deleteContent( model, doc.selection );
  456. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  457. .to.equal( 'x[]z' );
  458. } );
  459. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  460. setData(
  461. model,
  462. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  463. { rootName: 'bodyRoot' }
  464. );
  465. deleteContent( model, doc.selection );
  466. expect( getData( model, { rootName: 'bodyRoot' } ) )
  467. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  468. } );
  469. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  470. setData(
  471. model,
  472. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  473. { rootName: 'bodyRoot' }
  474. );
  475. deleteContent( model, doc.selection );
  476. expect( getData( model, { rootName: 'bodyRoot' } ) )
  477. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  478. } );
  479. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  480. setData(
  481. model,
  482. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  483. { rootName: 'bodyRoot' }
  484. );
  485. deleteContent( model, doc.selection );
  486. expect( getData( model, { rootName: 'bodyRoot' } ) )
  487. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  488. } );
  489. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  490. setData(
  491. model,
  492. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  493. { rootName: 'bodyRoot' }
  494. );
  495. deleteContent( model, doc.selection );
  496. expect( getData( model, { rootName: 'bodyRoot' } ) )
  497. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  498. } );
  499. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  500. setData(
  501. model,
  502. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  503. { rootName: 'bodyRoot' }
  504. );
  505. deleteContent( model, doc.selection );
  506. expect( getData( model, { rootName: 'bodyRoot' } ) )
  507. .to.equal( '<paragraph>[]</paragraph>' );
  508. } );
  509. it( 'does not create a paragraph when it is not allowed', () => {
  510. setData(
  511. model,
  512. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  513. { rootName: 'restrictedRoot' }
  514. );
  515. deleteContent( model, doc.selection );
  516. expect( getData( model, { rootName: 'restrictedRoot' } ) )
  517. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  518. } );
  519. } );
  520. describe( 'integration with inline limit elements', () => {
  521. beforeEach( () => {
  522. model = new Model();
  523. doc = model.document;
  524. doc.createRoot();
  525. const schema = model.schema;
  526. schema.register( 'inlineLimit', {
  527. isLimit: true,
  528. allowIn: '$root'
  529. } );
  530. schema.extend( '$text', {
  531. allowIn: [ 'inlineLimit', '$root', 'x' ]
  532. } );
  533. schema.register( 'x', { allowIn: '$root' } );
  534. } );
  535. test(
  536. 'should delete inside inline limit element',
  537. '<inlineLimit>foo [bar] baz</inlineLimit>',
  538. '<inlineLimit>foo [] baz</inlineLimit>'
  539. );
  540. test(
  541. 'should delete whole inline limit element',
  542. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  543. 'x[]x'
  544. );
  545. test(
  546. 'should delete from two inline limit elements',
  547. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  548. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  549. );
  550. test(
  551. 'merge option should be ignored if both elements are limits',
  552. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  553. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  554. );
  555. test(
  556. 'merge option should be ignored if the first element is a limit',
  557. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  558. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  559. );
  560. test(
  561. 'merge option should be ignored if the second element is a limit',
  562. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  563. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  564. );
  565. } );
  566. describe( 'integration with block limit elements', () => {
  567. beforeEach( () => {
  568. model = new Model();
  569. doc = model.document;
  570. doc.createRoot();
  571. const schema = model.schema;
  572. schema.register( 'blockLimit', {
  573. isLimit: true
  574. } );
  575. schema.extend( 'blockLimit', { allowIn: '$root' } );
  576. schema.extend( '$block', { allowIn: 'blockLimit' } );
  577. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  578. } );
  579. test(
  580. 'should delete inside block limit element',
  581. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  582. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  583. { leaveUnmerged: true }
  584. );
  585. test(
  586. 'should delete inside block limit element (with merge)',
  587. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  588. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  589. );
  590. test(
  591. 'should delete whole block limit element',
  592. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  593. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  594. );
  595. test(
  596. 'should delete from two block limit elements',
  597. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  598. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  599. );
  600. test(
  601. 'merge option should be ignored if any of the elements is a limit',
  602. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  603. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  604. );
  605. } );
  606. describe( 'should leave a paragraph if the entire content was selected', () => {
  607. beforeEach( () => {
  608. model = new Model();
  609. doc = model.document;
  610. doc.createRoot();
  611. const schema = model.schema;
  612. schema.register( 'div', {
  613. inheritAllFrom: '$block',
  614. isLimit: true
  615. } );
  616. schema.register( 'article', {
  617. inheritAllFrom: '$block',
  618. isLimit: true
  619. } );
  620. schema.register( 'image', {
  621. allowWhere: '$text',
  622. isObject: true
  623. } );
  624. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  625. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  626. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  627. schema.extend( '$text', { allowIn: '$root' } );
  628. schema.extend( 'image', { allowIn: '$root' } );
  629. schema.extend( 'image', { allowIn: 'heading1' } );
  630. schema.extend( 'heading1', { allowIn: 'div' } );
  631. schema.extend( 'paragraph', { allowIn: 'div' } );
  632. schema.extend( 'heading1', { allowIn: 'article' } );
  633. schema.extend( 'heading2', { allowIn: 'article' } );
  634. } );
  635. test(
  636. 'but not if only one block was selected',
  637. '<heading1>[xx]</heading1>',
  638. '<heading1>[]</heading1>'
  639. );
  640. test(
  641. 'when the entire heading and paragraph were selected',
  642. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  643. '<paragraph>[]</paragraph>'
  644. );
  645. test(
  646. 'when the entire content was selected',
  647. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  648. '<paragraph>[]</paragraph>'
  649. );
  650. test(
  651. 'inside the limit element when the entire heading and paragraph were inside',
  652. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  653. '<div><paragraph>[]</paragraph></div>'
  654. );
  655. test(
  656. 'but not if schema does not accept paragraph in limit element',
  657. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  658. '<article><heading1>[]</heading1></article>'
  659. );
  660. test(
  661. 'but not if selection is not containing the whole content',
  662. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  663. '<image></image><heading1>[]</heading1>'
  664. );
  665. test(
  666. 'but not if only single element is selected',
  667. '<heading1>[<image></image>xx]</heading1>',
  668. '<heading1>[]</heading1>'
  669. );
  670. it( 'when root element was not added as Schema limits work fine as well', () => {
  671. doc.createRoot( 'paragraph', 'paragraphRoot' );
  672. setData(
  673. model,
  674. 'x[<image></image><image></image>]z',
  675. { rootName: 'paragraphRoot' }
  676. );
  677. deleteContent( model, doc.selection );
  678. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  679. .to.equal( 'x[]z' );
  680. } );
  681. test(
  682. 'but not if the flag "doNotResetEntireContent" is set to true',
  683. '<heading1>[</heading1><paragraph>]</paragraph>',
  684. '<heading1>[]</heading1>',
  685. {
  686. doNotResetEntireContent: true
  687. }
  688. );
  689. } );
  690. function test( title, input, output, options ) {
  691. it( title, () => {
  692. setData( model, input );
  693. deleteContent( model, doc.selection, options );
  694. expect( getData( model ) ).to.equal( output );
  695. } );
  696. }
  697. } );
  698. } );