inlineautoformatediting.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. return VirtualTestEditor
  17. .create( {
  18. plugins: [ Enter, Paragraph, Autoformat ]
  19. } )
  20. .then( newEditor => {
  21. editor = newEditor;
  22. model = editor.model;
  23. doc = model.document;
  24. plugin = editor.plugins.get( 'Autoformat' );
  25. model.schema.extend( '$text', { allowAttributes: 'testAttribute' } );
  26. } );
  27. } );
  28. describe( 'attribute', () => {
  29. it( 'should stop early if there are less than 3 capture groups', () => {
  30. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, 'testAttribute' );
  31. setData( model, '<paragraph>*foobar[]</paragraph>' );
  32. model.change( writer => {
  33. writer.insertText( '*', doc.selection.getFirstPosition() );
  34. } );
  35. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  36. } );
  37. it( 'should apply an attribute when the pattern is matched', () => {
  38. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, 'testAttribute' );
  39. setData( model, '<paragraph>*foobar[]</paragraph>' );
  40. model.change( writer => {
  41. writer.insertText( '*', doc.selection.getFirstPosition() );
  42. } );
  43. expect( getData( model ) ).to.equal( '<paragraph><$text testAttribute="true">foobar</$text>[]</paragraph>' );
  44. } );
  45. it( 'should stop early if selection is not collapsed', () => {
  46. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, 'testAttribute' );
  47. setData( model, '<paragraph>*foob[ar]</paragraph>' );
  48. model.change( writer => {
  49. writer.insertText( '*', doc.selection.getFirstPosition() );
  50. } );
  51. expect( getData( model ) ).to.equal( '<paragraph>*foob*[ar]</paragraph>' );
  52. } );
  53. } );
  54. describe( 'callback', () => {
  55. it( 'should stop when there are no format ranges returned from testCallback', () => {
  56. const formatSpy = testUtils.sinon.spy();
  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 formatSpy = testUtils.sinon.spy();
  70. const testStub = testUtils.sinon.stub().returns( {
  71. format: [],
  72. remove: [ [] ]
  73. } );
  74. inlineAutoformatEditing( editor, plugin, testStub, formatSpy );
  75. setData( model, '<paragraph>*[]</paragraph>' );
  76. model.change( writer => {
  77. writer.insertText( ' ', doc.selection.getFirstPosition() );
  78. } );
  79. sinon.assert.notCalled( formatSpy );
  80. } );
  81. it( 'should stop early when there is no text', () => {
  82. const formatSpy = testUtils.sinon.spy();
  83. const testStub = testUtils.sinon.stub().returns( {
  84. format: [],
  85. remove: [ [] ]
  86. } );
  87. inlineAutoformatEditing( editor, plugin, testStub, formatSpy );
  88. setData( model, '<paragraph>[]</paragraph>' );
  89. model.change( writer => {
  90. writer.insertText( ' ', doc.selection.getFirstPosition() );
  91. } );
  92. sinon.assert.notCalled( formatSpy );
  93. } );
  94. it( 'should not run callback when the pattern is matched and plugin is disabled', () => {
  95. const callbackSpy = testUtils.sinon.spy().named( 'callback' );
  96. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, callbackSpy );
  97. plugin.isEnabled = false;
  98. setData( model, '<paragraph>*foobar[]</paragraph>' );
  99. model.change( writer => {
  100. writer.insertText( '*', doc.selection.getFirstPosition() );
  101. } );
  102. sinon.assert.notCalled( callbackSpy );
  103. } );
  104. it( 'should not autoformat if callback returned false', () => {
  105. setData( model, '<paragraph>Foobar[]</paragraph>' );
  106. const p = model.document.getRoot().getChild( 0 );
  107. const testCallback = () => ( {
  108. format: [ model.createRange( model.createPositionAt( p, 0 ), model.createPositionAt( p, 3 ) ) ],
  109. remove: [ model.createRange( model.createPositionAt( p, 0 ), model.createPositionAt( p, 3 ) ) ]
  110. } );
  111. const formatCallback = () => false;
  112. inlineAutoformatEditing( editor, plugin, testCallback, formatCallback );
  113. model.change( writer => {
  114. writer.insertText( ' ', doc.selection.getFirstPosition() );
  115. } );
  116. expect( getData( model ) ).to.equal( '<paragraph>Foobar []</paragraph>' );
  117. } );
  118. } );
  119. it( 'should ignore transparent batches', () => {
  120. inlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, 'testAttribute' );
  121. setData( model, '<paragraph>*foobar[]</paragraph>' );
  122. model.enqueueChange( 'transparent', writer => {
  123. writer.insertText( '*', doc.selection.getFirstPosition() );
  124. } );
  125. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  126. } );
  127. } );