deletecontent.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../../src/model/model';
  6. import Position from '../../../src/model/position';
  7. import Range from '../../../src/model/range';
  8. import Selection from '../../../src/model/selection';
  9. import Element from '../../../src/model/element';
  10. import deleteContent from '../../../src/model/utils/deletecontent';
  11. import { setData, getData } from '../../../src/dev-utils/model';
  12. import { stringify } from '../../../src/dev-utils/view';
  13. describe( 'DataController utils', () => {
  14. let model, doc;
  15. describe( 'deleteContent', () => {
  16. it( 'should use parent batch', () => {
  17. model = new Model();
  18. doc = model.document;
  19. doc.createRoot();
  20. model.schema.extend( '$text', { allowIn: '$root' } );
  21. setData( model, 'x[abc]x' );
  22. model.change( writer => {
  23. deleteContent( model, doc.selection );
  24. expect( writer.batch.operations ).to.length( 1 );
  25. } );
  26. } );
  27. it( 'should not do anything if the selection is already in graveyard', () => {
  28. model = new Model();
  29. doc = model.document;
  30. const gy = model.document.graveyard;
  31. gy._appendChild( new Element( 'paragraph' ) );
  32. const baseVersion = model.document.baseVersion;
  33. model.change( writer => {
  34. sinon.spy( writer, 'remove' );
  35. const selection = writer.createSelection( writer.createRangeIn( gy ) );
  36. deleteContent( model, selection );
  37. expect( writer.remove.called ).to.be.false;
  38. } );
  39. expect( model.document.baseVersion ).to.equal( baseVersion );
  40. } );
  41. describe( 'in simple scenarios', () => {
  42. beforeEach( () => {
  43. model = new Model();
  44. doc = model.document;
  45. doc.createRoot();
  46. const schema = model.schema;
  47. schema.register( 'image', { allowWhere: '$text' } );
  48. schema.extend( '$text', { allowIn: '$root' } );
  49. } );
  50. it( 'should be able to delete content at custom selection', () => {
  51. setData( model, 'a[]bcd' );
  52. const range = new Range(
  53. new Position( doc.getRoot(), [ 2 ] ),
  54. new Position( doc.getRoot(), [ 3 ] )
  55. );
  56. const selection = new Selection( [ range ] );
  57. model.change( () => {
  58. deleteContent( model, selection );
  59. expect( getData( model ) ).to.equal( 'a[]bd' );
  60. } );
  61. } );
  62. test(
  63. 'does nothing on collapsed selection',
  64. 'f[]oo',
  65. 'f[]oo'
  66. );
  67. test(
  68. 'deletes single character',
  69. 'f[o]o',
  70. 'f[]o'
  71. );
  72. it( 'deletes single character (backward selection)', () => {
  73. setData( model, 'f[o]o', { lastRangeBackward: true } );
  74. deleteContent( model, doc.selection );
  75. expect( getData( model ) ).to.equal( 'f[]o' );
  76. } );
  77. test(
  78. 'deletes whole text',
  79. '[foo]',
  80. '[]'
  81. );
  82. test(
  83. 'deletes whole text between nodes',
  84. '<image></image>[foo]<image></image>',
  85. '<image></image>[]<image></image>'
  86. );
  87. test(
  88. 'deletes an element',
  89. 'x[<image></image>]y',
  90. 'x[]y'
  91. );
  92. test(
  93. 'deletes a bunch of nodes',
  94. 'w[x<image></image>y]z',
  95. 'w[]z'
  96. );
  97. test(
  98. 'does not break things when option.merge passed',
  99. 'w[x<image></image>y]z',
  100. 'w[]z'
  101. );
  102. } );
  103. describe( 'with text attributes', () => {
  104. beforeEach( () => {
  105. model = new Model();
  106. doc = model.document;
  107. doc.createRoot();
  108. const schema = model.schema;
  109. schema.register( 'image', { allowWhere: '$text' } );
  110. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  111. schema.extend( '$text', {
  112. allowIn: '$root',
  113. allowAttributes: [ 'bold', 'italic' ]
  114. } );
  115. } );
  116. it( 'deletes characters (first half has attrs)', () => {
  117. setData( model, '<$text bold="true">fo[o</$text>b]ar' );
  118. deleteContent( model, doc.selection );
  119. expect( getData( model ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  120. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  121. } );
  122. it( 'deletes characters (2nd half has attrs)', () => {
  123. setData( model, 'fo[o<$text bold="true">b]ar</$text>' );
  124. deleteContent( model, doc.selection );
  125. expect( getData( model ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  126. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  127. } );
  128. it( 'clears selection attrs when emptied content', () => {
  129. setData( model, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  130. deleteContent( model, doc.selection );
  131. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  132. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  133. } );
  134. it( 'leaves selection attributes when text contains them', () => {
  135. setData(
  136. model,
  137. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  138. {
  139. selectionAttributes: {
  140. bold: true
  141. }
  142. }
  143. );
  144. deleteContent( model, doc.selection );
  145. expect( getData( model ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  146. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  147. } );
  148. } );
  149. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  150. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  151. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  152. // should not happen unless we're talking about lists or tables, but these features will need to cover
  153. // their scenarios themselves. In all generic scenarios elements are never nested.
  154. //
  155. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  156. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  157. // Those case should, again, be handled by their specific implementations.
  158. describe( 'in multi-element scenarios', () => {
  159. beforeEach( () => {
  160. model = new Model();
  161. doc = model.document;
  162. doc.createRoot();
  163. const schema = model.schema;
  164. schema.register( 'paragraph', {
  165. inheritAllFrom: '$block',
  166. allowIn: 'pparent',
  167. allowAttributes: 'align'
  168. } );
  169. schema.register( 'heading1', { inheritAllFrom: '$block', allowIn: 'pparent' } );
  170. schema.register( 'image', { inheritAllFrom: '$text' } );
  171. schema.register( 'pchild', { allowIn: 'paragraph' } );
  172. schema.register( 'pparent', { allowIn: '$root' } );
  173. schema.register( 'hchild', { allowIn: 'heading1' } );
  174. schema.register( 'widget', { isObject: true, allowWhere: '$text', isInline: true } );
  175. schema.extend( '$text', { allowIn: [ 'pchild', 'pparent', 'hchild' ] } );
  176. } );
  177. test(
  178. 'do not merge when no need to',
  179. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  180. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>'
  181. );
  182. test(
  183. 'merges second element into the first one (same name)',
  184. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  185. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
  186. );
  187. test(
  188. 'merges first element into the second element (it would become empty but second element would not) (same name)',
  189. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  190. '<paragraph>x</paragraph><paragraph>[]ar</paragraph><paragraph>y</paragraph>'
  191. );
  192. test(
  193. 'do not remove end block if selection ends at start position of it',
  194. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph>]bar</paragraph><paragraph>y</paragraph>',
  195. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>bar</paragraph><paragraph>y</paragraph>'
  196. );
  197. test(
  198. 'do not remove end block if selection ends at start position of it (multiple paragraphs)',
  199. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph>a</paragraph><paragraph>]bar</paragraph>',
  200. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>bar</paragraph>'
  201. );
  202. test(
  203. 'removes empty element (merges it into second element)',
  204. '<paragraph>x</paragraph><paragraph>[</paragraph><paragraph>]bar</paragraph><paragraph>y</paragraph>',
  205. '<paragraph>x</paragraph><paragraph>[]bar</paragraph><paragraph>y</paragraph>'
  206. );
  207. test(
  208. 'treats inline widget elements as content so parent element is not considered as empty after merging (same name)',
  209. '<paragraph>x</paragraph><paragraph><widget></widget>[foo</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  210. '<paragraph>x</paragraph><paragraph><widget></widget>[]ar</paragraph><paragraph>y</paragraph>'
  211. );
  212. test(
  213. 'does not merge second element into the first one (same name, !option.merge)',
  214. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  215. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  216. { leaveUnmerged: true }
  217. );
  218. test(
  219. 'does not remove first empty element when it\'s empty but second element is not empty (same name, !option.merge)',
  220. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  221. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  222. { leaveUnmerged: true }
  223. );
  224. test(
  225. 'merges second element into the first one (different name)',
  226. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  227. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
  228. );
  229. test(
  230. 'removes first element when it\'s empty but second element is not empty (different name)',
  231. '<paragraph>x</paragraph><heading1>[foo</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  232. '<paragraph>x</paragraph><paragraph>[]ar</paragraph><paragraph>y</paragraph>'
  233. );
  234. // Note: in all these cases we ignore the direction of merge.
  235. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  236. // forward and backward delete.
  237. it( 'merges second element into the first one (different name, backward selection)', () => {
  238. setData(
  239. model,
  240. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  241. { lastRangeBackward: true }
  242. );
  243. deleteContent( model, doc.selection );
  244. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  245. } );
  246. test(
  247. 'merges second element into the first one (different attrs)',
  248. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  249. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
  250. );
  251. test(
  252. 'removes empty first element',
  253. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  254. '<paragraph>x</paragraph><paragraph>[]o</paragraph><paragraph>y</paragraph>'
  255. );
  256. test(
  257. 'merges empty element into the first element',
  258. '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
  259. '<heading1>f[]</heading1><paragraph>x</paragraph>'
  260. );
  261. test(
  262. 'leaves just one element when all selected',
  263. '<paragraph>[x</paragraph><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  264. '<paragraph>[]bar</paragraph>'
  265. );
  266. test(
  267. 'leaves just one (last) element when all selected (first block would become empty) (different name)',
  268. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  269. '<paragraph>[]bar</paragraph>'
  270. );
  271. test(
  272. 'leaves just one (first) element when all selected (first block would not become empty) (different name)',
  273. '<heading1>foo[x</heading1><paragraph>bar</paragraph><paragraph>y]</paragraph>',
  274. '<heading1>foo[]</heading1>'
  275. );
  276. it( 'uses merge operation even if merged element is empty', () => {
  277. let mergeSpy;
  278. setData( model, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  279. model.change( writer => {
  280. mergeSpy = sinon.spy( writer, 'merge' );
  281. deleteContent( model, doc.selection );
  282. } );
  283. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  284. expect( mergeSpy.called ).to.be.true;
  285. } );
  286. it( 'uses merge operation even if merged element is empty #2', () => {
  287. let mergeSpy;
  288. setData( model, '<paragraph>ab[</paragraph><paragraph>]</paragraph>' );
  289. model.change( writer => {
  290. mergeSpy = sinon.spy( writer, 'merge' );
  291. deleteContent( model, doc.selection );
  292. } );
  293. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  294. expect( mergeSpy.called ).to.be.true;
  295. } );
  296. it( 'uses "merge" operation (from OT) if first element is empty (because of content delete) and last is not', () => {
  297. let mergeSpy;
  298. setData( model, '<paragraph>[abcd</paragraph><paragraph>ef]gh</paragraph>' );
  299. model.change( writer => {
  300. mergeSpy = sinon.spy( writer, 'merge' );
  301. deleteContent( model, doc.selection );
  302. } );
  303. expect( getData( model ) ).to.equal( '<paragraph>[]gh</paragraph>' );
  304. expect( mergeSpy.called ).to.be.true;
  305. } );
  306. it( 'uses merge operation if first element is empty and last is not', () => {
  307. let mergeSpy;
  308. setData( model, '<paragraph>[</paragraph><paragraph>ef]gh</paragraph>' );
  309. model.change( writer => {
  310. mergeSpy = sinon.spy( writer, 'merge' );
  311. deleteContent( model, doc.selection );
  312. } );
  313. expect( getData( model ) ).to.equal( '<paragraph>[]gh</paragraph>' );
  314. expect( mergeSpy.called ).to.be.true;
  315. } );
  316. it( 'does not try to move the second block if not needed', () => {
  317. let mergeSpy, moveSpy;
  318. setData( model, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
  319. model.change( writer => {
  320. mergeSpy = sinon.spy( writer, 'merge' );
  321. moveSpy = sinon.spy( writer, 'move' );
  322. deleteContent( model, doc.selection );
  323. } );
  324. expect( getData( model ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
  325. expect( moveSpy.called ).to.be.false;
  326. expect( mergeSpy.called ).to.be.true;
  327. } );
  328. // Note: in all these cases we ignore the direction of merge.
  329. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  330. // forward and backward delete.
  331. describe( 'with nested elements', () => {
  332. test(
  333. 'merges elements when deep nested',
  334. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  335. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
  336. );
  337. test(
  338. 'merges block element to the right (with nested element)',
  339. '<paragraph><pchild>[foo</pchild></paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  340. '<paragraph><pchild>[]ar</pchild></paragraph>'
  341. );
  342. test(
  343. 'does not remove block element with nested element and object',
  344. '<paragraph><pchild><widget></widget>[foo</pchild></paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  345. '<paragraph><pchild><widget></widget>[]ar</pchild></paragraph>'
  346. );
  347. test(
  348. 'merges nested elements',
  349. '<heading1><hchild>x[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  350. '<heading1><hchild>x[]ar</hchild></heading1>'
  351. );
  352. test(
  353. 'merges nested elements on multiple levels',
  354. '<heading1><hchild>x[foo</hchild></heading1><paragraph><pchild>b]ar</pchild>abc</paragraph>',
  355. '<heading1><hchild>x[]ar</hchild>abc</heading1>'
  356. );
  357. test(
  358. 'merges nested elements to the right if left side element would become empty',
  359. '<heading1><hchild>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  360. '<paragraph><pchild>[]ar</pchild></paragraph>'
  361. );
  362. test(
  363. 'merges to the left if first element contains object (considers it as a content of that element)',
  364. '<heading1><hchild><widget></widget>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  365. '<heading1><hchild><widget></widget>[]ar</hchild></heading1>'
  366. );
  367. test(
  368. 'merges elements when left end deep nested',
  369. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
  370. '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
  371. );
  372. test(
  373. 'merges nested elements to the right (on multiple levels) if left side element would become empty',
  374. '<heading1><hchild>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild>abc</paragraph>',
  375. '<paragraph><pchild>[]ar</pchild>abc</paragraph>'
  376. );
  377. test(
  378. 'merges to the right element when left end deep nested and will be empty',
  379. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  380. '<paragraph>[]ar</paragraph><paragraph>x</paragraph>'
  381. );
  382. test(
  383. 'merges elements when right end deep nested',
  384. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
  385. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
  386. );
  387. test(
  388. 'removes element when right end deep nested but left end would be empty',
  389. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  390. '<paragraph>x</paragraph><paragraph><pchild>[]ar</pchild></paragraph>'
  391. );
  392. test(
  393. 'merges elements when right end deep nested (in an empty container)',
  394. '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
  395. '<paragraph>fo[]</paragraph>'
  396. );
  397. test(
  398. 'removes elements when left end deep nested (in an empty container)',
  399. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  400. '<paragraph>[]ar</paragraph><paragraph>x</paragraph>'
  401. );
  402. describe( 'with 3rd level of nesting', () => {
  403. test(
  404. 'merges elements when deep nested (same name)',
  405. '<pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>' +
  406. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  407. '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>'
  408. );
  409. test(
  410. 'removes elements when deep nested (same name)',
  411. '<pparent><paragraph><pchild>[foo</pchild></paragraph></pparent>' +
  412. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  413. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  414. );
  415. test(
  416. 'removes elements up to common ancestor when deep nested (same name)',
  417. '<pparent>' +
  418. '<paragraph><pchild>[foo</pchild></paragraph>' +
  419. '<paragraph><pchild>b]ar</pchild>y</paragraph>y' +
  420. '</pparent>',
  421. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  422. );
  423. test(
  424. 'merges elements when deep nested (different name)',
  425. '<pparent>x<heading1>x<hchild>fo[o</hchild></heading1></pparent>' +
  426. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  427. '<pparent>x<heading1>x<hchild>fo[]ar</hchild>y</heading1>y</pparent>'
  428. );
  429. test(
  430. 'removes elements when deep nested (different name)',
  431. '<pparent><heading1><hchild>[foo</hchild></heading1></pparent>' +
  432. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  433. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  434. );
  435. test(
  436. 'merges elements up to common ancestor when deep nested (different names)',
  437. '<pparent>' +
  438. '<heading1><hchild>fo[o</hchild></heading1>' +
  439. '<paragraph><pchild>b]ar</pchild></paragraph>' +
  440. '</pparent>',
  441. '<pparent><heading1><hchild>fo[]ar</hchild></heading1></pparent>'
  442. );
  443. test(
  444. 'removes elements up to common ancestor when deep nested (different names)',
  445. '<pparent>' +
  446. '<heading1><hchild>[foo</hchild></heading1>' +
  447. '<paragraph><pchild>b]ar</pchild>y</paragraph>y' +
  448. '</pparent>',
  449. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  450. );
  451. } );
  452. describe( 'with 3rd level of nesting o the left end', () => {
  453. test(
  454. 'merges elements',
  455. '<pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent>' +
  456. '<paragraph>b]om</paragraph>',
  457. '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>'
  458. );
  459. test(
  460. 'merges elements (different names)',
  461. '<pparent>x<heading1>foo<hchild>ba[r</hchild></heading1></pparent>' +
  462. '<paragraph>b]om</paragraph>',
  463. '<pparent>x<heading1>foo<hchild>ba[]om</hchild></heading1></pparent>'
  464. );
  465. test(
  466. 'removes elements',
  467. '<pparent><paragraph><pchild>[bar</pchild></paragraph></pparent>' +
  468. '<paragraph>b]om</paragraph>',
  469. '<paragraph>[]om</paragraph>'
  470. );
  471. test(
  472. 'removes elements up to common ancestor (different names)',
  473. '<pparent>' +
  474. '<heading1><hchild>[foo</hchild></heading1>' +
  475. '<paragraph>b]ar</paragraph>y' +
  476. '</pparent>',
  477. '<pparent><paragraph>[]ar</paragraph>y</pparent>'
  478. );
  479. } );
  480. describe( 'with 3rd level of nesting o the right end', () => {
  481. test(
  482. 'merges elements',
  483. '<paragraph>b[om</paragraph>' +
  484. '<pparent><paragraph><pchild>ba]r</pchild></paragraph></pparent>',
  485. '<paragraph>b[]r</paragraph>'
  486. );
  487. test(
  488. 'merges elements (different names)',
  489. '<paragraph>bo[m</paragraph>' +
  490. '<pparent><heading1><hchild>b]ar</hchild></heading1></pparent>',
  491. '<paragraph>bo[]ar</paragraph>'
  492. );
  493. test(
  494. 'merges elements (different names, reversed)',
  495. '<heading1>bo[m</heading1>' +
  496. '<pparent><paragraph><pchild>b]ar</pchild></paragraph></pparent>',
  497. '<heading1>bo[]ar</heading1>'
  498. );
  499. test(
  500. 'removes elements',
  501. '<paragraph>[bom</paragraph>' +
  502. '<pparent><paragraph><pchild>b]ar</pchild></paragraph></pparent>',
  503. '<pparent><paragraph><pchild>[]ar</pchild></paragraph></pparent>'
  504. );
  505. test(
  506. 'removes elements up to common ancestor (different names)',
  507. '<pparent>' +
  508. '<heading1>[bar</heading1>y' +
  509. '<paragraph><pchild>f]oo</pchild></paragraph>' +
  510. '</pparent>',
  511. '<pparent><paragraph><pchild>[]oo</pchild></paragraph></pparent>'
  512. );
  513. } );
  514. } );
  515. describe( 'with object elements', () => {
  516. beforeEach( () => {
  517. const schema = model.schema;
  518. schema.register( 'blockWidget', {
  519. isObject: true
  520. } );
  521. schema.register( 'nestedEditable', {
  522. isLimit: true
  523. } );
  524. schema.extend( 'blockWidget', { allowIn: '$root' } );
  525. schema.extend( 'nestedEditable', { allowIn: 'blockWidget' } );
  526. schema.extend( '$text', { allowIn: 'nestedEditable' } );
  527. } );
  528. test(
  529. 'does not merge an object element (if it is first)',
  530. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  531. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
  532. );
  533. test(
  534. 'does not merge an object element (if it is second)',
  535. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  536. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
  537. );
  538. } );
  539. describe( 'with markers', () => {
  540. it( 'should merge left if the first element is not empty', () => {
  541. setData( model, '<heading1>foo[</heading1><paragraph>]bar</paragraph>' );
  542. model.enqueueChange( 'transparent', writer => {
  543. const root = doc.getRoot( );
  544. const range = writer.createRange(
  545. writer.createPositionFromPath( root, [ 0, 3 ] ),
  546. writer.createPositionFromPath( root, [ 1, 0 ] )
  547. );
  548. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  549. } );
  550. deleteContent( model, doc.selection );
  551. expect( getData( model ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  552. } );
  553. it( 'should merge right if the first element is empty', () => {
  554. setData( model, '<heading1>[</heading1><paragraph>]bar</paragraph>' );
  555. model.enqueueChange( 'transparent', writer => {
  556. const root = doc.getRoot( );
  557. const range = writer.createRange(
  558. writer.createPositionFromPath( root, [ 0, 0 ] ),
  559. writer.createPositionFromPath( root, [ 1, 0 ] )
  560. );
  561. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  562. } );
  563. deleteContent( model, doc.selection );
  564. expect( getData( model ) ).to.equal( '<paragraph>[]bar</paragraph>' );
  565. } );
  566. it( 'should merge left if the last element is empty', () => {
  567. setData( model, '<heading1>foo[</heading1><paragraph>]</paragraph>' );
  568. model.enqueueChange( 'transparent', writer => {
  569. const root = doc.getRoot( );
  570. const range = writer.createRange(
  571. writer.createPositionFromPath( root, [ 0, 3 ] ),
  572. writer.createPositionFromPath( root, [ 1, 0 ] )
  573. );
  574. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  575. } );
  576. deleteContent( model, doc.selection );
  577. expect( getData( model ) ).to.equal( '<heading1>foo[]</heading1>' );
  578. } );
  579. } );
  580. describe( 'filtering out', () => {
  581. beforeEach( () => {
  582. const schema = model.schema;
  583. schema.addAttributeCheck( ( ctx, attributeName ) => {
  584. // Disallow 'c' on pchild>pchild>$text.
  585. if ( ctx.endsWith( 'pchild pchild $text' ) && attributeName == 'c' ) {
  586. return false;
  587. }
  588. // Allow 'a' and 'b' on paragraph>$text.
  589. if ( ctx.endsWith( 'paragraph $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
  590. return true;
  591. }
  592. // Allow 'b' and 'c' in pchild>$text.
  593. if ( ctx.endsWith( 'pchild $text' ) && [ 'b', 'c' ].includes( attributeName ) ) {
  594. return true;
  595. }
  596. } );
  597. schema.extend( 'pchild', { allowIn: 'pchild' } );
  598. } );
  599. test(
  600. 'filters out disallowed attributes after left merge',
  601. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text a="1" b="1">z</$text></paragraph>',
  602. '<paragraph>x<pchild>fo[]<$text b="1">z</$text></pchild></paragraph>'
  603. );
  604. test(
  605. 'filters out disallowed attributes from nested nodes after left merge',
  606. '<paragraph>' +
  607. 'x' +
  608. '<pchild>fo[o</pchild>' +
  609. '</paragraph>' +
  610. '<paragraph>' +
  611. 'b]a<$text a="1" b="1">r</$text>' +
  612. '<pchild>b<$text b="1" c="1">i</$text>z</pchild>' +
  613. 'y' +
  614. '</paragraph>',
  615. '<paragraph>' +
  616. 'x' +
  617. '<pchild>' +
  618. 'fo[]a<$text b="1">r</$text>' +
  619. '<pchild>b<$text b="1">i</$text>z</pchild>' +
  620. 'y' +
  621. '</pchild>' +
  622. '</paragraph>'
  623. );
  624. test(
  625. 'filters out disallowed attributes after right merge',
  626. '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text b="1" c="1">y]z</$text></pchild></paragraph>',
  627. '<paragraph>fo[]<$text b="1">z</$text></paragraph>'
  628. );
  629. } );
  630. } );
  631. describe( 'in element selections scenarios', () => {
  632. beforeEach( () => {
  633. model = new Model();
  634. doc = model.document;
  635. // <p> like root.
  636. doc.createRoot( 'paragraph', 'paragraphRoot' );
  637. // <body> like root.
  638. doc.createRoot( '$root', 'bodyRoot' );
  639. // Special root which allows only blockWidgets inside itself.
  640. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  641. const schema = model.schema;
  642. schema.register( 'image', { allowWhere: '$text' } );
  643. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  644. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  645. schema.register( 'blockWidget', { isLimit: true } );
  646. schema.register( 'restrictedRoot', {
  647. isLimit: true
  648. } );
  649. schema.extend( '$block', { allowIn: '$root' } );
  650. schema.extend( 'blockWidget', { allowIn: '$root' } );
  651. schema.extend( 'blockWidget', { allowIn: 'restrictedRoot' } );
  652. } );
  653. // See also "in simple scenarios => deletes an element".
  654. it( 'deletes two inline elements', () => {
  655. model.schema.extend( 'paragraph', {
  656. isLimit: true
  657. } );
  658. setData(
  659. model,
  660. 'x[<image></image><image></image>]z',
  661. { rootName: 'paragraphRoot' }
  662. );
  663. deleteContent( model, doc.selection );
  664. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  665. .to.equal( 'x[]z' );
  666. } );
  667. it( 'moves the (custom) selection to the nearest paragraph', () => {
  668. setData(
  669. model,
  670. '<paragraph>[x]</paragraph><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  671. { rootName: 'bodyRoot' }
  672. );
  673. const root = doc.getRoot( 'bodyRoot' );
  674. // [<paragraph>yyy</paragraph>]
  675. const selection = new Selection( [
  676. new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) )
  677. ] );
  678. deleteContent( model, selection );
  679. expect( getData( model, { rootName: 'bodyRoot' } ) )
  680. .to.equal( '<paragraph>[x]</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  681. expect( stringify( root, selection ) )
  682. .to.equal( '<$root><paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph></$root>' );
  683. } );
  684. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  685. setData(
  686. model,
  687. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  688. { rootName: 'bodyRoot' }
  689. );
  690. deleteContent( model, doc.selection );
  691. expect( getData( model, { rootName: 'bodyRoot' } ) )
  692. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  693. } );
  694. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  695. setData(
  696. model,
  697. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>z</paragraph>',
  698. { rootName: 'bodyRoot' }
  699. );
  700. // [<heading1>yyy</heading1>]
  701. const range = new Range(
  702. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  703. new Position( doc.getRoot( 'bodyRoot' ), [ 2 ] )
  704. );
  705. deleteContent( model, new Selection( range ) );
  706. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  707. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  708. } );
  709. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  710. setData(
  711. model,
  712. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  713. { rootName: 'bodyRoot' }
  714. );
  715. // [<heading1>yyy</heading1><paragraph>yyy</paragraph>]
  716. const range = new Range(
  717. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  718. new Position( doc.getRoot( 'bodyRoot' ), [ 3 ] )
  719. );
  720. deleteContent( model, new Selection( range ) );
  721. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  722. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  723. } );
  724. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  725. setData(
  726. model,
  727. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  728. { rootName: 'bodyRoot' }
  729. );
  730. deleteContent( model, doc.selection );
  731. expect( getData( model, { rootName: 'bodyRoot' } ) )
  732. .to.equal( '<paragraph>[]</paragraph>' );
  733. } );
  734. it( 'does not create a paragraph when it is not allowed', () => {
  735. setData(
  736. model,
  737. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  738. { rootName: 'restrictedRoot' }
  739. );
  740. deleteContent( model, doc.selection );
  741. expect( getData( model, { rootName: 'restrictedRoot' } ) )
  742. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  743. } );
  744. it( 'does not create a paragraph when doNotAutoparagraph option is set to true', () => {
  745. setData(
  746. model,
  747. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  748. { rootName: 'bodyRoot' }
  749. );
  750. deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
  751. expect( getData( model, { rootName: 'bodyRoot' } ) )
  752. .to.equal( '<paragraph>x[]</paragraph><paragraph>z</paragraph>' );
  753. } );
  754. it( 'does not create a paragraph when after deletion there is no valid selection range (empty root)', () => {
  755. setData(
  756. model,
  757. '[<blockWidget></blockWidget>]',
  758. { rootName: 'bodyRoot' }
  759. );
  760. // This must be tested inside a change block to check results before the post-fixers get triggered.
  761. model.change( () => {
  762. deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
  763. expect( getData( model, { rootName: 'bodyRoot' } ) ).to.equal( '[]' );
  764. } );
  765. // Note that auto-paragraphing post-fixer injected a paragraph into the empty root.
  766. expect( getData( model, { rootName: 'bodyRoot' } ) ).to.equal( '<paragraph>[]</paragraph>' );
  767. } );
  768. } );
  769. describe( 'integration with inline limit elements', () => {
  770. beforeEach( () => {
  771. model = new Model();
  772. doc = model.document;
  773. doc.createRoot();
  774. const schema = model.schema;
  775. schema.register( 'inlineLimit', {
  776. isLimit: true,
  777. allowIn: '$root'
  778. } );
  779. schema.extend( '$text', {
  780. allowIn: [ 'inlineLimit', '$root', 'x' ]
  781. } );
  782. schema.register( 'x', { allowIn: '$root' } );
  783. } );
  784. test(
  785. 'should delete inside inline limit element',
  786. '<inlineLimit>foo [bar] baz</inlineLimit>',
  787. '<inlineLimit>foo [] baz</inlineLimit>'
  788. );
  789. test(
  790. 'should delete whole inline limit element',
  791. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  792. 'x[]x'
  793. );
  794. test(
  795. 'should delete from two inline limit elements',
  796. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  797. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  798. );
  799. test(
  800. 'merge option should be ignored if both elements are limits',
  801. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  802. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  803. );
  804. test(
  805. 'merge option should be ignored if the first element is a limit',
  806. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  807. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  808. );
  809. test(
  810. 'merge option should be ignored if the second element is a limit',
  811. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  812. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  813. );
  814. } );
  815. describe( 'integration with block limit elements', () => {
  816. beforeEach( () => {
  817. model = new Model();
  818. doc = model.document;
  819. doc.createRoot();
  820. const schema = model.schema;
  821. schema.register( 'blockLimit', {
  822. isLimit: true
  823. } );
  824. schema.extend( 'blockLimit', { allowIn: '$root' } );
  825. schema.extend( '$block', { allowIn: 'blockLimit' } );
  826. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  827. schema.register( 'blockQuote', {
  828. allowWhere: '$block',
  829. allowContentOf: '$root'
  830. } );
  831. } );
  832. test(
  833. 'should delete inside block limit element',
  834. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  835. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  836. { leaveUnmerged: true }
  837. );
  838. test(
  839. 'should delete inside block limit element (with merge)',
  840. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  841. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  842. );
  843. test(
  844. 'should delete whole block limit element',
  845. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  846. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  847. );
  848. test(
  849. 'should delete from two block limit elements',
  850. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  851. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  852. );
  853. test(
  854. 'merge option should be ignored if any of the elements is a limit',
  855. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  856. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  857. );
  858. // See: https://github.com/ckeditor/ckeditor5/issues/1265.
  859. it( 'should proper merge two elements which are inside limit element', () => {
  860. setData( model,
  861. '<blockLimit>' +
  862. '<blockQuote>' +
  863. '<paragraph>Foo</paragraph>' +
  864. '</blockQuote>' +
  865. '<paragraph>[]Bar</paragraph>' +
  866. '</blockLimit>'
  867. );
  868. model.modifySelection( doc.selection, { direction: 'backward' } );
  869. deleteContent( model, doc.selection );
  870. expect( getData( model ) ).to.equal(
  871. '<blockLimit>' +
  872. '<blockQuote>' +
  873. '<paragraph>Foo[]Bar</paragraph>' +
  874. '</blockQuote>' +
  875. '</blockLimit>' );
  876. } );
  877. it( 'should proper merge elements which are inside limit element (nested elements)', () => {
  878. setData( model,
  879. '<blockQuote>' +
  880. '<blockLimit>' +
  881. '<blockQuote>' +
  882. '<paragraph>Foo.</paragraph>' +
  883. '<blockQuote>' +
  884. '<paragraph>Foo</paragraph>' +
  885. '</blockQuote>' +
  886. '</blockQuote>' +
  887. '<paragraph>[]Bar</paragraph>' +
  888. '</blockLimit>' +
  889. '</blockQuote>'
  890. );
  891. model.modifySelection( doc.selection, { direction: 'backward' } );
  892. deleteContent( model, doc.selection );
  893. expect( getData( model ) ).to.equal(
  894. '<blockQuote>' +
  895. '<blockLimit>' +
  896. '<blockQuote>' +
  897. '<paragraph>Foo.</paragraph>' +
  898. '<blockQuote>' +
  899. '<paragraph>Foo[]Bar</paragraph>' +
  900. '</blockQuote>' +
  901. '</blockQuote>' +
  902. '</blockLimit>' +
  903. '</blockQuote>'
  904. );
  905. } );
  906. } );
  907. describe( 'should leave a paragraph if the entire content was selected', () => {
  908. beforeEach( () => {
  909. model = new Model();
  910. doc = model.document;
  911. doc.createRoot();
  912. const schema = model.schema;
  913. schema.register( 'div', {
  914. inheritAllFrom: '$block',
  915. isLimit: true
  916. } );
  917. schema.register( 'article', {
  918. inheritAllFrom: '$block',
  919. isLimit: true
  920. } );
  921. schema.register( 'image', {
  922. allowWhere: '$text',
  923. isObject: true
  924. } );
  925. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  926. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  927. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  928. schema.extend( '$text', { allowIn: '$root' } );
  929. schema.extend( 'image', { allowIn: '$root' } );
  930. schema.extend( 'image', { allowIn: 'heading1' } );
  931. schema.extend( 'heading1', { allowIn: 'div' } );
  932. schema.extend( 'paragraph', { allowIn: 'div' } );
  933. schema.extend( 'heading1', { allowIn: 'article' } );
  934. schema.extend( 'heading2', { allowIn: 'article' } );
  935. } );
  936. test(
  937. 'but not if only one block was selected',
  938. '<heading1>[xx]</heading1>',
  939. '<heading1>[]</heading1>'
  940. );
  941. test(
  942. 'when the entire heading and paragraph were selected',
  943. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  944. '<paragraph>[]</paragraph>'
  945. );
  946. test(
  947. 'when the entire content was selected',
  948. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  949. '<paragraph>[]</paragraph>'
  950. );
  951. test(
  952. 'inside the limit element when the entire heading and paragraph were inside',
  953. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  954. '<div><paragraph>[]</paragraph></div>'
  955. );
  956. test(
  957. 'but not if schema does not accept paragraph in limit element',
  958. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  959. '<article><heading1>[]</heading1></article>'
  960. );
  961. test(
  962. 'but not if selection is not containing the whole content',
  963. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  964. '<image></image><heading1>[]</heading1>'
  965. );
  966. test(
  967. 'but not if only single element is selected',
  968. '<heading1>[<image></image>xx]</heading1>',
  969. '<heading1>[]</heading1>'
  970. );
  971. it( 'when root element was not added as Schema limits work fine as well', () => {
  972. doc.createRoot( 'paragraph', 'paragraphRoot' );
  973. setData(
  974. model,
  975. 'x[<image></image><image></image>]z',
  976. { rootName: 'paragraphRoot' }
  977. );
  978. deleteContent( model, doc.selection );
  979. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  980. .to.equal( 'x[]z' );
  981. } );
  982. test(
  983. 'but not if the flag "doNotResetEntireContent" is set to true',
  984. '<heading1>[</heading1><paragraph>]</paragraph>',
  985. '<heading1>[]</heading1>',
  986. {
  987. doNotResetEntireContent: true
  988. }
  989. );
  990. } );
  991. function test( title, input, output, options ) {
  992. it( title, () => {
  993. model.enqueueChange( 'transparent', () => {
  994. setData( model, input );
  995. deleteContent( model, doc.selection, options );
  996. } );
  997. expect( getData( model ) ).to.equal( output );
  998. } );
  999. }
  1000. } );
  1001. } );