8
0

inlineautoformatengine.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import InlineAutoformatEngine from '/ckeditor5/autoformat/inlineautoformatengine.js';
  6. import Paragraph from '/ckeditor5/paragraph/paragraph.js';
  7. import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
  8. import Enter from '/ckeditor5/enter/enter.js';
  9. import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js';
  10. import testUtils from '/tests/core/_utils/utils.js';
  11. import Command from '/ckeditor5/core/command/command.js';
  12. testUtils.createSinonSandbox();
  13. describe( 'InlineAutoformatEngine', () => {
  14. let editor, doc, batch;
  15. beforeEach( () => {
  16. return VirtualTestEditor.create( {
  17. features: [ Enter, Paragraph ]
  18. } )
  19. .then( newEditor => {
  20. editor = newEditor;
  21. doc = editor.document;
  22. batch = doc.batch();
  23. } );
  24. } );
  25. describe( 'Command name', () => {
  26. it( 'should accept a string pattern', () => {
  27. const spy = testUtils.sinon.spy();
  28. editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
  29. new InlineAutoformatEngine( editor, '(\\*)(.+?)(\\*)', 'testCommand' );
  30. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  31. doc.enqueueChanges( () => {
  32. batch.insert( doc.selection.getFirstPosition(), '*' );
  33. } );
  34. sinon.assert.calledOnce( spy );
  35. } );
  36. it( 'should stop early if there are less than 3 capture groups', () => {
  37. const spy = testUtils.sinon.spy();
  38. editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
  39. new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testCommand' );
  40. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  41. doc.enqueueChanges( () => {
  42. batch.insert( doc.selection.getFirstPosition(), '*' );
  43. } );
  44. sinon.assert.notCalled( spy );
  45. } );
  46. it( 'should run a command when the pattern is matched', () => {
  47. const spy = testUtils.sinon.spy();
  48. editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
  49. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testCommand' );
  50. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  51. doc.enqueueChanges( () => {
  52. batch.insert( doc.selection.getFirstPosition(), '*' );
  53. } );
  54. sinon.assert.calledOnce( spy );
  55. } );
  56. it( 'should remove found pattern', () => {
  57. const spy = testUtils.sinon.spy();
  58. editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
  59. new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testCommand' );
  60. setData( doc, '<paragraph>*foobar[]</paragraph>' );
  61. doc.enqueueChanges( () => {
  62. batch.insert( doc.selection.getFirstPosition(), '*' );
  63. } );
  64. sinon.assert.calledOnce( spy );
  65. expect( getData( doc ) ).to.equal( '<paragraph>foobar[]</paragraph>' );
  66. } );
  67. } );
  68. describe( 'Callback', () => {
  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 );
  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 );
  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 );
  102. setData( doc, '<paragraph>[]</paragraph>' );
  103. doc.enqueueChanges( () => {
  104. batch.insert( doc.selection.getFirstPosition(), ' ' );
  105. } );
  106. sinon.assert.notCalled( formatSpy );
  107. } );
  108. it( 'takes text from nested elements', () => {
  109. const formatSpy = testUtils.sinon.spy();
  110. const testStub = testUtils.sinon.stub().returns( {
  111. format: [],
  112. remove: []
  113. } );
  114. new InlineAutoformatEngine( editor, testStub, formatSpy );
  115. setData( doc, '<paragraph><paragraph>foobar[]</paragraph></paragraph>' );
  116. doc.enqueueChanges( () => {
  117. batch.insert( doc.selection.getFirstPosition(), ' ' );
  118. } );
  119. sinon.assert.called( testStub );
  120. sinon.assert.notCalled( formatSpy );
  121. sinon.assert.calledWith( testStub, 'foobar' );
  122. } );
  123. } );
  124. } );
  125. /**
  126. * Dummy command to execute.
  127. */
  128. class TestCommand extends Command {
  129. /**
  130. * Creates an instance of the command.
  131. *
  132. * @param {core.editor.Editor} editor Editor instance.
  133. * @param {Function} onExecuteCallback _doExecute call hook
  134. */
  135. constructor( editor, onExecuteCallback ) {
  136. super( editor );
  137. this.onExecute = onExecuteCallback;
  138. }
  139. /**
  140. * Executes command.
  141. *
  142. * @protected
  143. */
  144. _doExecute() {
  145. this.onExecute();
  146. }
  147. }