getselectedcontent.js 13 KB

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