getselectedcontent.js 13 KB

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