/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import Autoformat from '/ckeditor5/autoformat/autoformat.js';
import Paragraph from '/ckeditor5/paragraph/paragraph.js';
import VirtualTestEditor from '/tests/core/_utils/virtualtesteditor.js';
import Enter from '/ckeditor5/enter/enter.js';
import { setData, getData } from '/ckeditor5/engine/dev-utils/model.js';
import testUtils from '/tests/core/_utils/utils.js';
testUtils.createSinonSandbox();
describe( 'Autoformat', () => {
let editor, doc, batch;
beforeEach( () => {
return VirtualTestEditor.create( {
features: [ 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*[]' );
} );
} );
} );