blockautoformatediting.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  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. } );
  95. it( 'should ignore transparent batches', () => {
  96. const spy = testUtils.sinon.spy();
  97. new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  98. setData( model, '<paragraph>*[]</paragraph>' );
  99. model.enqueueChange( 'transparent', writer => {
  100. writer.insertText( ' ', doc.selection.getFirstPosition() );
  101. } );
  102. sinon.assert.notCalled( spy );
  103. } );
  104. } );
  105. /**
  106. * Dummy command to execute.
  107. */
  108. class TestCommand extends Command {
  109. /**
  110. * Creates an instance of the command.
  111. *
  112. * @param {module:core/editor/editor~Editor} editor Editor instance.
  113. * @param {Function} onExecuteCallback execute call hook
  114. */
  115. constructor( editor, onExecuteCallback ) {
  116. super( editor );
  117. this.onExecute = onExecuteCallback;
  118. }
  119. /**
  120. * Executes command.
  121. *
  122. * @protected
  123. */
  124. execute() {
  125. this.onExecute();
  126. }
  127. }