blockautoformatediting.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import BlockAutoformatEditing from '../src/blockautoformatediting';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  9. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. import Command from '@ckeditor/ckeditor5-core/src/command';
  12. testUtils.createSinonSandbox();
  13. describe( 'BlockAutoformatEditing', () => {
  14. let editor, model, doc;
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ Enter, Paragraph ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. doc = model.document;
  24. } );
  25. } );
  26. describe( 'Command name', () => {
  27. it( 'should run a command when the pattern is matched', () => {
  28. const spy = testUtils.sinon.spy();
  29. editor.commands.add( 'testCommand', new TestCommand( editor, spy ) );
  30. new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  31. setData( model, '<paragraph>*[]</paragraph>' );
  32. model.change( writer => {
  33. writer.insertText( ' ', doc.selection.getFirstPosition() );
  34. } );
  35. sinon.assert.calledOnce( spy );
  36. } );
  37. it( 'should remove found pattern', () => {
  38. const spy = testUtils.sinon.spy();
  39. editor.commands.add( 'testCommand', new TestCommand( editor, spy ) );
  40. new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  41. setData( model, '<paragraph>*[]</paragraph>' );
  42. model.change( writer => {
  43. writer.insertText( ' ', doc.selection.getFirstPosition() );
  44. } );
  45. sinon.assert.calledOnce( spy );
  46. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  47. } );
  48. } );
  49. describe( 'Callback', () => {
  50. it( 'should run callback when the pattern is matched', () => {
  51. const spy = testUtils.sinon.spy();
  52. new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  53. setData( model, '<paragraph>*[]</paragraph>' );
  54. model.change( writer => {
  55. writer.insertText( ' ', doc.selection.getFirstPosition() );
  56. } );
  57. sinon.assert.calledOnce( spy );
  58. } );
  59. it( 'should ignore other delta operations', () => {
  60. const spy = testUtils.sinon.spy();
  61. new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  62. setData( model, '<paragraph>*[]</paragraph>' );
  63. model.change( writer => {
  64. writer.remove( doc.selection.getFirstRange() );
  65. } );
  66. sinon.assert.notCalled( spy );
  67. } );
  68. it( 'should stop if there is no text to run matching on', () => {
  69. const spy = testUtils.sinon.spy();
  70. new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  71. setData( model, '<paragraph>[]</paragraph>' );
  72. model.change( writer => {
  73. writer.insertText( ' ', doc.selection.getFirstPosition() );
  74. } );
  75. sinon.assert.notCalled( spy );
  76. } );
  77. } );
  78. it( 'should ignore transparent batches', () => {
  79. const spy = testUtils.sinon.spy();
  80. new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  81. setData( model, '<paragraph>*[]</paragraph>' );
  82. model.enqueueChange( 'transparent', writer => {
  83. writer.insertText( ' ', doc.selection.getFirstPosition() );
  84. } );
  85. sinon.assert.notCalled( spy );
  86. } );
  87. } );
  88. /**
  89. * Dummy command to execute.
  90. */
  91. class TestCommand extends Command {
  92. /**
  93. * Creates an instance of the command.
  94. *
  95. * @param {module:core/editor/editor~Editor} editor Editor instance.
  96. * @param {Function} onExecuteCallback execute call hook
  97. */
  98. constructor( editor, onExecuteCallback ) {
  99. super( editor );
  100. this.onExecute = onExecuteCallback;
  101. }
  102. /**
  103. * Executes command.
  104. *
  105. * @protected
  106. */
  107. execute() {
  108. this.onExecute();
  109. }
  110. }