entercommand.js 3.4 KB

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