deletecontent.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /**
  2. * @license Copyright (c) 2003-2018, 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 (paragraph selected)', () => {
  473. setData(
  474. model,
  475. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  476. { rootName: 'bodyRoot' }
  477. );
  478. deleteContent( model, doc.selection );
  479. expect( getData( model, { rootName: 'bodyRoot' } ) )
  480. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  481. } );
  482. it( 'creates a paragraph when text is not allowed (custom selection)', () => {
  483. setData(
  484. model,
  485. '<paragraph>[x]</paragraph><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  486. { rootName: 'bodyRoot' }
  487. );
  488. const root = doc.getRoot( 'bodyRoot' );
  489. const selection = new Selection( [
  490. new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) )
  491. ] );
  492. deleteContent( model, selection );
  493. expect( getData( model, { rootName: 'bodyRoot' } ) )
  494. .to.equal( '<paragraph>[x]</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  495. } );
  496. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  497. setData(
  498. model,
  499. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  500. { rootName: 'bodyRoot' }
  501. );
  502. deleteContent( model, doc.selection );
  503. expect( getData( model, { rootName: 'bodyRoot' } ) )
  504. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  505. } );
  506. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  507. setData(
  508. model,
  509. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>z</paragraph>',
  510. { rootName: 'bodyRoot' }
  511. );
  512. // [<heading1>yyy</heading1>]
  513. const range = new Range(
  514. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  515. new Position( doc.getRoot( 'bodyRoot' ), [ 2 ] )
  516. );
  517. deleteContent( model, new Selection( range ) );
  518. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  519. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  520. } );
  521. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  522. setData(
  523. model,
  524. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  525. { rootName: 'bodyRoot' }
  526. );
  527. // [<heading1>yyy</heading1><paragraph>yyy</paragraph>]
  528. const range = new Range(
  529. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  530. new Position( doc.getRoot( 'bodyRoot' ), [ 3 ] )
  531. );
  532. deleteContent( model, new Selection( range ) );
  533. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  534. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  535. } );
  536. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  537. setData(
  538. model,
  539. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  540. { rootName: 'bodyRoot' }
  541. );
  542. deleteContent( model, doc.selection );
  543. expect( getData( model, { rootName: 'bodyRoot' } ) )
  544. .to.equal( '<paragraph>[]</paragraph>' );
  545. } );
  546. it( 'does not create a paragraph when it is not allowed', () => {
  547. setData(
  548. model,
  549. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  550. { rootName: 'restrictedRoot' }
  551. );
  552. deleteContent( model, doc.selection );
  553. expect( getData( model, { rootName: 'restrictedRoot' } ) )
  554. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  555. } );
  556. } );
  557. describe( 'integration with inline limit elements', () => {
  558. beforeEach( () => {
  559. model = new Model();
  560. doc = model.document;
  561. doc.createRoot();
  562. const schema = model.schema;
  563. schema.register( 'inlineLimit', {
  564. isLimit: true,
  565. allowIn: '$root'
  566. } );
  567. schema.extend( '$text', {
  568. allowIn: [ 'inlineLimit', '$root', 'x' ]
  569. } );
  570. schema.register( 'x', { allowIn: '$root' } );
  571. } );
  572. test(
  573. 'should delete inside inline limit element',
  574. '<inlineLimit>foo [bar] baz</inlineLimit>',
  575. '<inlineLimit>foo [] baz</inlineLimit>'
  576. );
  577. test(
  578. 'should delete whole inline limit element',
  579. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  580. 'x[]x'
  581. );
  582. test(
  583. 'should delete from two inline limit elements',
  584. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  585. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  586. );
  587. test(
  588. 'merge option should be ignored if both elements are limits',
  589. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  590. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  591. );
  592. test(
  593. 'merge option should be ignored if the first element is a limit',
  594. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  595. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  596. );
  597. test(
  598. 'merge option should be ignored if the second element is a limit',
  599. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  600. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  601. );
  602. } );
  603. describe( 'integration with block limit elements', () => {
  604. beforeEach( () => {
  605. model = new Model();
  606. doc = model.document;
  607. doc.createRoot();
  608. const schema = model.schema;
  609. schema.register( 'blockLimit', {
  610. isLimit: true
  611. } );
  612. schema.extend( 'blockLimit', { allowIn: '$root' } );
  613. schema.extend( '$block', { allowIn: 'blockLimit' } );
  614. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  615. schema.register( 'blockQuote', {
  616. allowWhere: '$block',
  617. allowContentOf: '$root'
  618. } );
  619. } );
  620. test(
  621. 'should delete inside block limit element',
  622. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  623. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  624. { leaveUnmerged: true }
  625. );
  626. test(
  627. 'should delete inside block limit element (with merge)',
  628. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  629. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  630. );
  631. test(
  632. 'should delete whole block limit element',
  633. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  634. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  635. );
  636. test(
  637. 'should delete from two block limit elements',
  638. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  639. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  640. );
  641. test(
  642. 'merge option should be ignored if any of the elements is a limit',
  643. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  644. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  645. );
  646. // See: https://github.com/ckeditor/ckeditor5/issues/1265.
  647. it( 'should proper merge two elements which are inside limit element', () => {
  648. setData( model,
  649. '<blockLimit>' +
  650. '<blockQuote>' +
  651. '<paragraph>Foo</paragraph>' +
  652. '</blockQuote>' +
  653. '<paragraph>[]Bar</paragraph>' +
  654. '</blockLimit>'
  655. );
  656. model.modifySelection( doc.selection, { direction: 'backward' } );
  657. deleteContent( model, doc.selection );
  658. expect( getData( model ) ).to.equal(
  659. '<blockLimit>' +
  660. '<blockQuote>' +
  661. '<paragraph>Foo[]Bar</paragraph>' +
  662. '</blockQuote>' +
  663. '</blockLimit>' );
  664. } );
  665. it( 'should proper merge elements which are inside limit element (nested elements)', () => {
  666. setData( model,
  667. '<blockQuote>' +
  668. '<blockLimit>' +
  669. '<blockQuote>' +
  670. '<paragraph>Foo.</paragraph>' +
  671. '<blockQuote>' +
  672. '<paragraph>Foo</paragraph>' +
  673. '</blockQuote>' +
  674. '</blockQuote>' +
  675. '<paragraph>[]Bar</paragraph>' +
  676. '</blockLimit>' +
  677. '</blockQuote>'
  678. );
  679. model.modifySelection( doc.selection, { direction: 'backward' } );
  680. deleteContent( model, doc.selection );
  681. expect( getData( model ) ).to.equal(
  682. '<blockQuote>' +
  683. '<blockLimit>' +
  684. '<blockQuote>' +
  685. '<paragraph>Foo.</paragraph>' +
  686. '<blockQuote>' +
  687. '<paragraph>Foo[]Bar</paragraph>' +
  688. '</blockQuote>' +
  689. '</blockQuote>' +
  690. '</blockLimit>' +
  691. '</blockQuote>'
  692. );
  693. } );
  694. } );
  695. describe( 'should leave a paragraph if the entire content was selected', () => {
  696. beforeEach( () => {
  697. model = new Model();
  698. doc = model.document;
  699. doc.createRoot();
  700. const schema = model.schema;
  701. schema.register( 'div', {
  702. inheritAllFrom: '$block',
  703. isLimit: true
  704. } );
  705. schema.register( 'article', {
  706. inheritAllFrom: '$block',
  707. isLimit: true
  708. } );
  709. schema.register( 'image', {
  710. allowWhere: '$text',
  711. isObject: true
  712. } );
  713. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  714. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  715. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  716. schema.extend( '$text', { allowIn: '$root' } );
  717. schema.extend( 'image', { allowIn: '$root' } );
  718. schema.extend( 'image', { allowIn: 'heading1' } );
  719. schema.extend( 'heading1', { allowIn: 'div' } );
  720. schema.extend( 'paragraph', { allowIn: 'div' } );
  721. schema.extend( 'heading1', { allowIn: 'article' } );
  722. schema.extend( 'heading2', { allowIn: 'article' } );
  723. } );
  724. test(
  725. 'but not if only one block was selected',
  726. '<heading1>[xx]</heading1>',
  727. '<heading1>[]</heading1>'
  728. );
  729. test(
  730. 'when the entire heading and paragraph were selected',
  731. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  732. '<paragraph>[]</paragraph>'
  733. );
  734. test(
  735. 'when the entire content was selected',
  736. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  737. '<paragraph>[]</paragraph>'
  738. );
  739. test(
  740. 'inside the limit element when the entire heading and paragraph were inside',
  741. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  742. '<div><paragraph>[]</paragraph></div>'
  743. );
  744. test(
  745. 'but not if schema does not accept paragraph in limit element',
  746. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  747. '<article><heading1>[]</heading1></article>'
  748. );
  749. test(
  750. 'but not if selection is not containing the whole content',
  751. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  752. '<image></image><heading1>[]</heading1>'
  753. );
  754. test(
  755. 'but not if only single element is selected',
  756. '<heading1>[<image></image>xx]</heading1>',
  757. '<heading1>[]</heading1>'
  758. );
  759. it( 'when root element was not added as Schema limits work fine as well', () => {
  760. doc.createRoot( 'paragraph', 'paragraphRoot' );
  761. setData(
  762. model,
  763. 'x[<image></image><image></image>]z',
  764. { rootName: 'paragraphRoot' }
  765. );
  766. deleteContent( model, doc.selection );
  767. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  768. .to.equal( 'x[]z' );
  769. } );
  770. test(
  771. 'but not if the flag "doNotResetEntireContent" is set to true',
  772. '<heading1>[</heading1><paragraph>]</paragraph>',
  773. '<heading1>[]</heading1>',
  774. {
  775. doNotResetEntireContent: true
  776. }
  777. );
  778. } );
  779. function test( title, input, output, options ) {
  780. it( title, () => {
  781. model.enqueueChange( 'transparent', () => {
  782. setData( model, input );
  783. deleteContent( model, doc.selection, options );
  784. } );
  785. expect( getData( model ) ).to.equal( output );
  786. } );
  787. }
  788. } );
  789. } );