shiftentercommand.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /**
  2. * @license Copyright (c) 2003-2020, 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. describe( 'copyOnEnter', () => {
  85. beforeEach( () => {
  86. schema.extend( '$text', { allowAttributes: [ 'foo', 'bar' ] } );
  87. schema.setAttributeProperties( 'foo', { copyOnEnter: true } );
  88. } );
  89. test(
  90. 'allowed attributes are copied',
  91. '<p><$text foo="true">test[]</$text></p>',
  92. '<p><$text foo="true">test</$text><softBreak></softBreak><$text foo="true">[]</$text></p>'
  93. );
  94. test(
  95. 'unknown attributes are not copied',
  96. '<p><$text bar="true">test[]</$text></p>',
  97. '<p><$text bar="true">test</$text><softBreak></softBreak>[]</p>'
  98. );
  99. test(
  100. 'only allowed attributes are copied from mix set',
  101. '<p><$text bar="true" foo="true">test[]</$text></p>',
  102. '<p><$text bar="true" foo="true">test</$text><softBreak></softBreak><$text foo="true">[]</$text></p>'
  103. );
  104. } );
  105. } );
  106. describe( 'non-collapsed selection', () => {
  107. test(
  108. 'deletes the content and inserts the break when directly in the root',
  109. 'fo[ob]ar',
  110. 'fo<softBreak></softBreak>[]ar'
  111. );
  112. test(
  113. 'deletes text and adds break',
  114. '<p>ab[cd]ef</p><p>ghi</p>',
  115. '<p>ab<softBreak></softBreak>[]ef</p><p>ghi</p>'
  116. );
  117. test(
  118. 'places selection in the 2nd element',
  119. '<h>ab[c</h><p>d]ef</p><p>ghi</p>',
  120. '<h>ab</h><p>[]ef</p><p>ghi</p>'
  121. );
  122. test(
  123. 'does nothing for selection that contains more than one range',
  124. '<p>[abc]</p><p>[def]</p>',
  125. '<p>[abc]</p><p>[def]</p>'
  126. );
  127. test(
  128. 'inserts break in empty element after it was fully selected',
  129. '<p>x</p><p>[abcdef]</p><p>y</p>',
  130. '<p>x</p><p><softBreak></softBreak>[]</p><p>y</p>'
  131. );
  132. // See: comment in softBreakAction().
  133. test(
  134. 'leaves one empty element after two were fully selected',
  135. '<p>[abc</p><p>def]</p>',
  136. '<p>[]</p>'
  137. );
  138. test(
  139. 'should insert the break in inline limit element - collapsed',
  140. '<p><inlineLimit>foo[]bar</inlineLimit></p>',
  141. '<p><inlineLimit>foo<softBreak></softBreak>[]bar</inlineLimit></p>'
  142. );
  143. test(
  144. 'should insert the break in inline limit elements',
  145. '<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
  146. '<p><inlineLimit>foo<softBreak></softBreak>[]baz</inlineLimit></p>'
  147. );
  148. test(
  149. 'should insert the break at beginning of the inline limit elements',
  150. '<p><inlineLimit>foo[bar]baz</inlineLimit></p>',
  151. '<p><inlineLimit>foo<softBreak></softBreak>[]baz</inlineLimit></p>'
  152. );
  153. test(
  154. 'should insert the break at ending of the inline limit elements',
  155. '<p><inlineLimit>foobaz[]</inlineLimit></p>',
  156. '<p><inlineLimit>foobaz<softBreak></softBreak>[]</inlineLimit></p>'
  157. );
  158. it( 'should not break inline limit elements - selection partially inside', () => {
  159. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  160. model.change( () => {
  161. setData( model, '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>' );
  162. command.execute();
  163. expect( getData( model ) ).to.equal( '<p><inlineLimit>ba[r</inlineLimit></p><p>f]oo</p>' );
  164. } );
  165. } );
  166. test(
  167. 'should break paragraph in blockLimit',
  168. '<blockLimit><p>foo[]bar</p></blockLimit>',
  169. '<blockLimit><p>foo<softBreak></softBreak>[]bar</p></blockLimit>'
  170. );
  171. it( 'does nothing when break element cannot be inserted in specified context', () => {
  172. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  173. model.change( () => {
  174. setData( model, '<img>[]</img>' );
  175. command.execute();
  176. expect( getData( model ) ).to.equal( '<img>[]</img>' );
  177. } );
  178. } );
  179. it( 'leaves one empty element after two were fully selected (backward)', () => {
  180. setData( model, '<p>[abc</p><p>def]</p>' );
  181. // @TODO: Add option for setting selection direction to model utils.
  182. doc.selection._lastRangeBackward = true;
  183. command.execute();
  184. expect( getData( model ) ).to.equal( '<p>[]</p>' );
  185. } );
  186. it( 'uses DataController.deleteContent', () => {
  187. const spy = sinon.spy();
  188. editor.model.on( 'deleteContent', spy );
  189. setData( model, '<p>[x]</p>' );
  190. command.execute();
  191. expect( spy.calledOnce ).to.be.true;
  192. } );
  193. } );
  194. function test( title, input, output ) {
  195. it( title, () => {
  196. setData( model, input );
  197. command.execute();
  198. expect( getData( model ) ).to.equal( output );
  199. } );
  200. }
  201. } );
  202. describe( '#isEnabled', () => {
  203. it( 'should be disabled if <softBreak> cannot be inserted into element', () => {
  204. model.change( () => {
  205. setData( model, '<img>[]</img>' );
  206. expect( command.isEnabled ).to.equal( false );
  207. } );
  208. } );
  209. it( 'should be enabled for collapsed selection in $root', () => {
  210. setData( model, 'Foo.[]' );
  211. expect( command.isEnabled ).to.equal( true );
  212. } );
  213. it( 'should be enabled for collapsed selection in paragraph', () => {
  214. setData( model, '<p>Foo.[]</p>' );
  215. expect( command.isEnabled ).to.equal( true );
  216. } );
  217. it( 'should be enabled for collapsed selection in heading', () => {
  218. setData( model, '<h>Foo.[]</h>' );
  219. expect( command.isEnabled ).to.equal( true );
  220. } );
  221. it( 'should be enabled for collapsed selection in inline limit element', () => {
  222. setData( model, '<p><inlineLimit>Foo.[]</inlineLimit></p>' );
  223. expect( command.isEnabled ).to.equal( true );
  224. } );
  225. it( 'should be enabled for non-collapsed selection in inline limit element', () => {
  226. setData( model, '<p><inlineLimit>[Foo.]</inlineLimit></p>' );
  227. expect( command.isEnabled ).to.equal( true );
  228. } );
  229. it( 'should be enabled for collapsed selection in paragraph which is wrapped in a block limit element', () => {
  230. setData( model, '<blockLimit><p>Foo.[]</p></blockLimit>' );
  231. expect( command.isEnabled ).to.equal( true );
  232. } );
  233. it( 'should be enabled for non-collapsed selection in paragraph which is wrapped in a block limit element', () => {
  234. setData( model, '<blockLimit><p>F[oo.]</p></blockLimit>' );
  235. expect( command.isEnabled ).to.equal( true );
  236. } );
  237. it( 'should be enabled for non-collapsed selection in paragraphs', () => {
  238. setData( model, '<p>[Foo.</p><p>Bar.]</p>' );
  239. expect( command.isEnabled ).to.equal( true );
  240. } );
  241. it( 'should be enabled for non-collapsed selection in headings', () => {
  242. setData( model, '<h>[Foo.</h><h>Bar.]</h>' );
  243. expect( command.isEnabled ).to.equal( true );
  244. } );
  245. it( 'should be disabled for non-collapsed selection which starts in an inline limit element', () => {
  246. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  247. model.change( () => {
  248. setData( model, '<p><inlineLimit>F[oo.</inlineLimit>B]ar.</p>' );
  249. // Refresh it manually because we're in the middle of a change block.
  250. command.refresh();
  251. expect( command.isEnabled ).to.equal( false );
  252. } );
  253. } );
  254. it( 'should be disabled for non-collapsed selection which end in an inline limit element', () => {
  255. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  256. model.change( () => {
  257. setData( model, '<p>F[oo<inlineLimit>Bar].</inlineLimit></p>' );
  258. // Refresh it manually because we're in the middle of a change block.
  259. command.refresh();
  260. expect( command.isEnabled ).to.equal( false );
  261. } );
  262. } );
  263. it( 'should be disabled when break element cannot be inserted in specified context', () => {
  264. // Wrap all changes in one block to avoid post-fixing the selection (which is incorret) in the meantime.
  265. model.change( () => {
  266. setData( model, '<img>[]</img>' );
  267. // Refresh it manually because we're in the middle of a change block.
  268. command.refresh();
  269. expect( command.isEnabled ).to.equal( false );
  270. } );
  271. } );
  272. it( 'should be disabled for non-collapsed selection which starts in element inside a block limit element', () => {
  273. model.change( () => {
  274. setData( model, '<blockLimit><p>F[oo.</p></blockLimit><p>B]ar.</p>' );
  275. // Refresh it manually because we're in the middle of a change block.
  276. command.refresh();
  277. expect( command.isEnabled ).to.equal( false );
  278. } );
  279. } );
  280. it( 'should be disabled for non-collapsed selection which ends in element inside a block limit element', () => {
  281. model.change( () => {
  282. setData( model, '<p>Fo[o.</p><blockLimit><p>Bar].</p></blockLimit>' );
  283. // Refresh it manually because we're in the middle of a change block.
  284. command.refresh();
  285. expect( command.isEnabled ).to.equal( false );
  286. } );
  287. } );
  288. it( 'should be disabled for multi-ranges selection (1)', () => {
  289. setData( model, '<p>[x]</p><p>[foo]</p>' );
  290. expect( command.isEnabled ).to.equal( false );
  291. } );
  292. it( 'should be disabled for multi-ranges selection (2)', () => {
  293. setData( model, '<p>[]x</p><p>[]foo</p>' );
  294. expect( command.isEnabled ).to.equal( false );
  295. } );
  296. } );
  297. } );