inlineautoformatengine.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 run a command when the pattern is matched', () => {
  27. const spy = testUtils.sinon.spy();
  28. editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
  29. new InlineAutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
  30. setData( doc, '<paragraph>*[]</paragraph>' );
  31. doc.enqueueChanges( () => {
  32. batch.insert( doc.selection.getFirstPosition(), ' ' );
  33. } );
  34. sinon.assert.calledOnce( spy );
  35. } );
  36. it( 'should remove found pattern', () => {
  37. const spy = testUtils.sinon.spy();
  38. editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
  39. new InlineAutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
  40. setData( doc, '<paragraph>*[]</paragraph>' );
  41. doc.enqueueChanges( () => {
  42. batch.insert( doc.selection.getFirstPosition(), ' ' );
  43. } );
  44. sinon.assert.calledOnce( spy );
  45. expect( getData( doc ) ).to.equal( '<paragraph>[]</paragraph>' );
  46. } );
  47. } );
  48. describe( 'Callback', () => {
  49. it( 'should stop when there are no format ranges returned from testCallback', () => {
  50. const formatSpy = testUtils.sinon.spy();
  51. const testStub = testUtils.sinon.stub().returns( {
  52. format: [ [] ],
  53. remove: []
  54. } );
  55. new InlineAutoformatEngine( editor, testStub, formatSpy );
  56. setData( doc, '<paragraph>*[]</paragraph>' );
  57. doc.enqueueChanges( () => {
  58. batch.insert( doc.selection.getFirstPosition(), ' ' );
  59. } );
  60. sinon.assert.notCalled( formatSpy );
  61. } );
  62. it( 'should stop when there are no remove ranges returned from testCallback', () => {
  63. const formatSpy = testUtils.sinon.spy();
  64. const testStub = testUtils.sinon.stub().returns( {
  65. format: [],
  66. remove: [ [] ]
  67. } );
  68. new InlineAutoformatEngine( editor, testStub, formatSpy );
  69. setData( doc, '<paragraph>*[]</paragraph>' );
  70. doc.enqueueChanges( () => {
  71. batch.insert( doc.selection.getFirstPosition(), ' ' );
  72. } );
  73. sinon.assert.notCalled( formatSpy );
  74. } );
  75. it( 'takes text from nested elements', () => {
  76. const formatSpy = testUtils.sinon.spy();
  77. const testStub = testUtils.sinon.stub().returns( {
  78. format: [],
  79. remove: []
  80. } );
  81. new InlineAutoformatEngine( editor, testStub, formatSpy );
  82. setData( doc, '<paragraph><paragraph>foobar[]</paragraph></paragraph>' );
  83. doc.enqueueChanges( () => {
  84. batch.insert( doc.selection.getFirstPosition(), ' ' );
  85. } );
  86. sinon.assert.called( testStub );
  87. sinon.assert.notCalled( formatSpy );
  88. sinon.assert.calledWith( testStub, 'foobar' );
  89. } );
  90. } );
  91. } );
  92. /**
  93. * Dummy command to execute.
  94. */
  95. class TestCommand extends Command {
  96. /**
  97. * Creates an instance of the command.
  98. *
  99. * @param {core.editor.Editor} editor Editor instance.
  100. * @param {Function} onExecuteCallback _doExecute call hook
  101. */
  102. constructor( editor, onExecuteCallback ) {
  103. super( editor );
  104. this.onExecute = onExecuteCallback;
  105. }
  106. /**
  107. * Executes command.
  108. *
  109. * @protected
  110. */
  111. _doExecute() {
  112. this.onExecute();
  113. }
  114. }