blockautoformatediting.js 5.0 KB

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