/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import Autoformat from '../src/autoformat';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import ListEngine from '@ckeditor/ckeditor5-list/src/listengine';
import HeadingEngine from '@ckeditor/ckeditor5-heading/src/headingengine';
import BoldEngine from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
import CodeEngine from '@ckeditor/ckeditor5-basic-styles/src/codeengine';
import ItalicEngine from '@ckeditor/ckeditor5-basic-styles/src/italicengine';
import BlockQuoteEngine from '@ckeditor/ckeditor5-block-quote/src/blockquoteengine';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
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( 'Autoformat', () => {
let editor, doc, batch;
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [
Enter,
Paragraph,
Autoformat,
ListEngine,
HeadingEngine,
BoldEngine,
ItalicEngine,
CodeEngine,
BlockQuoteEngine
]
} )
.then( newEditor => {
editor = newEditor;
doc = editor.document;
batch = doc.batch();
} );
} );
afterEach( () => {
return editor.destroy();
} );
describe( 'Bulleted list', () => {
it( 'should replace asterisk with bulleted list item', () => {
setData( doc, '*[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should replace minus character with bulleted list item', () => {
setData( doc, '-[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should not replace minus character when inside bulleted list item', () => {
setData( doc, '-[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '- []' );
} );
} );
describe( 'Numbered list', () => {
it( 'should replace digit with numbered list item using the dot format', () => {
setData( doc, '1.[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should replace digit with numbered list item using the parenthesis format', () => {
setData( doc, '1)[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should not replace digit character when there is no . or ) in the format', () => {
setData( doc, '1[]' );
doc.enqueueChanges( () => {
batch.insert( doc.selection.getFirstPosition(), ' ' );
} );
expect( getData( doc ) ).to.equal( '1 []' );
} );
it( 'should not replace digit character when inside numbered list item', () => {
setData( doc, '1.[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '1. []' );
} );
} );
describe( 'Heading', () => {
it( 'should replace hash character with heading', () => {
setData( doc, '#[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should replace two hash characters with heading level 2', () => {
setData( doc, '##[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '[]' );
} );
it( 'should not replace hash character when inside heading', () => {
setData( doc, '#[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '# []' );
} );
it( 'should work with heading1-heading6 commands regardless of the config of the heading feature', () => {
const spy1 = sinon.spy();
const spy6 = sinon.spy();
class Heading6 extends Command {
execute() {
spy6();
}
}
class Heading1 extends Command {
execute() {
spy1();
}
}
function HeadingPlugin( editor ) {
editor.commands.add( 'heading1', new Heading1( editor ) );
editor.commands.add( 'heading6', new Heading6( editor ) );
}
return VirtualTestEditor
.create( {
plugins: [
Paragraph, Autoformat, HeadingPlugin
]
} )
.then( editor => {
const doc = editor.document;
setData( doc, '#[]' );
doc.enqueueChanges( () => {
doc.batch().insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( spy1.calledOnce ).to.be.true;
setData( doc, '######[]' );
doc.enqueueChanges( () => {
doc.batch().insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( spy6.calledOnce ).to.be.true;
return editor.destroy();
} );
} );
} );
describe( 'Block quote', () => {
it( 'should replace greater-than character with heading', () => {
setData( doc, '>[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '
[]
' );
} );
it( 'should not replace greater-than character when inside heading', () => {
setData( doc, '>[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '> []' );
} );
it( 'should not replace greater-than character when inside numbered list', () => {
setData( doc, '1. >[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '1. > []' );
} );
it( 'should not replace greater-than character when inside buletted list', () => {
setData( doc, '1. >[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '1. > []' );
} );
} );
describe( 'Inline autoformat', () => {
it( 'should replace both "**" with bold', () => {
setData( doc, '**foobar*[]' );
doc.enqueueChanges( () => {
batch.insertText( '*', 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.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '<$text italic="true">foobar$text>[]' );
} );
it( 'should replace both "`" with code', () => {
setData( doc, '`foobar[]' );
doc.enqueueChanges( () => {
batch.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '<$text code="true">foobar$text>[]' );
} );
it( 'nothing should be replaces when typing "*"', () => {
setData( doc, 'foobar[]' );
doc.enqueueChanges( () => {
batch.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( 'foobar*[]' );
} );
it( 'should format inside the text', () => {
setData( doc, 'foo **bar*[] baz' );
doc.enqueueChanges( () => {
batch.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( 'foo <$text bold="true">bar$text>[] baz' );
} );
} );
describe( 'without commands', () => {
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ Enter, Paragraph, Autoformat ]
} )
.then( newEditor => {
editor = newEditor;
doc = editor.document;
batch = doc.batch();
} );
} );
it( 'should not replace asterisk with bulleted list item', () => {
setData( doc, '*[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '* []' );
} );
it( 'should not replace minus character with bulleted list item', () => {
setData( doc, '-[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '- []' );
} );
it( 'should not replace digit with numbered list item', () => {
setData( doc, '1.[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '1. []' );
} );
it( 'should not replace hash character with heading', () => {
setData( doc, '#[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '# []' );
} );
it( 'should not replace two hash characters with heading level 2', () => {
setData( doc, '##[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '## []' );
} );
it( 'should not replace both "**" with bold', () => {
setData( doc, '**foobar*[]' );
doc.enqueueChanges( () => {
batch.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '**foobar**[]' );
} );
it( 'should not replace both "*" with italic', () => {
setData( doc, '*foobar[]' );
doc.enqueueChanges( () => {
batch.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '*foobar*[]' );
} );
it( 'should not replace both "`" with code', () => {
setData( doc, '`foobar[]' );
doc.enqueueChanges( () => {
batch.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '`foobar`[]' );
} );
it( 'should not replace ">" with block quote', () => {
setData( doc, '>[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '> []' );
} );
it( 'should use only configured headings', () => {
return VirtualTestEditor
.create( {
plugins: [ Enter, Paragraph, Autoformat, ListEngine, HeadingEngine ],
heading: {
options: [
{ modelElement: 'paragraph' },
{ modelElement: 'heading1', viewElement: 'h2' }
]
}
} )
.then( editor => {
doc = editor.document;
batch = doc.batch();
setData( doc, '##[]' );
doc.enqueueChanges( () => {
batch.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( doc ) ).to.equal( '## []' );
} );
} );
} );
} );