deletecontent.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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', { allowWhere: '$text' } );
  32. schema.extend( '$text', { allowIn: '$root' } );
  33. } );
  34. test(
  35. 'does nothing on collapsed selection',
  36. 'f[]oo',
  37. 'f[]oo'
  38. );
  39. test(
  40. 'deletes single character',
  41. 'f[o]o',
  42. 'f[]o'
  43. );
  44. it( 'deletes single character (backward selection)', () => {
  45. setData( model, 'f[o]o', { lastRangeBackward: true } );
  46. deleteContent( model, doc.selection );
  47. expect( getData( model ) ).to.equal( 'f[]o' );
  48. } );
  49. test(
  50. 'deletes whole text',
  51. '[foo]',
  52. '[]'
  53. );
  54. test(
  55. 'deletes whole text between nodes',
  56. '<image></image>[foo]<image></image>',
  57. '<image></image>[]<image></image>'
  58. );
  59. test(
  60. 'deletes an element',
  61. 'x[<image></image>]y',
  62. 'x[]y'
  63. );
  64. test(
  65. 'deletes a bunch of nodes',
  66. 'w[x<image></image>y]z',
  67. 'w[]z'
  68. );
  69. test(
  70. 'does not break things when option.merge passed',
  71. 'w[x<image></image>y]z',
  72. 'w[]z'
  73. );
  74. } );
  75. describe( 'with text attributes', () => {
  76. beforeEach( () => {
  77. model = new Model();
  78. doc = model.document;
  79. doc.createRoot();
  80. const schema = model.schema;
  81. schema.register( 'image', { allowWhere: '$text' } );
  82. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  83. schema.extend( '$text', {
  84. allowIn: '$root',
  85. allowAttributes: [ 'bold', 'italic' ]
  86. } );
  87. } );
  88. it( 'deletes characters (first half has attrs)', () => {
  89. setData( model, '<$text bold="true">fo[o</$text>b]ar' );
  90. deleteContent( model, doc.selection );
  91. expect( getData( model ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  92. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  93. } );
  94. it( 'deletes characters (2nd half has attrs)', () => {
  95. setData( model, 'fo[o<$text bold="true">b]ar</$text>' );
  96. deleteContent( model, doc.selection );
  97. expect( getData( model ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  98. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  99. } );
  100. it( 'clears selection attrs when emptied content', () => {
  101. setData( model, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  102. deleteContent( model, doc.selection );
  103. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  104. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  105. } );
  106. it( 'leaves selection attributes when text contains them', () => {
  107. setData(
  108. model,
  109. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  110. {
  111. selectionAttributes: {
  112. bold: true
  113. }
  114. }
  115. );
  116. deleteContent( model, doc.selection );
  117. expect( getData( model ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  118. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  119. } );
  120. } );
  121. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  122. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  123. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  124. // should not happen unless we're talking about lists or tables, but these features will need to cover
  125. // their scenarios themselves. In all generic scenarios elements are never nested.
  126. //
  127. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  128. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  129. // Those case should, again, be handled by their specific implementations.
  130. describe( 'in multi-element scenarios', () => {
  131. beforeEach( () => {
  132. model = new Model();
  133. doc = model.document;
  134. doc.createRoot();
  135. const schema = model.schema;
  136. schema.register( 'paragraph', {
  137. inheritAllFrom: '$block',
  138. allowIn: 'pparent',
  139. allowAttributes: 'align'
  140. } );
  141. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  142. schema.register( 'image', { inheritAllFrom: '$text' } );
  143. schema.register( 'pchild', { allowIn: 'paragraph' } );
  144. schema.register( 'pparent', { allowIn: '$root' } );
  145. schema.extend( '$text', { allowIn: [ 'pchild', 'pparent' ] } );
  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.on( 'checkAttribute', ( evt, args ) => {
  370. const ctx = args[ 0 ];
  371. const attributeName = args[ 1 ];
  372. // Allow 'a' and 'b' on paragraph>$text.
  373. if ( ctx.matchEnd( 'paragraph $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
  374. evt.stop();
  375. evt.return = true;
  376. }
  377. // Allow 'b' and 'c' in pchild>$text.
  378. if ( ctx.matchEnd( 'pchild $text' ) && [ 'b', 'c' ].includes( attributeName ) ) {
  379. evt.stop();
  380. evt.return = true;
  381. }
  382. // Disallow 'c' on pchild>pchild>$text.
  383. if ( ctx.matchEnd( 'pchild pchild $text' ) && attributeName == 'c' ) {
  384. evt.stop();
  385. evt.return = false;
  386. }
  387. }, { priority: 'high' } );
  388. schema.extend( 'pchild', { allowIn: 'pchild' } );
  389. } );
  390. test(
  391. 'filters out disallowed attributes after left merge',
  392. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text a="1" b="1">z</$text></paragraph>',
  393. '<paragraph>x<pchild>fo[]<$text b="1">z</$text></pchild></paragraph>'
  394. );
  395. test(
  396. 'filters out disallowed attributes from nested nodes after left merge',
  397. '<paragraph>' +
  398. 'x' +
  399. '<pchild>fo[o</pchild>' +
  400. '</paragraph>' +
  401. '<paragraph>' +
  402. 'b]a<$text a="1" b="1">r</$text>' +
  403. '<pchild>b<$text b="1" c="1">i</$text>z</pchild>' +
  404. 'y' +
  405. '</paragraph>',
  406. '<paragraph>' +
  407. 'x' +
  408. '<pchild>' +
  409. 'fo[]a<$text b="1">r</$text>' +
  410. '<pchild>b<$text b="1">i</$text>z</pchild>' +
  411. 'y' +
  412. '</pchild>' +
  413. '</paragraph>'
  414. );
  415. test(
  416. 'filters out disallowed attributes after right merge',
  417. '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text b="1" c="1">y]z</$text></pchild></paragraph>',
  418. '<paragraph>fo[]<$text b="1">z</$text></paragraph>'
  419. );
  420. } );
  421. } );
  422. describe( 'in element selections scenarios', () => {
  423. beforeEach( () => {
  424. model = new Model();
  425. doc = model.document;
  426. // <p> like root.
  427. doc.createRoot( 'paragraph', 'paragraphRoot' );
  428. // <body> like root.
  429. doc.createRoot( '$root', 'bodyRoot' );
  430. // Special root which allows only blockWidgets inside itself.
  431. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  432. const schema = model.schema;
  433. schema.register( 'image', { allowWhere: '$text' } );
  434. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  435. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  436. schema.register( 'blockWidget' );
  437. schema.register( 'restrictedRoot', {
  438. isLimit: true
  439. } );
  440. schema.extend( '$block', { allowIn: '$root' } );
  441. schema.extend( 'blockWidget', { allowIn: '$root' } );
  442. schema.extend( 'blockWidget', { allowIn: 'restrictedRoot' } );
  443. } );
  444. // See also "in simple scenarios => deletes an element".
  445. it( 'deletes two inline elements', () => {
  446. model.schema.extend( 'paragraph', {
  447. isLimit: true
  448. } );
  449. setData(
  450. model,
  451. 'x[<image></image><image></image>]z',
  452. { rootName: 'paragraphRoot' }
  453. );
  454. deleteContent( model, doc.selection );
  455. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  456. .to.equal( 'x[]z' );
  457. } );
  458. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  459. setData(
  460. model,
  461. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  462. { rootName: 'bodyRoot' }
  463. );
  464. deleteContent( model, doc.selection );
  465. expect( getData( model, { rootName: 'bodyRoot' } ) )
  466. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  467. } );
  468. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  469. setData(
  470. model,
  471. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  472. { rootName: 'bodyRoot' }
  473. );
  474. deleteContent( model, doc.selection );
  475. expect( getData( model, { rootName: 'bodyRoot' } ) )
  476. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  477. } );
  478. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  479. setData(
  480. model,
  481. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  482. { rootName: 'bodyRoot' }
  483. );
  484. deleteContent( model, doc.selection );
  485. expect( getData( model, { rootName: 'bodyRoot' } ) )
  486. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  487. } );
  488. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  489. setData(
  490. model,
  491. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  492. { rootName: 'bodyRoot' }
  493. );
  494. deleteContent( model, doc.selection );
  495. expect( getData( model, { rootName: 'bodyRoot' } ) )
  496. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  497. } );
  498. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  499. setData(
  500. model,
  501. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  502. { rootName: 'bodyRoot' }
  503. );
  504. deleteContent( model, doc.selection );
  505. expect( getData( model, { rootName: 'bodyRoot' } ) )
  506. .to.equal( '<paragraph>[]</paragraph>' );
  507. } );
  508. it( 'does not create a paragraph when it is not allowed', () => {
  509. setData(
  510. model,
  511. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  512. { rootName: 'restrictedRoot' }
  513. );
  514. deleteContent( model, doc.selection );
  515. expect( getData( model, { rootName: 'restrictedRoot' } ) )
  516. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  517. } );
  518. } );
  519. describe( 'integration with inline limit elements', () => {
  520. beforeEach( () => {
  521. model = new Model();
  522. doc = model.document;
  523. doc.createRoot();
  524. const schema = model.schema;
  525. schema.register( 'inlineLimit', {
  526. isLimit: true,
  527. allowIn: '$root'
  528. } );
  529. schema.extend( '$text', {
  530. allowIn: [ 'inlineLimit', '$root', 'x' ]
  531. } );
  532. schema.register( 'x', { allowIn: '$root' } );
  533. } );
  534. test(
  535. 'should delete inside inline limit element',
  536. '<inlineLimit>foo [bar] baz</inlineLimit>',
  537. '<inlineLimit>foo [] baz</inlineLimit>'
  538. );
  539. test(
  540. 'should delete whole inline limit element',
  541. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  542. 'x[]x'
  543. );
  544. test(
  545. 'should delete from two inline limit elements',
  546. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  547. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  548. );
  549. test(
  550. 'merge option should be ignored if both elements are limits',
  551. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  552. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  553. );
  554. test(
  555. 'merge option should be ignored if the first element is a limit',
  556. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  557. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  558. );
  559. test(
  560. 'merge option should be ignored if the second element is a limit',
  561. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  562. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  563. );
  564. } );
  565. describe( 'integration with block limit elements', () => {
  566. beforeEach( () => {
  567. model = new Model();
  568. doc = model.document;
  569. doc.createRoot();
  570. const schema = model.schema;
  571. schema.register( 'blockLimit', {
  572. isLimit: true
  573. } );
  574. schema.extend( 'blockLimit', { allowIn: '$root' } );
  575. schema.extend( '$block', { allowIn: 'blockLimit' } );
  576. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  577. } );
  578. test(
  579. 'should delete inside block limit element',
  580. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  581. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  582. { leaveUnmerged: true }
  583. );
  584. test(
  585. 'should delete inside block limit element (with merge)',
  586. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  587. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  588. );
  589. test(
  590. 'should delete whole block limit element',
  591. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  592. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  593. );
  594. test(
  595. 'should delete from two block limit elements',
  596. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  597. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  598. );
  599. test(
  600. 'merge option should be ignored if any of the elements is a limit',
  601. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  602. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  603. );
  604. } );
  605. describe( 'should leave a paragraph if the entire content was selected', () => {
  606. beforeEach( () => {
  607. model = new Model();
  608. doc = model.document;
  609. doc.createRoot();
  610. const schema = model.schema;
  611. schema.register( 'div', {
  612. inheritAllFrom: '$block',
  613. isLimit: true
  614. } );
  615. schema.register( 'article', {
  616. inheritAllFrom: '$block',
  617. isLimit: true
  618. } );
  619. schema.register( 'image', {
  620. allowWhere: '$text',
  621. isObject: true
  622. } );
  623. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  624. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  625. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  626. schema.extend( '$text', { allowIn: '$root' } );
  627. schema.extend( 'image', { allowIn: '$root' } );
  628. schema.extend( 'image', { allowIn: 'heading1' } );
  629. schema.extend( 'heading1', { allowIn: 'div' } );
  630. schema.extend( 'paragraph', { allowIn: 'div' } );
  631. schema.extend( 'heading1', { allowIn: 'article' } );
  632. schema.extend( 'heading2', { allowIn: 'article' } );
  633. } );
  634. test(
  635. 'but not if only one block was selected',
  636. '<heading1>[xx]</heading1>',
  637. '<heading1>[]</heading1>'
  638. );
  639. test(
  640. 'when the entire heading and paragraph were selected',
  641. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  642. '<paragraph>[]</paragraph>'
  643. );
  644. test(
  645. 'when the entire content was selected',
  646. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  647. '<paragraph>[]</paragraph>'
  648. );
  649. test(
  650. 'inside the limit element when the entire heading and paragraph were inside',
  651. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  652. '<div><paragraph>[]</paragraph></div>'
  653. );
  654. test(
  655. 'but not if schema does not accept paragraph in limit element',
  656. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  657. '<article><heading1>[]</heading1></article>'
  658. );
  659. test(
  660. 'but not if selection is not containing the whole content',
  661. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  662. '<image></image><heading1>[]</heading1>'
  663. );
  664. test(
  665. 'but not if only single element is selected',
  666. '<heading1>[<image></image>xx]</heading1>',
  667. '<heading1>[]</heading1>'
  668. );
  669. it( 'when root element was not added as Schema.limits works fine as well', () => {
  670. doc.createRoot( 'paragraph', 'paragraphRoot' );
  671. setData(
  672. model,
  673. 'x[<image></image><image></image>]z',
  674. { rootName: 'paragraphRoot' }
  675. );
  676. deleteContent( model, doc.selection );
  677. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  678. .to.equal( 'x[]z' );
  679. } );
  680. test(
  681. 'but not if the flag "doNotResetEntireContent" is set to true',
  682. '<heading1>[</heading1><paragraph>]</paragraph>',
  683. '<heading1>[]</heading1>',
  684. {
  685. doNotResetEntireContent: true
  686. }
  687. );
  688. } );
  689. function test( title, input, output, options ) {
  690. it( title, () => {
  691. setData( model, input );
  692. deleteContent( model, doc.selection, options );
  693. expect( getData( model ) ).to.equal( output );
  694. } );
  695. }
  696. } );
  697. } );