8
0

inlineautoformatediting.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 inlineAutoformatEditing from '../src/inlineautoformatediting';
  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. describe( 'inlineAutoformatEditing', () => {
  13. let editor, model, doc, plugin, formatSpy;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. formatSpy = testUtils.sinon.spy().named( 'formatCallback' );
  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. model.schema.extend( '$text', { allowAttributes: 'testAttribute' } );
  27. } );
  28. } );
  29. describe( 'regExp', () => {
  30. it( 'should not call the formatCallback if there are less than 3 capture groups', () => {
  31. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, formatSpy );
  32. setData( model, '<paragraph>*foobar[]</paragraph>' );
  33. model.change( writer => {
  34. writer.insertText( '*', doc.selection.getFirstPosition() );
  35. } );
  36. sinon.assert.notCalled( formatSpy );
  37. } );
  38. it( 'should call the formatCallback when the pattern is matched', () => {
  39. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, formatSpy );
  40. setData( model, '<paragraph>*foobar[]</paragraph>' );
  41. model.change( writer => {
  42. writer.insertText( '*', doc.selection.getFirstPosition() );
  43. } );
  44. sinon.assert.calledOnce( formatSpy );
  45. } );
  46. it( 'should not call the formatCallback if the selection is not collapsed', () => {
  47. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, formatSpy );
  48. setData( model, '<paragraph>*foob[ar]</paragraph>' );
  49. model.change( writer => {
  50. writer.insertText( '*', doc.selection.getFirstPosition() );
  51. } );
  52. sinon.assert.notCalled( formatSpy );
  53. } );
  54. } );
  55. describe( 'callback', () => {
  56. it( 'should stop when there are no format ranges returned from testCallback', () => {
  57. const testStub = testUtils.sinon.stub().returns( {
  58. format: [ [] ],
  59. remove: []
  60. } );
  61. inlineAutoformatEditing( editor, plugin, testStub, formatSpy );
  62. setData( model, '<paragraph>*[]</paragraph>' );
  63. model.change( writer => {
  64. writer.insertText( ' ', doc.selection.getFirstPosition() );
  65. } );
  66. sinon.assert.notCalled( formatSpy );
  67. } );
  68. it( 'should stop when there are no remove ranges returned from testCallback', () => {
  69. const testStub = testUtils.sinon.stub().returns( {
  70. format: [],
  71. remove: [ [] ]
  72. } );
  73. inlineAutoformatEditing( editor, plugin, testStub, formatSpy );
  74. setData( model, '<paragraph>*[]</paragraph>' );
  75. model.change( writer => {
  76. writer.insertText( ' ', doc.selection.getFirstPosition() );
  77. } );
  78. sinon.assert.notCalled( formatSpy );
  79. } );
  80. it( 'should stop early when there is no text', () => {
  81. const testStub = testUtils.sinon.stub().returns( {
  82. format: [],
  83. remove: [ [] ]
  84. } );
  85. inlineAutoformatEditing( editor, plugin, testStub, formatSpy );
  86. setData( model, '<paragraph>[]</paragraph>' );
  87. model.change( writer => {
  88. writer.insertText( ' ', doc.selection.getFirstPosition() );
  89. } );
  90. sinon.assert.notCalled( formatSpy );
  91. } );
  92. it( 'should not run the formatCallback when the pattern is matched but the plugin is disabled', () => {
  93. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, formatSpy );
  94. plugin.isEnabled = false;
  95. setData( model, '<paragraph>*foobar[]</paragraph>' );
  96. model.change( writer => {
  97. writer.insertText( '*', doc.selection.getFirstPosition() );
  98. } );
  99. sinon.assert.notCalled( formatSpy );
  100. } );
  101. it( 'should not autoformat if callback returned false', () => {
  102. setData( model, '<paragraph>Foobar[]</paragraph>' );
  103. const p = model.document.getRoot().getChild( 0 );
  104. const testCallback = () => ( {
  105. format: [ model.createRange( model.createPositionAt( p, 0 ), model.createPositionAt( p, 3 ) ) ],
  106. remove: [ model.createRange( model.createPositionAt( p, 0 ), model.createPositionAt( p, 3 ) ) ]
  107. } );
  108. const formatCallback = () => false;
  109. inlineAutoformatEditing( editor, plugin, testCallback, formatCallback );
  110. model.change( writer => {
  111. writer.insertText( ' ', doc.selection.getFirstPosition() );
  112. } );
  113. expect( getData( model ) ).to.equal( '<paragraph>Foobar []</paragraph>' );
  114. } );
  115. } );
  116. it( 'should ignore transparent batches', () => {
  117. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, formatSpy );
  118. setData( model, '<paragraph>*foobar[]</paragraph>' );
  119. model.enqueueChange( 'transparent', writer => {
  120. writer.insertText( '*', doc.selection.getFirstPosition() );
  121. } );
  122. sinon.assert.notCalled( formatSpy );
  123. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  124. } );
  125. } );