deletecontents.js 13 KB

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