| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- /**
- * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md.
- */
- import BlockAutoformatEngine from '../src/blockautoformatengine';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import Enter from '@ckeditor/ckeditor5-enter/src/enter';
- import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- import Command from '@ckeditor/ckeditor5-core/src/command';
- testUtils.createSinonSandbox();
- describe( 'BlockAutoformatEngine', () => {
- let editor, model, doc;
- beforeEach( () => {
- return VirtualTestEditor
- .create( {
- plugins: [ Enter, Paragraph ]
- } )
- .then( newEditor => {
- editor = newEditor;
- model = editor.model;
- doc = model.document;
- } );
- } );
- describe( 'Command name', () => {
- it( 'should run a command when the pattern is matched', () => {
- const spy = testUtils.sinon.spy();
- editor.commands.add( 'testCommand', new TestCommand( editor, spy ) );
- new BlockAutoformatEngine( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
- setData( model, '<paragraph>*[]</paragraph>' );
- model.change( writer => {
- writer.insertText( ' ', doc.selection.getFirstPosition() );
- } );
- sinon.assert.calledOnce( spy );
- } );
- it( 'should not run a command when changes are in transparent batch', () => {
- const spy = testUtils.sinon.spy();
- editor.commands.add( 'testCommand', new TestCommand( editor, spy ) );
- new BlockAutoformatEngine( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
- setData( model, '<paragraph>*[]</paragraph>' );
- model.enqueueChange( 'transparent', writer => {
- writer.insertText( ' ', doc.selection.getFirstPosition() );
- } );
- sinon.assert.notCalled( spy );
- } );
- it( 'should remove found pattern', () => {
- const spy = testUtils.sinon.spy();
- editor.commands.add( 'testCommand', new TestCommand( editor, spy ) );
- new BlockAutoformatEngine( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
- setData( model, '<paragraph>*[]</paragraph>' );
- model.change( writer => {
- writer.insertText( ' ', doc.selection.getFirstPosition() );
- } );
- sinon.assert.calledOnce( spy );
- expect( getData( model ) ).to.equal( '<paragraph>[]</paragraph>' );
- } );
- } );
- describe( 'Callback', () => {
- it( 'should run callback when the pattern is matched', () => {
- const spy = testUtils.sinon.spy();
- new BlockAutoformatEngine( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
- setData( model, '<paragraph>*[]</paragraph>' );
- model.change( writer => {
- writer.insertText( ' ', doc.selection.getFirstPosition() );
- } );
- sinon.assert.calledOnce( spy );
- } );
- it( 'should not run a callback when changes are in transparent batch', () => {
- const spy = testUtils.sinon.spy();
- new BlockAutoformatEngine( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
- setData( model, '<paragraph>*[]</paragraph>' );
- model.enqueueChange( 'transparent', writer => {
- writer.insertText( ' ', doc.selection.getFirstPosition() );
- } );
- sinon.assert.notCalled( spy );
- } );
- it( 'should ignore other delta operations', () => {
- const spy = testUtils.sinon.spy();
- new BlockAutoformatEngine( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
- setData( model, '<paragraph>*[]</paragraph>' );
- model.change( writer => {
- writer.remove( doc.selection.getFirstRange() );
- } );
- sinon.assert.notCalled( spy );
- } );
- it( 'should stop if there is no text to run matching on', () => {
- const spy = testUtils.sinon.spy();
- new BlockAutoformatEngine( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
- setData( model, '<paragraph>[]</paragraph>' );
- model.change( writer => {
- writer.insertText( ' ', doc.selection.getFirstPosition() );
- } );
- sinon.assert.notCalled( spy );
- } );
- } );
- } );
- /**
- * Dummy command to execute.
- */
- class TestCommand extends Command {
- /**
- * Creates an instance of the command.
- *
- * @param {module:core/editor/editor~Editor} editor Editor instance.
- * @param {Function} onExecuteCallback execute call hook
- */
- constructor( editor, onExecuteCallback ) {
- super( editor );
- this.onExecute = onExecuteCallback;
- }
- /**
- * Executes command.
- *
- * @protected
- */
- execute() {
- this.onExecute();
- }
- }
|