getselectedcontent.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import Document from '../../src/model/document';
  6. import DocumentFragment from '../../src/model/documentfragment';
  7. import getSelectedContent from '../../src/controller/getselectedcontent';
  8. import { setData, stringify } from '../../src/dev-utils/model';
  9. describe( 'Delete utils', () => {
  10. let doc;
  11. describe( 'getSelectedContent', () => {
  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. schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
  21. schema.allow( { name: '$inline', attributes: [ 'italic' ] } );
  22. } );
  23. it( 'returns empty fragment for no selection', () => {
  24. setData( doc, 'abc' );
  25. const frag = getSelectedContent( doc.selection );
  26. expect( frag ).instanceOf( DocumentFragment );
  27. expect( frag.isEmpty ).to.be.true;
  28. } );
  29. it( 'returns empty fragment for empty selection', () => {
  30. setData( doc, 'a[]bc' );
  31. const frag = getSelectedContent( doc.selection );
  32. expect( frag ).instanceOf( DocumentFragment );
  33. expect( frag.isEmpty ).to.be.true;
  34. } );
  35. it( 'gets one character', () => {
  36. setData( doc, 'a[b]c' );
  37. const frag = getSelectedContent( doc.selection );
  38. const content = stringify( frag );
  39. expect( frag ).instanceOf( DocumentFragment );
  40. expect( content ).to.equal( 'b' );
  41. } );
  42. it( 'gets full text', () => {
  43. setData( doc, '[abc]' );
  44. const content = stringify( getSelectedContent( doc.selection ) );
  45. expect( content ).to.equal( 'abc' );
  46. } );
  47. it( 'gets text with an attribute', () => {
  48. setData( doc, 'xxx<$text bold="true">a[b]c</$text>' );
  49. const content = stringify( getSelectedContent( doc.selection ) );
  50. expect( content ).to.equal( '<$text bold="true">b</$text>' );
  51. } );
  52. it( 'gets text with attributes', () => {
  53. setData( doc, 'x<$text bold="true">a[b</$text><$text italic="true">c]d</$text>x' );
  54. const content = stringify( getSelectedContent( doc.selection ) );
  55. expect( content ).to.equal( '<$text bold="true">b</$text><$text italic="true">c</$text>' );
  56. } );
  57. it( 'gets text with and without attribute', () => {
  58. setData( doc, '<$text bold="true">a[b</$text>c]d' );
  59. const content = stringify( getSelectedContent( doc.selection ) );
  60. expect( content ).to.equal( '<$text bold="true">b</$text>c' );
  61. } );
  62. it( 'gets text and element', () => {
  63. setData( doc, '[ab<image></image>c]' );
  64. const content = stringify( getSelectedContent( doc.selection ) );
  65. expect( content ).to.equal( 'ab<image></image>c' );
  66. } );
  67. it( 'gets one element', () => {
  68. setData( doc, 'a[<image></image>]b' );
  69. const content = stringify( getSelectedContent( doc.selection ) );
  70. expect( content ).to.equal( '<image></image>' );
  71. } );
  72. it( 'gets multiple elements', () => {
  73. setData( doc, '[<image></image><image></image>]' );
  74. const content = stringify( getSelectedContent( doc.selection ) );
  75. expect( content ).to.equal( '<image></image><image></image>' );
  76. } );
  77. } );
  78. describe( 'in blocks', () => {
  79. beforeEach( () => {
  80. doc = new Document();
  81. doc.createRoot();
  82. const schema = doc.schema;
  83. schema.registerItem( 'paragraph', '$block' );
  84. schema.registerItem( 'heading1', '$block' );
  85. schema.registerItem( 'blockImage' );
  86. schema.registerItem( 'caption' );
  87. schema.registerItem( 'image', '$inline' );
  88. schema.allow( { name: 'blockImage', inside: '$root' } );
  89. schema.allow( { name: 'caption', inside: 'blockImage' } );
  90. schema.allow( { name: '$inline', inside: 'caption' } );
  91. schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
  92. } );
  93. it( 'gets one character', () => {
  94. setData( doc, '<paragraph>a[b]c</paragraph>' );
  95. const content = stringify( getSelectedContent( doc.selection ) );
  96. expect( content ).to.equal( 'b' );
  97. } );
  98. it( 'gets entire paragraph content', () => {
  99. setData( doc, '<paragraph>[a<image></image>b]</paragraph>' );
  100. const content = stringify( getSelectedContent( doc.selection ) );
  101. expect( content ).to.equal( 'a<image></image>b' );
  102. } );
  103. it( 'gets two blocks - partial, partial', () => {
  104. setData( doc, '<heading1>a[bc</heading1><paragraph>de]f</paragraph>' );
  105. const content = stringify( getSelectedContent( doc.selection ) );
  106. expect( content ).to.equal( '<heading1>bc</heading1><paragraph>de</paragraph>' );
  107. } );
  108. it( 'gets two blocks - full, partial', () => {
  109. setData( doc, '<heading1>[abc</heading1><paragraph>de]f</paragraph>' );
  110. const content = stringify( getSelectedContent( doc.selection ) );
  111. expect( content ).to.equal( '<heading1>abc</heading1><paragraph>de</paragraph>' );
  112. } );
  113. it( 'gets two blocks - full, partial 2', () => {
  114. setData( doc, '<heading1>[abc</heading1><paragraph>de<image></image>]f</paragraph>' );
  115. const content = stringify( getSelectedContent( doc.selection ) );
  116. expect( content ).to.equal( '<heading1>abc</heading1><paragraph>de<image></image></paragraph>' );
  117. } );
  118. it( 'gets two blocks - full, partial 3', () => {
  119. setData( doc, '<heading1>x</heading1><heading1>[abc</heading1><paragraph><image></image>de]f</paragraph>' );
  120. const content = stringify( getSelectedContent( doc.selection ) );
  121. expect( content ).to.equal( '<heading1>abc</heading1><paragraph><image></image>de</paragraph>' );
  122. } );
  123. it( 'gets two blocks - full, partial 4', () => {
  124. setData( doc, '<heading1>[abc</heading1><paragraph>de]f<image></image></paragraph>' );
  125. const content = stringify( getSelectedContent( doc.selection ) );
  126. expect( content ).to.equal( '<heading1>abc</heading1><paragraph>de</paragraph>' );
  127. } );
  128. it( 'gets two blocks - partial, full', () => {
  129. setData( doc, '<heading1>a[bc</heading1><paragraph>def]</paragraph>' );
  130. const content = stringify( getSelectedContent( doc.selection ) );
  131. expect( content ).to.equal( '<heading1>bc</heading1><paragraph>def</paragraph>' );
  132. } );
  133. it( 'gets two blocks - partial, full 2', () => {
  134. setData( doc, '<heading1>a[<image></image>bc</heading1><paragraph>def]</paragraph>' );
  135. const content = stringify( getSelectedContent( doc.selection ) );
  136. expect( content ).to.equal( '<heading1><image></image>bc</heading1><paragraph>def</paragraph>' );
  137. } );
  138. // See https://github.com/ckeditor/ckeditor5-engine/issues/652#issuecomment-261358484
  139. it( 'gets two blocks - empty, full', () => {
  140. setData( doc, '<heading1>abc[</heading1><paragraph>def]</paragraph>' );
  141. const content = stringify( getSelectedContent( doc.selection ) );
  142. expect( content ).to.equal( '<paragraph>def</paragraph>' );
  143. } );
  144. // See https://github.com/ckeditor/ckeditor5-engine/issues/652#issuecomment-261358484
  145. it( 'gets two blocks - partial, empty', () => {
  146. setData( doc, '<heading1>a[bc</heading1><paragraph>]def</paragraph>' );
  147. const content = stringify( getSelectedContent( doc.selection ) );
  148. expect( content ).to.equal( '<heading1>bc</heading1>' );
  149. } );
  150. it( 'gets three blocks', () => {
  151. setData( doc, '<heading1>a[bc</heading1><paragraph>x</paragraph><paragraph>de]f</paragraph>' );
  152. const content = stringify( getSelectedContent( doc.selection ) );
  153. expect( content ).to.equal( '<heading1>bc</heading1><paragraph>x</paragraph><paragraph>de</paragraph>' );
  154. } );
  155. it( 'gets block image', () => {
  156. setData( doc, '<paragraph>a</paragraph>[<blockImage><caption>Foo</caption></blockImage>]<paragraph>b</paragraph>' );
  157. const content = stringify( getSelectedContent( doc.selection ) );
  158. expect( content ).to.equal( '<blockImage><caption>Foo</caption></blockImage>' );
  159. } );
  160. it( 'gets two blocks', () => {
  161. setData( doc, '<paragraph>a</paragraph>[<blockImage></blockImage><blockImage></blockImage>]<paragraph>b</paragraph>' );
  162. const content = stringify( getSelectedContent( doc.selection ) );
  163. expect( content ).to.equal( '<blockImage></blockImage><blockImage></blockImage>' );
  164. } );
  165. // Purely related to the current implementation.
  166. it( 'gets content when multiple text items needs to be removed from the right excess', () => {
  167. setData( doc, '<paragraph>a[b</paragraph><paragraph>c]d<$text bold="true">e</$text>f</paragraph>' );
  168. const content = stringify( getSelectedContent( doc.selection ) );
  169. expect( content )
  170. .to.equal( '<paragraph>b</paragraph><paragraph>c</paragraph>' );
  171. } );
  172. // Purely related to the current implementation.
  173. it( 'gets content when multiple text items needs to be removed from the left excess', () => {
  174. setData( doc, '<paragraph>a<$text bold="true">b</$text>c[d</paragraph><paragraph>e]f</paragraph>' );
  175. const content = stringify( getSelectedContent( doc.selection ) );
  176. expect( content )
  177. .to.equal( '<paragraph>d</paragraph><paragraph>e</paragraph>' );
  178. } );
  179. } );
  180. describe( 'in blocks (deeply nested)', () => {
  181. beforeEach( () => {
  182. doc = new Document();
  183. doc.createRoot();
  184. const schema = doc.schema;
  185. schema.registerItem( 'paragraph', '$block' );
  186. schema.registerItem( 'heading1', '$block' );
  187. schema.registerItem( 'quote' );
  188. schema.allow( { name: '$block', inside: 'quote' } );
  189. schema.allow( { name: 'quote', inside: '$root' } );
  190. } );
  191. it( 'gets content when ends are equally deeply nested', () => {
  192. setData( doc, '<heading1>x</heading1><quote><paragraph>a[bc</paragraph><paragraph>de]f</paragraph></quote>' );
  193. const content = stringify( getSelectedContent( doc.selection ) );
  194. expect( content ).to.equal( '<paragraph>bc</paragraph><paragraph>de</paragraph>' );
  195. } );
  196. it( 'gets content when left end nested deeper', () => {
  197. setData( doc, '<quote><paragraph>a[bc</paragraph></quote><paragraph>de]f</paragraph>' );
  198. const content = stringify( getSelectedContent( doc.selection ) );
  199. expect( content ).to.equal( '<quote><paragraph>bc</paragraph></quote><paragraph>de</paragraph>' );
  200. } );
  201. it( 'gets content when left end nested deeper 2', () => {
  202. setData( doc, '<quote><paragraph>a[bc</paragraph><heading1>x</heading1></quote><paragraph>de]f</paragraph>' );
  203. const content = stringify( getSelectedContent( doc.selection ) );
  204. expect( content ).to.equal( '<quote><paragraph>bc</paragraph><heading1>x</heading1></quote><paragraph>de</paragraph>' );
  205. } );
  206. it( 'gets content when left end nested deeper 3', () => {
  207. setData( doc, '<quote><heading1>x</heading1><paragraph>a[bc</paragraph></quote><paragraph>de]f</paragraph>' );
  208. const content = stringify( getSelectedContent( doc.selection ) );
  209. expect( content ).to.equal( '<quote><paragraph>bc</paragraph></quote><paragraph>de</paragraph>' );
  210. } );
  211. // See https://github.com/ckeditor/ckeditor5-engine/issues/652#issuecomment-261358484
  212. it( 'gets content when left end nested deeper 4', () => {
  213. setData( doc, '<quote><heading1>x[</heading1><paragraph>abc</paragraph></quote><paragraph>de]f</paragraph>' );
  214. const content = stringify( getSelectedContent( doc.selection ) );
  215. expect( content ).to.equal( '<quote><paragraph>abc</paragraph></quote><paragraph>de</paragraph>' );
  216. } );
  217. it( 'gets content when right end nested deeper', () => {
  218. setData( doc, '<paragraph>a[bc</paragraph><quote><paragraph>de]f</paragraph></quote>' );
  219. const content = stringify( getSelectedContent( doc.selection ) );
  220. expect( content ).to.equal( '<paragraph>bc</paragraph><quote><paragraph>de</paragraph></quote>' );
  221. } );
  222. it( 'gets content when both ends nested deeper than the middle element', () => {
  223. setData( doc, '<quote><heading1>a[bc</heading1></quote><heading1>x</heading1><quote><heading1>de]f</heading1></quote>' );
  224. const content = stringify( getSelectedContent( doc.selection ) );
  225. expect( content )
  226. .to.equal( '<quote><heading1>bc</heading1></quote><heading1>x</heading1><quote><heading1>de</heading1></quote>' );
  227. } );
  228. } );
  229. } );
  230. } );