deletecontent.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  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]</paragraph>',
  189. '<heading1>[]</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.registerItem( 'image', '$inline' );
  355. schema.registerItem( 'paragraph', '$block' );
  356. schema.registerItem( 'heading1', '$block' );
  357. schema.registerItem( 'blockWidget' );
  358. schema.registerItem( 'restrictedRoot' );
  359. schema.allow( { name: '$block', inside: '$root' } );
  360. schema.allow( { name: 'blockWidget', inside: '$root' } );
  361. schema.allow( { name: 'blockWidget', inside: 'restrictedRoot' } );
  362. } );
  363. // See also "in simple scenarios => deletes an element".
  364. it( 'deletes two inline elements', () => {
  365. setData(
  366. doc,
  367. 'x[<image></image><image></image>]z',
  368. { rootName: 'paragraphRoot' }
  369. );
  370. deleteContent( doc.selection, doc.batch() );
  371. expect( getData( doc, { rootName: 'paragraphRoot' } ) )
  372. .to.equal( 'x[]z' );
  373. } );
  374. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  375. setData(
  376. doc,
  377. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  378. { rootName: 'bodyRoot' }
  379. );
  380. deleteContent( doc.selection, doc.batch() );
  381. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  382. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  383. } );
  384. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  385. setData(
  386. doc,
  387. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  388. { rootName: 'bodyRoot' }
  389. );
  390. deleteContent( doc.selection, doc.batch() );
  391. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  392. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  393. } );
  394. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  395. setData(
  396. doc,
  397. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  398. { rootName: 'bodyRoot' }
  399. );
  400. deleteContent( doc.selection, doc.batch() );
  401. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  402. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  403. } );
  404. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  405. setData(
  406. doc,
  407. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  408. { rootName: 'bodyRoot' }
  409. );
  410. deleteContent( doc.selection, doc.batch() );
  411. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  412. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  413. } );
  414. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  415. setData(
  416. doc,
  417. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  418. { rootName: 'bodyRoot' }
  419. );
  420. deleteContent( doc.selection, doc.batch() );
  421. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  422. .to.equal( '<paragraph>[]</paragraph>' );
  423. } );
  424. it( 'does not create a paragraph when it is not allowed', () => {
  425. setData(
  426. doc,
  427. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  428. { rootName: 'restrictedRoot' }
  429. );
  430. deleteContent( doc.selection, doc.batch() );
  431. expect( getData( doc, { rootName: 'restrictedRoot' } ) )
  432. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  433. } );
  434. } );
  435. describe( 'integration with inline limit elements', () => {
  436. beforeEach( () => {
  437. doc = new Document();
  438. doc.createRoot();
  439. const schema = doc.schema;
  440. schema.registerItem( 'inlineLimit' );
  441. schema.allow( { name: 'inlineLimit', inside: '$root' } );
  442. schema.allow( { name: '$text', inside: 'inlineLimit' } );
  443. schema.limits.add( 'inlineLimit' );
  444. schema.allow( { name: '$inline', inside: '$root' } );
  445. schema.registerItem( 'x' );
  446. schema.allow( { name: '$text', inside: 'x' } );
  447. schema.allow( { name: 'x', inside: '$root' } );
  448. } );
  449. test(
  450. 'should delete inside inline limit element',
  451. '<inlineLimit>foo [bar] baz</inlineLimit>',
  452. '<inlineLimit>foo [] baz</inlineLimit>'
  453. );
  454. test(
  455. 'should delete whole inline limit element',
  456. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  457. 'x[]x'
  458. );
  459. test(
  460. 'should delete from two inline limit elements',
  461. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  462. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  463. );
  464. test(
  465. 'merge option should be ignored if both elements are limits',
  466. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  467. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  468. );
  469. test(
  470. 'merge option should be ignored if the first element is a limit',
  471. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  472. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  473. );
  474. test(
  475. 'merge option should be ignored if the second element is a limit',
  476. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  477. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  478. );
  479. } );
  480. describe( 'integration with block limit elements', () => {
  481. beforeEach( () => {
  482. doc = new Document();
  483. doc.createRoot();
  484. const schema = doc.schema;
  485. schema.registerItem( 'blockLimit' );
  486. schema.allow( { name: 'blockLimit', inside: '$root' } );
  487. schema.allow( { name: '$block', inside: 'blockLimit' } );
  488. schema.limits.add( 'blockLimit' );
  489. schema.registerItem( 'paragraph', '$block' );
  490. } );
  491. test(
  492. 'should delete inside block limit element',
  493. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  494. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  495. { leaveUnmerged: true }
  496. );
  497. test(
  498. 'should delete inside block limit element (with merge)',
  499. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  500. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  501. );
  502. test(
  503. 'should delete whole block limit element',
  504. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  505. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  506. );
  507. test(
  508. 'should delete from two block limit elements',
  509. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  510. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  511. );
  512. test(
  513. 'merge option should be ignored if any of the elements is a limit',
  514. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  515. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  516. );
  517. } );
  518. function test( title, input, output, options ) {
  519. it( title, () => {
  520. setData( doc, input );
  521. deleteContent( doc.selection, doc.batch(), options );
  522. expect( getData( doc ) ).to.equal( output );
  523. } );
  524. }
  525. } );
  526. } );