deletecontent.js 25 KB

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