deletecontents.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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' );
  75. deleteContents( doc.batch(), doc.selection );
  76. expect( getData( doc ) ).to.equal( '<$text bold="true">fo[]</$text>ar' );
  77. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  78. } );
  79. it( 'deletes characters (2nd half has attrs)', () => {
  80. setData( doc, 'fo[o<$text bold="true">b]ar</$text>' );
  81. deleteContents( doc.batch(), doc.selection );
  82. expect( getData( doc ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  83. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  84. } );
  85. it( 'clears selection attrs when emptied content', () => {
  86. setData( doc, '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>' );
  87. deleteContents( doc.batch(), doc.selection );
  88. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  89. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  90. } );
  91. it( 'leaves selection attributes when text contains them', () => {
  92. setData(
  93. doc,
  94. '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>',
  95. {
  96. selectionAttributes: {
  97. bold: true
  98. }
  99. }
  100. );
  101. deleteContents( doc.batch(), doc.selection );
  102. expect( getData( doc ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  103. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  104. } );
  105. } );
  106. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  107. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  108. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  109. // should not happen unless we're talking about lists or tables, but these features will need to cover
  110. // their scenarios themselves. In all generic scenarios elements are never nested.
  111. //
  112. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  113. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  114. // Those case should, again, be handled by their specific implementations.
  115. describe( 'in multi-element scenarios', () => {
  116. beforeEach( () => {
  117. doc = new Document();
  118. doc.createRoot();
  119. const schema = doc.schema;
  120. schema.registerItem( 'paragraph', '$block' );
  121. schema.registerItem( 'heading1', '$block' );
  122. schema.registerItem( 'pchild' );
  123. schema.registerItem( 'image', '$inline' );
  124. schema.allow( { name: 'pchild', inside: 'paragraph' } );
  125. schema.allow( { name: 'paragraph', attributes: [ 'align' ] } );
  126. } );
  127. test(
  128. 'do not merge when no need to',
  129. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  130. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>',
  131. { merge: true }
  132. );
  133. test(
  134. 'merges second element into the first one (same name)',
  135. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  136. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  137. { merge: true }
  138. );
  139. test(
  140. 'does not merge second element into the first one (same name, !option.merge)',
  141. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  142. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>'
  143. );
  144. test(
  145. 'merges second element into the first one (same name)',
  146. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  147. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  148. { merge: true }
  149. );
  150. test(
  151. 'merges second element into the first one (different name)',
  152. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  153. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>',
  154. { merge: true }
  155. );
  156. it( 'merges second element into the first one (different name, backward selection)', () => {
  157. setData(
  158. doc,
  159. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  160. { lastRangeBackward: true }
  161. );
  162. deleteContents( doc.batch(), doc.selection, { merge: true } );
  163. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  164. } );
  165. test(
  166. 'merges second element into the first one (different attrs)',
  167. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  168. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>',
  169. { merge: true }
  170. );
  171. test(
  172. 'merges second element to an empty first element',
  173. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  174. '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>',
  175. { merge: true }
  176. );
  177. test(
  178. 'merges elements when deep nested',
  179. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  180. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>',
  181. { merge: true }
  182. );
  183. // For code coverage reasons.
  184. test(
  185. 'merges element when selection is in two consecutive nodes even when it is empty',
  186. '<paragraph>foo[</paragraph><paragraph>]bar</paragraph>',
  187. '<paragraph>foo[]bar</paragraph>',
  188. { merge: true }
  189. );
  190. // If you disagree with this case please read the notes before this section.
  191. test(
  192. 'merges elements when left end deep nested',
  193. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph>',
  194. '<paragraph>x<pchild>fo[]</pchild>ary</paragraph>',
  195. { merge: true }
  196. );
  197. // If you disagree with this case please read the notes before this section.
  198. test(
  199. 'merges elements when right end deep nested',
  200. '<paragraph>xfo[o</paragraph><paragraph><pchild>b]ar</pchild>y<image></image></paragraph>',
  201. '<paragraph>xfo[]<pchild>ar</pchild>y<image></image></paragraph>',
  202. { merge: true }
  203. );
  204. test(
  205. 'merges elements when more content in the right branch',
  206. '<paragraph>xfo[o</paragraph><paragraph>b]a<pchild>r</pchild>y</paragraph>',
  207. '<paragraph>xfo[]a<pchild>r</pchild>y</paragraph>',
  208. { merge: true }
  209. );
  210. test(
  211. 'leaves just one element when all selected',
  212. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  213. '<heading1>[]</heading1>',
  214. { merge: true }
  215. );
  216. } );
  217. describe( 'in element selections scenarios', () => {
  218. beforeEach( () => {
  219. doc = new Document();
  220. // <p> like root.
  221. doc.createRoot( 'paragraph', 'paragraphRoot' );
  222. // <body> like root.
  223. doc.createRoot( '$root', 'bodyRoot' );
  224. // Special root which allows only blockWidgets inside itself.
  225. doc.createRoot( 'restrictedRoot', 'restrictedRoot' );
  226. const schema = doc.schema;
  227. schema.registerItem( 'image', '$inline' );
  228. schema.registerItem( 'paragraph', '$block' );
  229. schema.registerItem( 'heading1', '$block' );
  230. schema.registerItem( 'blockWidget' );
  231. schema.registerItem( 'restrictedRoot' );
  232. schema.allow( { name: '$block', inside: '$root' } );
  233. schema.allow( { name: 'blockWidget', inside: '$root' } );
  234. schema.allow( { name: 'blockWidget', inside: 'restrictedRoot' } );
  235. } );
  236. // See also "in simple scenarios => deletes an element".
  237. it( 'deletes two inline elements', () => {
  238. setData(
  239. doc,
  240. '<paragraph>x[<image></image><image></image>]z</paragraph>',
  241. { rootName: 'paragraphRoot' }
  242. );
  243. deleteContents( doc.batch(), doc.selection );
  244. expect( getData( doc, { rootName: 'paragraphRoot' } ) )
  245. .to.equal( '<paragraph>x[]z</paragraph>' );
  246. } );
  247. it( 'creates a paragraph when text is not allowed (paragraph selected)', () => {
  248. setData(
  249. doc,
  250. '<paragraph>x</paragraph>[<paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  251. { rootName: 'bodyRoot' }
  252. );
  253. deleteContents( doc.batch(), doc.selection );
  254. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  255. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  256. } );
  257. it( 'creates a paragraph when text is not allowed (block widget selected)', () => {
  258. setData(
  259. doc,
  260. '<paragraph>x</paragraph>[<blockWidget></blockWidget>]<paragraph>z</paragraph>',
  261. { rootName: 'bodyRoot' }
  262. );
  263. deleteContents( doc.batch(), doc.selection );
  264. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  265. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  266. } );
  267. it( 'creates paragraph when text is not allowed (heading selected)', () => {
  268. setData(
  269. doc,
  270. '<paragraph>x</paragraph>[<heading1>yyy</heading1>]<paragraph>z</paragraph>',
  271. { rootName: 'bodyRoot' }
  272. );
  273. deleteContents( doc.batch(), doc.selection );
  274. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  275. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  276. } );
  277. it( 'creates paragraph when text is not allowed (two blocks selected)', () => {
  278. setData(
  279. doc,
  280. '<paragraph>x</paragraph>[<heading1>yyy</heading1><paragraph>yyy</paragraph>]<paragraph>z</paragraph>',
  281. { rootName: 'bodyRoot' }
  282. );
  283. deleteContents( doc.batch(), doc.selection );
  284. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  285. .to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>z</paragraph>' );
  286. } );
  287. it( 'creates paragraph when text is not allowed (all content selected)', () => {
  288. setData(
  289. doc,
  290. '[<heading1>x</heading1><paragraph>z</paragraph>]',
  291. { rootName: 'bodyRoot' }
  292. );
  293. deleteContents( doc.batch(), doc.selection );
  294. expect( getData( doc, { rootName: 'bodyRoot' } ) )
  295. .to.equal( '<paragraph>[]</paragraph>' );
  296. } );
  297. it( 'does not create a paragraph when it is not allowed', () => {
  298. setData(
  299. doc,
  300. '<blockWidget></blockWidget>[<blockWidget></blockWidget>]<blockWidget></blockWidget>',
  301. { rootName: 'restrictedRoot' }
  302. );
  303. deleteContents( doc.batch(), doc.selection );
  304. expect( getData( doc, { rootName: 'restrictedRoot' } ) )
  305. .to.equal( '<blockWidget></blockWidget>[]<blockWidget></blockWidget>' );
  306. } );
  307. } );
  308. function test( title, input, output, options ) {
  309. it( title, () => {
  310. setData( doc, input );
  311. deleteContents( doc.batch(), doc.selection, options );
  312. expect( getData( doc ) ).to.equal( output );
  313. } );
  314. }
  315. } );
  316. } );