/**
* @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
*/
import Autoformat from '../src/autoformat';
import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
import ListEditing from '@ckeditor/ckeditor5-list/src/listediting';
import HeadingEditing from '@ckeditor/ckeditor5-heading/src/headingediting';
import BoldEditing from '@ckeditor/ckeditor5-basic-styles/src/bold/boldediting';
import StrikethroughEditing from '@ckeditor/ckeditor5-basic-styles/src/strikethrough/strikethroughediting';
import CodeEditing from '@ckeditor/ckeditor5-basic-styles/src/code/codeediting';
import ItalicEditing from '@ckeditor/ckeditor5-basic-styles/src/italic/italicediting';
import BlockQuoteEditing from '@ckeditor/ckeditor5-block-quote/src/blockquoteediting';
import CodeBlockEditing from '@ckeditor/ckeditor5-code-block/src/codeblockediting';
import Enter from '@ckeditor/ckeditor5-enter/src/enter';
import ShiftEnter from '@ckeditor/ckeditor5-enter/src/shiftenter';
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 HeadingCommand from '@ckeditor/ckeditor5-heading/src/headingcommand';
describe( 'Autoformat', () => {
let editor, model, doc;
testUtils.createSinonSandbox();
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [
Enter,
Paragraph,
Autoformat,
ListEditing,
HeadingEditing,
BoldEditing,
ItalicEditing,
CodeEditing,
StrikethroughEditing,
BlockQuoteEditing,
CodeBlockEditing,
ShiftEnter
]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
doc = model.document;
} );
} );
afterEach( () => {
return editor.destroy();
} );
describe( 'Bulleted list', () => {
it( 'should replace asterisk with bulleted list item', () => {
setData( model, '*[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should replace minus character with bulleted list item', () => {
setData( model, '-[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should replace a non-empty paragraph using the asterisk', () => {
setData( model, '*[]sample text' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]sample text' );
} );
it( 'should not replace minus character when inside bulleted list item', () => {
setData( model, '-[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '- []' );
} );
it( 'should not replace asterisk character after ', () => {
setData( model, 'Foo*[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( 'Foo* []' );
} );
} );
describe( 'Numbered list', () => {
it( 'should replace digit with numbered list item using the dot format', () => {
setData( model, '1.[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should replace digit with numbered list item using the parenthesis format', () => {
setData( model, '1)[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should replace a non-empty paragraph using the parenthesis format', () => {
setData( model, '1)[]sample text' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]sample text' );
} );
it( 'should not replace digit character when there is no . or ) in the format', () => {
setData( model, '1[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '1 []' );
} );
it( 'should not replace digit character when inside numbered list item', () => {
setData( model, '1.[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '1. []' );
} );
it( 'should not replace digit with numbered list item when digit is different than "1"', () => {
setData( model, '3.[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '3. []' );
} );
it( 'should not replace digit character after ', () => {
setData( model, 'Foo1.[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( 'Foo1. []' );
} );
it( 'should be converted from a header', () => {
setData( model, '1.[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should be converted from a bulleted list', () => {
setData( model, '1.[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
} );
describe( 'Heading', () => {
it( 'should replace hash character with heading', () => {
setData( model, '#[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should replace two hash characters with heading level 2', () => {
setData( model, '##[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should not replace hash character when inside heading', () => {
setData( model, '#[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '# []' );
} );
it( 'should work with heading1-heading6 commands regardless of the config of the heading feature', () => {
const command = new HeadingCommand( editor, [ 'heading1', 'heading6' ] );
const spy = sinon.spy( command, 'execute' );
function HeadingPlugin( editor ) {
editor.commands.add( 'heading', command );
command.refresh();
}
return VirtualTestEditor
.create( {
plugins: [
Paragraph, Autoformat, HeadingPlugin
]
} )
.then( editor => {
const model = editor.model;
const doc = model.document;
setData( model, '#[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
sinon.assert.calledOnce( spy );
sinon.assert.calledWithExactly( spy, { value: 'heading1' } );
spy.resetHistory();
setData( model, '######[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
sinon.assert.calledOnce( spy );
sinon.assert.calledWithExactly( spy, { value: 'heading6' } );
return editor.destroy();
} );
} );
it( 'should not replace if heading command is disabled', () => {
setData( model, '#[]' );
model.change( writer => {
editor.commands.get( 'heading' ).refresh = () => {};
editor.commands.get( 'heading' ).isEnabled = false;
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '# []' );
} );
it( 'should not replace hash character after ', () => {
setData( model, 'Foo#[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( 'Foo# []' );
} );
it( 'should convert a header that already contains a text', () => {
setData( model, '###[]foo' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]foo' );
} );
} );
describe( 'Block quote', () => {
it( 'should replace greater-than character with block quote', () => {
setData( model, '>[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]
' );
} );
it( 'should replace greater-than character in a non-empty paragraph', () => {
setData( model, '>[]foo' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]foo
' );
} );
it( 'should wrap the heading if greater-than character was used', () => {
setData( model, '>[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]
' );
} );
it( 'should not replace greater-than character when inside numbered list', () => {
setData( model, '1. >[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '1. > []' );
} );
it( 'should not replace greater-than character when inside buletted list', () => {
setData( model, '1. >[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '1. > []' );
} );
it( 'should not replace greater-than character after ', () => {
setData( model, 'Foo>[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( 'Foo> []' );
} );
} );
describe( 'Code block', () => {
it( 'should replace triple grave accents with a code block', () => {
setData( model, '``[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should replace triple grave accents in a heading', () => {
setData( model, '``[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]' );
} );
it( 'should replace triple grave accents in a non-empty paragraph', () => {
setData( model, '``[]let foo = 1;' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '[]let foo = 1;' );
} );
it( 'should not replace triple grave accents when already in a code block', () => {
setData( model, '``[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '```[]' );
} );
it( 'should not replace triple grave accents when inside numbered list', () => {
setData( model, '1. ``[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '1. ```[]' );
} );
it( 'should not replace triple grave accents when inside buletted list', () => {
setData( model, '1. ``[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '1. ```[]' );
} );
} );
describe( 'Inline autoformat', () => {
it( 'should replace both "**" with bold', () => {
setData( model, '**foobar*[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '<$text bold="true">foobar$text>[]' );
} );
it( 'should replace both "*" with italic', () => {
setData( model, '*foobar[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '<$text italic="true">foobar$text>[]' );
} );
it( 'should replace both "`" with code', () => {
setData( model, '`foobar[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '<$text code="true">foobar$text>[]' );
} );
it( 'should replace both "~~" with strikethrough', () => {
setData( model, '~~foobar~[]' );
model.change( writer => {
writer.insertText( '~', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '<$text strikethrough="true">foobar$text>[]' );
} );
it( 'nothing should be replaces when typing "*"', () => {
setData( model, 'foobar[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( 'foobar*[]' );
} );
it( 'should format inside the text', () => {
setData( model, 'foo **bar*[] baz' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( 'foo <$text bold="true">bar$text>[] baz' );
} );
it( 'should not format if the command is not enabled', () => {
model.schema.addAttributeCheck( ( context, attributeName ) => {
if ( attributeName == 'bold' ) {
return false;
}
} );
setData( model, '**foobar*[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '**foobar**[]' );
} );
it( 'should not format if the plugin is disabled', () => {
editor.plugins.get( 'Autoformat' ).forceDisabled( 'Test' );
setData( model, '**foobar*[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '**foobar**[]' );
} );
describe( 'with code element', () => {
describe( 'should not format (inside)', () => {
it( '* inside', () => {
setData( model, '<$text code="true">fo*obar[]$text>' );
model.change( writer => {
writer.insertText( '*', { code: true }, doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo*obar*[]$text>' );
} );
it( '__ inside', () => {
setData( model, '<$text code="true">fo__obar_[]$text>' );
model.change( writer => {
writer.insertText( '_', { code: true }, doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo__obar__[]$text>' );
} );
it( '~~ inside', () => {
setData( model, '<$text code="true">fo~~obar~[]$text>' );
model.change( writer => {
writer.insertText( '~', { code: true }, doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo~~obar~~[]$text>' );
} );
it( '` inside', () => {
setData( model, '<$text code="true">fo`obar[]$text>' );
model.change( writer => {
writer.insertText( '`', { code: true }, doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo`obar`[]$text>' );
} );
} );
describe( 'should not format (across)', () => {
it( '* across', () => {
setData( model, '<$text code="true">fo*o$text>bar[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo*o$text>bar*[]' );
} );
it( '__ across', () => {
setData( model, '<$text code="true">fo__o$text>bar_[]' );
model.change( writer => {
writer.insertText( '_', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo__o$text>bar__[]' );
} );
it( '~~ across', () => {
setData( model, '<$text code="true">fo~~o$text>bar~[]' );
model.change( writer => {
writer.insertText( '~', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo~~o$text>bar~~[]' );
} );
it( '` across', () => {
setData( model, '<$text code="true">fo`o$text>bar[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo`o$text>bar`[]' );
} );
} );
describe( 'should format', () => {
it( '* after', () => {
setData( model, '<$text code="true">fo*o$text>b*ar[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo*o$text>b<$text italic="true">ar$text>[]' );
} );
it( '__ after', () => {
setData( model, '<$text code="true">fo__o$text>b__ar_[]' );
model.change( writer => {
writer.insertText( '_', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo__o$text>b<$text bold="true">ar$text>[]' );
} );
it( '~~ after', () => {
setData( model, '<$text code="true">fo~~o$text>b~~ar~[]' );
model.change( writer => {
writer.insertText( '~', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo~~o$text>b<$text strikethrough="true">ar$text>[]' );
} );
it( '` after', () => {
setData( model, '<$text code="true">fo`o$text>b`ar[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to
.equal( '<$text code="true">fo`o$text>b<$text code="true">ar$text>[]' );
} );
} );
} );
it( 'should work with s in paragraph', () => {
setData( model, 'foo**barbaz*[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( 'foo<$text bold="true">barbaz$text>[]' );
} );
} );
describe( 'without commands', () => {
beforeEach( () => {
return VirtualTestEditor
.create( {
plugins: [ Enter, Paragraph, Autoformat ]
} )
.then( newEditor => {
editor = newEditor;
model = editor.model;
doc = model.document;
} );
} );
it( 'should not replace asterisk with bulleted list item', () => {
setData( model, '*[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '* []' );
} );
it( 'should not replace minus character with bulleted list item', () => {
setData( model, '-[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '- []' );
} );
it( 'should not replace digit with numbered list item', () => {
setData( model, '1.[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '1. []' );
} );
it( 'should not replace hash character with heading', () => {
setData( model, '#[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '# []' );
} );
it( 'should not replace two hash characters with heading level 2', () => {
setData( model, '##[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '## []' );
} );
it( 'should not replace both "**" with bold', () => {
setData( model, '**foobar*[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '**foobar**[]' );
} );
it( 'should not replace both "*" with italic', () => {
setData( model, '*foobar[]' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '*foobar*[]' );
} );
it( 'should not replace both "`" with code', () => {
setData( model, '`foobar[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '`foobar`[]' );
} );
it( 'should not replace ">" with block quote', () => {
setData( model, '>[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '> []' );
} );
it( 'should not replace "```" with code block', () => {
setData( model, '``[]' );
model.change( writer => {
writer.insertText( '`', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '```[]' );
} );
it( 'should use only configured headings', () => {
return VirtualTestEditor
.create( {
plugins: [ Enter, Paragraph, Autoformat, ListEditing, HeadingEditing ],
heading: {
options: [
{ model: 'paragraph' },
{ model: 'heading1', view: 'h2' }
]
}
} )
.then( editor => {
model = editor.model;
doc = model.document;
setData( model, '##[]' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );
expect( getData( model ) ).to.equal( '## []' );
} );
} );
} );
} );