deletecontents.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. beforeEach( () => {
  12. doc = new Document();
  13. doc.createRoot();
  14. const schema = doc.schema;
  15. schema.registerItem( 'image', '$inline' );
  16. schema.registerItem( 'paragraph', '$block' );
  17. schema.registerItem( 'heading1', '$block' );
  18. schema.registerItem( 'pchild' );
  19. schema.allow( { name: 'pchild', inside: 'paragraph' } );
  20. schema.allow( { name: '$text', inside: '$root' } );
  21. schema.allow( { name: 'image', inside: '$root' } );
  22. schema.allow( { name: '$text', attributes: [ 'bold', 'italic' ] } );
  23. schema.allow( { name: 'paragraph', attributes: [ 'align' ] } );
  24. } );
  25. describe( 'deleteContents', () => {
  26. describe( 'in simple scenarios', () => {
  27. test(
  28. 'does nothing on collapsed selection',
  29. 'f[]oo',
  30. 'f[]oo'
  31. );
  32. test(
  33. 'deletes single character',
  34. 'f[o]o',
  35. 'f[]o'
  36. );
  37. it( 'deletes single character (backward selection)' , () => {
  38. setData( doc, 'f[o]o', { lastRangeBackward: true } );
  39. deleteContents( doc.batch(), doc.selection );
  40. expect( getData( doc ) ).to.equal( 'f[]o' );
  41. } );
  42. test(
  43. 'deletes whole text',
  44. '[foo]',
  45. '[]'
  46. );
  47. test(
  48. 'deletes whole text between nodes',
  49. '<image></image>[foo]<image></image>',
  50. '<image></image>[]<image></image>'
  51. );
  52. test(
  53. 'deletes an element',
  54. 'x[<image></image>]y',
  55. 'x[]y'
  56. );
  57. test(
  58. 'deletes a bunch of nodes',
  59. 'w[x<image></image>y]z',
  60. 'w[]z'
  61. );
  62. test(
  63. 'does not break things when option.merge passed',
  64. 'w[x<image></image>y]z',
  65. 'w[]z',
  66. { merge: true }
  67. );
  68. } );
  69. describe( 'with text attributes', () => {
  70. it( 'deletes characters (first half has attrs)', () => {
  71. setData( doc, '<$text bold="true">fo[o</$text>b]ar', { selectionAttributes: {
  72. bold: true
  73. } } );
  74. deleteContents( doc.batch(), doc.selection );
  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>', { selectionAttributes: {
  80. bold: true
  81. } } );
  82. deleteContents( doc.batch(), doc.selection );
  83. expect( getData( doc ) ).to.equal( 'fo[]<$text bold="true">ar</$text>' );
  84. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  85. } );
  86. it( 'clears selection attrs when emptied content', () => {
  87. setData(
  88. doc,
  89. '<paragraph>x</paragraph><paragraph>[<$text bold="true">foo</$text>]</paragraph><paragraph>y</paragraph>',
  90. {
  91. selectionAttributes: {
  92. bold: true
  93. }
  94. }
  95. );
  96. deleteContents( doc.batch(), doc.selection );
  97. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>' );
  98. expect( doc.selection.getAttribute( 'bold' ) ).to.undefined;
  99. } );
  100. it( 'leaves selection attributes when text contains them', () => {
  101. setData( doc, '<paragraph>x<$text bold="true">a[foo]b</$text>y</paragraph>', { selectionAttributes: {
  102. bold: true
  103. } } );
  104. deleteContents( doc.batch(), doc.selection );
  105. expect( getData( doc ) ).to.equal( '<paragraph>x<$text bold="true">a[]b</$text>y</paragraph>' );
  106. expect( doc.selection.getAttribute( 'bold' ) ).to.equal( true );
  107. } );
  108. } );
  109. // Note: The algorithm does not care what kind of it's merging as it knows nothing useful about these elements.
  110. // In most cases it handles all elements like you'd expect to handle block elements in HTML. However,
  111. // in some scenarios where the tree depth is bigger results may be hard to justify. In fact, such cases
  112. // should not happen unless we're talking about lists or tables, but these features will need to cover
  113. // their scenarios themselves. In all generic scenarios elements are never nested.
  114. //
  115. // You may also be thinking – but I don't want my elements to be merged. It means that there are some special rules,
  116. // like – multiple editing hosts (cE=true/false in use) or block limit elements like <td>.
  117. // Those case should, again, be handled by their specific implementations.
  118. describe( 'in multi-element scenarios', () => {
  119. test(
  120. 'do not merge when no need to',
  121. '<paragraph>x</paragraph><paragraph>[foo]</paragraph><paragraph>y</paragraph>',
  122. '<paragraph>x</paragraph><paragraph>[]</paragraph><paragraph>y</paragraph>',
  123. { merge: true }
  124. );
  125. test(
  126. 'merges second element into the first one (same name)',
  127. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  128. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  129. { merge: true }
  130. );
  131. test(
  132. 'does not merge second element into the first one (same name, !option.merge)',
  133. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  134. '<paragraph>x</paragraph><paragraph>fo[]</paragraph><paragraph>ar</paragraph><paragraph>y</paragraph>'
  135. );
  136. test(
  137. 'merges second element into the first one (same name)',
  138. '<paragraph>x</paragraph><paragraph>fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  139. '<paragraph>x</paragraph><paragraph>fo[]ar</paragraph><paragraph>y</paragraph>',
  140. { merge: true }
  141. );
  142. test(
  143. 'merges second element into the first one (different name)',
  144. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  145. '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>',
  146. { merge: true }
  147. );
  148. it( 'merges second element into the first one (different name, backward selection)', () => {
  149. setData(
  150. doc,
  151. '<paragraph>x</paragraph><heading1>fo[o</heading1><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  152. { lastRangeBackward: true }
  153. );
  154. deleteContents( doc.batch(), doc.selection, { merge: true } );
  155. expect( getData( doc ) ).to.equal( '<paragraph>x</paragraph><heading1>fo[]ar</heading1><paragraph>y</paragraph>' );
  156. } );
  157. test(
  158. 'merges second element into the first one (different attrs)',
  159. '<paragraph>x</paragraph><paragraph align="l">fo[o</paragraph><paragraph>b]ar</paragraph><paragraph>y</paragraph>',
  160. '<paragraph>x</paragraph><paragraph align="l">fo[]ar</paragraph><paragraph>y</paragraph>',
  161. { merge: true }
  162. );
  163. test(
  164. 'merges second element to an empty first element',
  165. '<paragraph>x</paragraph><heading1>[</heading1><paragraph>fo]o</paragraph><paragraph>y</paragraph>',
  166. '<paragraph>x</paragraph><heading1>[]o</heading1><paragraph>y</paragraph>',
  167. { merge: true }
  168. );
  169. test(
  170. 'merges elements when deep nested',
  171. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph><pchild>b]ar</pchild>y</paragraph>',
  172. '<paragraph>x<pchild>fo[]ar</pchild>y</paragraph>',
  173. { merge: true }
  174. );
  175. // For code coverage reasons.
  176. test(
  177. 'merges element when selection is in two consecutive nodes even when it is empty',
  178. '<paragraph>foo[</paragraph><paragraph>]bar</paragraph>',
  179. '<paragraph>foo[]bar</paragraph>',
  180. { merge: true }
  181. );
  182. // If you disagree with this case please read the notes before this section.
  183. test(
  184. 'merges elements when left end deep nested',
  185. '<paragraph>x<pchild>fo[o</pchild></paragraph><paragraph>b]ary</paragraph>',
  186. '<paragraph>x<pchild>fo[]</pchild>ary</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 right end deep nested',
  192. '<paragraph>xfo[o</paragraph><paragraph><pchild>b]ar</pchild>y<image></image></paragraph>',
  193. '<paragraph>xfo[]<pchild>ar</pchild>y<image></image></paragraph>',
  194. { merge: true }
  195. );
  196. test(
  197. 'merges elements when more content in the right branch',
  198. '<paragraph>xfo[o</paragraph><paragraph>b]a<pchild>r</pchild>y</paragraph>',
  199. '<paragraph>xfo[]a<pchild>r</pchild>y</paragraph>',
  200. { merge: true }
  201. );
  202. test(
  203. 'leaves just one element when all selected',
  204. '<heading1>[x</heading1><paragraph>foo</paragraph><paragraph>y]</paragraph>',
  205. '<heading1>[]</heading1>',
  206. { merge: true }
  207. );
  208. } );
  209. function test( title, input, output, options ) {
  210. it( title, () => {
  211. setData( doc, input );
  212. deleteContents( doc.batch(), doc.selection, options );
  213. expect( getData( doc ) ).to.equal( output );
  214. } );
  215. }
  216. } );
  217. } );