blockautoformatediting.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 Autoformat from '../src/autoformat';
  6. import BlockAutoformatEditing from '../src/blockautoformatediting';
  7. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  8. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  9. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  10. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. import Command from '@ckeditor/ckeditor5-core/src/command';
  13. describe( 'BlockAutoformatEditing', () => {
  14. let editor, model, doc;
  15. testUtils.createSinonSandbox();
  16. beforeEach( () => {
  17. return VirtualTestEditor
  18. .create( {
  19. plugins: [ Enter, Paragraph, Autoformat ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. model = editor.model;
  24. doc = model.document;
  25. } );
  26. } );
  27. it( 'should have pluginName', () => {
  28. expect( BlockAutoformatEditing.pluginName ).to.equal( 'BlockAutoformatEditing' );
  29. } );
  30. describe( 'command name', () => {
  31. it( 'should run a command when the pattern is matched', () => {
  32. const spy = testUtils.sinon.spy();
  33. const testCommand = new TestCommand( editor, spy );
  34. editor.commands.add( 'testCommand', testCommand );
  35. new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  36. setData( model, '<paragraph>*[]</paragraph>' );
  37. model.change( writer => {
  38. writer.insertText( ' ', doc.selection.getFirstPosition() );
  39. } );
  40. sinon.assert.calledOnce( spy );
  41. } );
  42. it( 'should remove found pattern', () => {
  43. const spy = testUtils.sinon.spy();
  44. const testCommand = new TestCommand( editor, spy );
  45. editor.commands.add( 'testCommand', testCommand );
  46. new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  47. setData( model, '<paragraph>*[]</paragraph>' );
  48. model.change( writer => {
  49. writer.insertText( ' ', doc.selection.getFirstPosition() );
  50. } );
  51. sinon.assert.calledOnce( spy );
  52. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  53. } );
  54. it( 'should not autoformat if command is disabled', () => {
  55. const spy = testUtils.sinon.spy();
  56. const testCommand = new TestCommand( editor, spy );
  57. testCommand.refresh = function() {
  58. this.isEnabled = false;
  59. };
  60. editor.commands.add( 'testCommand', testCommand );
  61. new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  62. setData( model, '<paragraph>*[]</paragraph>' );
  63. model.change( writer => {
  64. writer.insertText( ' ', doc.selection.getFirstPosition() );
  65. } );
  66. sinon.assert.notCalled( spy );
  67. } );
  68. } );
  69. describe( 'callback', () => {
  70. it( 'should run callback when the pattern is matched', () => {
  71. const spy = testUtils.sinon.spy();
  72. new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  73. setData( model, '<paragraph>*[]</paragraph>' );
  74. model.change( writer => {
  75. writer.insertText( ' ', doc.selection.getFirstPosition() );
  76. } );
  77. sinon.assert.calledOnce( spy );
  78. } );
  79. it( 'should ignore other delta operations', () => {
  80. const spy = testUtils.sinon.spy();
  81. new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  82. setData( model, '<paragraph>*[]</paragraph>' );
  83. model.change( writer => {
  84. writer.remove( doc.selection.getFirstRange() );
  85. } );
  86. sinon.assert.notCalled( spy );
  87. } );
  88. it( 'should stop if there is no text to run matching on', () => {
  89. const spy = testUtils.sinon.spy();
  90. new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  91. setData( model, '<paragraph>[]</paragraph>' );
  92. model.change( writer => {
  93. writer.insertText( ' ', doc.selection.getFirstPosition() );
  94. } );
  95. sinon.assert.notCalled( spy );
  96. } );
  97. it( 'should stop if callback returned false', () => {
  98. new BlockAutoformatEditing( editor, /^[*]\s$/, () => false ); // eslint-disable-line no-new
  99. setData( model, '<paragraph>*[]</paragraph>' );
  100. model.change( writer => {
  101. writer.insertText( ' ', doc.selection.getFirstPosition() );
  102. } );
  103. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  104. } );
  105. } );
  106. it( 'should ignore transparent batches', () => {
  107. const spy = testUtils.sinon.spy();
  108. new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  109. setData( model, '<paragraph>*[]</paragraph>' );
  110. model.enqueueChange( 'transparent', writer => {
  111. writer.insertText( ' ', doc.selection.getFirstPosition() );
  112. } );
  113. sinon.assert.notCalled( spy );
  114. } );
  115. } );
  116. /**
  117. * Dummy command to execute.
  118. */
  119. class TestCommand extends Command {
  120. /**
  121. * Creates an instance of the command.
  122. *
  123. * @param {module:core/editor/editor~Editor} editor Editor instance.
  124. * @param {Function} onExecuteCallback execute call hook
  125. */
  126. constructor( editor, onExecuteCallback ) {
  127. super( editor );
  128. this.onExecute = onExecuteCallback;
  129. }
  130. /**
  131. * Executes command.
  132. *
  133. * @protected
  134. */
  135. execute() {
  136. this.onExecute();
  137. }
  138. }