/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import Autoformat from 'ckeditor5-autoformat/src/autoformat';
import Paragraph from 'ckeditor5-paragraph/src/paragraph';
import VirtualTestEditor from 'ckeditor5-core/tests/_utils/virtualtesteditor';
import Enter from 'ckeditor5-enter/src/enter';
import { setData, getData } from 'ckeditor5-engine/src/dev-utils/model';
import testUtils from 'ckeditor5-core/tests/_utils/utils';
testUtils.createSinonSandbox();
describe( 'Autoformat', () => {
let editor, doc, batch;
beforeEach( () => {
return VirtualTestEditor.create( {
plugins: [ Enter, Paragraph, Autoformat ]
} )
.then( newEditor => {
editor = newEditor;
doc = editor.document;
batch = doc.batch();
} );
} );
describe( 'Bulleted list', () => {
it( 'should replace asterisk with bulleted list item', () => {
setData( doc, '*[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should replace minus character with bulleted list item', () => {
setData( doc, '-[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should not replace minus character when inside bulleted list item', () => {
setData( doc, '-[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '- []' );
} );
} );
describe( 'Numbered list', () => {
it( 'should replace digit with numbered list item', () => {
setData( doc, '1.[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should not replace digit character when inside numbered list item', () => {
setData( doc, '1.[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '1. []' );
} );
} );
describe( 'Heading', () => {
it( 'should replace hash character with heading', () => {
setData( doc, '#[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should replace two hash characters with heading level 2', () => {
setData( doc, '##[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should not replace minus character when inside heading', () => {
setData( doc, '#[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '# []' );
} );
} );
describe( 'Inline autoformat', () => {
it( 'should replace both `**` with bold', () => {
setData( doc, '**foobar*[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
} );
expect( getData( doc ) ).to.equal( '<$text bold="true">foobar$text>[]' );
} );
it( 'should replace both `*` with italic', () => {
setData( doc, '*foobar[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
} );
expect( getData( doc ) ).to.equal( '<$text italic="true">foobar$text>[]' );
} );
it( 'nothing should be replaces when typing `*`', () => {
setData( doc, 'foobar[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
} );
expect( getData( doc ) ).to.equal( 'foobar*[]' );
} );
it( 'should format inside the text', () => {
setData( doc, 'foo **bar*[] baz' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), '*' );
} );
expect( getData( doc ) ).to.equal( 'foo <$text bold="true">bar$text>[] baz' );
} );
} );
} );