deletecontent.js 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import Model from '../../../src/model/model';
  6. import Position from '../../../src/model/position';
  7. import Range from '../../../src/model/range';
  8. import Selection from '../../../src/model/selection';
  9. import Element from '../../../src/model/element';
  10. import deleteContent from '../../../src/model/utils/deletecontent';
  11. import { setData, getData } from '../../../src/dev-utils/model';
  12. import { stringify } from '../../../src/dev-utils/view';
  13. describe( 'DataController utils', () => {
  14. let model, doc;
  15. describe( 'deleteContent', () => {
  16. it( 'should use parent batch', () => {
  17. model = new Model();
  18. doc = model.document;
  19. doc.createRoot();
  20. model.schema.extend( '$text', { allowIn: '$root' } );
  21. setData( model, 'x[abc]x' );
  22. model.change( writer => {
  23. deleteContent( model, doc.selection );
  24. expect( writer.batch.operations ).to.length( 1 );
  25. } );
  26. } );
  27. it( 'should not do anything if the selection is already in graveyard', () => {
  28. model = new Model();
  29. doc = model.document;
  30. const gy = model.document.graveyard;
  31. gy._appendChild( new Element( 'paragraph' ) );
  32. const baseVersion = model.document.baseVersion;
  33. model.change( writer => {
  34. sinon.spy( writer, 'remove' );
  35. const selection = writer.createSelection( writer.createRangeIn( gy ) );
  36. deleteContent( model, selection );
  37. expect( writer.remove.called ).to.be.false;
  38. } );
  39. expect( model.document.baseVersion ).to.equal( baseVersion );
  40. } );
  41. describe( 'in simple scenarios', () => {
  42. beforeEach( () => {
  43. model = new Model();
  44. doc = model.document;
  45. doc.createRoot();
  46. const schema = model.schema;
  47. schema.register( 'image', { allowWhere: '$text' } );
  48. schema.extend( '$text', { allowIn: '$root' } );
  49. } );
  50. it( 'should be able to delete content at custom selection', () => {
  51. setData( model, 'a[]bcd' );
  52. const range = new Range(
  53. new Position( doc.getRoot(), [ 2 ] ),
  54. new Position( doc.getRoot(), [ 3 ] )
  55. );
  56. const selection = new Selection( [ range ] );
  57. model.change( () => {
  58. deleteContent( model, selection );
  59. expect( getData( model ) ).to.equal( 'a[]bd' );
  60. } );
  61. } );
  62. test(
  63. 'does nothing on collapsed selection',
  64. 'f[]oo',
  65. 'f[]oo'
  66. );
  67. test(
  68. 'deletes single character',
  69. 'f[o]o',
  70. 'f[]o'
  71. );
  72. it( 'deletes single character (backward selection)', () => {
  73. setData( model, 'f[o]o', { lastRangeBackward: true } );
  74. deleteContent( model, doc.selection );
  75. expect( getData( model ) ).to.equal( 'f[]o' );
  76. } );
  77. test(
  78. 'deletes whole text',
  79. '[foo]',
  80. '[]'
  81. );
  82. test(
  83. 'deletes whole text between nodes',
  84. '<image></image>[foo]<image></image>',
  85. '<image></image>[]<image></image>'
  86. );
  87. test(
  88. 'deletes an element',
  89. 'x[<image></image>]y',
  90. 'x[]y'
  91. );
  92. test(
  93. 'deletes a bunch of nodes',
  94. 'w[x<image></image>y]z',
  95. 'w[]z'
  96. );
  97. test(
  98. 'does not break things when option.merge passed',
  99. 'w[x<image></image>y]z',
  100. 'w[]z'
  101. );
  102. } );
  103. describe( 'with text attributes', () => {
  104. beforeEach( () => {
  105. model = new Model();
  106. doc = model.document;
  107. doc.createRoot();
  108. const schema = model.schema;
  109. schema.register( 'image', { allowWhere: '$text' } );
  110. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  111. schema.extend( '$text', {
  112. allowIn: '$root',
  113. allowAttributes: [ 'bold', 'italic' ]
  114. } );
  115. } );
  116. it( 'deletes characters (first half has attrs)', () => {
  117. setData( model, '<$text bold="true">fo[o</$text>b]ar' );
  118. deleteContent( model, doc.selection );
  119. expect( getData( model ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  120. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  121. } );
  122. it( 'deletes characters (2nd half has attrs)', () => {
  123. setData( model, 'fo[o<$text bold="true">b]ar</$text>' );
  124. deleteContent( model, doc.selection );
  125. expect( getData( model ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  126. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  127. } );
  128. it( 'clears selection attrs when emptied content', () => {
  129. setData( model, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  130. deleteContent( model, doc.selection );
  131. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  132. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  133. } );
  134. it( 'leaves selection attributes when text contains them', () => {
  135. setData(
  136. model,
  137. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  138. {
  139. selectionAttributes: {
  140. bold: true
  141. }
  142. }
  143. );
  144. deleteContent( model, doc.selection );
  145. expect( getData( model ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  146. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  147. } );
  148. } );
  149. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  150. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  151. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  152. // should not happen unless we're talking about lists or tables, but these features will need to cover
  153. // their scenarios themselves. In all generic scenarios elements are never nested.
  154. //
  155. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  156. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  157. // Those case should, again, be handled by their specific implementations.
  158. describe( 'in multi-element scenarios', () => {
  159. beforeEach( () => {
  160. model = new Model();
  161. doc = model.document;
  162. doc.createRoot();
  163. const schema = model.schema;
  164. schema.register( 'paragraph', {
  165. inheritAllFrom: '$block',
  166. allowIn: 'pparent',
  167. allowAttributes: 'align'
  168. } );
  169. schema.register( 'heading1', { inheritAllFrom: '$block', allowIn: 'pparent' } );
  170. schema.register( 'image', { inheritAllFrom: '$text' } );
  171. schema.register( 'pchild', { allowIn: 'paragraph' } );
  172. schema.register( 'pparent', { allowIn: '$root' } );
  173. schema.register( 'hchild', { allowIn: 'heading1' } );
  174. schema.register( 'widget', { isObject: true, allowWhere: '$text', isInline: true } );
  175. schema.extend( '$text', { allowIn: [ 'pchild', 'pparent', 'hchild' ] } );
  176. } );
  177. test(
  178. 'do not merge when no need to',
  179. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  180. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>'
  181. );
  182. test(
  183. 'merges second element into the first one (same name)',
  184. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  185. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>'
  186. );
  187. test(
  188. 'merges first element into the second element (it would become empty but second element would not) (same name)',
  189. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  190. '<paragraph>x</paragraph><paragraph>[]ar</paragraph><paragraph>y</paragraph>'
  191. );
  192. test(
  193. 'do not remove end block if selection ends at start position of it',
  194. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph>]bar</paragraph><paragraph>y</paragraph>',
  195. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>bar</paragraph><paragraph>y</paragraph>'
  196. );
  197. test(
  198. 'removes empty element (merges it into second element)',
  199. '<paragraph>x</paragraph><paragraph>[</paragraph><paragraph>]bar</paragraph><paragraph>y</paragraph>',
  200. '<paragraph>x</paragraph><paragraph>[]bar</paragraph><paragraph>y</paragraph>'
  201. );
  202. test(
  203. 'treats inline widget elements as content so parent element is not considered as empty after merging (same name)',
  204. '<paragraph>x</paragraph><paragraph><widget></widget>[foo</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  205. '<paragraph>x</paragraph><paragraph><widget></widget>[]ar</paragraph><paragraph>y</paragraph>'
  206. );
  207. test(
  208. 'does not merge second element into the first one (same name, !option.merge)',
  209. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  210. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  211. { leaveUnmerged: true }
  212. );
  213. test(
  214. 'does not remove first empty element when it\'s empty but second element is not empty (same name, !option.merge)',
  215. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  216. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>',
  217. { leaveUnmerged: true }
  218. );
  219. test(
  220. 'merges second element into the first one (different name)',
  221. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  222. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>'
  223. );
  224. test(
  225. 'removes first element when it\'s empty but second element is not empty (different name)',
  226. '<paragraph>x</paragraph><heading1>[foo</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  227. '<paragraph>x</paragraph><paragraph>[]ar</paragraph><paragraph>y</paragraph>'
  228. );
  229. // Note: in all these cases we ignore the direction of merge.
  230. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  231. // forward and backward delete.
  232. it( 'merges second element into the first one (different name, backward selection)', () => {
  233. setData(
  234. model,
  235. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  236. { lastRangeBackward: true }
  237. );
  238. deleteContent( model, doc.selection );
  239. expect( getData( model ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  240. } );
  241. test(
  242. 'merges second element into the first one (different attrs)',
  243. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  244. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>'
  245. );
  246. test(
  247. 'removes empty first element',
  248. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  249. '<paragraph>x</paragraph><paragraph>[]o</paragraph><paragraph>y</paragraph>'
  250. );
  251. test(
  252. 'merges empty element into the first element',
  253. '<heading1>f[oo</heading1><paragraph>bar]</paragraph><paragraph>x</paragraph>',
  254. '<heading1>f[]</heading1><paragraph>x</paragraph>'
  255. );
  256. test(
  257. 'leaves just one element when all selected',
  258. '<paragraph>[x</paragraph><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  259. '<paragraph>[]bar</paragraph>'
  260. );
  261. test(
  262. 'leaves just one (last) element when all selected (first block would become empty) (different name)',
  263. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]bar</paragraph>',
  264. '<paragraph>[]bar</paragraph>'
  265. );
  266. test(
  267. 'leaves just one (first) element when all selected (first block would not become empty) (different name)',
  268. '<heading1>foo[x</heading1><paragraph>bar</paragraph><paragraph>y]</paragraph>',
  269. '<heading1>foo[]</heading1>'
  270. );
  271. it( 'uses merge operation even if merged element is empty', () => {
  272. let mergeSpy;
  273. setData( model, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  274. model.change( writer => {
  275. mergeSpy = sinon.spy( writer, 'merge' );
  276. deleteContent( model, doc.selection );
  277. } );
  278. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  279. expect( mergeSpy.called ).to.be.true;
  280. } );
  281. it( 'uses merge operation even if merged element is empty #2', () => {
  282. let mergeSpy;
  283. setData( model, '<paragraph>ab[</paragraph><paragraph>]</paragraph>' );
  284. model.change( writer => {
  285. mergeSpy = sinon.spy( writer, 'merge' );
  286. deleteContent( model, doc.selection );
  287. } );
  288. expect( getData( model ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  289. expect( mergeSpy.called ).to.be.true;
  290. } );
  291. it( 'uses "merge" operation (from OT) if first element is empty (because of content delete) and last is not', () => {
  292. let mergeSpy;
  293. setData( model, '<paragraph>[abcd</paragraph><paragraph>ef]gh</paragraph>' );
  294. model.change( writer => {
  295. mergeSpy = sinon.spy( writer, 'merge' );
  296. deleteContent( model, doc.selection );
  297. } );
  298. expect( getData( model ) ).to.equal( '<paragraph>[]gh</paragraph>' );
  299. expect( mergeSpy.called ).to.be.true;
  300. } );
  301. it( 'uses merge operation if first element is empty and last is not', () => {
  302. let mergeSpy;
  303. setData( model, '<paragraph>[</paragraph><paragraph>ef]gh</paragraph>' );
  304. model.change( writer => {
  305. mergeSpy = sinon.spy( writer, 'merge' );
  306. deleteContent( model, doc.selection );
  307. } );
  308. expect( getData( model ) ).to.equal( '<paragraph>[]gh</paragraph>' );
  309. expect( mergeSpy.called ).to.be.true;
  310. } );
  311. it( 'does not try to move the second block if not needed', () => {
  312. let mergeSpy, moveSpy;
  313. setData( model, '<paragraph>ab[cd</paragraph><paragraph>ef]gh</paragraph>' );
  314. model.change( writer => {
  315. mergeSpy = sinon.spy( writer, 'merge' );
  316. moveSpy = sinon.spy( writer, 'move' );
  317. deleteContent( model, doc.selection );
  318. } );
  319. expect( getData( model ) ).to.equal( '<paragraph>ab[]gh</paragraph>' );
  320. expect( moveSpy.called ).to.be.false;
  321. expect( mergeSpy.called ).to.be.true;
  322. } );
  323. // Note: in all these cases we ignore the direction of merge.
  324. // If https://github.com/ckeditor/ckeditor5-engine/issues/470 was fixed we could differently treat
  325. // forward and backward delete.
  326. describe( 'with nested elements', () => {
  327. test(
  328. 'merges elements when deep nested',
  329. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  330. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>'
  331. );
  332. test(
  333. 'merges block element to the right (with nested element)',
  334. '<paragraph><pchild>[foo</pchild></paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  335. '<paragraph><pchild>[]ar</pchild></paragraph>'
  336. );
  337. test(
  338. 'does not remove block element with nested element and object',
  339. '<paragraph><pchild><widget></widget>[foo</pchild></paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  340. '<paragraph><pchild><widget></widget>[]ar</pchild></paragraph>'
  341. );
  342. test(
  343. 'merges nested elements',
  344. '<heading1><hchild>x[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  345. '<heading1><hchild>x[]ar</hchild></heading1>'
  346. );
  347. test(
  348. 'merges nested elements on multiple levels',
  349. '<heading1><hchild>x[foo</hchild></heading1><paragraph><pchild>b]ar</pchild>abc</paragraph>',
  350. '<heading1><hchild>x[]ar</hchild>abc</heading1>'
  351. );
  352. test(
  353. 'merges nested elements to the right if left side element would become empty',
  354. '<heading1><hchild>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  355. '<paragraph><pchild>[]ar</pchild></paragraph>'
  356. );
  357. test(
  358. 'merges to the left if first element contains object (considers it as a content of that element)',
  359. '<heading1><hchild><widget></widget>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild></paragraph>',
  360. '<heading1><hchild><widget></widget>[]ar</hchild></heading1>'
  361. );
  362. test(
  363. 'merges elements when left end deep nested',
  364. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph><paragraph>x</paragraph>',
  365. '<paragraph>x<pchild>fo[]ary</pchild></paragraph><paragraph>x</paragraph>'
  366. );
  367. test(
  368. 'merges nested elements to the right (on multiple levels) if left side element would become empty',
  369. '<heading1><hchild>[foo</hchild></heading1><paragraph><pchild>b]ar</pchild>abc</paragraph>',
  370. '<paragraph><pchild>[]ar</pchild>abc</paragraph>'
  371. );
  372. test(
  373. 'merges to the right element when left end deep nested and will be empty',
  374. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  375. '<paragraph>[]ar</paragraph><paragraph>x</paragraph>'
  376. );
  377. test(
  378. 'merges elements when right end deep nested',
  379. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph><pchild>b]ar</pchild>x</paragraph>',
  380. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>x</paragraph>'
  381. );
  382. test(
  383. 'removes element when right end deep nested but left end would be empty',
  384. '<paragraph>x</paragraph><paragraph>[foo</paragraph><paragraph><pchild>b]ar</pchild></paragraph>',
  385. '<paragraph>x</paragraph><paragraph><pchild>[]ar</pchild></paragraph>'
  386. );
  387. test(
  388. 'merges elements when right end deep nested (in an empty container)',
  389. '<paragraph>fo[o</paragraph><paragraph><pchild>bar]</pchild></paragraph>',
  390. '<paragraph>fo[]</paragraph>'
  391. );
  392. test(
  393. 'removes elements when left end deep nested (in an empty container)',
  394. '<paragraph><pchild>[foo</pchild></paragraph><paragraph>b]ar</paragraph><paragraph>x</paragraph>',
  395. '<paragraph>[]ar</paragraph><paragraph>x</paragraph>'
  396. );
  397. describe( 'with 3rd level of nesting', () => {
  398. test(
  399. 'merges elements when deep nested (same name)',
  400. '<pparent>x<paragraph>x<pchild>fo[o</pchild></paragraph></pparent>' +
  401. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  402. '<pparent>x<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>y</pparent>'
  403. );
  404. test(
  405. 'removes elements when deep nested (same name)',
  406. '<pparent><paragraph><pchild>[foo</pchild></paragraph></pparent>' +
  407. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  408. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  409. );
  410. test(
  411. 'removes elements up to common ancestor when deep nested (same name)',
  412. '<pparent>' +
  413. '<paragraph><pchild>[foo</pchild></paragraph>' +
  414. '<paragraph><pchild>b]ar</pchild>y</paragraph>y' +
  415. '</pparent>',
  416. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  417. );
  418. test(
  419. 'merges elements when deep nested (different name)',
  420. '<pparent>x<heading1>x<hchild>fo[o</hchild></heading1></pparent>' +
  421. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  422. '<pparent>x<heading1>x<hchild>fo[]ar</hchild>y</heading1>y</pparent>'
  423. );
  424. test(
  425. 'removes elements when deep nested (different name)',
  426. '<pparent><heading1><hchild>[foo</hchild></heading1></pparent>' +
  427. '<pparent><paragraph><pchild>b]ar</pchild>y</paragraph>y</pparent>',
  428. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  429. );
  430. test(
  431. 'merges elements up to common ancestor when deep nested (different names)',
  432. '<pparent>' +
  433. '<heading1><hchild>fo[o</hchild></heading1>' +
  434. '<paragraph><pchild>b]ar</pchild></paragraph>' +
  435. '</pparent>',
  436. '<pparent><heading1><hchild>fo[]ar</hchild></heading1></pparent>'
  437. );
  438. test(
  439. 'removes elements up to common ancestor when deep nested (different names)',
  440. '<pparent>' +
  441. '<heading1><hchild>[foo</hchild></heading1>' +
  442. '<paragraph><pchild>b]ar</pchild>y</paragraph>y' +
  443. '</pparent>',
  444. '<pparent><paragraph><pchild>[]ar</pchild>y</paragraph>y</pparent>'
  445. );
  446. } );
  447. describe( 'with 3rd level of nesting o the left end', () => {
  448. test(
  449. 'merges elements',
  450. '<pparent>x<paragraph>foo<pchild>ba[r</pchild></paragraph></pparent>' +
  451. '<paragraph>b]om</paragraph>',
  452. '<pparent>x<paragraph>foo<pchild>ba[]om</pchild></paragraph></pparent>'
  453. );
  454. test(
  455. 'merges elements (different names)',
  456. '<pparent>x<heading1>foo<hchild>ba[r</hchild></heading1></pparent>' +
  457. '<paragraph>b]om</paragraph>',
  458. '<pparent>x<heading1>foo<hchild>ba[]om</hchild></heading1></pparent>'
  459. );
  460. test(
  461. 'removes elements',
  462. '<pparent><paragraph><pchild>[bar</pchild></paragraph></pparent>' +
  463. '<paragraph>b]om</paragraph>',
  464. '<paragraph>[]om</paragraph>'
  465. );
  466. test(
  467. 'removes elements up to common ancestor (different names)',
  468. '<pparent>' +
  469. '<heading1><hchild>[foo</hchild></heading1>' +
  470. '<paragraph>b]ar</paragraph>y' +
  471. '</pparent>',
  472. '<pparent><paragraph>[]ar</paragraph>y</pparent>'
  473. );
  474. } );
  475. describe( 'with 3rd level of nesting o the right end', () => {
  476. test(
  477. 'merges elements',
  478. '<paragraph>b[om</paragraph>' +
  479. '<pparent><paragraph><pchild>ba]r</pchild></paragraph></pparent>',
  480. '<paragraph>b[]r</paragraph>'
  481. );
  482. test(
  483. 'merges elements (different names)',
  484. '<paragraph>bo[m</paragraph>' +
  485. '<pparent><heading1><hchild>b]ar</hchild></heading1></pparent>',
  486. '<paragraph>bo[]ar</paragraph>'
  487. );
  488. test(
  489. 'merges elements (different names, reversed)',
  490. '<heading1>bo[m</heading1>' +
  491. '<pparent><paragraph><pchild>b]ar</pchild></paragraph></pparent>',
  492. '<heading1>bo[]ar</heading1>'
  493. );
  494. test(
  495. 'removes elements',
  496. '<paragraph>[bom</paragraph>' +
  497. '<pparent><paragraph><pchild>b]ar</pchild></paragraph></pparent>',
  498. '<pparent><paragraph><pchild>[]ar</pchild></paragraph></pparent>'
  499. );
  500. test(
  501. 'removes elements up to common ancestor (different names)',
  502. '<pparent>' +
  503. '<heading1>[bar</heading1>y' +
  504. '<paragraph><pchild>f]oo</pchild></paragraph>' +
  505. '</pparent>',
  506. '<pparent><paragraph><pchild>[]oo</pchild></paragraph></pparent>'
  507. );
  508. } );
  509. } );
  510. describe( 'with object elements', () => {
  511. beforeEach( () => {
  512. const schema = model.schema;
  513. schema.register( 'blockWidget', {
  514. isObject: true
  515. } );
  516. schema.register( 'nestedEditable', {
  517. isLimit: true
  518. } );
  519. schema.extend( 'blockWidget', { allowIn: '$root' } );
  520. schema.extend( 'nestedEditable', { allowIn: 'blockWidget' } );
  521. schema.extend( '$text', { allowIn: 'nestedEditable' } );
  522. } );
  523. test(
  524. 'does not merge an object element (if it is first)',
  525. '<blockWidget><nestedEditable>fo[o</nestedEditable></blockWidget><paragraph>b]ar</paragraph>',
  526. '<blockWidget><nestedEditable>fo[]</nestedEditable></blockWidget><paragraph>ar</paragraph>'
  527. );
  528. test(
  529. 'does not merge an object element (if it is second)',
  530. '<paragraph>ba[r</paragraph><blockWidget><nestedEditable>f]oo</nestedEditable></blockWidget>',
  531. '<paragraph>ba[]</paragraph><blockWidget><nestedEditable>oo</nestedEditable></blockWidget>'
  532. );
  533. } );
  534. describe( 'with markers', () => {
  535. it( 'should merge left if the first element is not empty', () => {
  536. setData( model, '<heading1>foo[</heading1><paragraph>]bar</paragraph>' );
  537. model.enqueueChange( 'transparent', writer => {
  538. const root = doc.getRoot( );
  539. const range = writer.createRange(
  540. writer.createPositionFromPath( root, [ 0, 3 ] ),
  541. writer.createPositionFromPath( root, [ 1, 0 ] )
  542. );
  543. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  544. } );
  545. deleteContent( model, doc.selection );
  546. expect( getData( model ) ).to.equal( '<heading1>foo[]bar</heading1>' );
  547. } );
  548. it( 'should merge right if the first element is empty', () => {
  549. setData( model, '<heading1>[</heading1><paragraph>]bar</paragraph>' );
  550. model.enqueueChange( 'transparent', writer => {
  551. const root = doc.getRoot( );
  552. const range = writer.createRange(
  553. writer.createPositionFromPath( root, [ 0, 0 ] ),
  554. writer.createPositionFromPath( root, [ 1, 0 ] )
  555. );
  556. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  557. } );
  558. deleteContent( model, doc.selection );
  559. expect( getData( model ) ).to.equal( '<paragraph>[]bar</paragraph>' );
  560. } );
  561. it( 'should merge left if the last element is empty', () => {
  562. setData( model, '<heading1>foo[</heading1><paragraph>]</paragraph>' );
  563. model.enqueueChange( 'transparent', writer => {
  564. const root = doc.getRoot( );
  565. const range = writer.createRange(
  566. writer.createPositionFromPath( root, [ 0, 3 ] ),
  567. writer.createPositionFromPath( root, [ 1, 0 ] )
  568. );
  569. writer.addMarker( 'comment1', { range, usingOperation: true, affectsData: true } );
  570. } );
  571. deleteContent( model, doc.selection );
  572. expect( getData( model ) ).to.equal( '<heading1>foo[]</heading1>' );
  573. } );
  574. } );
  575. describe( 'filtering out', () => {
  576. beforeEach( () => {
  577. const schema = model.schema;
  578. schema.addAttributeCheck( ( ctx, attributeName ) => {
  579. // Disallow 'c' on pchild>pchild>$text.
  580. if ( ctx.endsWith( 'pchild pchild $text' ) && attributeName == 'c' ) {
  581. return false;
  582. }
  583. // Allow 'a' and 'b' on paragraph>$text.
  584. if ( ctx.endsWith( 'paragraph $text' ) && [ 'a', 'b' ].includes( attributeName ) ) {
  585. return true;
  586. }
  587. // Allow 'b' and 'c' in pchild>$text.
  588. if ( ctx.endsWith( 'pchild $text' ) && [ 'b', 'c' ].includes( attributeName ) ) {
  589. return true;
  590. }
  591. } );
  592. schema.extend( 'pchild', { allowIn: 'pchild' } );
  593. } );
  594. test(
  595. 'filters out disallowed attributes after left merge',
  596. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>y]<$text a="1" b="1">z</$text></paragraph>',
  597. '<paragraph>x<pchild>fo[]<$text b="1">z</$text></pchild></paragraph>'
  598. );
  599. test(
  600. 'filters out disallowed attributes from nested nodes after left merge',
  601. '<paragraph>' +
  602. 'x' +
  603. '<pchild>fo[o</pchild>' +
  604. '</paragraph>' +
  605. '<paragraph>' +
  606. 'b]a<$text a="1" b="1">r</$text>' +
  607. '<pchild>b<$text b="1" c="1">i</$text>z</pchild>' +
  608. 'y' +
  609. '</paragraph>',
  610. '<paragraph>' +
  611. 'x' +
  612. '<pchild>' +
  613. 'fo[]a<$text b="1">r</$text>' +
  614. '<pchild>b<$text b="1">i</$text>z</pchild>' +
  615. 'y' +
  616. '</pchild>' +
  617. '</paragraph>'
  618. );
  619. test(
  620. 'filters out disallowed attributes after right merge',
  621. '<paragraph>fo[o</paragraph><paragraph><pchild>x<$text b="1" c="1">y]z</$text></pchild></paragraph>',
  622. '<paragraph>fo[]<$text b="1">z</$text></paragraph>'
  623. );
  624. } );
  625. } );
  626. describe( 'in element selections scenarios', () => {
  627. beforeEach( () => {
  628. model = new Model();
  629. doc = model.document;
  630. // <p> like root.
  631. doc.createRoot( 'paragraph', 'paragraphRoot' );
  632. // <body> like root.
  633. doc.createRoot( '$root', 'bodyRoot' );
  634. // Special root which allows only blockWidgets inside itself.
  635. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  636. const schema = model.schema;
  637. schema.register( 'image', { allowWhere: '$text' } );
  638. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  639. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  640. schema.register( 'blockWidget', { isLimit: true } );
  641. schema.register( 'restrictedRoot', {
  642. isLimit: true
  643. } );
  644. schema.extend( '$block', { allowIn: '$root' } );
  645. schema.extend( 'blockWidget', { allowIn: '$root' } );
  646. schema.extend( 'blockWidget', { allowIn: 'restrictedRoot' } );
  647. } );
  648. // See also "in simple scenarios => deletes an element".
  649. it( 'deletes two inline elements', () => {
  650. model.schema.extend( 'paragraph', {
  651. isLimit: true
  652. } );
  653. setData(
  654. model,
  655. 'x[<image></image><image></image>]z',
  656. { rootName: 'paragraphRoot' }
  657. );
  658. deleteContent( model, doc.selection );
  659. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  660. .to.equal( 'x[]z' );
  661. } );
  662. it( 'moves the (custom) selection to the nearest paragraph', () => {
  663. setData(
  664. model,
  665. '<paragraph>[x]</paragraph><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  666. { rootName: 'bodyRoot' }
  667. );
  668. const root = doc.getRoot( 'bodyRoot' );
  669. // [<paragraph>yyy</paragraph>]
  670. const selection = new Selection( [
  671. new Range( new Position( root, [ 1 ] ), new Position( root, [ 2 ] ) )
  672. ] );
  673. deleteContent( model, selection );
  674. expect( getData( model, { rootName: 'bodyRoot' } ) )
  675. .to.equal( '<paragraph>[x]</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  676. expect( stringify( root, selection ) )
  677. .to.equal( '<$root><paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph></$root>' );
  678. } );
  679. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  680. setData(
  681. model,
  682. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  683. { rootName: 'bodyRoot' }
  684. );
  685. deleteContent( model, doc.selection );
  686. expect( getData( model, { rootName: 'bodyRoot' } ) )
  687. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  688. } );
  689. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  690. setData(
  691. model,
  692. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>z</paragraph>',
  693. { rootName: 'bodyRoot' }
  694. );
  695. // [<heading1>yyy</heading1>]
  696. const range = new Range(
  697. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  698. new Position( doc.getRoot( 'bodyRoot' ), [ 2 ] )
  699. );
  700. deleteContent( model, new Selection( range ) );
  701. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  702. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  703. } );
  704. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  705. setData(
  706. model,
  707. '<paragraph>x</paragraph><heading1>yyy</heading1><paragraph>yyy</paragraph><paragraph>z</paragraph>',
  708. { rootName: 'bodyRoot' }
  709. );
  710. // [<heading1>yyy</heading1><paragraph>yyy</paragraph>]
  711. const range = new Range(
  712. new Position( doc.getRoot( 'bodyRoot' ), [ 1 ] ),
  713. new Position( doc.getRoot( 'bodyRoot' ), [ 3 ] )
  714. );
  715. deleteContent( model, new Selection( range ) );
  716. expect( getData( model, { rootName: 'bodyRoot', withoutSelection: true } ) )
  717. .to.equal( '<paragraph>x</paragraph><paragraph></paragraph><paragraph>z</paragraph>' );
  718. } );
  719. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  720. setData(
  721. model,
  722. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  723. { rootName: 'bodyRoot' }
  724. );
  725. deleteContent( model, doc.selection );
  726. expect( getData( model, { rootName: 'bodyRoot' } ) )
  727. .to.equal( '<paragraph>[]</paragraph>' );
  728. } );
  729. it( 'does not create a paragraph when it is not allowed', () => {
  730. setData(
  731. model,
  732. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  733. { rootName: 'restrictedRoot' }
  734. );
  735. deleteContent( model, doc.selection );
  736. expect( getData( model, { rootName: 'restrictedRoot' } ) )
  737. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  738. } );
  739. it( 'does not create a paragraph when doNotAutoparagraph option is set to true', () => {
  740. setData(
  741. model,
  742. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  743. { rootName: 'bodyRoot' }
  744. );
  745. deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
  746. expect( getData( model, { rootName: 'bodyRoot' } ) )
  747. .to.equal( '<paragraph>x[]</paragraph><paragraph>z</paragraph>' );
  748. } );
  749. it( 'does not create a paragraph when after deletion there is no valid selection range (empty root)', () => {
  750. setData(
  751. model,
  752. '[<blockWidget></blockWidget>]',
  753. { rootName: 'bodyRoot' }
  754. );
  755. deleteContent( model, doc.selection, { doNotAutoparagraph: true } );
  756. expect( getData( model, { rootName: 'bodyRoot' } ) )
  757. .to.equal( '[]' );
  758. } );
  759. } );
  760. describe( 'integration with inline limit elements', () => {
  761. beforeEach( () => {
  762. model = new Model();
  763. doc = model.document;
  764. doc.createRoot();
  765. const schema = model.schema;
  766. schema.register( 'inlineLimit', {
  767. isLimit: true,
  768. allowIn: '$root'
  769. } );
  770. schema.extend( '$text', {
  771. allowIn: [ 'inlineLimit', '$root', 'x' ]
  772. } );
  773. schema.register( 'x', { allowIn: '$root' } );
  774. } );
  775. test(
  776. 'should delete inside inline limit element',
  777. '<inlineLimit>foo [bar] baz</inlineLimit>',
  778. '<inlineLimit>foo [] baz</inlineLimit>'
  779. );
  780. test(
  781. 'should delete whole inline limit element',
  782. 'x[<inlineLimit>foo bar</inlineLimit>]x',
  783. 'x[]x'
  784. );
  785. test(
  786. 'should delete from two inline limit elements',
  787. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  788. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  789. );
  790. test(
  791. 'merge option should be ignored if both elements are limits',
  792. '<inlineLimit>foo [bar</inlineLimit><inlineLimit>baz] qux</inlineLimit>',
  793. '<inlineLimit>foo []</inlineLimit><inlineLimit> qux</inlineLimit>'
  794. );
  795. test(
  796. 'merge option should be ignored if the first element is a limit',
  797. '<inlineLimit>foo [bar</inlineLimit><x>baz] qux</x>',
  798. '<inlineLimit>foo []</inlineLimit><x> qux</x>'
  799. );
  800. test(
  801. 'merge option should be ignored if the second element is a limit',
  802. '<x>baz [qux</x><inlineLimit>foo] bar</inlineLimit>',
  803. '<x>baz []</x><inlineLimit> bar</inlineLimit>'
  804. );
  805. } );
  806. describe( 'integration with block limit elements', () => {
  807. beforeEach( () => {
  808. model = new Model();
  809. doc = model.document;
  810. doc.createRoot();
  811. const schema = model.schema;
  812. schema.register( 'blockLimit', {
  813. isLimit: true
  814. } );
  815. schema.extend( 'blockLimit', { allowIn: '$root' } );
  816. schema.extend( '$block', { allowIn: 'blockLimit' } );
  817. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  818. schema.register( 'blockQuote', {
  819. allowWhere: '$block',
  820. allowContentOf: '$root'
  821. } );
  822. } );
  823. test(
  824. 'should delete inside block limit element',
  825. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  826. '<blockLimit><paragraph>fo[]</paragraph><paragraph>ar</paragraph></blockLimit>',
  827. { leaveUnmerged: true }
  828. );
  829. test(
  830. 'should delete inside block limit element (with merge)',
  831. '<blockLimit><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph></blockLimit>',
  832. '<blockLimit><paragraph>fo[]ar</paragraph></blockLimit>'
  833. );
  834. test(
  835. 'should delete whole block limit element',
  836. '<paragraph>x</paragraph>[<blockLimit><paragraph>foo</paragraph></blockLimit>]<paragraph>x</paragraph>',
  837. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>x</paragraph>'
  838. );
  839. test(
  840. 'should delete from two block limit elements',
  841. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  842. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  843. );
  844. test(
  845. 'merge option should be ignored if any of the elements is a limit',
  846. '<blockLimit><paragraph>foo [bar</paragraph></blockLimit><blockLimit><paragraph>baz] qux</paragraph></blockLimit>',
  847. '<blockLimit><paragraph>foo []</paragraph></blockLimit><blockLimit><paragraph> qux</paragraph></blockLimit>'
  848. );
  849. // See: https://github.com/ckeditor/ckeditor5/issues/1265.
  850. it( 'should proper merge two elements which are inside limit element', () => {
  851. setData( model,
  852. '<blockLimit>' +
  853. '<blockQuote>' +
  854. '<paragraph>Foo</paragraph>' +
  855. '</blockQuote>' +
  856. '<paragraph>[]Bar</paragraph>' +
  857. '</blockLimit>'
  858. );
  859. model.modifySelection( doc.selection, { direction: 'backward' } );
  860. deleteContent( model, doc.selection );
  861. expect( getData( model ) ).to.equal(
  862. '<blockLimit>' +
  863. '<blockQuote>' +
  864. '<paragraph>Foo[]Bar</paragraph>' +
  865. '</blockQuote>' +
  866. '</blockLimit>' );
  867. } );
  868. it( 'should proper merge elements which are inside limit element (nested elements)', () => {
  869. setData( model,
  870. '<blockQuote>' +
  871. '<blockLimit>' +
  872. '<blockQuote>' +
  873. '<paragraph>Foo.</paragraph>' +
  874. '<blockQuote>' +
  875. '<paragraph>Foo</paragraph>' +
  876. '</blockQuote>' +
  877. '</blockQuote>' +
  878. '<paragraph>[]Bar</paragraph>' +
  879. '</blockLimit>' +
  880. '</blockQuote>'
  881. );
  882. model.modifySelection( doc.selection, { direction: 'backward' } );
  883. deleteContent( model, doc.selection );
  884. expect( getData( model ) ).to.equal(
  885. '<blockQuote>' +
  886. '<blockLimit>' +
  887. '<blockQuote>' +
  888. '<paragraph>Foo.</paragraph>' +
  889. '<blockQuote>' +
  890. '<paragraph>Foo[]Bar</paragraph>' +
  891. '</blockQuote>' +
  892. '</blockQuote>' +
  893. '</blockLimit>' +
  894. '</blockQuote>'
  895. );
  896. } );
  897. } );
  898. describe( 'should leave a paragraph if the entire content was selected', () => {
  899. beforeEach( () => {
  900. model = new Model();
  901. doc = model.document;
  902. doc.createRoot();
  903. const schema = model.schema;
  904. schema.register( 'div', {
  905. inheritAllFrom: '$block',
  906. isLimit: true
  907. } );
  908. schema.register( 'article', {
  909. inheritAllFrom: '$block',
  910. isLimit: true
  911. } );
  912. schema.register( 'image', {
  913. allowWhere: '$text',
  914. isObject: true
  915. } );
  916. schema.register( 'paragraph', { inheritAllFrom: '$block' } );
  917. schema.register( 'heading1', { inheritAllFrom: '$block' } );
  918. schema.register( 'heading2', { inheritAllFrom: '$block' } );
  919. schema.extend( '$text', { allowIn: '$root' } );
  920. schema.extend( 'image', { allowIn: '$root' } );
  921. schema.extend( 'image', { allowIn: 'heading1' } );
  922. schema.extend( 'heading1', { allowIn: 'div' } );
  923. schema.extend( 'paragraph', { allowIn: 'div' } );
  924. schema.extend( 'heading1', { allowIn: 'article' } );
  925. schema.extend( 'heading2', { allowIn: 'article' } );
  926. } );
  927. test(
  928. 'but not if only one block was selected',
  929. '<heading1>[xx]</heading1>',
  930. '<heading1>[]</heading1>'
  931. );
  932. test(
  933. 'when the entire heading and paragraph were selected',
  934. '<heading1>[xx</heading1><paragraph>yy]</paragraph>',
  935. '<paragraph>[]</paragraph>'
  936. );
  937. test(
  938. 'when the entire content was selected',
  939. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  940. '<paragraph>[]</paragraph>'
  941. );
  942. test(
  943. 'inside the limit element when the entire heading and paragraph were inside',
  944. '<div><heading1>[xx</heading1><paragraph>yy]</paragraph></div>',
  945. '<div><paragraph>[]</paragraph></div>'
  946. );
  947. test(
  948. 'but not if schema does not accept paragraph in limit element',
  949. '<article><heading1>[xx</heading1><heading2>yy]</heading2></article>',
  950. '<article><heading1>[]</heading1></article>'
  951. );
  952. test(
  953. 'but not if selection is not containing the whole content',
  954. '<image></image><heading1>[xx</heading1><paragraph>yy]</paragraph>',
  955. '<image></image><heading1>[]</heading1>'
  956. );
  957. test(
  958. 'but not if only single element is selected',
  959. '<heading1>[<image></image>xx]</heading1>',
  960. '<heading1>[]</heading1>'
  961. );
  962. it( 'when root element was not added as Schema limits work fine as well', () => {
  963. doc.createRoot( 'paragraph', 'paragraphRoot' );
  964. setData(
  965. model,
  966. 'x[<image></image><image></image>]z',
  967. { rootName: 'paragraphRoot' }
  968. );
  969. deleteContent( model, doc.selection );
  970. expect( getData( model, { rootName: 'paragraphRoot' } ) )
  971. .to.equal( 'x[]z' );
  972. } );
  973. test(
  974. 'but not if the flag "doNotResetEntireContent" is set to true',
  975. '<heading1>[</heading1><paragraph>]</paragraph>',
  976. '<heading1>[]</heading1>',
  977. {
  978. doNotResetEntireContent: true
  979. }
  980. );
  981. } );
  982. function test( title, input, output, options ) {
  983. it( title, () => {
  984. model.enqueueChange( 'transparent', () => {
  985. setData( model, input );
  986. deleteContent( model, doc.selection, options );
  987. } );
  988. expect( getData( model ) ).to.equal( output );
  989. } );
  990. }
  991. } );
  992. } );