blockautoformatengine.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import BlockAutoformatEngine from '../src/blockautoformatengine';
  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( 'BlockAutoformatEngine', () => {
  14. let editor, doc, batch;
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ Enter, Paragraph ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. doc = editor.document;
  23. batch = doc.batch();
  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 BlockAutoformatEngine( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  31. setData( doc, '<paragraph>*[]</paragraph>' );
  32. doc.enqueueChanges( () => {
  33. batch.insertText( ' ', doc.selection.getFirstPosition() );
  34. } );
  35. sinon.assert.calledOnce( spy );
  36. } );
  37. it( 'should not run a command when changes are in transparent batch', () => {
  38. const spy = testUtils.sinon.spy();
  39. editor.commands.add( 'testCommand', new TestCommand( editor, spy ) );
  40. new BlockAutoformatEngine( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  41. setData( doc, '<paragraph>*[]</paragraph>' );
  42. doc.enqueueChanges( () => {
  43. doc.batch( 'transparent' ).insertText( ' ', doc.selection.getFirstPosition() );
  44. } );
  45. sinon.assert.notCalled( spy );
  46. } );
  47. it( 'should remove found pattern', () => {
  48. const spy = testUtils.sinon.spy();
  49. editor.commands.add( 'testCommand', new TestCommand( editor, spy ) );
  50. new BlockAutoformatEngine( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  51. setData( doc, '<paragraph>*[]</paragraph>' );
  52. doc.enqueueChanges( () => {
  53. batch.insertText( ' ', doc.selection.getFirstPosition() );
  54. } );
  55. sinon.assert.calledOnce( spy );
  56. expect( getData( doc ) ).to.equal( '<paragraph>[]</paragraph>' );
  57. } );
  58. } );
  59. describe( 'Callback', () => {
  60. it( 'should run callback when the pattern is matched', () => {
  61. const spy = testUtils.sinon.spy();
  62. new BlockAutoformatEngine( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  63. setData( doc, '<paragraph>*[]</paragraph>' );
  64. doc.enqueueChanges( () => {
  65. batch.insertText( ' ', doc.selection.getFirstPosition() );
  66. } );
  67. sinon.assert.calledOnce( spy );
  68. } );
  69. it( 'should not run a callback when changes are in transparent batch', () => {
  70. const spy = testUtils.sinon.spy();
  71. new BlockAutoformatEngine( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  72. setData( doc, '<paragraph>*[]</paragraph>' );
  73. doc.enqueueChanges( () => {
  74. doc.batch( 'transparent' ).insertText( ' ', doc.selection.getFirstPosition() );
  75. } );
  76. sinon.assert.notCalled( spy );
  77. } );
  78. it( 'should ignore other delta operations', () => {
  79. const spy = testUtils.sinon.spy();
  80. new BlockAutoformatEngine( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  81. setData( doc, '<paragraph>*[]</paragraph>' );
  82. doc.enqueueChanges( () => {
  83. batch.remove( doc.selection.getFirstRange() );
  84. } );
  85. sinon.assert.notCalled( spy );
  86. } );
  87. it( 'should stop if there is no text to run matching on', () => {
  88. const spy = testUtils.sinon.spy();
  89. new BlockAutoformatEngine( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  90. setData( doc, '<paragraph>[]</paragraph>' );
  91. doc.enqueueChanges( () => {
  92. batch.insertText( ' ', doc.selection.getFirstPosition() );
  93. } );
  94. sinon.assert.notCalled( spy );
  95. } );
  96. } );
  97. } );
  98. /**
  99. * Dummy command to execute.
  100. */
  101. class TestCommand extends Command {
  102. /**
  103. * Creates an instance of the command.
  104. *
  105. * @param {module:core/editor/editor~Editor} editor Editor instance.
  106. * @param {Function} onExecuteCallback execute call hook
  107. */
  108. constructor( editor, onExecuteCallback ) {
  109. super( editor );
  110. this.onExecute = onExecuteCallback;
  111. }
  112. /**
  113. * Executes command.
  114. *
  115. * @protected
  116. */
  117. execute() {
  118. this.onExecute();
  119. }
  120. }