inlineautoformatediting.js 5.2 KB

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