8
0

deletecontent.js 25 KB

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