deletecontent.js 26 KB

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