deletecontent.js 30 KB

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