blockautoformatengine.js 4.4 KB

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