deletecontent.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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. schema.allow( { name: 'image', attributes: [ 'a', 'b' ], inside: 'paragraph' } );
  133. schema.allow( { name: 'image', attributes: [ 'b' ], inside: 'heading1' } );
  134. schema.allow( { name: '$text', attributes: [ 'a', 'b', 'c' ], inside: 'paragraph' } );
  135. schema.allow( { name: '$text', attributes: [ 'b' ], inside: 'heading1' } );
  136. schema.allow( { name: '$text', attributes: [ 'c', 'd' ], inside: 'pchild' } );
  137. } );
  138. test(
  139. 'do not merge when no need to',
  140. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  141. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>'
  142. );
  143. test(
  144. 'merges second element into the first one (same name)',
  145. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  146. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
  147. );
  148. test(
  149. 'does not merge second element into the first one (same name, !option.merge)',
  150. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  151. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  152. { leaveUnmerged: true }
  153. );
  154. test(
  155. 'merges second element into the first one (different name)',
  156. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  157. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
  158. );
  159. // Note: in all these cases we ignore the direction of merge.
  160. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  161. // forward and backward delete.
  162. it( 'merges second element into the first one (different name, backward selection)', () => {
  163. setData(
  164. doc,
  165. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  166. { lastRangeBackward: true }
  167. );
  168. deleteContent( doc.selection, doc.batch() );
  169. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  170. } );
  171. test(
  172. 'merges second element into the first one (different attrs)',
  173. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  174. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
  175. );
  176. test(
  177. 'merges second element to an empty first element',
  178. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  179. '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>'
  180. );
  181. test(
  182. 'merges empty element into the first element',
  183. '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
  184. '<heading1>f[]</heading1><paragraph>x</paragraph>'
  185. );
  186. test(
  187. 'leaves just one element when all selected',
  188. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  189. '<heading1>[]bar</heading1>'
  190. );
  191. it( 'uses remove delta instead of merge delta if merged element is empty', () => {
  192. setData( doc, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  193. const batch = doc.batch();
  194. const spyMerge = sinon.spy( batch, 'merge' );
  195. const spyRemove = sinon.spy( batch, 'remove' );
  196. deleteContent( doc.selection, batch );
  197. expect( getData( doc ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  198. expect( spyMerge.called ).to.be.false;
  199. expect( spyRemove.called ).to.be.true;
  200. } );
  201. it( 'does not try to move the second block if not needed', () => {
  202. setData( doc, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
  203. const batch = doc.batch();
  204. const spyMerge = sinon.spy( batch, 'merge' );
  205. const spyMove = sinon.spy( batch, 'move' );
  206. deleteContent( doc.selection, batch );
  207. expect( getData( doc ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
  208. expect( spyMove.called ).to.be.false;
  209. expect( spyMerge.called ).to.be.true;
  210. } );
  211. test(
  212. 'filters out disallowed attributes after merge',
  213. '<heading1>fo[o</heading1><paragraph>b]a<$text a="1" b="1">r</$text><image a="1" b="1"></image></paragraph>',
  214. '<heading1>fo[]a<$text b="1">r</$text><image b="1"></image></heading1>'
  215. );
  216. // Note: in all these cases we ignore the direction of merge.
  217. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  218. // forward and backward delete.
  219. describe( 'with nested elements', () => {
  220. test(
  221. 'merges elements when deep nested',
  222. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  223. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
  224. );
  225. it( 'merges elements when deep nested (3rd level)', () => {
  226. const root = doc.getRoot();
  227. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  228. // <pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>
  229. // <pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>
  230. root.appendChildren(
  231. new Element( 'pparent', null, [
  232. 'x',
  233. new Element( 'paragraph', null, [
  234. 'x',
  235. new Element( 'pchild', null, 'foo' )
  236. ] )
  237. ] )
  238. );
  239. root.appendChildren(
  240. new Element( 'pparent', null, [
  241. new Element( 'paragraph', null, [
  242. new Element( 'pchild', null, 'bar' ),
  243. 'y'
  244. ] ),
  245. 'y'
  246. ] )
  247. );
  248. const range = new Range(
  249. new Position( doc.getRoot(), [ 0, 1, 1, 2 ] ), // fo[o
  250. new Position( doc.getRoot(), [ 1, 0, 0, 1 ] ) // b]ar
  251. );
  252. doc.selection.setRanges( [ range ] );
  253. deleteContent( doc.selection, doc.batch() );
  254. expect( getData( doc ) )
  255. .to.equal( '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>' );
  256. } );
  257. test(
  258. 'merges elements when left end deep nested',
  259. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
  260. '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
  261. );
  262. test(
  263. 'merges elements when right end deep nested',
  264. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
  265. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
  266. );
  267. it( 'merges elements when left end deep nested (3rd level)', () => {
  268. const root = doc.getRoot();
  269. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  270. // <pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent><paragraph>b]om</paragraph>
  271. root.appendChildren(
  272. new Element( 'pparent', null, [
  273. 'x',
  274. new Element( 'paragraph', null, [
  275. 'foo',
  276. new Element( 'pchild', null, 'bar' )
  277. ] )
  278. ] )
  279. );
  280. root.appendChildren(
  281. new Element( 'paragraph', null, 'bom' )
  282. );
  283. const range = new Range(
  284. new Position( doc.getRoot(), [ 0, 1, 3, 2 ] ), // ba[r
  285. new Position( doc.getRoot(), [ 1, 1 ] ) // b]om
  286. );
  287. doc.selection.setRanges( [ range ] );
  288. deleteContent( doc.selection, doc.batch() );
  289. expect( getData( doc ) )
  290. .to.equal( '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>' );
  291. } );
  292. test(
  293. 'merges elements when right end deep nested (in an empty container)',
  294. '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
  295. '<paragraph>fo[]</paragraph>'
  296. );
  297. test(
  298. 'merges elements when left end deep nested (in an empty container)',
  299. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  300. '<paragraph><pchild>[]ar</pchild></paragraph><paragraph>x</paragraph>'
  301. );
  302. it( 'merges elements when left end deep nested (3rd level)', () => {
  303. const root = doc.getRoot();
  304. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  305. // <paragraph>fo[o</paragraph><pparent><paragraph><pchild>bar]</pchild></paragraph></pparent>
  306. root.appendChildren(
  307. new Element( 'paragraph', null, 'foo' )
  308. );
  309. root.appendChildren(
  310. new Element( 'pparent', null, [
  311. new Element( 'paragraph', null, [
  312. new Element( 'pchild', null, 'bar' )
  313. ] )
  314. ] )
  315. );
  316. const range = new Range(
  317. new Position( doc.getRoot(), [ 0, 2 ] ), // f[oo
  318. new Position( doc.getRoot(), [ 1, 0, 0, 3 ] ) // bar]
  319. );
  320. doc.selection.setRanges( [ range ] );
  321. deleteContent( doc.selection, doc.batch() );
  322. expect( getData( doc ) )
  323. .to.equal( '<paragraph>fo[]</paragraph>' );
  324. } );
  325. test(
  326. 'filters out disallowed attributes after left merge',
  327. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text b="true" c="true">z</$text></paragraph>',
  328. '<paragraph>x<pchild>fo[]<$text c="true">z</$text></pchild></paragraph>'
  329. );
  330. test(
  331. 'filters out disallowed attributes after right merge',
  332. '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text c="true" d="true">y]z</$text></pchild></paragraph>',
  333. '<paragraph>fo[]<$text c="true">z</$text></paragraph>'
  334. );
  335. } );
  336. describe( 'with object elements', () => {
  337. beforeEach( () => {
  338. const schema = doc.schema;
  339. schema.registerItem( 'blockWidget' );
  340. schema.registerItem( 'nestedEditable' );
  341. schema.allow( { name: 'blockWidget', inside: '$root' } );
  342. schema.allow( { name: 'nestedEditable', inside: 'blockWidget' } );
  343. schema.allow( { name: '$text', inside: 'nestedEditable' } );
  344. schema.objects.add( 'blockWidget' );
  345. schema.limits.add( 'nestedEditable' );
  346. } );
  347. test(
  348. 'does not merge an object element (if it is first)',
  349. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  350. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
  351. );
  352. test(
  353. 'does not merge an object element (if it is second)',
  354. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  355. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
  356. );
  357. } );
  358. } );
  359. describe( 'in element selections scenarios', () => {
  360. beforeEach( () => {
  361. doc = new Document();
  362. // <p> like root.
  363. doc.createRoot( 'paragraph', 'paragraphRoot' );
  364. // <body> like root.
  365. doc.createRoot( '$root', 'bodyRoot' );
  366. // Special root which allows only blockWidgets inside itself.
  367. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  368. const schema = doc.schema;
  369. schema.limits.add( 'restrictedRoot' );
  370. schema.registerItem( 'image', '$inline' );
  371. schema.registerItem( 'paragraph', '$block' );
  372. schema.registerItem( 'heading1', '$block' );
  373. schema.registerItem( 'blockWidget' );
  374. schema.registerItem( 'restrictedRoot' );
  375. schema.allow( { name: '$block', inside: '$root' } );
  376. schema.allow( { name: 'blockWidget', inside: '$root' } );
  377. schema.allow( { name: 'blockWidget', inside: 'restrictedRoot' } );
  378. } );
  379. // See also "in simple scenarios => deletes an element".
  380. it( 'deletes two inline elements', () => {
  381. doc.schema.limits.add( 'paragraph' );
  382. setData(
  383. doc,
  384. 'x[<image></image><image></image>]z',
  385. { rootName: 'paragraphRoot' }
  386. );
  387. deleteContent( doc.selection, doc.batch() );
  388. expect( getData( doc, { rootName: 'paragraphRoot' } ) )
  389. .to.equal( 'x[]z' );
  390. } );
  391. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  392. setData(
  393. doc,
  394. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  395. { rootName: 'bodyRoot' }
  396. );
  397. deleteContent( doc.selection, doc.batch() );
  398. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  399. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  400. } );
  401. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  402. setData(
  403. doc,
  404. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  405. { rootName: 'bodyRoot' }
  406. );
  407. deleteContent( doc.selection, doc.batch() );
  408. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  409. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  410. } );
  411. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  412. setData(
  413. doc,
  414. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  415. { rootName: 'bodyRoot' }
  416. );
  417. deleteContent( doc.selection, doc.batch() );
  418. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  419. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  420. } );
  421. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  422. setData(
  423. doc,
  424. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  425. { rootName: 'bodyRoot' }
  426. );
  427. deleteContent( doc.selection, doc.batch() );
  428. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  429. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  430. } );
  431. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  432. setData(
  433. doc,
  434. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  435. { rootName: 'bodyRoot' }
  436. );
  437. deleteContent( doc.selection, doc.batch() );
  438. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  439. .to.equal( '<paragraph>[]</paragraph>' );
  440. } );
  441. it( 'does not create a paragraph when it is not allowed', () => {
  442. setData(
  443. doc,
  444. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  445. { rootName: 'restrictedRoot' }
  446. );
  447. deleteContent( doc.selection, doc.batch() );
  448. expect( getData( doc, { rootName: 'restrictedRoot' } ) )
  449. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  450. } );
  451. } );
  452. describe( 'integration with inline limit elements', () => {
  453. beforeEach( () => {
  454. doc = new Document();
  455. doc.createRoot();
  456. const schema = doc.schema;
  457. schema.registerItem( 'inlineLimit' );
  458. schema.allow( { name: 'inlineLimit', inside: '$root' } );
  459. schema.allow( { name: '$text', inside: 'inlineLimit' } );
  460. schema.limits.add( 'inlineLimit' );
  461. schema.allow( { name: '$inline', inside: '$root' } );
  462. schema.registerItem( 'x' );
  463. schema.allow( { name: '$text', inside: 'x' } );
  464. schema.allow( { name: 'x', inside: '$root' } );
  465. } );
  466. test(
  467. 'should delete inside inline limit element',
  468. '<inlineLimit>foo [bar] baz</inlineLimit>',
  469. '<inlineLimit>foo [] baz</inlineLimit>'
  470. );
  471. test(
  472. 'should delete whole inline limit element',
  473. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  474. 'x[]x'
  475. );
  476. test(
  477. 'should delete from two inline limit elements',
  478. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  479. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  480. );
  481. test(
  482. 'merge option should be ignored if both elements are limits',
  483. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  484. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  485. );
  486. test(
  487. 'merge option should be ignored if the first element is a limit',
  488. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  489. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  490. );
  491. test(
  492. 'merge option should be ignored if the second element is a limit',
  493. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  494. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  495. );
  496. } );
  497. describe( 'integration with block limit elements', () => {
  498. beforeEach( () => {
  499. doc = new Document();
  500. doc.createRoot();
  501. const schema = doc.schema;
  502. schema.registerItem( 'blockLimit' );
  503. schema.allow( { name: 'blockLimit', inside: '$root' } );
  504. schema.allow( { name: '$block', inside: 'blockLimit' } );
  505. schema.limits.add( 'blockLimit' );
  506. schema.registerItem( 'paragraph', '$block' );
  507. } );
  508. test(
  509. 'should delete inside block limit element',
  510. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  511. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  512. { leaveUnmerged: true }
  513. );
  514. test(
  515. 'should delete inside block limit element (with merge)',
  516. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  517. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  518. );
  519. test(
  520. 'should delete whole block limit element',
  521. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  522. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  523. );
  524. test(
  525. 'should delete from two block limit elements',
  526. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  527. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  528. );
  529. test(
  530. 'merge option should be ignored if any of the elements is a limit',
  531. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  532. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  533. );
  534. } );
  535. describe( 'should leave a paragraph if the entire content was selected', () => {
  536. beforeEach( () => {
  537. doc = new Document();
  538. doc.createRoot();
  539. const schema = doc.schema;
  540. schema.registerItem( 'div', '$block' );
  541. schema.limits.add( 'div' );
  542. schema.registerItem( 'article', '$block' );
  543. schema.limits.add( 'article' );
  544. schema.registerItem( 'image', '$inline' );
  545. schema.objects.add( 'image' );
  546. schema.registerItem( 'paragraph', '$block' );
  547. schema.registerItem( 'heading1', '$block' );
  548. schema.registerItem( 'heading2', '$block' );
  549. schema.allow( { name: '$text', inside: '$root' } );
  550. schema.allow( { name: 'image', inside: '$root' } );
  551. schema.allow( { name: 'image', inside: 'heading1' } );
  552. schema.allow( { name: 'heading1', inside: 'div' } );
  553. schema.allow( { name: 'paragraph', inside: 'div' } );
  554. schema.allow( { name: 'heading1', inside: 'article' } );
  555. schema.allow( { name: 'heading2', inside: 'article' } );
  556. } );
  557. test(
  558. 'but not if only one block was selected',
  559. '<heading1>[xx]</heading1>',
  560. '<heading1>[]</heading1>'
  561. );
  562. test(
  563. 'when the entire heading and paragraph were selected',
  564. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  565. '<paragraph>[]</paragraph>'
  566. );
  567. test(
  568. 'when the entire content was selected',
  569. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  570. '<paragraph>[]</paragraph>'
  571. );
  572. test(
  573. 'inside the limit element when the entire heading and paragraph were inside',
  574. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  575. '<div><paragraph>[]</paragraph></div>'
  576. );
  577. test(
  578. 'but not if schema does not accept paragraph in limit element',
  579. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  580. '<article><heading1>[]</heading1></article>'
  581. );
  582. test(
  583. 'but not if selection is not containing the whole content',
  584. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  585. '<image></image><heading1>[]</heading1>'
  586. );
  587. test(
  588. 'but not if only single element is selected',
  589. '<heading1>[<image></image>xx]</heading1>',
  590. '<heading1>[]</heading1>'
  591. );
  592. it( 'when root element was not added as Schema.limits works fine as well', () => {
  593. doc.createRoot( 'paragraph', 'paragraphRoot' );
  594. setData(
  595. doc,
  596. 'x[<image></image><image></image>]z',
  597. { rootName: 'paragraphRoot' }
  598. );
  599. deleteContent( doc.selection, doc.batch() );
  600. expect( getData( doc, { rootName: 'paragraphRoot' } ) )
  601. .to.equal( 'x[]z' );
  602. } );
  603. test(
  604. 'but not if the flag "doNotResetEntireContent" is set to true',
  605. '<heading1>[</heading1><paragraph>]</paragraph>',
  606. '<heading1>[]</heading1>',
  607. {
  608. doNotResetEntireContent: true
  609. }
  610. );
  611. } );
  612. function test( title, input, output, options ) {
  613. it( title, () => {
  614. setData( doc, input );
  615. deleteContent( doc.selection, doc.batch(), options );
  616. expect( getData( doc ) ).to.equal( output );
  617. } );
  618. }
  619. } );
  620. } );