8
0

blockautoformatediting.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. 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 not call callback when after inline element (typing after softBreak in a "matching" paragraph)', () => {
  122. // Configure the schema.
  123. model.schema.register( 'softBreak', {
  124. allowWhere: '$text',
  125. isInline: true
  126. } );
  127. editor.conversion.for( 'upcast' )
  128. .elementToElement( {
  129. model: 'softBreak',
  130. view: 'br'
  131. } );
  132. editor.conversion.for( 'downcast' )
  133. .elementToElement( {
  134. model: 'softBreak',
  135. view: ( modelElement, viewWriter ) => viewWriter.createEmptyElement( 'br' )
  136. } );
  137. const spy = testUtils.sinon.spy();
  138. new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
  139. setData( model, '<paragraph>* <softBreak></softBreak>[]</paragraph>' );
  140. model.change( writer => {
  141. writer.insertText( ' ', doc.selection.getFirstPosition() );
  142. } );
  143. sinon.assert.notCalled( spy );
  144. } );
  145. it( 'should stop if callback returned false', () => {
  146. new BlockAutoformatEditing( editor, /^[*]\s$/, () => false ); // eslint-disable-line no-new
  147. setData( model, '<paragraph>*[]</paragraph>' );
  148. model.change( writer => {
  149. writer.insertText( ' ', doc.selection.getFirstPosition() );
  150. } );
  151. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  152. } );
  153. } );
  154. it( 'should ignore transparent batches', () => {
  155. const spy = testUtils.sinon.spy();
  156. new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
  157. setData( model, '<paragraph>*[]</paragraph>' );
  158. model.enqueueChange( 'transparent', writer => {
  159. writer.insertText( ' ', doc.selection.getFirstPosition() );
  160. } );
  161. sinon.assert.notCalled( spy );
  162. } );
  163. } );
  164. /**
  165. * Dummy command to execute.
  166. */
  167. class TestCommand extends Command {
  168. /**
  169. * Creates an instance of the command.
  170. *
  171. * @param {module:core/editor/editor~Editor} editor Editor instance.
  172. * @param {Function} onExecuteCallback execute call hook
  173. */
  174. constructor( editor, onExecuteCallback ) {
  175. super( editor );
  176. this.onExecute = onExecuteCallback;
  177. }
  178. /**
  179. * Executes command.
  180. *
  181. * @protected
  182. */
  183. execute() {
  184. this.onExecute();
  185. }
  186. }