8
0

deletecontent.js 31 KB

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