blockautoformatediting.js 6.7 KB

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