8
0

deletecontents.js 7.1 KB

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