deletecontent.js 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164
  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. 'removes first element when it\'s empty but second element is not empty (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. 'excludes end block from deletion 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. 'removes empty element',
  199. '<paragraph>x</paragraph><paragraph>[</paragraph><paragraph>]bar</paragraph><paragraph>y</paragraph>',
  200. '<paragraph>x</paragraph><paragraph>[]bar</paragraph><paragraph>y</paragraph>'
  201. );
  202. test(
  203. 'does not remove first element when first element contains object (same name)',
  204. '<paragraph>x</paragraph><paragraph><widget></widget>[foo</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  205. '<paragraph>x</paragraph><paragraph><widget></widget>[]ar</paragraph><paragraph>y</paragraph>'
  206. );
  207. test(
  208. 'does not merge second element into the first one (same name, !option.merge)',
  209. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  210. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  211. { leaveUnmerged: true }
  212. );
  213. test(
  214. 'does not remove first empty element when it\'s empty but second element is not empty (same name, !option.merge)',
  215. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  216. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  217. { leaveUnmerged: true }
  218. );
  219. test(
  220. 'merges second element into the first one (different name)',
  221. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  222. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
  223. );
  224. test(
  225. 'removes first element when it\'s empty but second element is not empty (different name)',
  226. '<paragraph>x</paragraph><heading1>[foo</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  227. '<paragraph>x</paragraph><paragraph>[]ar</paragraph><paragraph>y</paragraph>'
  228. );
  229. // Note: in all these cases we ignore the direction of merge.
  230. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  231. // forward and backward delete.
  232. it( 'merges second element into the first one (different name, backward selection)', () => {
  233. setData(
  234. model,
  235. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  236. { lastRangeBackward: true }
  237. );
  238. deleteContent( model, doc.selection );
  239. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  240. } );
  241. test(
  242. 'merges second element into the first one (different attrs)',
  243. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  244. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
  245. );
  246. test(
  247. 'removes empty first element',
  248. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  249. '<paragraph>x</paragraph><paragraph>[]o</paragraph><paragraph>y</paragraph>'
  250. );
  251. test(
  252. 'merges empty element into the first element',
  253. '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
  254. '<heading1>f[]</heading1><paragraph>x</paragraph>'
  255. );
  256. test(
  257. 'leaves just one element when all selected',
  258. '<paragraph>[x</paragraph><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  259. '<paragraph>[]bar</paragraph>'
  260. );
  261. test(
  262. 'leaves just one element when all selected (different name)',
  263. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  264. '<paragraph>[]bar</paragraph>'
  265. );
  266. test(
  267. 'leaves just one (first) element when all selected (different name)',
  268. '<heading1>foo[x</heading1><paragraph>bar</paragraph><paragraph>y]</paragraph>',
  269. '<heading1>foo[]</heading1>'
  270. );
  271. it( 'uses merge operation even if merged element is empty', () => {
  272. let mergeSpy;
  273. setData( model, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  274. model.change( writer => {
  275. mergeSpy = sinon.spy( writer, 'merge' );
  276. deleteContent( model, doc.selection );
  277. } );
  278. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  279. expect( mergeSpy.called ).to.be.true;
  280. } );
  281. it( 'uses merge operation even if merged element is empty #2', () => {
  282. let mergeSpy;
  283. setData( model, '<paragraph>ab[</paragraph><paragraph>]</paragraph>' );
  284. model.change( writer => {
  285. mergeSpy = sinon.spy( writer, 'merge' );
  286. deleteContent( model, doc.selection );
  287. } );
  288. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  289. expect( mergeSpy.called ).to.be.true;
  290. } );
  291. it( 'uses merge operation if first element is empty (because of content delete) and last is not', () => {
  292. let mergeSpy;
  293. setData( model, '<paragraph>[abcd</paragraph><paragraph>ef]gh</paragraph>' );
  294. model.change( writer => {
  295. mergeSpy = sinon.spy( writer, 'merge' );
  296. deleteContent( model, doc.selection );
  297. } );
  298. expect( getData( model ) ).to.equal( '<paragraph>[]gh</paragraph>' );
  299. expect( mergeSpy.called ).to.be.true;
  300. } );
  301. it( 'uses merge operation if first element is empty and last is not', () => {
  302. let mergeSpy;
  303. setData( model, '<paragraph>[</paragraph><paragraph>ef]gh</paragraph>' );
  304. model.change( writer => {
  305. mergeSpy = sinon.spy( writer, 'merge' );
  306. deleteContent( model, doc.selection );
  307. } );
  308. expect( getData( model ) ).to.equal( '<paragraph>[]gh</paragraph>' );
  309. expect( mergeSpy.called ).to.be.true;
  310. } );
  311. it( 'does not try to move the second block if not needed', () => {
  312. let mergeSpy, moveSpy;
  313. setData( model, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
  314. model.change( writer => {
  315. mergeSpy = sinon.spy( writer, 'merge' );
  316. moveSpy = sinon.spy( writer, 'move' );
  317. deleteContent( model, doc.selection );
  318. } );
  319. expect( getData( model ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
  320. expect( moveSpy.called ).to.be.false;
  321. expect( mergeSpy.called ).to.be.true;
  322. } );
  323. // Note: in all these cases we ignore the direction of merge.
  324. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  325. // forward and backward delete.
  326. describe( 'with nested elements', () => {
  327. test(
  328. 'merges elements when deep nested',
  329. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  330. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
  331. );
  332. test(
  333. 'removes block element with nested element',
  334. '<paragraph><pchild>[foo</pchild></paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  335. '<paragraph><pchild>[]ar</pchild></paragraph>'
  336. );
  337. test(
  338. 'does not remove block element with nested element and object',
  339. '<paragraph><pchild><widget></widget>[foo</pchild></paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  340. '<paragraph><pchild><widget></widget>[]ar</pchild></paragraph>'
  341. );
  342. test(
  343. 'merges heading element',
  344. '<heading1><hchild>x[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  345. '<heading1><hchild>x[]ar</hchild></heading1>'
  346. );
  347. test(
  348. 'removes heading element',
  349. '<heading1><hchild>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  350. '<paragraph><pchild>[]ar</pchild></paragraph>'
  351. );
  352. test(
  353. 'does not remove heading element if contains object',
  354. '<heading1><hchild><widget></widget>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  355. '<heading1><hchild><widget></widget>[]ar</hchild></heading1>'
  356. );
  357. test(
  358. 'merges elements when left end deep nested',
  359. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
  360. '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
  361. );
  362. test(
  363. 'removes elements when left end deep nested and will be empty',
  364. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  365. '<paragraph>[]ar</paragraph><paragraph>x</paragraph>'
  366. );
  367. test(
  368. 'merges elements when right end deep nested',
  369. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
  370. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
  371. );
  372. test(
  373. 'removes element when right end deep nested but left end would be empty',
  374. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  375. '<paragraph>x</paragraph><paragraph><pchild>[]ar</pchild></paragraph>'
  376. );
  377. test(
  378. 'merges elements when right end deep nested (in an empty container)',
  379. '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
  380. '<paragraph>fo[]</paragraph>'
  381. );
  382. test(
  383. 'removes elements when left end deep nested (in an empty container)',
  384. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  385. '<paragraph>[]ar</paragraph><paragraph>x</paragraph>'
  386. );
  387. describe( 'with 3rd level of nesting', () => {
  388. test(
  389. 'merges elements when deep nested (same name)',
  390. '<pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>' +
  391. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  392. '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>'
  393. );
  394. test(
  395. 'removes elements when deep nested (same name)',
  396. '<pparent><paragraph><pchild>[foo</pchild></paragraph></pparent>' +
  397. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  398. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  399. );
  400. test(
  401. 'removes elements up to common ancestor when deep nested (same name)',
  402. '<pparent>' +
  403. '<paragraph><pchild>[foo</pchild></paragraph>' +
  404. '<paragraph><pchild>b]ar</pchild>y</paragraph>y' +
  405. '</pparent>',
  406. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  407. );
  408. test(
  409. 'merges elements when deep nested (different name)',
  410. '<pparent>x<heading1>x<hchild>fo[o</hchild></heading1></pparent>' +
  411. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  412. '<pparent>x<heading1>x<hchild>fo[]ar</hchild>y</heading1>y</pparent>'
  413. );
  414. test(
  415. 'removes elements when deep nested (different name)',
  416. '<pparent><heading1><hchild>[foo</hchild></heading1></pparent>' +
  417. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  418. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  419. );
  420. test(
  421. 'merges elements up to common ancestor when deep nested (different names)',
  422. '<pparent>' +
  423. '<heading1><hchild>fo[o</hchild></heading1>' +
  424. '<paragraph><pchild>b]ar</pchild></paragraph>' +
  425. '</pparent>',
  426. '<pparent><heading1><hchild>fo[]ar</hchild></heading1></pparent>'
  427. );
  428. test(
  429. 'removes elements up to common ancestor when deep nested (different names)',
  430. '<pparent>' +
  431. '<heading1><hchild>[foo</hchild></heading1>' +
  432. '<paragraph><pchild>b]ar</pchild>y</paragraph>y' +
  433. '</pparent>',
  434. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  435. );
  436. } );
  437. describe( 'with 3rd level of nesting o the left end', () => {
  438. test(
  439. 'merges elements',
  440. '<pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent>' +
  441. '<paragraph>b]om</paragraph>',
  442. '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>'
  443. );
  444. test(
  445. 'merges elements (different names)',
  446. '<pparent>x<heading1>foo<hchild>ba[r</hchild></heading1></pparent>' +
  447. '<paragraph>b]om</paragraph>',
  448. '<pparent>x<heading1>foo<hchild>ba[]om</hchild></heading1></pparent>'
  449. );
  450. test(
  451. 'removes elements',
  452. '<pparent><paragraph><pchild>[bar</pchild></paragraph></pparent>' +
  453. '<paragraph>b]om</paragraph>',
  454. '<paragraph>[]om</paragraph>'
  455. );
  456. test(
  457. 'removes elements up to common ancestor (different names)',
  458. '<pparent>' +
  459. '<heading1><hchild>[foo</hchild></heading1>' +
  460. '<paragraph>b]ar</paragraph>y' +
  461. '</pparent>',
  462. '<pparent><paragraph>[]ar</paragraph>y</pparent>'
  463. );
  464. } );
  465. describe( 'with 3rd level of nesting o the right end', () => {
  466. test(
  467. 'merges elements',
  468. '<paragraph>b[om</paragraph>' +
  469. '<pparent><paragraph><pchild>ba]r</pchild></paragraph></pparent>',
  470. '<paragraph>b[]r</paragraph>'
  471. );
  472. test(
  473. 'merges elements (different names)',
  474. '<paragraph>bo[m</paragraph>' +
  475. '<pparent><heading1><hchild>b]ar</hchild></heading1></pparent>',
  476. '<paragraph>bo[]ar</paragraph>'
  477. );
  478. test(
  479. 'merges elements (different names, reversed)',
  480. '<heading1>bo[m</heading1>' +
  481. '<pparent><paragraph><pchild>b]ar</pchild></paragraph></pparent>',
  482. '<heading1>bo[]ar</heading1>'
  483. );
  484. test(
  485. 'removes elements',
  486. '<paragraph>[bom</paragraph>' +
  487. '<pparent><paragraph><pchild>b]ar</pchild></paragraph></pparent>',
  488. '<pparent><paragraph><pchild>[]ar</pchild></paragraph></pparent>'
  489. );
  490. test(
  491. 'removes elements up to common ancestor (different names)',
  492. '<pparent>' +
  493. '<heading1>[bar</heading1>y' +
  494. '<paragraph><pchild>f]oo</pchild></paragraph>' +
  495. '</pparent>',
  496. '<pparent><paragraph><pchild>[]oo</pchild></paragraph></pparent>'
  497. );
  498. } );
  499. } );
  500. describe( 'with object elements', () => {
  501. beforeEach( () => {
  502. const schema = model.schema;
  503. schema.register( 'blockWidget', {
  504. isObject: true
  505. } );
  506. schema.register( 'nestedEditable', {
  507. isLimit: true
  508. } );
  509. schema.extend( 'blockWidget', { allowIn: '$root' } );
  510. schema.extend( 'nestedEditable', { allowIn: 'blockWidget' } );
  511. schema.extend( '$text', { allowIn: 'nestedEditable' } );
  512. } );
  513. test(
  514. 'does not merge an object element (if it is first)',
  515. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  516. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
  517. );
  518. test(
  519. 'does not merge an object element (if it is second)',
  520. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  521. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
  522. );
  523. } );
  524. describe( 'filtering out', () => {
  525. beforeEach( () => {
  526. const schema = model.schema;
  527. schema.addAttributeCheck( ( ctx, attributeName ) => {
  528. // Disallow 'c' on pchild>pchild>$text.
  529. if ( ctx.endsWith( 'pchild pchild $text' ) && attributeName == 'c' ) {
  530. return false;
  531. }
  532. // Allow 'a' and 'b' on paragraph>$text.
  533. if ( ctx.endsWith( 'paragraph $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
  534. return true;
  535. }
  536. // Allow 'b' and 'c' in pchild>$text.
  537. if ( ctx.endsWith( 'pchild $text' ) && [ 'b', 'c' ].includes( attributeName ) ) {
  538. return true;
  539. }
  540. } );
  541. schema.extend( 'pchild', { allowIn: 'pchild' } );
  542. } );
  543. test(
  544. 'filters out disallowed attributes after left merge',
  545. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text a="1" b="1">z</$text></paragraph>',
  546. '<paragraph>x<pchild>fo[]<$text b="1">z</$text></pchild></paragraph>'
  547. );
  548. test(
  549. 'filters out disallowed attributes from nested nodes after left merge',
  550. '<paragraph>' +
  551. 'x' +
  552. '<pchild>fo[o</pchild>' +
  553. '</paragraph>' +
  554. '<paragraph>' +
  555. 'b]a<$text a="1" b="1">r</$text>' +
  556. '<pchild>b<$text b="1" c="1">i</$text>z</pchild>' +
  557. 'y' +
  558. '</paragraph>',
  559. '<paragraph>' +
  560. 'x' +
  561. '<pchild>' +
  562. 'fo[]a<$text b="1">r</$text>' +
  563. '<pchild>b<$text b="1">i</$text>z</pchild>' +
  564. 'y' +
  565. '</pchild>' +
  566. '</paragraph>'
  567. );
  568. test(
  569. 'filters out disallowed attributes after right merge',
  570. '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text b="1" c="1">y]z</$text></pchild></paragraph>',
  571. '<paragraph>fo[]<$text b="1">z</$text></paragraph>'
  572. );
  573. } );
  574. } );
  575. describe( 'in element selections scenarios', () => {
  576. beforeEach( () => {
  577. model = new Model();
  578. doc = model.document;
  579. // <p> like root.
  580. doc.createRoot( 'paragraph', 'paragraphRoot' );
  581. // <body> like root.
  582. doc.createRoot( '$root', 'bodyRoot' );
  583. // Special root which allows only blockWidgets inside itself.
  584. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  585. const schema = model.schema;
  586. schema.register( 'image', { allowWhere: '$text' } );
  587. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  588. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  589. schema.register( 'blockWidget', { isLimit: true } );
  590. schema.register( 'restrictedRoot', {
  591. isLimit: true
  592. } );
  593. schema.extend( '$block', { allowIn: '$root' } );
  594. schema.extend( 'blockWidget', { allowIn: '$root' } );
  595. schema.extend( 'blockWidget', { allowIn: 'restrictedRoot' } );
  596. } );
  597. // See also "in simple scenarios => deletes an element".
  598. it( 'deletes two inline elements', () => {
  599. model.schema.extend( 'paragraph', {
  600. isLimit: true
  601. } );
  602. setData(
  603. model,
  604. 'x[<image></image><image></image>]z',
  605. { rootName: 'paragraphRoot' }
  606. );
  607. deleteContent( model, doc.selection );
  608. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  609. .to.equal( 'x[]z' );
  610. } );
  611. it( 'moves the (custom) selection to the nearest paragraph', () => {
  612. setData(
  613. model,
  614. '<paragraph>[x]</paragraph><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  615. { rootName: 'bodyRoot' }
  616. );
  617. const root = doc.getRoot( 'bodyRoot' );
  618. // [<paragraph>yyy</paragraph>]
  619. const selection = new Selection( [
  620. new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) )
  621. ] );
  622. deleteContent( model, selection );
  623. expect( getData( model, { rootName: 'bodyRoot' } ) )
  624. .to.equal( '<paragraph>[x]</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  625. expect( stringify( root, selection ) )
  626. .to.equal( '<$root><paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph></$root>' );
  627. } );
  628. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  629. setData(
  630. model,
  631. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  632. { rootName: 'bodyRoot' }
  633. );
  634. deleteContent( model, doc.selection );
  635. expect( getData( model, { rootName: 'bodyRoot' } ) )
  636. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  637. } );
  638. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  639. setData(
  640. model,
  641. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>z</paragraph>',
  642. { rootName: 'bodyRoot' }
  643. );
  644. // [<heading1>yyy</heading1>]
  645. const range = new Range(
  646. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  647. new Position( doc.getRoot( 'bodyRoot' ), [ 2 ] )
  648. );
  649. deleteContent( model, new Selection( range ) );
  650. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  651. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  652. } );
  653. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  654. setData(
  655. model,
  656. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  657. { rootName: 'bodyRoot' }
  658. );
  659. // [<heading1>yyy</heading1><paragraph>yyy</paragraph>]
  660. const range = new Range(
  661. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  662. new Position( doc.getRoot( 'bodyRoot' ), [ 3 ] )
  663. );
  664. deleteContent( model, new Selection( range ) );
  665. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  666. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  667. } );
  668. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  669. setData(
  670. model,
  671. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  672. { rootName: 'bodyRoot' }
  673. );
  674. deleteContent( model, doc.selection );
  675. expect( getData( model, { rootName: 'bodyRoot' } ) )
  676. .to.equal( '<paragraph>[]</paragraph>' );
  677. } );
  678. it( 'does not create a paragraph when it is not allowed', () => {
  679. setData(
  680. model,
  681. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  682. { rootName: 'restrictedRoot' }
  683. );
  684. deleteContent( model, doc.selection );
  685. expect( getData( model, { rootName: 'restrictedRoot' } ) )
  686. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  687. } );
  688. it( 'does not create a paragraph when doNotAutoparagraph option is set to true', () => {
  689. setData(
  690. model,
  691. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  692. { rootName: 'bodyRoot' }
  693. );
  694. deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
  695. expect( getData( model, { rootName: 'bodyRoot' } ) )
  696. .to.equal( '<paragraph>x[]</paragraph><paragraph>z</paragraph>' );
  697. } );
  698. it( 'does not create a paragraph when after deletion there is no valid selection range (empty root)', () => {
  699. setData(
  700. model,
  701. '[<blockWidget></blockWidget>]',
  702. { rootName: 'bodyRoot' }
  703. );
  704. deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
  705. expect( getData( model, { rootName: 'bodyRoot' } ) )
  706. .to.equal( '[]' );
  707. } );
  708. } );
  709. describe( 'integration with inline limit elements', () => {
  710. beforeEach( () => {
  711. model = new Model();
  712. doc = model.document;
  713. doc.createRoot();
  714. const schema = model.schema;
  715. schema.register( 'inlineLimit', {
  716. isLimit: true,
  717. allowIn: '$root'
  718. } );
  719. schema.extend( '$text', {
  720. allowIn: [ 'inlineLimit', '$root', 'x' ]
  721. } );
  722. schema.register( 'x', { allowIn: '$root' } );
  723. } );
  724. test(
  725. 'should delete inside inline limit element',
  726. '<inlineLimit>foo [bar] baz</inlineLimit>',
  727. '<inlineLimit>foo [] baz</inlineLimit>'
  728. );
  729. test(
  730. 'should delete whole inline limit element',
  731. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  732. 'x[]x'
  733. );
  734. test(
  735. 'should delete from two inline limit elements',
  736. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  737. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  738. );
  739. test(
  740. 'merge option should be ignored if both elements are limits',
  741. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  742. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  743. );
  744. test(
  745. 'merge option should be ignored if the first element is a limit',
  746. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  747. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  748. );
  749. test(
  750. 'merge option should be ignored if the second element is a limit',
  751. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  752. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  753. );
  754. } );
  755. describe( 'integration with block limit elements', () => {
  756. beforeEach( () => {
  757. model = new Model();
  758. doc = model.document;
  759. doc.createRoot();
  760. const schema = model.schema;
  761. schema.register( 'blockLimit', {
  762. isLimit: true
  763. } );
  764. schema.extend( 'blockLimit', { allowIn: '$root' } );
  765. schema.extend( '$block', { allowIn: 'blockLimit' } );
  766. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  767. schema.register( 'blockQuote', {
  768. allowWhere: '$block',
  769. allowContentOf: '$root'
  770. } );
  771. } );
  772. test(
  773. 'should delete inside block limit element',
  774. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  775. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  776. { leaveUnmerged: true }
  777. );
  778. test(
  779. 'should delete inside block limit element (with merge)',
  780. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  781. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  782. );
  783. test(
  784. 'should delete whole block limit element',
  785. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  786. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  787. );
  788. test(
  789. 'should delete from two block limit elements',
  790. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  791. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  792. );
  793. test(
  794. 'merge option should be ignored if any of the elements is a limit',
  795. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  796. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  797. );
  798. // See: https://github.com/ckeditor/ckeditor5/issues/1265.
  799. it( 'should proper merge two elements which are inside limit element', () => {
  800. setData( model,
  801. '<blockLimit>' +
  802. '<blockQuote>' +
  803. '<paragraph>Foo</paragraph>' +
  804. '</blockQuote>' +
  805. '<paragraph>[]Bar</paragraph>' +
  806. '</blockLimit>'
  807. );
  808. model.modifySelection( doc.selection, { direction: 'backward' } );
  809. deleteContent( model, doc.selection );
  810. expect( getData( model ) ).to.equal(
  811. '<blockLimit>' +
  812. '<blockQuote>' +
  813. '<paragraph>Foo[]Bar</paragraph>' +
  814. '</blockQuote>' +
  815. '</blockLimit>' );
  816. } );
  817. it( 'should proper merge elements which are inside limit element (nested elements)', () => {
  818. setData( model,
  819. '<blockQuote>' +
  820. '<blockLimit>' +
  821. '<blockQuote>' +
  822. '<paragraph>Foo.</paragraph>' +
  823. '<blockQuote>' +
  824. '<paragraph>Foo</paragraph>' +
  825. '</blockQuote>' +
  826. '</blockQuote>' +
  827. '<paragraph>[]Bar</paragraph>' +
  828. '</blockLimit>' +
  829. '</blockQuote>'
  830. );
  831. model.modifySelection( doc.selection, { direction: 'backward' } );
  832. deleteContent( model, doc.selection );
  833. expect( getData( model ) ).to.equal(
  834. '<blockQuote>' +
  835. '<blockLimit>' +
  836. '<blockQuote>' +
  837. '<paragraph>Foo.</paragraph>' +
  838. '<blockQuote>' +
  839. '<paragraph>Foo[]Bar</paragraph>' +
  840. '</blockQuote>' +
  841. '</blockQuote>' +
  842. '</blockLimit>' +
  843. '</blockQuote>'
  844. );
  845. } );
  846. } );
  847. describe( 'should leave a paragraph if the entire content was selected', () => {
  848. beforeEach( () => {
  849. model = new Model();
  850. doc = model.document;
  851. doc.createRoot();
  852. const schema = model.schema;
  853. schema.register( 'div', {
  854. inheritAllFrom: '$block',
  855. isLimit: true
  856. } );
  857. schema.register( 'article', {
  858. inheritAllFrom: '$block',
  859. isLimit: true
  860. } );
  861. schema.register( 'image', {
  862. allowWhere: '$text',
  863. isObject: true
  864. } );
  865. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  866. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  867. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  868. schema.extend( '$text', { allowIn: '$root' } );
  869. schema.extend( 'image', { allowIn: '$root' } );
  870. schema.extend( 'image', { allowIn: 'heading1' } );
  871. schema.extend( 'heading1', { allowIn: 'div' } );
  872. schema.extend( 'paragraph', { allowIn: 'div' } );
  873. schema.extend( 'heading1', { allowIn: 'article' } );
  874. schema.extend( 'heading2', { allowIn: 'article' } );
  875. } );
  876. test(
  877. 'but not if only one block was selected',
  878. '<heading1>[xx]</heading1>',
  879. '<heading1>[]</heading1>'
  880. );
  881. test(
  882. 'when the entire heading and paragraph were selected',
  883. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  884. '<paragraph>[]</paragraph>'
  885. );
  886. test(
  887. 'when the entire content was selected',
  888. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  889. '<paragraph>[]</paragraph>'
  890. );
  891. test(
  892. 'inside the limit element when the entire heading and paragraph were inside',
  893. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  894. '<div><paragraph>[]</paragraph></div>'
  895. );
  896. test(
  897. 'but not if schema does not accept paragraph in limit element',
  898. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  899. '<article><heading1>[]</heading1></article>'
  900. );
  901. test(
  902. 'but not if selection is not containing the whole content',
  903. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  904. '<image></image><heading1>[]</heading1>'
  905. );
  906. test(
  907. 'but not if only single element is selected',
  908. '<heading1>[<image></image>xx]</heading1>',
  909. '<heading1>[]</heading1>'
  910. );
  911. it( 'when root element was not added as Schema limits work fine as well', () => {
  912. doc.createRoot( 'paragraph', 'paragraphRoot' );
  913. setData(
  914. model,
  915. 'x[<image></image><image></image>]z',
  916. { rootName: 'paragraphRoot' }
  917. );
  918. deleteContent( model, doc.selection );
  919. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  920. .to.equal( 'x[]z' );
  921. } );
  922. test(
  923. 'but not if the flag "doNotResetEntireContent" is set to true',
  924. '<heading1>[</heading1><paragraph>]</paragraph>',
  925. '<heading1>[]</heading1>',
  926. {
  927. doNotResetEntireContent: true
  928. }
  929. );
  930. } );
  931. function test( title, input, output, options ) {
  932. it( title, () => {
  933. model.enqueueChange( 'transparent', () => {
  934. setData( model, input );
  935. deleteContent( model, doc.selection, options );
  936. } );
  937. expect( getData( model ) ).to.equal( output );
  938. } );
  939. }
  940. } );
  941. } );