inlineautoformatediting.js 5.0 KB

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