inlineautoformatengine.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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' );
  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' );
  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 stop early if selection is not collapsed', () => {
  43. new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testAttribute' );
  44. setData( doc, '<paragraph>*foob[ar]</paragraph>' );
  45. doc.enqueueChanges( () => {
  46. batch.insert( doc.selection.getFirstPosition(), '*' );
  47. } );
  48. expect( getData( doc ) ).to.equal( '<paragraph>*foob*[ar]</paragraph>' );
  49. } );
  50. } );
  51. describe( 'Callback', () => {
  52. it( 'should stop when there are no format ranges returned from testCallback', () => {
  53. const formatSpy = testUtils.sinon.spy();
  54. const testStub = testUtils.sinon.stub().returns( {
  55. format: [ [] ],
  56. remove: []
  57. } );
  58. new InlineAutoformatEngine( editor, testStub, formatSpy );
  59. setData( doc, '<paragraph>*[]</paragraph>' );
  60. doc.enqueueChanges( () => {
  61. batch.insert( doc.selection.getFirstPosition(), ' ' );
  62. } );
  63. sinon.assert.notCalled( formatSpy );
  64. } );
  65. it( 'should stop when there are no remove ranges returned from testCallback', () => {
  66. const formatSpy = testUtils.sinon.spy();
  67. const testStub = testUtils.sinon.stub().returns( {
  68. format: [],
  69. remove: [ [] ]
  70. } );
  71. new InlineAutoformatEngine( editor, testStub, formatSpy );
  72. setData( doc, '<paragraph>*[]</paragraph>' );
  73. doc.enqueueChanges( () => {
  74. batch.insert( doc.selection.getFirstPosition(), ' ' );
  75. } );
  76. sinon.assert.notCalled( formatSpy );
  77. } );
  78. it( 'should stop early when there is no text', () => {
  79. const formatSpy = testUtils.sinon.spy();
  80. const testStub = testUtils.sinon.stub().returns( {
  81. format: [],
  82. remove: [ [] ]
  83. } );
  84. new InlineAutoformatEngine( editor, testStub, formatSpy );
  85. setData( doc, '<paragraph>[]</paragraph>' );
  86. doc.enqueueChanges( () => {
  87. batch.insert( doc.selection.getFirstPosition(), ' ' );
  88. } );
  89. sinon.assert.notCalled( formatSpy );
  90. } );
  91. } );
  92. } );