8
0

deletecontent.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from 'ckeditor5/engine/model/document.js';
  6. import deleteContent from 'ckeditor5/engine/controller/deletecontent.js';
  7. import { setData, getData } from 'ckeditor5/engine/dev-utils/model.js';
  8. describe( 'Delete utils', () => {
  9. let doc;
  10. describe( 'deleteContent', () => {
  11. describe( 'in simple scenarios', () => {
  12. beforeEach( () => {
  13. doc = new Document();
  14. doc.createRoot();
  15. const schema = doc.schema;
  16. schema.registerItem( 'image', '$inline' );
  17. schema.allow( { name: '$text', inside: '$root' } );
  18. schema.allow( { name: 'image', inside: '$root' } );
  19. } );
  20. test(
  21. 'does nothing on collapsed selection',
  22. 'f[]oo',
  23. 'f[]oo'
  24. );
  25. test(
  26. 'deletes single character',
  27. 'f[o]o',
  28. 'f[]o'
  29. );
  30. it( 'deletes single character (backward selection)' , () => {
  31. setData( doc, 'f[o]o', { lastRangeBackward: true } );
  32. deleteContent( doc.selection, doc.batch() );
  33. expect( getData( doc ) ).to.equal( 'f[]o' );
  34. } );
  35. test(
  36. 'deletes whole text',
  37. '[foo]',
  38. '[]'
  39. );
  40. test(
  41. 'deletes whole text between nodes',
  42. '<image></image>[foo]<image></image>',
  43. '<image></image>[]<image></image>'
  44. );
  45. test(
  46. 'deletes an element',
  47. 'x[<image></image>]y',
  48. 'x[]y'
  49. );
  50. test(
  51. 'deletes a bunch of nodes',
  52. 'w[x<image></image>y]z',
  53. 'w[]z'
  54. );
  55. test(
  56. 'does not break things when option.merge passed',
  57. 'w[x<image></image>y]z',
  58. 'w[]z',
  59. { merge: true }
  60. );
  61. } );
  62. describe( 'with text attributes', () => {
  63. beforeEach( () => {
  64. doc = new Document();
  65. doc.createRoot();
  66. const schema = doc.schema;
  67. schema.registerItem( 'image', '$inline' );
  68. schema.registerItem( 'paragraph', '$block' );
  69. schema.allow( { name: '$text', inside: '$root' } );
  70. schema.allow( { name: '$text', attributes: [ 'bold', 'italic' ] } );
  71. } );
  72. it( 'deletes characters (first half has attrs)', () => {
  73. setData( doc, '<$text bold="true">fo[o</$text>b]ar' );
  74. deleteContent( doc.selection, doc.batch() );
  75. expect( getData( doc ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  76. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  77. } );
  78. it( 'deletes characters (2nd half has attrs)', () => {
  79. setData( doc, 'fo[o<$text bold="true">b]ar</$text>' );
  80. deleteContent( doc.selection, doc.batch() );
  81. expect( getData( doc ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  82. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  83. } );
  84. it( 'clears selection attrs when emptied content', () => {
  85. setData( doc, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  86. deleteContent( doc.selection, doc.batch() );
  87. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  88. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  89. } );
  90. it( 'leaves selection attributes when text contains them', () => {
  91. setData(
  92. doc,
  93. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  94. {
  95. selectionAttributes: {
  96. bold: true
  97. }
  98. }
  99. );
  100. deleteContent( doc.selection, doc.batch() );
  101. expect( getData( doc ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  102. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  103. } );
  104. } );
  105. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  106. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  107. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  108. // should not happen unless we're talking about lists or tables, but these features will need to cover
  109. // their scenarios themselves. In all generic scenarios elements are never nested.
  110. //
  111. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  112. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  113. // Those case should, again, be handled by their specific implementations.
  114. describe( 'in multi-element scenarios', () => {
  115. beforeEach( () => {
  116. doc = new Document();
  117. doc.createRoot();
  118. const schema = doc.schema;
  119. schema.registerItem( 'paragraph', '$block' );
  120. schema.registerItem( 'heading1', '$block' );
  121. schema.registerItem( 'pchild' );
  122. schema.registerItem( 'image', '$inline' );
  123. schema.allow( { name: 'pchild', inside: 'paragraph' } );
  124. schema.allow( { name: 'paragraph', attributes: [ 'align' ] } );
  125. } );
  126. test(
  127. 'do not merge when no need to',
  128. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  129. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>',
  130. { merge: true }
  131. );
  132. test(
  133. 'merges second element into the first one (same name)',
  134. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  135. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  136. { merge: true }
  137. );
  138. test(
  139. 'does not merge second element into the first one (same name, !option.merge)',
  140. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  141. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>'
  142. );
  143. test(
  144. 'merges second element into the first one (same name)',
  145. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  146. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  147. { merge: true }
  148. );
  149. test(
  150. 'merges second element into the first one (different name)',
  151. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  152. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>',
  153. { merge: true }
  154. );
  155. it( 'merges second element into the first one (different name, backward selection)', () => {
  156. setData(
  157. doc,
  158. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  159. { lastRangeBackward: true }
  160. );
  161. deleteContent( doc.selection, doc.batch(), { merge: true } );
  162. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  163. } );
  164. test(
  165. 'merges second element into the first one (different attrs)',
  166. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  167. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>',
  168. { merge: true }
  169. );
  170. test(
  171. 'merges second element to an empty first element',
  172. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  173. '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>',
  174. { merge: true }
  175. );
  176. test(
  177. 'merges elements when deep nested',
  178. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  179. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>',
  180. { merge: true }
  181. );
  182. // For code coverage reasons.
  183. test(
  184. 'merges element when selection is in two consecutive nodes even when it is empty',
  185. '<paragraph>foo[</paragraph><paragraph>]bar</paragraph>',
  186. '<paragraph>foo[]bar</paragraph>',
  187. { merge: true }
  188. );
  189. // If you disagree with this case please read the notes before this section.
  190. test(
  191. 'merges elements when left end deep nested',
  192. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph>',
  193. '<paragraph>x<pchild>fo[]</pchild>ary</paragraph>',
  194. { merge: true }
  195. );
  196. // If you disagree with this case please read the notes before this section.
  197. test(
  198. 'merges elements when right end deep nested',
  199. '<paragraph>xfo[o</paragraph><paragraph><pchild>b]ar</pchild>y<image></image></paragraph>',
  200. '<paragraph>xfo[]<pchild>ar</pchild>y<image></image></paragraph>',
  201. { merge: true }
  202. );
  203. test(
  204. 'merges elements when more content in the right branch',
  205. '<paragraph>xfo[o</paragraph><paragraph>b]a<pchild>r</pchild>y</paragraph>',
  206. '<paragraph>xfo[]a<pchild>r</pchild>y</paragraph>',
  207. { merge: true }
  208. );
  209. test(
  210. 'leaves just one element when all selected',
  211. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  212. '<heading1>[]</heading1>',
  213. { merge: true }
  214. );
  215. it( 'uses remove delta instead of merge delta if merged element is empty', () => {
  216. setData( doc, '<paragraph>ab[cd</paragraph><paragraph>efgh]</paragraph>' );
  217. const batch = doc.batch();
  218. const spyMerge = sinon.spy( batch, 'merge' );
  219. const spyRemove = sinon.spy( batch, 'remove' );
  220. deleteContent( doc.selection, batch, { merge: true } );
  221. expect( getData( doc ) ).to.equal( '<paragraph>ab[]</paragraph>' );
  222. expect( spyMerge.called ).to.be.false;
  223. expect( spyRemove.called ).to.be.true;
  224. } );
  225. } );
  226. describe( 'in element selections scenarios', () => {
  227. beforeEach( () => {
  228. doc = new Document();
  229. // <p> like root.
  230. doc.createRoot( 'paragraph', 'paragraphRoot' );
  231. // <body> like root.
  232. doc.createRoot( '$root', 'bodyRoot' );
  233. // Special root which allows only blockWidgets inside itself.
  234. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  235. const schema = doc.schema;
  236. schema.registerItem( 'image', '$inline' );
  237. schema.registerItem( 'paragraph', '$block' );
  238. schema.registerItem( 'heading1', '$block' );
  239. schema.registerItem( 'blockWidget' );
  240. schema.registerItem( 'restrictedRoot' );
  241. schema.allow( { name: '$block', inside: '$root' } );
  242. schema.allow( { name: 'blockWidget', inside: '$root' } );
  243. schema.allow( { name: 'blockWidget', inside: 'restrictedRoot' } );
  244. } );
  245. // See also "in simple scenarios => deletes an element".
  246. it( 'deletes two inline elements', () => {
  247. setData(
  248. doc,
  249. '<paragraph>x[<image></image><image></image>]z</paragraph>',
  250. { rootName: 'paragraphRoot' }
  251. );
  252. deleteContent( doc.selection, doc.batch() );
  253. expect( getData( doc, { rootName: 'paragraphRoot' } ) )
  254. .to.equal( '<paragraph>x[]z</paragraph>' );
  255. } );
  256. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  257. setData(
  258. doc,
  259. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  260. { rootName: 'bodyRoot' }
  261. );
  262. deleteContent( doc.selection, doc.batch() );
  263. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  264. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  265. } );
  266. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  267. setData(
  268. doc,
  269. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  270. { rootName: 'bodyRoot' }
  271. );
  272. deleteContent( doc.selection, doc.batch() );
  273. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  274. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  275. } );
  276. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  277. setData(
  278. doc,
  279. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  280. { rootName: 'bodyRoot' }
  281. );
  282. deleteContent( doc.selection, doc.batch() );
  283. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  284. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  285. } );
  286. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  287. setData(
  288. doc,
  289. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  290. { rootName: 'bodyRoot' }
  291. );
  292. deleteContent( doc.selection, doc.batch() );
  293. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  294. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  295. } );
  296. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  297. setData(
  298. doc,
  299. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  300. { rootName: 'bodyRoot' }
  301. );
  302. deleteContent( doc.selection, doc.batch() );
  303. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  304. .to.equal( '<paragraph>[]</paragraph>' );
  305. } );
  306. it( 'does not create a paragraph when it is not allowed', () => {
  307. setData(
  308. doc,
  309. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  310. { rootName: 'restrictedRoot' }
  311. );
  312. deleteContent( doc.selection, doc.batch() );
  313. expect( getData( doc, { rootName: 'restrictedRoot' } ) )
  314. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  315. } );
  316. } );
  317. function test( title, input, output, options ) {
  318. it( title, () => {
  319. setData( doc, input );
  320. deleteContent( doc.selection, doc.batch(), options );
  321. expect( getData( doc ) ).to.equal( output );
  322. } );
  323. }
  324. } );
  325. } );