entercommand.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import ModelTestEditor from 'tests/core/_utils/modeltesteditor.js';
  6. import EnterCommand from 'ckeditor5/enter/entercommand.js';
  7. import { getData, setData } from 'ckeditor5/engine/dev-utils/model.js';
  8. let editor, doc, schema, command;
  9. beforeEach( () => {
  10. return ModelTestEditor.create()
  11. .then( newEditor => {
  12. editor = newEditor;
  13. doc = editor.document;
  14. command = new EnterCommand( editor );
  15. editor.commands.set( 'enter', command );
  16. schema = doc.schema;
  17. // Note: We could use real names like 'paragraph', but that would make test patterns too long.
  18. // Plus, this is actually a good test that the algorithm can be used for any model.
  19. schema.registerItem( 'img', '$inline' );
  20. schema.registerItem( 'p', '$block' );
  21. schema.registerItem( 'h', '$block' );
  22. schema.allow( { name: '$text', inside: '$root' } );
  23. } );
  24. } );
  25. describe( 'EnterCommand', () => {
  26. it( 'enters a block using enqueueChanges', () => {
  27. setData( doc, '<p>foo[]</p>' );
  28. const spy = sinon.spy( doc, 'enqueueChanges' );
  29. editor.execute( 'enter' );
  30. expect( getData( doc, { withoutSelection: true } ) ).to.equal( '<p>foo</p><p></p>' );
  31. expect( spy.calledOnce ).to.be.true;
  32. } );
  33. } );
  34. describe( '_doExecute', () => {
  35. describe( 'collapsed selection', () => {
  36. test(
  37. 'does nothing in the root',
  38. 'foo[]bar',
  39. 'foo[]bar'
  40. );
  41. test(
  42. 'splits block',
  43. '<p>x</p><p>foo[]bar</p><p>y</p>',
  44. '<p>x</p><p>foo</p><p>[]bar</p><p>y</p>'
  45. );
  46. test(
  47. 'splits block at the end',
  48. '<p>x</p><p>foo[]</p><p>y</p>',
  49. '<p>x</p><p>foo</p><p>[]</p><p>y</p>'
  50. );
  51. test(
  52. 'splits block at the beginning',
  53. '<p>x</p><p>[]foo</p><p>y</p>',
  54. '<p>x</p><p></p><p>[]foo</p><p>y</p>'
  55. );
  56. test(
  57. 'inserts new block after empty one',
  58. '<p>x</p><p>[]</p><p>y</p>',
  59. '<p>x</p><p></p><p>[]</p><p>y</p>'
  60. );
  61. } );
  62. describe( 'non-collapsed selection', () => {
  63. test(
  64. 'only deletes the content when directly in the root',
  65. 'fo[ob]ar',
  66. 'fo[]ar'
  67. );
  68. test(
  69. 'deletes text and splits',
  70. '<p>ab[cd]ef</p><p>ghi</p>',
  71. '<p>ab</p><p>[]ef</p><p>ghi</p>'
  72. );
  73. test(
  74. 'places selection in the 2nd element',
  75. '<h>ab[c</h><p>d]ef</p><p>ghi</p>',
  76. '<h>ab</h><p>[]ef</p><p>ghi</p>'
  77. );
  78. test(
  79. 'leaves one empty element after one was fully selected',
  80. '<p>x</p><p>[abcdef]</p><p>y</p>',
  81. '<p>x</p><p>[]</p><p>y</p>'
  82. );
  83. test(
  84. 'leaves one empty element after two were fully selected',
  85. '<p>[abc</p><p>def]</p>',
  86. '<p>[]</p>'
  87. );
  88. it( 'leaves one empty element after two were fully selected (backward)', () => {
  89. setData( doc, '<p>[abc</p><p>def]</p>' );
  90. // @TODO: Add option for setting selection direction to model utils.
  91. doc.selection._lastRangeBackward = true;
  92. command._doExecute();
  93. expect( getData( doc ) ).to.equal( '<p>[]</p>' );
  94. } );
  95. it( 'uses composer.deleteContents', () => {
  96. const spy = sinon.spy();
  97. doc.composer.on( 'deleteContents', spy );
  98. setData( doc, '<p>[x]</p>' );
  99. command._doExecute();
  100. expect( spy.calledOnce ).to.be.true;
  101. } );
  102. } );
  103. function test( title, input, output ) {
  104. it( title, () => {
  105. setData( doc, input );
  106. command._doExecute();
  107. expect( getData( doc ) ).to.equal( output );
  108. } );
  109. }
  110. } );