deletecontent.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  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. { merge: true }
  63. );
  64. } );
  65. describe( 'with text attributes', () => {
  66. beforeEach( () => {
  67. doc = new Document();
  68. doc.createRoot();
  69. const schema = doc.schema;
  70. schema.registerItem( 'image', '$inline' );
  71. schema.registerItem( 'paragraph', '$block' );
  72. schema.allow( { name: '$text', inside: '$root' } );
  73. schema.allow( { name: '$text', attributes: [ 'bold', 'italic' ] } );
  74. } );
  75. it( 'deletes characters (first half has attrs)', () => {
  76. setData( doc, '<$text bold="true">fo[o</$text>b]ar' );
  77. deleteContent( doc.selection, doc.batch() );
  78. expect( getData( doc ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  79. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  80. } );
  81. it( 'deletes characters (2nd half has attrs)', () => {
  82. setData( doc, 'fo[o<$text bold="true">b]ar</$text>' );
  83. deleteContent( doc.selection, doc.batch() );
  84. expect( getData( doc ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  85. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  86. } );
  87. it( 'clears selection attrs when emptied content', () => {
  88. setData( doc, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  89. deleteContent( doc.selection, doc.batch() );
  90. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  91. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  92. } );
  93. it( 'leaves selection attributes when text contains them', () => {
  94. setData(
  95. doc,
  96. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  97. {
  98. selectionAttributes: {
  99. bold: true
  100. }
  101. }
  102. );
  103. deleteContent( doc.selection, doc.batch() );
  104. expect( getData( doc ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  105. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  106. } );
  107. } );
  108. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  109. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  110. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  111. // should not happen unless we're talking about lists or tables, but these features will need to cover
  112. // their scenarios themselves. In all generic scenarios elements are never nested.
  113. //
  114. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  115. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  116. // Those case should, again, be handled by their specific implementations.
  117. describe( 'in multi-element scenarios', () => {
  118. beforeEach( () => {
  119. doc = new Document();
  120. doc.createRoot();
  121. const schema = doc.schema;
  122. schema.registerItem( 'paragraph', '$block' );
  123. schema.registerItem( 'heading1', '$block' );
  124. schema.registerItem( 'pchild' );
  125. schema.registerItem( 'pparent' );
  126. schema.registerItem( 'image', '$inline' );
  127. schema.allow( { name: 'pchild', inside: 'paragraph' } );
  128. schema.allow( { name: '$text', inside: 'pchild' } );
  129. schema.allow( { name: 'paragraph', inside: 'pparent' } );
  130. schema.allow( { name: 'pparent', inside: '$root' } );
  131. schema.allow( { name: '$text', inside: 'pparent' } );
  132. schema.allow( { name: 'paragraph', attributes: [ 'align' ] } );
  133. } );
  134. test(
  135. 'do not merge when no need to',
  136. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  137. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>',
  138. { merge: true }
  139. );
  140. test(
  141. 'merges second element into the first one (same name)',
  142. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  143. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  144. { merge: true }
  145. );
  146. test(
  147. 'does not merge second element into the first one (same name, !option.merge)',
  148. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  149. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>'
  150. );
  151. test(
  152. 'merges second element into the first one (same name)',
  153. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  154. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  155. { merge: true }
  156. );
  157. test(
  158. 'merges second element into the first one (different name)',
  159. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  160. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>',
  161. { merge: true }
  162. );
  163. // Note: in all these cases we ignore the direction of merge.
  164. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  165. // forward and backward delete.
  166. it( 'merges second element into the first one (different name, backward selection)', () => {
  167. setData(
  168. doc,
  169. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  170. { lastRangeBackward: true }
  171. );
  172. deleteContent( doc.selection, doc.batch(), { merge: true } );
  173. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  174. } );
  175. test(
  176. 'merges second element into the first one (different attrs)',
  177. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  178. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>',
  179. { merge: true }
  180. );
  181. test(
  182. 'merges second element to an empty first element',
  183. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  184. '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>',
  185. { merge: true }
  186. );
  187. test(
  188. 'merges empty element into the first element',
  189. '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
  190. '<heading1>f[]</heading1><paragraph>x</paragraph>',
  191. { merge: true }
  192. );
  193. test(
  194. 'leaves just one element when all selected',
  195. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  196. '<heading1>[]</heading1>',
  197. { merge: true }
  198. );
  199. it( 'uses remove delta instead of merge delta if merged element is empty', () => {
  200. setData( doc, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  201. const batch = doc.batch();
  202. const spyMerge = sinon.spy( batch, 'merge' );
  203. const spyRemove = sinon.spy( batch, 'remove' );
  204. deleteContent( doc.selection, batch, { merge: true } );
  205. expect( getData( doc ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  206. expect( spyMerge.called ).to.be.false;
  207. expect( spyRemove.called ).to.be.true;
  208. } );
  209. it( 'does not try to move the second block if not needed', () => {
  210. setData( doc, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
  211. const batch = doc.batch();
  212. const spyMerge = sinon.spy( batch, 'merge' );
  213. const spyMove = sinon.spy( batch, 'move' );
  214. deleteContent( doc.selection, batch, { merge: true } );
  215. expect( getData( doc ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
  216. expect( spyMove.called ).to.be.false;
  217. expect( spyMerge.called ).to.be.true;
  218. } );
  219. // Note: in all these cases we ignore the direction of merge.
  220. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  221. // forward and backward delete.
  222. describe( 'with nested elements', () => {
  223. test(
  224. 'merges elements when deep nested',
  225. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  226. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>',
  227. { merge: true }
  228. );
  229. it( 'merges elements when deep nested (3rd level)', () => {
  230. const root = doc.getRoot();
  231. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  232. // <pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>
  233. // <pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>
  234. root.appendChildren(
  235. new Element( 'pparent', null, [
  236. 'x',
  237. new Element( 'paragraph', null, [
  238. 'x',
  239. new Element( 'pchild', null, 'foo' )
  240. ] )
  241. ] )
  242. );
  243. root.appendChildren(
  244. new Element( 'pparent', null, [
  245. new Element( 'paragraph', null, [
  246. new Element( 'pchild', null, 'bar' ),
  247. 'y'
  248. ] ),
  249. 'y'
  250. ] )
  251. );
  252. const range = new Range(
  253. new Position( doc.getRoot(), [ 0, 1, 1, 2 ] ), // fo[o
  254. new Position( doc.getRoot(), [ 1, 0, 0, 1 ] ) // b]ar
  255. );
  256. doc.selection.setRanges( [ range ] );
  257. deleteContent( doc.selection, doc.batch(), { merge: true } );
  258. expect( getData( doc ) )
  259. .to.equal( '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>' );
  260. } );
  261. test(
  262. 'merges elements when left end deep nested',
  263. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
  264. '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>',
  265. { merge: true }
  266. );
  267. test(
  268. 'merges elements when right end deep nested',
  269. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
  270. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>',
  271. { merge: true }
  272. );
  273. it( 'merges elements when left end deep nested (3rd level)', () => {
  274. const root = doc.getRoot();
  275. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  276. // <pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent><paragraph>b]om</paragraph>
  277. root.appendChildren(
  278. new Element( 'pparent', null, [
  279. 'x',
  280. new Element( 'paragraph', null, [
  281. 'foo',
  282. new Element( 'pchild', null, 'bar' )
  283. ] )
  284. ] )
  285. );
  286. root.appendChildren(
  287. new Element( 'paragraph', null, 'bom' )
  288. );
  289. const range = new Range(
  290. new Position( doc.getRoot(), [ 0, 1, 3, 2 ] ), // ba[r
  291. new Position( doc.getRoot(), [ 1, 1 ] ) // b]om
  292. );
  293. doc.selection.setRanges( [ range ] );
  294. deleteContent( doc.selection, doc.batch(), { merge: true } );
  295. expect( getData( doc ) )
  296. .to.equal( '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>' );
  297. } );
  298. test(
  299. 'merges elements when right end deep nested (in an empty container)',
  300. '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
  301. '<paragraph>fo[]</paragraph>',
  302. { merge: true }
  303. );
  304. test(
  305. 'merges elements when left end deep nested (in an empty container)',
  306. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  307. '<paragraph><pchild>[]ar</pchild></paragraph><paragraph>x</paragraph>',
  308. { merge: true }
  309. );
  310. it( 'merges elements when left end deep nested (3rd level)', () => {
  311. const root = doc.getRoot();
  312. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  313. // <paragraph>fo[o</paragraph><pparent><paragraph><pchild>bar]</pchild></paragraph></pparent>
  314. root.appendChildren(
  315. new Element( 'paragraph', null, 'foo' )
  316. );
  317. root.appendChildren(
  318. new Element( 'pparent', null, [
  319. new Element( 'paragraph', null, [
  320. new Element( 'pchild', null, 'bar' )
  321. ] )
  322. ] )
  323. );
  324. const range = new Range(
  325. new Position( doc.getRoot(), [ 0, 2 ] ), // f[oo
  326. new Position( doc.getRoot(), [ 1, 0, 0, 3 ] ) // bar]
  327. );
  328. doc.selection.setRanges( [ range ] );
  329. deleteContent( doc.selection, doc.batch(), { merge: true } );
  330. expect( getData( doc ) )
  331. .to.equal( '<paragraph>fo[]</paragraph>' );
  332. } );
  333. } );
  334. describe( 'with object elements', () => {
  335. beforeEach( () => {
  336. const schema = doc.schema;
  337. schema.registerItem( 'blockWidget' );
  338. schema.registerItem( 'nestedEditable' );
  339. schema.allow( { name: 'blockWidget', inside: '$root' } );
  340. schema.allow( { name: 'nestedEditable', inside: 'blockWidget' } );
  341. schema.allow( { name: '$text', inside: 'nestedEditable' } );
  342. schema.objects.add( 'blockWidget' );
  343. schema.limits.add( 'nestedEditable' );
  344. } );
  345. test(
  346. 'does not merge an object element (if it is first)',
  347. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  348. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>',
  349. { merge: true }
  350. );
  351. test(
  352. 'does not merge an object element (if it is second)',
  353. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  354. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>',
  355. { merge: true }
  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.registerItem( 'image', '$inline' );
  370. schema.registerItem( 'paragraph', '$block' );
  371. schema.registerItem( 'heading1', '$block' );
  372. schema.registerItem( 'blockWidget' );
  373. schema.registerItem( 'restrictedRoot' );
  374. schema.allow( { name: '$block', inside: '$root' } );
  375. schema.allow( { name: 'blockWidget', inside: '$root' } );
  376. schema.allow( { name: 'blockWidget', inside: 'restrictedRoot' } );
  377. } );
  378. // See also "in simple scenarios => deletes an element".
  379. it( 'deletes two inline elements', () => {
  380. setData(
  381. doc,
  382. 'x[<image></image><image></image>]z',
  383. { rootName: 'paragraphRoot' }
  384. );
  385. deleteContent( doc.selection, doc.batch() );
  386. expect( getData( doc, { rootName: 'paragraphRoot' } ) )
  387. .to.equal( 'x[]z' );
  388. } );
  389. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  390. setData(
  391. doc,
  392. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  393. { rootName: 'bodyRoot' }
  394. );
  395. deleteContent( doc.selection, doc.batch() );
  396. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  397. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  398. } );
  399. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  400. setData(
  401. doc,
  402. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  403. { rootName: 'bodyRoot' }
  404. );
  405. deleteContent( doc.selection, doc.batch() );
  406. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  407. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  408. } );
  409. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  410. setData(
  411. doc,
  412. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  413. { rootName: 'bodyRoot' }
  414. );
  415. deleteContent( doc.selection, doc.batch() );
  416. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  417. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  418. } );
  419. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  420. setData(
  421. doc,
  422. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  423. { rootName: 'bodyRoot' }
  424. );
  425. deleteContent( doc.selection, doc.batch() );
  426. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  427. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  428. } );
  429. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  430. setData(
  431. doc,
  432. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  433. { rootName: 'bodyRoot' }
  434. );
  435. deleteContent( doc.selection, doc.batch() );
  436. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  437. .to.equal( '<paragraph>[]</paragraph>' );
  438. } );
  439. it( 'does not create a paragraph when it is not allowed', () => {
  440. setData(
  441. doc,
  442. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  443. { rootName: 'restrictedRoot' }
  444. );
  445. deleteContent( doc.selection, doc.batch() );
  446. expect( getData( doc, { rootName: 'restrictedRoot' } ) )
  447. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  448. } );
  449. } );
  450. describe( 'integration with inline limit elements', () => {
  451. beforeEach( () => {
  452. doc = new Document();
  453. doc.createRoot();
  454. const schema = doc.schema;
  455. schema.registerItem( 'inlineLimit' );
  456. schema.allow( { name: 'inlineLimit', inside: '$root' } );
  457. schema.allow( { name: '$text', inside: 'inlineLimit' } );
  458. schema.limits.add( 'inlineLimit' );
  459. schema.allow( { name: '$inline', inside: '$root' } );
  460. schema.registerItem( 'x' );
  461. schema.allow( { name: '$text', inside: 'x' } );
  462. schema.allow( { name: 'x', inside: '$root' } );
  463. } );
  464. test(
  465. 'should delete inside inline limit element',
  466. '<inlineLimit>foo [bar] baz</inlineLimit>',
  467. '<inlineLimit>foo [] baz</inlineLimit>'
  468. );
  469. test(
  470. 'should delete whole inline limit element',
  471. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  472. 'x[]x'
  473. );
  474. test(
  475. 'should delete from two inline limit elements',
  476. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  477. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  478. );
  479. test(
  480. 'merge option should be ignored if both elements are limits',
  481. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  482. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>',
  483. { merge: true }
  484. );
  485. test(
  486. 'merge option should be ignored if the first element is a limit',
  487. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  488. '<inlineLimit>foo []</inlineLimit><x> qux</x>',
  489. { merge: true }
  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. { merge: true }
  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. );
  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. { merge: true }
  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. { merge: true }
  535. );
  536. } );
  537. function test( title, input, output, options ) {
  538. it( title, () => {
  539. setData( doc, input );
  540. deleteContent( doc.selection, doc.batch(), options );
  541. expect( getData( doc ) ).to.equal( output );
  542. } );
  543. }
  544. } );
  545. } );