8
0

deletecontent.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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' } );
  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.extend( '$text', { allowIn: [ 'pchild', 'pparent', 'hchild' ] } );
  175. } );
  176. test(
  177. 'do not merge when no need to',
  178. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  179. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>'
  180. );
  181. test(
  182. 'merges second element into the first one (same name)',
  183. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  184. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
  185. );
  186. test(
  187. 'does not merge second element into the first one (same name, !option.merge)',
  188. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  189. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  190. { leaveUnmerged: true }
  191. );
  192. test(
  193. 'merges second element into the first one (different name)',
  194. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  195. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
  196. );
  197. // Note: in all these cases we ignore the direction of merge.
  198. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  199. // forward and backward delete.
  200. it( 'merges second element into the first one (different name, backward selection)', () => {
  201. setData(
  202. model,
  203. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  204. { lastRangeBackward: true }
  205. );
  206. deleteContent( model, doc.selection );
  207. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  208. } );
  209. test(
  210. 'merges second element into the first one (different attrs)',
  211. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  212. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
  213. );
  214. test(
  215. 'removes empty first element',
  216. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  217. '<paragraph>x</paragraph><paragraph>[]o</paragraph><paragraph>y</paragraph>'
  218. );
  219. test(
  220. 'merges empty element into the first element',
  221. '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
  222. '<heading1>f[]</heading1><paragraph>x</paragraph>'
  223. );
  224. test(
  225. 'leaves just one element when all selected',
  226. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  227. '<paragraph>[]bar</paragraph>'
  228. );
  229. it( 'uses merge operation even if merged element is empty', () => {
  230. let mergeSpy;
  231. setData( model, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  232. model.change( writer => {
  233. mergeSpy = sinon.spy( writer, 'merge' );
  234. deleteContent( model, doc.selection );
  235. } );
  236. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  237. expect( mergeSpy.called ).to.be.true;
  238. } );
  239. it( 'uses merge operation even if merged element is empty #2', () => {
  240. let mergeSpy;
  241. setData( model, '<paragraph>ab[</paragraph><paragraph>]</paragraph>' );
  242. model.change( writer => {
  243. mergeSpy = sinon.spy( writer, 'merge' );
  244. deleteContent( model, doc.selection );
  245. } );
  246. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  247. expect( mergeSpy.called ).to.be.true;
  248. } );
  249. it( 'does not try to move the second block if not needed', () => {
  250. let mergeSpy, moveSpy;
  251. setData( model, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
  252. model.change( writer => {
  253. mergeSpy = sinon.spy( writer, 'merge' );
  254. moveSpy = sinon.spy( writer, 'move' );
  255. deleteContent( model, doc.selection );
  256. } );
  257. expect( getData( model ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
  258. expect( moveSpy.called ).to.be.false;
  259. expect( mergeSpy.called ).to.be.true;
  260. } );
  261. // Note: in all these cases we ignore the direction of merge.
  262. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  263. // forward and backward delete.
  264. describe( 'with nested elements', () => {
  265. test(
  266. 'merges elements when deep nested',
  267. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  268. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
  269. );
  270. test(
  271. 'removes block element with nested element',
  272. '<paragraph><pchild>[foo</pchild></paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  273. '<paragraph><pchild>[]ar</pchild></paragraph>'
  274. );
  275. test(
  276. 'removes heading element',
  277. '<heading1><hchild>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  278. '<paragraph><pchild>[]ar</pchild></paragraph>'
  279. );
  280. test(
  281. 'merges elements when deep nested (3rd level)',
  282. '<pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>' +
  283. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  284. '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>'
  285. );
  286. test(
  287. 'merges elements when left end deep nested',
  288. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
  289. '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
  290. );
  291. test(
  292. 'merges elements when right end deep nested',
  293. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
  294. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
  295. );
  296. test(
  297. 'merges elements when left end deep nested (3rd level)',
  298. '<pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent><paragraph>b]om</paragraph>',
  299. '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>'
  300. );
  301. test(
  302. 'merges elements when right end deep nested (in an empty container)',
  303. '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
  304. '<paragraph>fo[]</paragraph>'
  305. );
  306. test(
  307. 'merges elements when left end deep nested (in an empty container)',
  308. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  309. '<paragraph>[]ar</paragraph><paragraph>x</paragraph>'
  310. );
  311. test(
  312. 'merges elements when right end deep nested (3rd level)',
  313. '<paragraph>fo[o</paragraph><pparent><paragraph><pchild>bar]</pchild></paragraph></pparent>',
  314. '<paragraph>fo[]</paragraph>'
  315. );
  316. } );
  317. describe( 'with object elements', () => {
  318. beforeEach( () => {
  319. const schema = model.schema;
  320. schema.register( 'blockWidget', {
  321. isObject: true
  322. } );
  323. schema.register( 'nestedEditable', {
  324. isLimit: true
  325. } );
  326. schema.extend( 'blockWidget', { allowIn: '$root' } );
  327. schema.extend( 'nestedEditable', { allowIn: 'blockWidget' } );
  328. schema.extend( '$text', { allowIn: 'nestedEditable' } );
  329. } );
  330. test(
  331. 'does not merge an object element (if it is first)',
  332. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  333. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
  334. );
  335. test(
  336. 'does not merge an object element (if it is second)',
  337. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  338. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
  339. );
  340. } );
  341. describe( 'filtering out', () => {
  342. beforeEach( () => {
  343. const schema = model.schema;
  344. schema.addAttributeCheck( ( ctx, attributeName ) => {
  345. // Disallow 'c' on pchild>pchild>$text.
  346. if ( ctx.endsWith( 'pchild pchild $text' ) && attributeName == 'c' ) {
  347. return false;
  348. }
  349. // Allow 'a' and 'b' on paragraph>$text.
  350. if ( ctx.endsWith( 'paragraph $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
  351. return true;
  352. }
  353. // Allow 'b' and 'c' in pchild>$text.
  354. if ( ctx.endsWith( 'pchild $text' ) && [ 'b', 'c' ].includes( attributeName ) ) {
  355. return true;
  356. }
  357. } );
  358. schema.extend( 'pchild', { allowIn: 'pchild' } );
  359. } );
  360. test(
  361. 'filters out disallowed attributes after left merge',
  362. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text a="1" b="1">z</$text></paragraph>',
  363. '<paragraph>x<pchild>fo[]<$text b="1">z</$text></pchild></paragraph>'
  364. );
  365. test(
  366. 'filters out disallowed attributes from nested nodes after left merge',
  367. '<paragraph>' +
  368. 'x' +
  369. '<pchild>fo[o</pchild>' +
  370. '</paragraph>' +
  371. '<paragraph>' +
  372. 'b]a<$text a="1" b="1">r</$text>' +
  373. '<pchild>b<$text b="1" c="1">i</$text>z</pchild>' +
  374. 'y' +
  375. '</paragraph>',
  376. '<paragraph>' +
  377. 'x' +
  378. '<pchild>' +
  379. 'fo[]a<$text b="1">r</$text>' +
  380. '<pchild>b<$text b="1">i</$text>z</pchild>' +
  381. 'y' +
  382. '</pchild>' +
  383. '</paragraph>'
  384. );
  385. test(
  386. 'filters out disallowed attributes after right merge',
  387. '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text b="1" c="1">y]z</$text></pchild></paragraph>',
  388. '<paragraph>fo[]<$text b="1">z</$text></paragraph>'
  389. );
  390. } );
  391. } );
  392. describe( 'in element selections scenarios', () => {
  393. beforeEach( () => {
  394. model = new Model();
  395. doc = model.document;
  396. // <p> like root.
  397. doc.createRoot( 'paragraph', 'paragraphRoot' );
  398. // <body> like root.
  399. doc.createRoot( '$root', 'bodyRoot' );
  400. // Special root which allows only blockWidgets inside itself.
  401. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  402. const schema = model.schema;
  403. schema.register( 'image', { allowWhere: '$text' } );
  404. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  405. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  406. schema.register( 'blockWidget', { isLimit: true } );
  407. schema.register( 'restrictedRoot', {
  408. isLimit: true
  409. } );
  410. schema.extend( '$block', { allowIn: '$root' } );
  411. schema.extend( 'blockWidget', { allowIn: '$root' } );
  412. schema.extend( 'blockWidget', { allowIn: 'restrictedRoot' } );
  413. } );
  414. // See also "in simple scenarios => deletes an element".
  415. it( 'deletes two inline elements', () => {
  416. model.schema.extend( 'paragraph', {
  417. isLimit: true
  418. } );
  419. setData(
  420. model,
  421. 'x[<image></image><image></image>]z',
  422. { rootName: 'paragraphRoot' }
  423. );
  424. deleteContent( model, doc.selection );
  425. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  426. .to.equal( 'x[]z' );
  427. } );
  428. it( 'moves the (custom) selection to the nearest paragraph', () => {
  429. setData(
  430. model,
  431. '<paragraph>[x]</paragraph><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  432. { rootName: 'bodyRoot' }
  433. );
  434. const root = doc.getRoot( 'bodyRoot' );
  435. // [<paragraph>yyy</paragraph>]
  436. const selection = new Selection( [
  437. new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) )
  438. ] );
  439. deleteContent( model, selection );
  440. expect( getData( model, { rootName: 'bodyRoot' } ) )
  441. .to.equal( '<paragraph>[x]</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  442. expect( stringify( root, selection ) )
  443. .to.equal( '<$root><paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph></$root>' );
  444. } );
  445. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  446. setData(
  447. model,
  448. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  449. { rootName: 'bodyRoot' }
  450. );
  451. deleteContent( model, doc.selection );
  452. expect( getData( model, { rootName: 'bodyRoot' } ) )
  453. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  454. } );
  455. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  456. setData(
  457. model,
  458. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>z</paragraph>',
  459. { rootName: 'bodyRoot' }
  460. );
  461. // [<heading1>yyy</heading1>]
  462. const range = new Range(
  463. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  464. new Position( doc.getRoot( 'bodyRoot' ), [ 2 ] )
  465. );
  466. deleteContent( model, new Selection( range ) );
  467. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  468. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  469. } );
  470. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  471. setData(
  472. model,
  473. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  474. { rootName: 'bodyRoot' }
  475. );
  476. // [<heading1>yyy</heading1><paragraph>yyy</paragraph>]
  477. const range = new Range(
  478. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  479. new Position( doc.getRoot( 'bodyRoot' ), [ 3 ] )
  480. );
  481. deleteContent( model, new Selection( range ) );
  482. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  483. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  484. } );
  485. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  486. setData(
  487. model,
  488. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  489. { rootName: 'bodyRoot' }
  490. );
  491. deleteContent( model, doc.selection );
  492. expect( getData( model, { rootName: 'bodyRoot' } ) )
  493. .to.equal( '<paragraph>[]</paragraph>' );
  494. } );
  495. it( 'does not create a paragraph when it is not allowed', () => {
  496. setData(
  497. model,
  498. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  499. { rootName: 'restrictedRoot' }
  500. );
  501. deleteContent( model, doc.selection );
  502. expect( getData( model, { rootName: 'restrictedRoot' } ) )
  503. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  504. } );
  505. it( 'does not create a paragraph when doNotAutoparagraph option is set to true', () => {
  506. setData(
  507. model,
  508. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  509. { rootName: 'bodyRoot' }
  510. );
  511. deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
  512. expect( getData( model, { rootName: 'bodyRoot' } ) )
  513. .to.equal( '<paragraph>x[]</paragraph><paragraph>z</paragraph>' );
  514. } );
  515. it( 'does not create a paragraph when after deletion there is no valid selection range (empty root)', () => {
  516. setData(
  517. model,
  518. '[<blockWidget></blockWidget>]',
  519. { rootName: 'bodyRoot' }
  520. );
  521. deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
  522. expect( getData( model, { rootName: 'bodyRoot' } ) )
  523. .to.equal( '[]' );
  524. } );
  525. } );
  526. describe( 'integration with inline limit elements', () => {
  527. beforeEach( () => {
  528. model = new Model();
  529. doc = model.document;
  530. doc.createRoot();
  531. const schema = model.schema;
  532. schema.register( 'inlineLimit', {
  533. isLimit: true,
  534. allowIn: '$root'
  535. } );
  536. schema.extend( '$text', {
  537. allowIn: [ 'inlineLimit', '$root', 'x' ]
  538. } );
  539. schema.register( 'x', { allowIn: '$root' } );
  540. } );
  541. test(
  542. 'should delete inside inline limit element',
  543. '<inlineLimit>foo [bar] baz</inlineLimit>',
  544. '<inlineLimit>foo [] baz</inlineLimit>'
  545. );
  546. test(
  547. 'should delete whole inline limit element',
  548. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  549. 'x[]x'
  550. );
  551. test(
  552. 'should delete from two inline limit elements',
  553. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  554. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  555. );
  556. test(
  557. 'merge option should be ignored if both elements are limits',
  558. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  559. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  560. );
  561. test(
  562. 'merge option should be ignored if the first element is a limit',
  563. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  564. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  565. );
  566. test(
  567. 'merge option should be ignored if the second element is a limit',
  568. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  569. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  570. );
  571. } );
  572. describe( 'integration with block limit elements', () => {
  573. beforeEach( () => {
  574. model = new Model();
  575. doc = model.document;
  576. doc.createRoot();
  577. const schema = model.schema;
  578. schema.register( 'blockLimit', {
  579. isLimit: true
  580. } );
  581. schema.extend( 'blockLimit', { allowIn: '$root' } );
  582. schema.extend( '$block', { allowIn: 'blockLimit' } );
  583. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  584. schema.register( 'blockQuote', {
  585. allowWhere: '$block',
  586. allowContentOf: '$root'
  587. } );
  588. } );
  589. test(
  590. 'should delete inside block limit element',
  591. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  592. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  593. { leaveUnmerged: true }
  594. );
  595. test(
  596. 'should delete inside block limit element (with merge)',
  597. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  598. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  599. );
  600. test(
  601. 'should delete whole block limit element',
  602. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  603. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  604. );
  605. test(
  606. 'should delete from two block limit elements',
  607. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  608. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  609. );
  610. test(
  611. 'merge option should be ignored if any of the elements is a limit',
  612. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  613. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  614. );
  615. // See: https://github.com/ckeditor/ckeditor5/issues/1265.
  616. it( 'should proper merge two elements which are inside limit element', () => {
  617. setData( model,
  618. '<blockLimit>' +
  619. '<blockQuote>' +
  620. '<paragraph>Foo</paragraph>' +
  621. '</blockQuote>' +
  622. '<paragraph>[]Bar</paragraph>' +
  623. '</blockLimit>'
  624. );
  625. model.modifySelection( doc.selection, { direction: 'backward' } );
  626. deleteContent( model, doc.selection );
  627. expect( getData( model ) ).to.equal(
  628. '<blockLimit>' +
  629. '<blockQuote>' +
  630. '<paragraph>Foo[]Bar</paragraph>' +
  631. '</blockQuote>' +
  632. '</blockLimit>' );
  633. } );
  634. it( 'should proper merge elements which are inside limit element (nested elements)', () => {
  635. setData( model,
  636. '<blockQuote>' +
  637. '<blockLimit>' +
  638. '<blockQuote>' +
  639. '<paragraph>Foo.</paragraph>' +
  640. '<blockQuote>' +
  641. '<paragraph>Foo</paragraph>' +
  642. '</blockQuote>' +
  643. '</blockQuote>' +
  644. '<paragraph>[]Bar</paragraph>' +
  645. '</blockLimit>' +
  646. '</blockQuote>'
  647. );
  648. model.modifySelection( doc.selection, { direction: 'backward' } );
  649. deleteContent( model, doc.selection );
  650. expect( getData( model ) ).to.equal(
  651. '<blockQuote>' +
  652. '<blockLimit>' +
  653. '<blockQuote>' +
  654. '<paragraph>Foo.</paragraph>' +
  655. '<blockQuote>' +
  656. '<paragraph>Foo[]Bar</paragraph>' +
  657. '</blockQuote>' +
  658. '</blockQuote>' +
  659. '</blockLimit>' +
  660. '</blockQuote>'
  661. );
  662. } );
  663. } );
  664. describe( 'should leave a paragraph if the entire content was selected', () => {
  665. beforeEach( () => {
  666. model = new Model();
  667. doc = model.document;
  668. doc.createRoot();
  669. const schema = model.schema;
  670. schema.register( 'div', {
  671. inheritAllFrom: '$block',
  672. isLimit: true
  673. } );
  674. schema.register( 'article', {
  675. inheritAllFrom: '$block',
  676. isLimit: true
  677. } );
  678. schema.register( 'image', {
  679. allowWhere: '$text',
  680. isObject: true
  681. } );
  682. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  683. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  684. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  685. schema.extend( '$text', { allowIn: '$root' } );
  686. schema.extend( 'image', { allowIn: '$root' } );
  687. schema.extend( 'image', { allowIn: 'heading1' } );
  688. schema.extend( 'heading1', { allowIn: 'div' } );
  689. schema.extend( 'paragraph', { allowIn: 'div' } );
  690. schema.extend( 'heading1', { allowIn: 'article' } );
  691. schema.extend( 'heading2', { allowIn: 'article' } );
  692. } );
  693. test(
  694. 'but not if only one block was selected',
  695. '<heading1>[xx]</heading1>',
  696. '<heading1>[]</heading1>'
  697. );
  698. test(
  699. 'when the entire heading and paragraph were selected',
  700. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  701. '<paragraph>[]</paragraph>'
  702. );
  703. test(
  704. 'when the entire content was selected',
  705. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  706. '<paragraph>[]</paragraph>'
  707. );
  708. test(
  709. 'inside the limit element when the entire heading and paragraph were inside',
  710. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  711. '<div><paragraph>[]</paragraph></div>'
  712. );
  713. test(
  714. 'but not if schema does not accept paragraph in limit element',
  715. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  716. '<article><heading1>[]</heading1></article>'
  717. );
  718. test(
  719. 'but not if selection is not containing the whole content',
  720. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  721. '<image></image><heading1>[]</heading1>'
  722. );
  723. test(
  724. 'but not if only single element is selected',
  725. '<heading1>[<image></image>xx]</heading1>',
  726. '<heading1>[]</heading1>'
  727. );
  728. it( 'when root element was not added as Schema limits work fine as well', () => {
  729. doc.createRoot( 'paragraph', 'paragraphRoot' );
  730. setData(
  731. model,
  732. 'x[<image></image><image></image>]z',
  733. { rootName: 'paragraphRoot' }
  734. );
  735. deleteContent( model, doc.selection );
  736. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  737. .to.equal( 'x[]z' );
  738. } );
  739. test(
  740. 'but not if the flag "doNotResetEntireContent" is set to true',
  741. '<heading1>[</heading1><paragraph>]</paragraph>',
  742. '<heading1>[]</heading1>',
  743. {
  744. doNotResetEntireContent: true
  745. }
  746. );
  747. } );
  748. function test( title, input, output, options ) {
  749. it( title, () => {
  750. model.enqueueChange( 'transparent', () => {
  751. setData( model, input );
  752. deleteContent( model, doc.selection, options );
  753. } );
  754. expect( getData( model ) ).to.equal( output );
  755. } );
  756. }
  757. } );
  758. } );