inlineautoformatediting.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @license Copyright (c) 2003-2018, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import InlineAutoformatEditing from '../src/inlineautoformatediting';
  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( 'InlineAutoformatEditing', () => {
  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.extend( '$text', { allowAttributes: 'testAttribute' } );
  24. } );
  25. } );
  26. describe( 'attribute', () => {
  27. it( 'should stop early if there are less than 3 capture groups', () => {
  28. new InlineAutoformatEditing( 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 InlineAutoformatEditing( 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 stop early if selection is not collapsed', () => {
  44. new InlineAutoformatEditing( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
  45. setData( model, '<paragraph>*foob[ar]</paragraph>' );
  46. model.change( writer => {
  47. writer.insertText( '*', doc.selection.getFirstPosition() );
  48. } );
  49. expect( getData( model ) ).to.equal( '<paragraph>*foob*[ar]</paragraph>' );
  50. } );
  51. } );
  52. describe( 'Callback', () => {
  53. it( 'should stop when there are no format ranges returned from testCallback', () => {
  54. const formatSpy = testUtils.sinon.spy();
  55. const testStub = testUtils.sinon.stub().returns( {
  56. format: [ [] ],
  57. remove: []
  58. } );
  59. new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
  60. setData( model, '<paragraph>*[]</paragraph>' );
  61. model.change( writer => {
  62. writer.insertText( ' ', doc.selection.getFirstPosition() );
  63. } );
  64. sinon.assert.notCalled( formatSpy );
  65. } );
  66. it( 'should stop when there are no remove ranges returned from testCallback', () => {
  67. const formatSpy = testUtils.sinon.spy();
  68. const testStub = testUtils.sinon.stub().returns( {
  69. format: [],
  70. remove: [ [] ]
  71. } );
  72. new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
  73. setData( model, '<paragraph>*[]</paragraph>' );
  74. model.change( writer => {
  75. writer.insertText( ' ', doc.selection.getFirstPosition() );
  76. } );
  77. sinon.assert.notCalled( formatSpy );
  78. } );
  79. it( 'should stop early when there is no text', () => {
  80. const formatSpy = testUtils.sinon.spy();
  81. const testStub = testUtils.sinon.stub().returns( {
  82. format: [],
  83. remove: [ [] ]
  84. } );
  85. new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
  86. setData( model, '<paragraph>[]</paragraph>' );
  87. model.change( writer => {
  88. writer.insertText( ' ', doc.selection.getFirstPosition() );
  89. } );
  90. sinon.assert.notCalled( formatSpy );
  91. } );
  92. } );
  93. } );