blockautoformatediting.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 the callback when the pattern is matched but the 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 typing in the middle of block text', () => {
  130. const spy = testUtils.sinon.spy();
  131. blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
  132. setData( model, '<paragraph>* foo[]bar</paragraph>' );
  133. model.change( writer => {
  134. writer.insertText( ' ', doc.selection.getFirstPosition() );
  135. } );
  136. sinon.assert.notCalled( spy );
  137. } );
  138. it( 'should not call callback when after inline element (typing after softBreak in a "matching" paragraph)', () => {
  139. // Configure the schema.
  140. model.schema.register( 'softBreak', {
  141. allowWhere: '$text',
  142. isInline: true
  143. } );
  144. editor.conversion.for( 'upcast' )
  145. .elementToElement( {
  146. model: 'softBreak',
  147. view: 'br'
  148. } );
  149. editor.conversion.for( 'downcast' )
  150. .elementToElement( {
  151. model: 'softBreak',
  152. view: ( modelElement, viewWriter ) => viewWriter.createEmptyElement( 'br' )
  153. } );
  154. const spy = testUtils.sinon.spy();
  155. blockAutoformatEditing( editor, plugin, /^[*]\s/, spy );
  156. setData( model, '<paragraph>* <softBreak></softBreak>[]</paragraph>' );
  157. model.change( writer => {
  158. writer.insertText( ' ', doc.selection.getFirstPosition() );
  159. } );
  160. sinon.assert.notCalled( spy );
  161. } );
  162. it( 'should stop if callback returned false', () => {
  163. blockAutoformatEditing( editor, plugin, /^[*]\s$/, () => false );
  164. setData( model, '<paragraph>*[]</paragraph>' );
  165. model.change( writer => {
  166. writer.insertText( ' ', doc.selection.getFirstPosition() );
  167. } );
  168. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  169. } );
  170. } );
  171. it( 'should ignore transparent batches', () => {
  172. const spy = testUtils.sinon.spy();
  173. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  174. setData( model, '<paragraph>*[]</paragraph>' );
  175. model.enqueueChange( 'transparent', writer => {
  176. writer.insertText( ' ', doc.selection.getFirstPosition() );
  177. } );
  178. sinon.assert.notCalled( spy );
  179. } );
  180. } );
  181. /**
  182. * Dummy command to execute.
  183. */
  184. class TestCommand extends Command {
  185. /**
  186. * Creates an instance of the command.
  187. *
  188. * @param {module:core/editor/editor~Editor} editor Editor instance.
  189. * @param {Function} onExecuteCallback execute call hook
  190. */
  191. constructor( editor, onExecuteCallback ) {
  192. super( editor );
  193. this.onExecute = onExecuteCallback;
  194. }
  195. /**
  196. * Executes command.
  197. *
  198. * @protected
  199. */
  200. execute() {
  201. this.onExecute();
  202. }
  203. }