blockautoformatediting.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /**
  2. * @license Copyright (c) 2003-2020, 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, plugin;
  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. plugin = editor.plugins.get( 'Autoformat' );
  26. } );
  27. } );
  28. describe( 'command name', () => {
  29. it( 'should run a command when the pattern is matched', () => {
  30. const spy = testUtils.sinon.spy();
  31. const testCommand = new TestCommand( editor, spy );
  32. editor.commands.add( 'testCommand', testCommand );
  33. blockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' );
  34. setData( model, '<paragraph>*[]</paragraph>' );
  35. model.change( writer => {
  36. writer.insertText( ' ', doc.selection.getFirstPosition() );
  37. } );
  38. sinon.assert.calledOnce( spy );
  39. } );
  40. it( 'should remove found pattern', () => {
  41. const spy = testUtils.sinon.spy();
  42. const testCommand = new TestCommand( editor, spy );
  43. editor.commands.add( 'testCommand', testCommand );
  44. blockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' );
  45. setData( model, '<paragraph>*[]</paragraph>' );
  46. model.change( writer => {
  47. writer.insertText( ' ', doc.selection.getFirstPosition() );
  48. } );
  49. sinon.assert.calledOnce( spy );
  50. expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
  51. } );
  52. it( 'should not autoformat if command is disabled', () => {
  53. const spy = testUtils.sinon.spy();
  54. const testCommand = new TestCommand( editor, spy );
  55. testCommand.refresh = function() {
  56. this.isEnabled = false;
  57. };
  58. editor.commands.add( 'testCommand', testCommand );
  59. blockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' );
  60. setData( model, '<paragraph>*[]</paragraph>' );
  61. model.change( writer => {
  62. writer.insertText( ' ', doc.selection.getFirstPosition() );
  63. } );
  64. sinon.assert.notCalled( spy );
  65. } );
  66. } );
  67. describe( 'callback', () => {
  68. it( 'should run callback when the pattern is matched', () => {
  69. const spy = testUtils.sinon.spy();
  70. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  71. setData( model, '<paragraph>*[]</paragraph>' );
  72. model.change( writer => {
  73. writer.insertText( ' ', doc.selection.getFirstPosition() );
  74. } );
  75. sinon.assert.calledOnce( spy );
  76. } );
  77. it( 'should not call callback when the pattern is matched and plugin is disabled', () => {
  78. const callbackSpy = testUtils.sinon.spy().named( 'callback' );
  79. blockAutoformatEditing( editor, plugin, /^[*]\s$/, callbackSpy );
  80. plugin.isEnabled = false;
  81. setData( model, '<paragraph>*[]</paragraph>' );
  82. model.change( writer => {
  83. writer.insertText( ' ', doc.selection.getFirstPosition() );
  84. } );
  85. sinon.assert.notCalled( callbackSpy );
  86. } );
  87. it( 'should ignore other delta operations', () => {
  88. const spy = testUtils.sinon.spy();
  89. blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
  90. setData( model, '<paragraph>*[]</paragraph>' );
  91. model.change( writer => {
  92. writer.remove( doc.selection.getFirstRange() );
  93. } );
  94. sinon.assert.notCalled( spy );
  95. } );
  96. it( 'should stop if there is no text to run matching on', () => {
  97. const spy = testUtils.sinon.spy();
  98. blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
  99. setData( model, '<paragraph>[]</paragraph>' );
  100. model.change( writer => {
  101. writer.insertText( ' ', doc.selection.getFirstPosition() );
  102. } );
  103. sinon.assert.notCalled( spy );
  104. } );
  105. it( 'should not call callback when after inline element', () => {
  106. // Configure the schema.
  107. model.schema.register( 'softBreak', {
  108. allowWhere: '$text',
  109. isInline: true
  110. } );
  111. editor.conversion.for( 'upcast' )
  112. .elementToElement( {
  113. model: 'softBreak',
  114. view: 'br'
  115. } );
  116. editor.conversion.for( 'downcast' )
  117. .elementToElement( {
  118. model: 'softBreak',
  119. view: ( modelElement, viewWriter ) => viewWriter.createEmptyElement( 'br' )
  120. } );
  121. const spy = testUtils.sinon.spy();
  122. blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
  123. setData( model, '<paragraph>*<softBreak></softBreak>[]</paragraph>' );
  124. model.change( writer => {
  125. writer.insertText( ' ', doc.selection.getFirstPosition() );
  126. } );
  127. sinon.assert.notCalled( spy );
  128. } );
  129. it( 'should not call callback when after inline element (typing after softBreak in a "matching" paragraph)', () => {
  130. // Configure the schema.
  131. model.schema.register( 'softBreak', {
  132. allowWhere: '$text',
  133. isInline: true
  134. } );
  135. editor.conversion.for( 'upcast' )
  136. .elementToElement( {
  137. model: 'softBreak',
  138. view: 'br'
  139. } );
  140. editor.conversion.for( 'downcast' )
  141. .elementToElement( {
  142. model: 'softBreak',
  143. view: ( modelElement, viewWriter ) => viewWriter.createEmptyElement( 'br' )
  144. } );
  145. const spy = testUtils.sinon.spy();
  146. blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
  147. setData( model, '<paragraph>* <softBreak></softBreak>[]</paragraph>' );
  148. model.change( writer => {
  149. writer.insertText( ' ', doc.selection.getFirstPosition() );
  150. } );
  151. sinon.assert.notCalled( spy );
  152. } );
  153. it( 'should stop if callback returned false', () => {
  154. blockAutoformatEditing( editor, plugin, /^[*]\s$/, () => false );
  155. setData( model, '<paragraph>*[]</paragraph>' );
  156. model.change( writer => {
  157. writer.insertText( ' ', doc.selection.getFirstPosition() );
  158. } );
  159. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  160. } );
  161. } );
  162. it( 'should ignore transparent batches', () => {
  163. const spy = testUtils.sinon.spy();
  164. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  165. setData( model, '<paragraph>*[]</paragraph>' );
  166. model.enqueueChange( 'transparent', writer => {
  167. writer.insertText( ' ', doc.selection.getFirstPosition() );
  168. } );
  169. sinon.assert.notCalled( spy );
  170. } );
  171. } );
  172. /**
  173. * Dummy command to execute.
  174. */
  175. class TestCommand extends Command {
  176. /**
  177. * Creates an instance of the command.
  178. *
  179. * @param {module:core/editor/editor~Editor} editor Editor instance.
  180. * @param {Function} onExecuteCallback execute call hook
  181. */
  182. constructor( editor, onExecuteCallback ) {
  183. super( editor );
  184. this.onExecute = onExecuteCallback;
  185. }
  186. /**
  187. * Executes command.
  188. *
  189. * @protected
  190. */
  191. execute() {
  192. this.onExecute();
  193. }
  194. }