inlineautoformatengine.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import InlineAutoformatEngine from '../src/inlineautoformatengine';
  6. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  7. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  8. import Enter from '@ckeditor/ckeditor5-enter/src/enter';
  9. import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
  10. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  11. testUtils.createSinonSandbox();
  12. describe( 'InlineAutoformatEngine', () => {
  13. let editor, model, doc;
  14. beforeEach( () => {
  15. return VirtualTestEditor
  16. .create( {
  17. plugins: [ Enter, Paragraph ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. model = editor.model;
  22. doc = model.document;
  23. model.schema.allow( { name: '$inline', attributes: [ 'testAttribute' ] } );
  24. } );
  25. } );
  26. describe( 'attribute', () => {
  27. it( 'should stop early if there are less than 3 capture groups', () => {
  28. new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
  29. setData( model, '<paragraph>*foobar[]</paragraph>' );
  30. model.change( writer => {
  31. writer.insertText( '*', doc.selection.getFirstPosition() );
  32. } );
  33. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  34. } );
  35. it( 'should apply an attribute when the pattern is matched', () => {
  36. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
  37. setData( model, '<paragraph>*foobar[]</paragraph>' );
  38. model.change( writer => {
  39. writer.insertText( '*', doc.selection.getFirstPosition() );
  40. } );
  41. expect( getData( model ) ).to.equal( '<paragraph><$text testAttribute="true">foobar</$text>[]</paragraph>' );
  42. } );
  43. it( 'should not apply an attribute when changes are in transparent batch', () => {
  44. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
  45. setData( model, '<paragraph>*foobar[]</paragraph>' );
  46. model.enqueueChange( 'transparent', writer => {
  47. writer.insertText( '*', doc.selection.getFirstPosition() );
  48. } );
  49. expect( getData( model ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  50. } );
  51. it( 'should stop early if selection is not collapsed', () => {
  52. new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
  53. setData( model, '<paragraph>*foob[ar]</paragraph>' );
  54. model.change( writer => {
  55. writer.insertText( '*', doc.selection.getFirstPosition() );
  56. } );
  57. expect( getData( model ) ).to.equal( '<paragraph>*foob*[ar]</paragraph>' );
  58. } );
  59. } );
  60. describe( 'Callback', () => {
  61. it( 'should not run a callback when changes are in transparent batch', () => {
  62. const spy = testUtils.sinon.spy();
  63. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, spy ); // eslint-disable-line no-new
  64. setData( model, '<paragraph>*foobar[]</paragraph>' );
  65. model.enqueueChange( 'transparent', writer => {
  66. writer.insertText( '*', doc.selection.getFirstPosition() );
  67. } );
  68. sinon.assert.notCalled( spy );
  69. } );
  70. it( 'should stop when there are no format ranges returned from testCallback', () => {
  71. const formatSpy = testUtils.sinon.spy();
  72. const testStub = testUtils.sinon.stub().returns( {
  73. format: [ [] ],
  74. remove: []
  75. } );
  76. new InlineAutoformatEngine( 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 when there are no remove ranges returned from testCallback', () => {
  84. const formatSpy = testUtils.sinon.spy();
  85. const testStub = testUtils.sinon.stub().returns( {
  86. format: [],
  87. remove: [ [] ]
  88. } );
  89. new InlineAutoformatEngine( 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 stop early when there is no text', () => {
  97. const formatSpy = testUtils.sinon.spy();
  98. const testStub = testUtils.sinon.stub().returns( {
  99. format: [],
  100. remove: [ [] ]
  101. } );
  102. new InlineAutoformatEngine( editor, testStub, formatSpy ); // eslint-disable-line no-new
  103. setData( model, '<paragraph>[]</paragraph>' );
  104. model.change( writer => {
  105. writer.insertText( ' ', doc.selection.getFirstPosition() );
  106. } );
  107. sinon.assert.notCalled( formatSpy );
  108. } );
  109. it( 'should detach removed ranges', () => {
  110. const detachSpies = [];
  111. const callback = fixBatch => testUtils.sinon.stub( fixBatch, 'remove' ).callsFake( saveDetachSpy );
  112. testUtils.sinon.stub( editor.model.schema, 'getValidRanges' )
  113. .callThrough()
  114. .callsFake( ranges => ranges.map( saveDetachSpy ) );
  115. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, callback ); // eslint-disable-line no-new
  116. setData( model, '<paragraph>*foobar[]</paragraph>' );
  117. model.change( writer => {
  118. writer.insertText( '*', doc.selection.getFirstPosition() );
  119. } );
  120. // There should be two removed ranges and one range used to apply autoformat.
  121. expect( detachSpies ).to.have.length( 3 );
  122. for ( const spy of detachSpies ) {
  123. testUtils.sinon.assert.calledOnce( spy );
  124. }
  125. function saveDetachSpy( range ) {
  126. detachSpies.push( testUtils.sinon.spy( range, 'detach' ) );
  127. }
  128. } );
  129. } );
  130. } );