shiftentercommand.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
  6. import InsertOperation from '@ckeditor/ckeditor5-engine/src/model/operation/insertoperation';
  7. import { getData, setData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  8. import ShiftEnter from '../src/shiftenter';
  9. describe( 'ShiftEnterCommand', () => {
  10. let editor, model, doc, schema, command;
  11. beforeEach( () => {
  12. return ModelTestEditor.create( { plugins: [ ShiftEnter ] } )
  13. .then( newEditor => {
  14. editor = newEditor;
  15. model = editor.model;
  16. doc = model.document;
  17. command = editor.commands.get( 'shiftEnter' );
  18. schema = model.schema;
  19. // Note: We could use real names like 'paragraph', but that would make test patterns too long.
  20. // Plus, this is actually a good test that the algorithm can be used for any model.
  21. schema.register( 'img', { allowWhere: '$block' } );
  22. schema.register( 'p', {
  23. inheritAllFrom: '$block',
  24. allowIn: 'blockLimit'
  25. } );
  26. schema.register( 'h', { inheritAllFrom: '$block' } );
  27. schema.register( 'inlineLimit', {
  28. allowIn: 'p',
  29. isLimit: true
  30. } );
  31. schema.register( 'blockLimit', {
  32. allowIn: '$root',
  33. isLimit: true
  34. } );
  35. schema.extend( '$text', {
  36. allowIn: [ 'inlineLimit', '$root' ]
  37. } );
  38. } );
  39. } );
  40. describe( 'ShiftEnterCommand', () => {
  41. it( 'soft breaks a block using parent batch', () => {
  42. setData( model, '<p>foo[]</p>' );
  43. model.change( writer => {
  44. expect( writer.batch.operations ).to.length( 0 );
  45. editor.execute( 'shiftEnter' );
  46. expect( writer.batch.operations ).to.length.above( 0 );
  47. } );
  48. } );
  49. it( 'creates InsertOperation if soft enter is at the beginning of block', () => {
  50. setData( model, '<p>[]foo</p>' );
  51. editor.execute( 'shiftEnter' );
  52. const operations = Array.from( doc.history.getOperations() );
  53. expect( operations[ operations.length - 1 ] ).to.be.instanceof( InsertOperation );
  54. } );
  55. it( 'creates InsertOperation if soft enter is at the end of block', () => {
  56. setData( model, '<p>foo[]</p>' );
  57. editor.execute( 'shiftEnter' );
  58. const operations = Array.from( doc.history.getOperations() );
  59. expect( operations[ operations.length - 1 ] ).to.be.instanceof( InsertOperation );
  60. } );
  61. } );
  62. describe( 'execute()', () => {
  63. describe( 'collapsed selection', () => {
  64. test(
  65. 'inserts in the root',
  66. 'foo[]bar',
  67. 'foo<softBreak></softBreak>[]bar'
  68. );
  69. test(
  70. 'inserts inside block',
  71. '<p>x</p><p>foo[]bar</p><p>y</p>',
  72. '<p>x</p><p>foo<softBreak></softBreak>[]bar</p><p>y</p>'
  73. );
  74. test(
  75. 'inserts at the end of block',
  76. '<p>x</p><p>foo[]</p><p>y</p>',
  77. '<p>x</p><p>foo<softBreak></softBreak>[]</p><p>y</p>'
  78. );
  79. test(
  80. 'inserts at the beginning of block',
  81. '<p>x</p><p>[]foo</p><p>y</p>',
  82. '<p>x</p><p><softBreak></softBreak>[]foo</p><p>y</p>'
  83. );
  84. } );
  85. describe( 'non-collapsed selection', () => {
  86. test(
  87. 'deletes the content and inserts the break when directly in the root',
  88. 'fo[ob]ar',
  89. 'fo<softBreak></softBreak>[]ar'
  90. );
  91. test(
  92. 'deletes text and adds break',
  93. '<p>ab[cd]ef</p><p>ghi</p>',
  94. '<p>ab<softBreak></softBreak>[]ef</p><p>ghi</p>'
  95. );
  96. test(
  97. 'places selection in the 2nd element',
  98. '<h>ab[c</h><p>d]ef</p><p>ghi</p>',
  99. '<h>ab</h><p>[]ef</p><p>ghi</p>'
  100. );
  101. test(
  102. 'does nothing for selection that contains more than one range',
  103. '<p>[abc]</p><p>[def]</p>',
  104. '<p>[abc]</p><p>[def]</p>'
  105. );
  106. test(
  107. 'inserts break in empty element after it was fully selected',
  108. '<p>x</p><p>[abcdef]</p><p>y</p>',
  109. '<p>x</p><p><softBreak></softBreak>[]</p><p>y</p>'
  110. );
  111. // See: comment in softBreakAction().
  112. test(
  113. 'leaves one empty element after two were fully selected',
  114. '<p>[abc</p><p>def]</p>',
  115. '<p>[]</p>'
  116. );
  117. test(
  118. 'should insert the break in inline limit element - collapsed',
  119. '<p><inlineLimit>foo[]bar</inlineLimit></p>',
  120. '<p><inlineLimit>foo<softBreak></softBreak>[]bar</inlineLimit></p>'
  121. );
  122. test(
  123. 'should insert the break in inline limit elements',
  124. '<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
  125. '<p><inlineLimit>foo<softBreak></softBreak>[]baz</inlineLimit></p>'
  126. );
  127. test(
  128. 'should insert the break at beginning of the inline limit elements',
  129. '<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
  130. '<p><inlineLimit>foo<softBreak></softBreak>[]baz</inlineLimit></p>'
  131. );
  132. test(
  133. 'should insert the break at ending of the inline limit elements',
  134. '<p><inlineLimit>foobaz[]</inlineLimit></p>',
  135. '<p><inlineLimit>foobaz<softBreak></softBreak>[]</inlineLimit></p>'
  136. );
  137. it( 'should not break inline limit elements - selection partially inside', () => {
  138. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  139. model.change( () => {
  140. setData( model, '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>' );
  141. command.execute();
  142. expect( getData( model ) ).to.equal( '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>' );
  143. } );
  144. } );
  145. test(
  146. 'should break paragraph in blockLimit',
  147. '<blockLimit><p>foo[]bar</p></blockLimit>',
  148. '<blockLimit><p>foo<softBreak></softBreak>[]bar</p></blockLimit>'
  149. );
  150. it( 'does nothing when break element cannot be inserted in specified context', () => {
  151. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  152. model.change( () => {
  153. setData( model, '<img>[]</img>' );
  154. command.execute();
  155. expect( getData( model ) ).to.equal( '<img>[]</img>' );
  156. } );
  157. } );
  158. it( 'leaves one empty element after two were fully selected (backward)', () => {
  159. setData( model, '<p>[abc</p><p>def]</p>' );
  160. // @TODO: Add option for setting selection direction to model utils.
  161. doc.selection._lastRangeBackward = true;
  162. command.execute();
  163. expect( getData( model ) ).to.equal( '<p>[]</p>' );
  164. } );
  165. it( 'uses DataController.deleteContent', () => {
  166. const spy = sinon.spy();
  167. editor.model.on( 'deleteContent', spy );
  168. setData( model, '<p>[x]</p>' );
  169. command.execute();
  170. expect( spy.calledOnce ).to.be.true;
  171. } );
  172. } );
  173. function test( title, input, output ) {
  174. it( title, () => {
  175. setData( model, input );
  176. command.execute();
  177. expect( getData( model ) ).to.equal( output );
  178. } );
  179. }
  180. } );
  181. describe( '#isEnabled', () => {
  182. it( 'should be disabled if <softBreak> cannot be inserted into element', () => {
  183. model.change( () => {
  184. setData( model, '<img>[]</img>' );
  185. expect( command.isEnabled ).to.equal( false );
  186. } );
  187. } );
  188. it( 'should be enabled for collapsed selection in $root', () => {
  189. setData( model, 'Foo.[]' );
  190. expect( command.isEnabled ).to.equal( true );
  191. } );
  192. it( 'should be enabled for collapsed selection in paragraph', () => {
  193. setData( model, '<p>Foo.[]</p>' );
  194. expect( command.isEnabled ).to.equal( true );
  195. } );
  196. it( 'should be enabled for collapsed selection in heading', () => {
  197. setData( model, '<h>Foo.[]</h>' );
  198. expect( command.isEnabled ).to.equal( true );
  199. } );
  200. it( 'should be enabled for collapsed selection in inline limit element', () => {
  201. setData( model, '<p><inlineLimit>Foo.[]</inlineLimit></p>' );
  202. expect( command.isEnabled ).to.equal( true );
  203. } );
  204. it( 'should be enabled for non-collapsed selection in inline limit element', () => {
  205. setData( model, '<p><inlineLimit>[Foo.]</inlineLimit></p>' );
  206. expect( command.isEnabled ).to.equal( true );
  207. } );
  208. it( 'should be enabled for collapsed selection in paragraph which is wrapped in a block limit element', () => {
  209. setData( model, '<blockLimit><p>Foo.[]</p></blockLimit>' );
  210. expect( command.isEnabled ).to.equal( true );
  211. } );
  212. it( 'should be enabled for non-collapsed selection in paragraph which is wrapped in a block limit element', () => {
  213. setData( model, '<blockLimit><p>F[oo.]</p></blockLimit>' );
  214. expect( command.isEnabled ).to.equal( true );
  215. } );
  216. it( 'should be enabled for non-collapsed selection in paragraphs', () => {
  217. setData( model, '<p>[Foo.</p><p>Bar.]</p>' );
  218. expect( command.isEnabled ).to.equal( true );
  219. } );
  220. it( 'should be enabled for non-collapsed selection in headings', () => {
  221. setData( model, '<h>[Foo.</h><h>Bar.]</h>' );
  222. expect( command.isEnabled ).to.equal( true );
  223. } );
  224. it( 'should be disabled for non-collapsed selection which starts in an inline limit element', () => {
  225. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  226. model.change( () => {
  227. setData( model, '<p><inlineLimit>F[oo.</inlineLimit>B]ar.</p>' );
  228. // Refresh it manually because we're in the middle of a change block.
  229. command.refresh();
  230. expect( command.isEnabled ).to.equal( false );
  231. } );
  232. } );
  233. it( 'should be disabled for non-collapsed selection which end in an inline limit element', () => {
  234. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  235. model.change( () => {
  236. setData( model, '<p>F[oo<inlineLimit>Bar].</inlineLimit></p>' );
  237. // Refresh it manually because we're in the middle of a change block.
  238. command.refresh();
  239. expect( command.isEnabled ).to.equal( false );
  240. } );
  241. } );
  242. it( 'should be disabled when break element cannot be inserted in specified context', () => {
  243. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  244. model.change( () => {
  245. setData( model, '<img>[]</img>' );
  246. // Refresh it manually because we're in the middle of a change block.
  247. command.refresh();
  248. expect( command.isEnabled ).to.equal( false );
  249. } );
  250. } );
  251. it( 'should be disabled for non-collapsed selection which starts in element inside a block limit element', () => {
  252. model.change( () => {
  253. setData( model, '<blockLimit><p>F[oo.</p></blockLimit><p>B]ar.</p>' );
  254. // Refresh it manually because we're in the middle of a change block.
  255. command.refresh();
  256. expect( command.isEnabled ).to.equal( false );
  257. } );
  258. } );
  259. it( 'should be disabled for non-collapsed selection which ends in element inside a block limit element', () => {
  260. model.change( () => {
  261. setData( model, '<p>Fo[o.</p><blockLimit><p>Bar].</p></blockLimit>' );
  262. // Refresh it manually because we're in the middle of a change block.
  263. command.refresh();
  264. expect( command.isEnabled ).to.equal( false );
  265. } );
  266. } );
  267. it( 'should be disabled for multi-ranges selection (1)', () => {
  268. setData( model, '<p>[x]</p><p>[foo]</p>' );
  269. expect( command.isEnabled ).to.equal( false );
  270. } );
  271. it( 'should be disabled for multi-ranges selection (2)', () => {
  272. setData( model, '<p>[]x</p><p>[]foo</p>' );
  273. expect( command.isEnabled ).to.equal( false );
  274. } );
  275. } );
  276. } );