blockautoformatengine.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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/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.set( 'testCommand', new TestCommand( editor, spy ) );
  29. new BlockAutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
  30. setData( doc, '<paragraph>*[]</paragraph>' );
  31. doc.enqueueChanges( () => {
  32. batch.insert( doc.selection.getFirstPosition(), ' ' );
  33. } );
  34. sinon.assert.calledOnce( spy );
  35. } );
  36. it( 'should remove found pattern', () => {
  37. const spy = testUtils.sinon.spy();
  38. editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
  39. new BlockAutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
  40. setData( doc, '<paragraph>*[]</paragraph>' );
  41. doc.enqueueChanges( () => {
  42. batch.insert( doc.selection.getFirstPosition(), ' ' );
  43. } );
  44. sinon.assert.calledOnce( spy );
  45. expect( getData( doc ) ).to.equal( '<paragraph>[]</paragraph>' );
  46. } );
  47. } );
  48. describe( 'Callback', () => {
  49. it( 'should run callback when the pattern is matched', () => {
  50. const spy = testUtils.sinon.spy();
  51. new BlockAutoformatEngine( editor, /^[\*]\s$/, spy );
  52. setData( doc, '<paragraph>*[]</paragraph>' );
  53. doc.enqueueChanges( () => {
  54. batch.insert( doc.selection.getFirstPosition(), ' ' );
  55. } );
  56. sinon.assert.calledOnce( spy );
  57. } );
  58. it( 'should ignore other delta operations', () => {
  59. const spy = testUtils.sinon.spy();
  60. new BlockAutoformatEngine( editor, /^[\*]\s/, spy );
  61. setData( doc, '<paragraph>*[]</paragraph>' );
  62. doc.enqueueChanges( () => {
  63. batch.remove( doc.selection.getFirstRange() );
  64. } );
  65. sinon.assert.notCalled( spy );
  66. } );
  67. it( 'should stop if there is no text to run matching on', () => {
  68. const spy = testUtils.sinon.spy();
  69. new BlockAutoformatEngine( editor, /^[\*]\s/, spy );
  70. setData( doc, '<paragraph>[]</paragraph>' );
  71. doc.enqueueChanges( () => {
  72. batch.insert( doc.selection.getFirstPosition(), '' );
  73. } );
  74. sinon.assert.notCalled( spy );
  75. } );
  76. } );
  77. } );
  78. /**
  79. * Dummy command to execute.
  80. */
  81. class TestCommand extends Command {
  82. /**
  83. * Creates an instance of the command.
  84. *
  85. * @param {module:core/editor~Editor} editor Editor instance.
  86. * @param {Function} onExecuteCallback _doExecute call hook
  87. */
  88. constructor( editor, onExecuteCallback ) {
  89. super( editor );
  90. this.onExecute = onExecuteCallback;
  91. }
  92. /**
  93. * Executes command.
  94. *
  95. * @protected
  96. */
  97. _doExecute() {
  98. this.onExecute();
  99. }
  100. }