blockautoformatediting.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. it( 'should have pluginName', () => {
  28. expect( BlockAutoformatEditing.pluginName ).to.equal( 'BlockAutoformatEditing' );
  29. } );
  30. describe( 'command name', () => {
  31. it( 'should run a command when the pattern is matched', () => {
  32. const spy = testUtils.sinon.spy();
  33. const testCommand = new TestCommand( editor, spy );
  34. editor.commands.add( 'testCommand', testCommand );
  35. new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  36. setData( model, '<paragraph>*[]</paragraph>' );
  37. model.change( writer => {
  38. writer.insertText( ' ', doc.selection.getFirstPosition() );
  39. } );
  40. sinon.assert.calledOnce( spy );
  41. } );
  42. it( 'should remove found pattern', () => {
  43. const spy = testUtils.sinon.spy();
  44. const testCommand = new TestCommand( editor, spy );
  45. editor.commands.add( 'testCommand', testCommand );
  46. new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  47. setData( model, '<paragraph>*[]</paragraph>' );
  48. model.change( writer => {
  49. writer.insertText( ' ', doc.selection.getFirstPosition() );
  50. } );
  51. sinon.assert.calledOnce( spy );
  52. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  53. } );
  54. it( 'should not autoformat if command is disabled', () => {
  55. const spy = testUtils.sinon.spy();
  56. const testCommand = new TestCommand( editor, spy );
  57. testCommand.refresh = function() {
  58. this.isEnabled = false;
  59. };
  60. editor.commands.add( 'testCommand', testCommand );
  61. new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
  62. setData( model, '<paragraph>*[]</paragraph>' );
  63. model.change( writer => {
  64. writer.insertText( ' ', doc.selection.getFirstPosition() );
  65. } );
  66. sinon.assert.notCalled( spy );
  67. } );
  68. } );
  69. describe( 'callback', () => {
  70. it( 'should run callback when the pattern is matched', () => {
  71. const spy = testUtils.sinon.spy();
  72. new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  73. setData( model, '<paragraph>*[]</paragraph>' );
  74. model.change( writer => {
  75. writer.insertText( ' ', doc.selection.getFirstPosition() );
  76. } );
  77. sinon.assert.calledOnce( spy );
  78. } );
  79. it( 'should ignore other delta operations', () => {
  80. const spy = testUtils.sinon.spy();
  81. new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  82. setData( model, '<paragraph>*[]</paragraph>' );
  83. model.change( writer => {
  84. writer.remove( doc.selection.getFirstRange() );
  85. } );
  86. sinon.assert.notCalled( spy );
  87. } );
  88. it( 'should stop if there is no text to run matching on', () => {
  89. const spy = testUtils.sinon.spy();
  90. new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  91. setData( model, '<paragraph>[]</paragraph>' );
  92. model.change( writer => {
  93. writer.insertText( ' ', doc.selection.getFirstPosition() );
  94. } );
  95. sinon.assert.notCalled( spy );
  96. } );
  97. it( 'should not call callback when after inline element', () => {
  98. // Configure the schema.
  99. model.schema.register( 'softBreak', {
  100. allowWhere: '$text',
  101. isInline: true
  102. } );
  103. editor.conversion.for( 'upcast' )
  104. .elementToElement( {
  105. model: 'softBreak',
  106. view: 'br'
  107. } );
  108. editor.conversion.for( 'downcast' )
  109. .elementToElement( {
  110. model: 'softBreak',
  111. view: ( modelElement, viewWriter ) => viewWriter.createEmptyElement( 'br' )
  112. } );
  113. const spy = testUtils.sinon.spy();
  114. new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  115. setData( model, '<paragraph>*<softBreak></softBreak>[]</paragraph>' );
  116. model.change( writer => {
  117. writer.insertText( ' ', doc.selection.getFirstPosition() );
  118. } );
  119. sinon.assert.notCalled( spy );
  120. } );
  121. it( 'should stop if callback returned false', () => {
  122. new BlockAutoformatEditing( editor, /^[*]\s$/, () => false ); // eslint-disable-line no-new
  123. setData( model, '<paragraph>*[]</paragraph>' );
  124. model.change( writer => {
  125. writer.insertText( ' ', doc.selection.getFirstPosition() );
  126. } );
  127. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  128. } );
  129. } );
  130. it( 'should ignore transparent batches', () => {
  131. const spy = testUtils.sinon.spy();
  132. new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  133. setData( model, '<paragraph>*[]</paragraph>' );
  134. model.enqueueChange( 'transparent', writer => {
  135. writer.insertText( ' ', doc.selection.getFirstPosition() );
  136. } );
  137. sinon.assert.notCalled( spy );
  138. } );
  139. } );
  140. /**
  141. * Dummy command to execute.
  142. */
  143. class TestCommand extends Command {
  144. /**
  145. * Creates an instance of the command.
  146. *
  147. * @param {module:core/editor/editor~Editor} editor Editor instance.
  148. * @param {Function} onExecuteCallback execute call hook
  149. */
  150. constructor( editor, onExecuteCallback ) {
  151. super( editor );
  152. this.onExecute = onExecuteCallback;
  153. }
  154. /**
  155. * Executes command.
  156. *
  157. * @protected
  158. */
  159. execute() {
  160. this.onExecute();
  161. }
  162. }