deletecontents.js 7.1 KB

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