8
0

deletecontent.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Model from '../../../src/model/model';
  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/model/utils/deletecontent';
  10. import { setData, getData } from '../../../src/dev-utils/model';
  11. describe( 'DataController utils', () => {
  12. let model, doc;
  13. describe( 'deleteContent', () => {
  14. it( 'should use parent batch', () => {
  15. model = new Model();
  16. doc = model.document;
  17. doc.createRoot();
  18. model.schema.extend( '$text', { allowIn: '$root' } );
  19. setData( model, 'x[abc]x' );
  20. model.change( writer => {
  21. deleteContent( model, doc.selection );
  22. expect( writer.batch.deltas ).to.length( 1 );
  23. } );
  24. } );
  25. describe( 'in simple scenarios', () => {
  26. beforeEach( () => {
  27. model = new Model();
  28. doc = model.document;
  29. doc.createRoot();
  30. const schema = model.schema;
  31. schema.register( 'image', { inheritAllFrom: '$inline' } );
  32. schema.extend( '$text', { allowIn: '$root' } );
  33. schema.extend( 'image', { allowIn: '$root' } );
  34. } );
  35. test(
  36. 'does nothing on collapsed selection',
  37. 'f[]oo',
  38. 'f[]oo'
  39. );
  40. test(
  41. 'deletes single character',
  42. 'f[o]o',
  43. 'f[]o'
  44. );
  45. it( 'deletes single character (backward selection)', () => {
  46. setData( model, 'f[o]o', { lastRangeBackward: true } );
  47. deleteContent( model, doc.selection );
  48. expect( getData( model ) ).to.equal( 'f[]o' );
  49. } );
  50. test(
  51. 'deletes whole text',
  52. '[foo]',
  53. '[]'
  54. );
  55. test(
  56. 'deletes whole text between nodes',
  57. '<image></image>[foo]<image></image>',
  58. '<image></image>[]<image></image>'
  59. );
  60. test(
  61. 'deletes an element',
  62. 'x[<image></image>]y',
  63. 'x[]y'
  64. );
  65. test(
  66. 'deletes a bunch of nodes',
  67. 'w[x<image></image>y]z',
  68. 'w[]z'
  69. );
  70. test(
  71. 'does not break things when option.merge passed',
  72. 'w[x<image></image>y]z',
  73. 'w[]z'
  74. );
  75. } );
  76. describe( 'with text attributes', () => {
  77. beforeEach( () => {
  78. model = new Model();
  79. doc = model.document;
  80. doc.createRoot();
  81. const schema = model.schema;
  82. schema.register( 'image', { inheritAllFrom: '$inline' } );
  83. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  84. schema.extend( '$text', { allowIn: '$root' } );
  85. schema.allow( { name: '$text', attributes: [ 'bold', 'italic' ] } );
  86. } );
  87. it( 'deletes characters (first half has attrs)', () => {
  88. setData( model, '<$text bold="true">fo[o</$text>b]ar' );
  89. deleteContent( model, doc.selection );
  90. expect( getData( model ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  91. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  92. } );
  93. it( 'deletes characters (2nd half has attrs)', () => {
  94. setData( model, 'fo[o<$text bold="true">b]ar</$text>' );
  95. deleteContent( model, doc.selection );
  96. expect( getData( model ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  97. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  98. } );
  99. it( 'clears selection attrs when emptied content', () => {
  100. setData( model, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  101. deleteContent( model, doc.selection );
  102. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  103. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  104. } );
  105. it( 'leaves selection attributes when text contains them', () => {
  106. setData(
  107. model,
  108. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  109. {
  110. selectionAttributes: {
  111. bold: true
  112. }
  113. }
  114. );
  115. deleteContent( model, doc.selection );
  116. expect( getData( model ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  117. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  118. } );
  119. } );
  120. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  121. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  122. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  123. // should not happen unless we're talking about lists or tables, but these features will need to cover
  124. // their scenarios themselves. In all generic scenarios elements are never nested.
  125. //
  126. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  127. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  128. // Those case should, again, be handled by their specific implementations.
  129. describe( 'in multi-element scenarios', () => {
  130. beforeEach( () => {
  131. model = new Model();
  132. doc = model.document;
  133. doc.createRoot();
  134. const schema = model.schema;
  135. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  136. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  137. schema.register( 'image', { inheritAllFrom: '$inline' } );
  138. schema.register( 'pchild' );
  139. schema.register( 'pparent' );
  140. schema.extend( 'pchild', { allowIn: 'paragraph' } );
  141. schema.extend( '$text', { allowIn: 'pchild' } );
  142. schema.extend( 'paragraph', { allowIn: 'pparent' } );
  143. schema.extend( 'pparent', { allowIn: '$root' } );
  144. schema.extend( '$text', { allowIn: 'pparent' } );
  145. schema.allow( { name: 'paragraph', attributes: [ 'align' ] } );
  146. } );
  147. test(
  148. 'do not merge when no need to',
  149. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  150. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>'
  151. );
  152. test(
  153. 'merges second element into the first one (same name)',
  154. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  155. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
  156. );
  157. test(
  158. 'does not merge second element into the first one (same name, !option.merge)',
  159. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  160. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  161. { leaveUnmerged: true }
  162. );
  163. test(
  164. 'merges second element into the first one (different name)',
  165. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  166. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
  167. );
  168. // Note: in all these cases we ignore the direction of merge.
  169. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  170. // forward and backward delete.
  171. it( 'merges second element into the first one (different name, backward selection)', () => {
  172. setData(
  173. model,
  174. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  175. { lastRangeBackward: true }
  176. );
  177. deleteContent( model, doc.selection );
  178. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  179. } );
  180. test(
  181. 'merges second element into the first one (different attrs)',
  182. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  183. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
  184. );
  185. test(
  186. 'merges second element to an empty first element',
  187. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  188. '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>'
  189. );
  190. test(
  191. 'merges empty element into the first element',
  192. '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
  193. '<heading1>f[]</heading1><paragraph>x</paragraph>'
  194. );
  195. test(
  196. 'leaves just one element when all selected',
  197. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  198. '<heading1>[]bar</heading1>'
  199. );
  200. it( 'uses merge delta even if merged element is empty', () => {
  201. let mergeSpy;
  202. setData( model, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  203. model.change( writer => {
  204. mergeSpy = sinon.spy( writer, 'merge' );
  205. deleteContent( model, doc.selection );
  206. } );
  207. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  208. expect( mergeSpy.called ).to.be.true;
  209. } );
  210. it( 'uses merge delta even if merged element is empty #2', () => {
  211. let mergeSpy;
  212. setData( model, '<paragraph>ab[</paragraph><paragraph>]</paragraph>' );
  213. model.change( writer => {
  214. mergeSpy = sinon.spy( writer, 'merge' );
  215. deleteContent( model, doc.selection );
  216. } );
  217. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  218. expect( mergeSpy.called ).to.be.true;
  219. } );
  220. it( 'does not try to move the second block if not needed', () => {
  221. let mergeSpy, moveSpy;
  222. setData( model, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
  223. model.change( writer => {
  224. mergeSpy = sinon.spy( writer, 'merge' );
  225. moveSpy = sinon.spy( writer, 'move' );
  226. deleteContent( model, doc.selection );
  227. } );
  228. expect( getData( model ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
  229. expect( moveSpy.called ).to.be.false;
  230. expect( mergeSpy.called ).to.be.true;
  231. } );
  232. // Note: in all these cases we ignore the direction of merge.
  233. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  234. // forward and backward delete.
  235. describe( 'with nested elements', () => {
  236. test(
  237. 'merges elements when deep nested',
  238. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  239. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
  240. );
  241. it( 'merges elements when deep nested (3rd level)', () => {
  242. const root = doc.getRoot();
  243. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  244. // <pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>
  245. // <pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>
  246. root.appendChildren(
  247. new Element( 'pparent', null, [
  248. 'x',
  249. new Element( 'paragraph', null, [
  250. 'x',
  251. new Element( 'pchild', null, 'foo' )
  252. ] )
  253. ] )
  254. );
  255. root.appendChildren(
  256. new Element( 'pparent', null, [
  257. new Element( 'paragraph', null, [
  258. new Element( 'pchild', null, 'bar' ),
  259. 'y'
  260. ] ),
  261. 'y'
  262. ] )
  263. );
  264. const range = new Range(
  265. new Position( doc.getRoot(), [ 0, 1, 1, 2 ] ), // fo[o
  266. new Position( doc.getRoot(), [ 1, 0, 0, 1 ] ) // b]ar
  267. );
  268. doc.selection.setRanges( [ range ] );
  269. deleteContent( model, doc.selection );
  270. expect( getData( model ) )
  271. .to.equal( '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>' );
  272. } );
  273. test(
  274. 'merges elements when left end deep nested',
  275. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
  276. '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
  277. );
  278. test(
  279. 'merges elements when right end deep nested',
  280. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
  281. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
  282. );
  283. it( 'merges elements when left end deep nested (3rd level)', () => {
  284. const root = doc.getRoot();
  285. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  286. // <pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent><paragraph>b]om</paragraph>
  287. root.appendChildren(
  288. new Element( 'pparent', null, [
  289. 'x',
  290. new Element( 'paragraph', null, [
  291. 'foo',
  292. new Element( 'pchild', null, 'bar' )
  293. ] )
  294. ] )
  295. );
  296. root.appendChildren(
  297. new Element( 'paragraph', null, 'bom' )
  298. );
  299. const range = new Range(
  300. new Position( doc.getRoot(), [ 0, 1, 3, 2 ] ), // ba[r
  301. new Position( doc.getRoot(), [ 1, 1 ] ) // b]om
  302. );
  303. doc.selection.setRanges( [ range ] );
  304. deleteContent( model, doc.selection );
  305. expect( getData( model ) )
  306. .to.equal( '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>' );
  307. } );
  308. test(
  309. 'merges elements when right end deep nested (in an empty container)',
  310. '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
  311. '<paragraph>fo[]</paragraph>'
  312. );
  313. test(
  314. 'merges elements when left end deep nested (in an empty container)',
  315. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  316. '<paragraph><pchild>[]ar</pchild></paragraph><paragraph>x</paragraph>'
  317. );
  318. it( 'merges elements when left end deep nested (3rd level)', () => {
  319. const root = doc.getRoot();
  320. // We need to use the raw API due to https://github.com/ckeditor/ckeditor5-engine/issues/905.
  321. // <paragraph>fo[o</paragraph><pparent><paragraph><pchild>bar]</pchild></paragraph></pparent>
  322. root.appendChildren(
  323. new Element( 'paragraph', null, 'foo' )
  324. );
  325. root.appendChildren(
  326. new Element( 'pparent', null, [
  327. new Element( 'paragraph', null, [
  328. new Element( 'pchild', null, 'bar' )
  329. ] )
  330. ] )
  331. );
  332. const range = new Range(
  333. new Position( doc.getRoot(), [ 0, 2 ] ), // f[oo
  334. new Position( doc.getRoot(), [ 1, 0, 0, 3 ] ) // bar]
  335. );
  336. doc.selection.setRanges( [ range ] );
  337. deleteContent( model, doc.selection );
  338. expect( getData( model ) )
  339. .to.equal( '<paragraph>fo[]</paragraph>' );
  340. } );
  341. } );
  342. describe( 'with object elements', () => {
  343. beforeEach( () => {
  344. const schema = model.schema;
  345. schema.register( 'blockWidget', {
  346. isObject: true
  347. } );
  348. schema.register( 'nestedEditable', {
  349. isLimit: true
  350. } );
  351. schema.extend( 'blockWidget', { allowIn: '$root' } );
  352. schema.extend( 'nestedEditable', { allowIn: 'blockWidget' } );
  353. schema.extend( '$text', { allowIn: 'nestedEditable' } );
  354. } );
  355. test(
  356. 'does not merge an object element (if it is first)',
  357. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  358. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
  359. );
  360. test(
  361. 'does not merge an object element (if it is second)',
  362. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  363. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
  364. );
  365. } );
  366. describe( 'filtering out', () => {
  367. beforeEach( () => {
  368. const schema = model.schema;
  369. schema.allow( { name: '$text', attributes: [ 'a', 'b' ], inside: 'paragraph' } );
  370. schema.allow( { name: '$text', attributes: [ 'b', 'c' ], inside: 'pchild' } );
  371. schema.extend( 'pchild', { allowIn: 'pchild' } );
  372. schema.disallow( { name: '$text', attributes: [ 'c' ], inside: 'pchild pchild' } );
  373. } );
  374. test(
  375. 'filters out disallowed attributes after left merge',
  376. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text a="1" b="1">z</$text></paragraph>',
  377. '<paragraph>x<pchild>fo[]<$text b="1">z</$text></pchild></paragraph>'
  378. );
  379. test(
  380. 'filters out disallowed attributes from nested nodes after left merge',
  381. '<paragraph>' +
  382. 'x' +
  383. '<pchild>fo[o</pchild>' +
  384. '</paragraph>' +
  385. '<paragraph>' +
  386. 'b]a<$text a="1" b="1">r</$text>' +
  387. '<pchild>b<$text b="1" c="1">i</$text>z</pchild>' +
  388. 'y' +
  389. '</paragraph>',
  390. '<paragraph>' +
  391. 'x' +
  392. '<pchild>' +
  393. 'fo[]a<$text b="1">r</$text>' +
  394. '<pchild>b<$text b="1">i</$text>z</pchild>' +
  395. 'y' +
  396. '</pchild>' +
  397. '</paragraph>'
  398. );
  399. test(
  400. 'filters out disallowed attributes after right merge',
  401. '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text b="1" c="1">y]z</$text></pchild></paragraph>',
  402. '<paragraph>fo[]<$text b="1">z</$text></paragraph>'
  403. );
  404. } );
  405. } );
  406. describe( 'in element selections scenarios', () => {
  407. beforeEach( () => {
  408. model = new Model();
  409. doc = model.document;
  410. // <p> like root.
  411. doc.createRoot( 'paragraph', 'paragraphRoot' );
  412. // <body> like root.
  413. doc.createRoot( '$root', 'bodyRoot' );
  414. // Special root which allows only blockWidgets inside itself.
  415. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  416. const schema = model.schema;
  417. schema.register( 'image', { inheritAllFrom: '$inline' } );
  418. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  419. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  420. schema.register( 'blockWidget' );
  421. schema.register( 'restrictedRoot', {
  422. isLimit: true
  423. } );
  424. schema.extend( '$block', { allowIn: '$root' } );
  425. schema.extend( 'blockWidget', { allowIn: '$root' } );
  426. schema.extend( 'blockWidget', { allowIn: 'restrictedRoot' } );
  427. } );
  428. // See also "in simple scenarios => deletes an element".
  429. it( 'deletes two inline elements', () => {
  430. model.schema.extend( 'paragraph', {
  431. isLimit: true
  432. } );
  433. setData(
  434. model,
  435. 'x[<image></image><image></image>]z',
  436. { rootName: 'paragraphRoot' }
  437. );
  438. deleteContent( model, doc.selection );
  439. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  440. .to.equal( 'x[]z' );
  441. } );
  442. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  443. setData(
  444. model,
  445. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  446. { rootName: 'bodyRoot' }
  447. );
  448. deleteContent( model, doc.selection );
  449. expect( getData( model, { rootName: 'bodyRoot' } ) )
  450. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  451. } );
  452. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  453. setData(
  454. model,
  455. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  456. { rootName: 'bodyRoot' }
  457. );
  458. deleteContent( model, doc.selection );
  459. expect( getData( model, { rootName: 'bodyRoot' } ) )
  460. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  461. } );
  462. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  463. setData(
  464. model,
  465. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  466. { rootName: 'bodyRoot' }
  467. );
  468. deleteContent( model, doc.selection );
  469. expect( getData( model, { rootName: 'bodyRoot' } ) )
  470. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  471. } );
  472. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  473. setData(
  474. model,
  475. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  476. { rootName: 'bodyRoot' }
  477. );
  478. deleteContent( model, doc.selection );
  479. expect( getData( model, { rootName: 'bodyRoot' } ) )
  480. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  481. } );
  482. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  483. setData(
  484. model,
  485. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  486. { rootName: 'bodyRoot' }
  487. );
  488. deleteContent( model, doc.selection );
  489. expect( getData( model, { rootName: 'bodyRoot' } ) )
  490. .to.equal( '<paragraph>[]</paragraph>' );
  491. } );
  492. it( 'does not create a paragraph when it is not allowed', () => {
  493. setData(
  494. model,
  495. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  496. { rootName: 'restrictedRoot' }
  497. );
  498. deleteContent( model, doc.selection );
  499. expect( getData( model, { rootName: 'restrictedRoot' } ) )
  500. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  501. } );
  502. } );
  503. describe( 'integration with inline limit elements', () => {
  504. beforeEach( () => {
  505. model = new Model();
  506. doc = model.document;
  507. doc.createRoot();
  508. const schema = model.schema;
  509. schema.register( 'inlineLimit', {
  510. isLimit: true
  511. } );
  512. schema.extend( 'inlineLimit', { allowIn: '$root' } );
  513. schema.extend( '$text', { allowIn: 'inlineLimit' } );
  514. schema.extend( '$inline', { allowIn: '$root' } );
  515. schema.register( 'x' );
  516. schema.extend( '$text', { allowIn: 'x' } );
  517. schema.extend( 'x', { allowIn: '$root' } );
  518. } );
  519. test(
  520. 'should delete inside inline limit element',
  521. '<inlineLimit>foo [bar] baz</inlineLimit>',
  522. '<inlineLimit>foo [] baz</inlineLimit>'
  523. );
  524. test(
  525. 'should delete whole inline limit element',
  526. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  527. 'x[]x'
  528. );
  529. test(
  530. 'should delete from two inline limit elements',
  531. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  532. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  533. );
  534. test(
  535. 'merge option should be ignored if both elements are limits',
  536. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  537. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  538. );
  539. test(
  540. 'merge option should be ignored if the first element is a limit',
  541. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  542. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  543. );
  544. test(
  545. 'merge option should be ignored if the second element is a limit',
  546. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  547. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  548. );
  549. } );
  550. describe( 'integration with block limit elements', () => {
  551. beforeEach( () => {
  552. model = new Model();
  553. doc = model.document;
  554. doc.createRoot();
  555. const schema = model.schema;
  556. schema.register( 'blockLimit', {
  557. isLimit: true
  558. } );
  559. schema.extend( 'blockLimit', { allowIn: '$root' } );
  560. schema.extend( '$block', { allowIn: 'blockLimit' } );
  561. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  562. } );
  563. test(
  564. 'should delete inside block limit element',
  565. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  566. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  567. { leaveUnmerged: true }
  568. );
  569. test(
  570. 'should delete inside block limit element (with merge)',
  571. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  572. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  573. );
  574. test(
  575. 'should delete whole block limit element',
  576. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  577. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  578. );
  579. test(
  580. 'should delete from two block limit elements',
  581. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  582. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  583. );
  584. test(
  585. 'merge option should be ignored if any of the elements is a limit',
  586. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  587. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  588. );
  589. } );
  590. describe( 'should leave a paragraph if the entire content was selected', () => {
  591. beforeEach( () => {
  592. model = new Model();
  593. doc = model.document;
  594. doc.createRoot();
  595. const schema = model.schema;
  596. schema.register( 'div', {
  597. inheritAllFrom: '$block',
  598. isLimit: true
  599. } );
  600. schema.register( 'article', {
  601. inheritAllFrom: '$block',
  602. isLimit: true
  603. } );
  604. schema.register( 'image', {
  605. inheritAllFrom: '$inline',
  606. isObject: true
  607. } );
  608. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  609. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  610. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  611. schema.extend( '$text', { allowIn: '$root' } );
  612. schema.extend( 'image', { allowIn: '$root' } );
  613. schema.extend( 'image', { allowIn: 'heading1' } );
  614. schema.extend( 'heading1', { allowIn: 'div' } );
  615. schema.extend( 'paragraph', { allowIn: 'div' } );
  616. schema.extend( 'heading1', { allowIn: 'article' } );
  617. schema.extend( 'heading2', { allowIn: 'article' } );
  618. } );
  619. test(
  620. 'but not if only one block was selected',
  621. '<heading1>[xx]</heading1>',
  622. '<heading1>[]</heading1>'
  623. );
  624. test(
  625. 'when the entire heading and paragraph were selected',
  626. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  627. '<paragraph>[]</paragraph>'
  628. );
  629. test(
  630. 'when the entire content was selected',
  631. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  632. '<paragraph>[]</paragraph>'
  633. );
  634. test(
  635. 'inside the limit element when the entire heading and paragraph were inside',
  636. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  637. '<div><paragraph>[]</paragraph></div>'
  638. );
  639. test(
  640. 'but not if schema does not accept paragraph in limit element',
  641. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  642. '<article><heading1>[]</heading1></article>'
  643. );
  644. test(
  645. 'but not if selection is not containing the whole content',
  646. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  647. '<image></image><heading1>[]</heading1>'
  648. );
  649. test(
  650. 'but not if only single element is selected',
  651. '<heading1>[<image></image>xx]</heading1>',
  652. '<heading1>[]</heading1>'
  653. );
  654. it( 'when root element was not added as Schema.limits works fine as well', () => {
  655. doc.createRoot( 'paragraph', 'paragraphRoot' );
  656. setData(
  657. model,
  658. 'x[<image></image><image></image>]z',
  659. { rootName: 'paragraphRoot' }
  660. );
  661. deleteContent( model, doc.selection );
  662. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  663. .to.equal( 'x[]z' );
  664. } );
  665. test(
  666. 'but not if the flag "doNotResetEntireContent" is set to true',
  667. '<heading1>[</heading1><paragraph>]</paragraph>',
  668. '<heading1>[]</heading1>',
  669. {
  670. doNotResetEntireContent: true
  671. }
  672. );
  673. } );
  674. function test( title, input, output, options ) {
  675. it( title, () => {
  676. setData( model, input );
  677. deleteContent( model, doc.selection, options );
  678. expect( getData( model ) ).to.equal( output );
  679. } );
  680. }
  681. } );
  682. } );