inlineautoformatengine.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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, doc, batch;
  14. beforeEach( () => {
  15. return VirtualTestEditor.create( {
  16. plugins: [ Enter, Paragraph ]
  17. } )
  18. .then( newEditor => {
  19. editor = newEditor;
  20. doc = editor.document;
  21. batch = doc.batch();
  22. doc.schema.allow( { name: '$inline', attributes: [ 'testAttribute' ] } );
  23. } );
  24. } );
  25. describe( 'attribute', () => {
  26. it( 'should stop early if there are less than 3 capture groups', () => {
  27. new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
  28. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  29. doc.enqueueChanges( () => {
  30. batch.insert( doc.selection.getFirstPosition(), '*' );
  31. } );
  32. expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  33. } );
  34. it( 'should apply an attribute when the pattern is matched', () => {
  35. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
  36. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  37. doc.enqueueChanges( () => {
  38. batch.insert( doc.selection.getFirstPosition(), '*' );
  39. } );
  40. expect( getData( doc ) ).to.equal( '<paragraph><$text testAttribute="true">foobar</$text>[]</paragraph>' );
  41. } );
  42. it( 'should not apply an attribute when changes are in transparent batch', () => {
  43. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
  44. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  45. doc.enqueueChanges( () => {
  46. doc.batch( 'transparent' ).insert( doc.selection.getFirstPosition(), '*' );
  47. } );
  48. expect( getData( doc ) ).to.equal( '<paragraph>*foobar*[]</paragraph>' );
  49. } );
  50. it( 'should stop early if selection is not collapsed', () => {
  51. new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
  52. setData( doc, '<paragraph>*foob[ar]</paragraph>' );
  53. doc.enqueueChanges( () => {
  54. batch.insert( doc.selection.getFirstPosition(), '*' );
  55. } );
  56. expect( getData( doc ) ).to.equal( '<paragraph>*foob*[ar]</paragraph>' );
  57. } );
  58. } );
  59. describe( 'Callback', () => {
  60. it( 'should not run a callback when changes are in transparent batch', () => {
  61. const spy = testUtils.sinon.spy();
  62. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, spy ); // eslint-disable-line no-new
  63. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  64. doc.enqueueChanges( () => {
  65. doc.batch( 'transparent' ).insert( doc.selection.getFirstPosition(), '*' );
  66. } );
  67. sinon.assert.notCalled( spy );
  68. } );
  69. it( 'should stop when there are no format ranges returned from testCallback', () => {
  70. const formatSpy = testUtils.sinon.spy();
  71. const testStub = testUtils.sinon.stub().returns( {
  72. format: [ [] ],
  73. remove: []
  74. } );
  75. new InlineAutoformatEngine( editor, testStub, formatSpy ); // eslint-disable-line no-new
  76. setData( doc, '<paragraph>*[]</paragraph>' );
  77. doc.enqueueChanges( () => {
  78. batch.insert( doc.selection.getFirstPosition(), ' ' );
  79. } );
  80. sinon.assert.notCalled( formatSpy );
  81. } );
  82. it( 'should stop when there are no remove ranges returned from testCallback', () => {
  83. const formatSpy = testUtils.sinon.spy();
  84. const testStub = testUtils.sinon.stub().returns( {
  85. format: [],
  86. remove: [ [] ]
  87. } );
  88. new InlineAutoformatEngine( editor, testStub, formatSpy ); // eslint-disable-line no-new
  89. setData( doc, '<paragraph>*[]</paragraph>' );
  90. doc.enqueueChanges( () => {
  91. batch.insert( doc.selection.getFirstPosition(), ' ' );
  92. } );
  93. sinon.assert.notCalled( formatSpy );
  94. } );
  95. it( 'should stop early when there is no text', () => {
  96. const formatSpy = testUtils.sinon.spy();
  97. const testStub = testUtils.sinon.stub().returns( {
  98. format: [],
  99. remove: [ [] ]
  100. } );
  101. new InlineAutoformatEngine( editor, testStub, formatSpy ); // eslint-disable-line no-new
  102. setData( doc, '<paragraph>[]</paragraph>' );
  103. doc.enqueueChanges( () => {
  104. batch.insert( doc.selection.getFirstPosition(), ' ' );
  105. } );
  106. sinon.assert.notCalled( formatSpy );
  107. } );
  108. } );
  109. } );