8
0

blockautoformatediting.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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 ignore a ranged selection', () => {
  97. model.schema.extend( '$text', { allowAttributes: 'foo' } );
  98. const spy = testUtils.sinon.spy();
  99. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  100. setData( model, '<paragraph>[* ]foo</paragraph>' );
  101. model.change( writer => {
  102. writer.setAttribute( 'foo', true, model.document.selection.getFirstRange() );
  103. } );
  104. sinon.assert.notCalled( spy );
  105. } );
  106. it( 'should stop if there is no text to run matching on', () => {
  107. const spy = testUtils.sinon.spy();
  108. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  109. setData( model, '<paragraph>[]</paragraph>' );
  110. model.change( writer => {
  111. writer.insertText( ' ', doc.selection.getFirstPosition() );
  112. } );
  113. sinon.assert.notCalled( spy );
  114. } );
  115. it( 'should not call callback when after inline element', () => {
  116. // Configure the schema.
  117. model.schema.register( 'softBreak', {
  118. allowWhere: '$text',
  119. isInline: true
  120. } );
  121. editor.conversion.for( 'upcast' )
  122. .elementToElement( {
  123. model: 'softBreak',
  124. view: 'br'
  125. } );
  126. editor.conversion.for( 'downcast' )
  127. .elementToElement( {
  128. model: 'softBreak',
  129. view: ( modelElement, { writer } ) => writer.createEmptyElement( 'br' )
  130. } );
  131. const spy = testUtils.sinon.spy();
  132. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  133. setData( model, '<paragraph>*<softBreak></softBreak>[]</paragraph>' );
  134. model.change( writer => {
  135. writer.insertText( ' ', doc.selection.getFirstPosition() );
  136. } );
  137. sinon.assert.notCalled( spy );
  138. } );
  139. it( 'should not call callback when typing in the middle of block text', () => {
  140. const spy = testUtils.sinon.spy();
  141. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  142. setData( model, '<paragraph>* foo[]bar</paragraph>' );
  143. model.change( writer => {
  144. writer.insertText( ' ', doc.selection.getFirstPosition() );
  145. } );
  146. sinon.assert.notCalled( spy );
  147. } );
  148. it( 'should not call callback when after inline element (typing after softBreak in a "matching" paragraph)', () => {
  149. // Configure the schema.
  150. model.schema.register( 'softBreak', {
  151. allowWhere: '$text',
  152. isInline: true
  153. } );
  154. editor.conversion.for( 'upcast' )
  155. .elementToElement( {
  156. model: 'softBreak',
  157. view: 'br'
  158. } );
  159. editor.conversion.for( 'downcast' )
  160. .elementToElement( {
  161. model: 'softBreak',
  162. view: ( modelElement, { writer } ) => writer.createEmptyElement( 'br' )
  163. } );
  164. const spy = testUtils.sinon.spy();
  165. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  166. setData( model, '<paragraph>* <softBreak></softBreak>[]</paragraph>' );
  167. model.change( writer => {
  168. writer.insertText( ' ', doc.selection.getFirstPosition() );
  169. } );
  170. sinon.assert.notCalled( spy );
  171. } );
  172. it( 'should stop if callback returned false', () => {
  173. blockAutoformatEditing( editor, plugin, /^[*]\s$/, () => false );
  174. setData( model, '<paragraph>*[]</paragraph>' );
  175. model.change( writer => {
  176. writer.insertText( ' ', doc.selection.getFirstPosition() );
  177. } );
  178. expect( getData( model ) ).to.equal( '<paragraph>* []</paragraph>' );
  179. } );
  180. } );
  181. it( 'should ignore transparent batches', () => {
  182. const spy = testUtils.sinon.spy();
  183. blockAutoformatEditing( editor, plugin, /^[*]\s$/, spy );
  184. setData( model, '<paragraph>*[]</paragraph>' );
  185. model.enqueueChange( 'transparent', writer => {
  186. writer.insertText( ' ', doc.selection.getFirstPosition() );
  187. } );
  188. sinon.assert.notCalled( spy );
  189. } );
  190. } );
  191. /**
  192. * Dummy command to execute.
  193. */
  194. class TestCommand extends Command {
  195. /**
  196. * Creates an instance of the command.
  197. *
  198. * @param {module:core/editor/editor~Editor} editor Editor instance.
  199. * @param {Function} onExecuteCallback execute call hook
  200. */
  201. constructor( editor, onExecuteCallback ) {
  202. super( editor );
  203. this.onExecute = onExecuteCallback;
  204. }
  205. /**
  206. * Executes command.
  207. *
  208. * @protected
  209. */
  210. execute() {
  211. this.onExecute();
  212. }
  213. }