Skip to content

Commit

Permalink
Merge pull request #8619 from ckeditor/i/2388
Browse files Browse the repository at this point in the history
Fix (autoformat): Formatting will not be applied to `snake_case_scenarios` anymore. Closes #2388.
  • Loading branch information
Reinmar authored Dec 11, 2020
2 parents 0ad1d6a + e3d5ed9 commit 82d486d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
8 changes: 4 additions & 4 deletions packages/ckeditor5-autoformat/src/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,17 @@ export default class Autoformat extends Plugin {
if ( commands.get( 'bold' ) ) {
const boldCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'bold' );

inlineAutoformatEditing( this.editor, this, /(\*\*)([^*]+)(\*\*)$/g, boldCallback );
inlineAutoformatEditing( this.editor, this, /(__)([^_]+)(__)$/g, boldCallback );
inlineAutoformatEditing( this.editor, this, /(?:^|\s)(\*\*)([^*]+)(\*\*)$/g, boldCallback );
inlineAutoformatEditing( this.editor, this, /(?:^|\s)(__)([^_]+)(__)$/g, boldCallback );
}

if ( commands.get( 'italic' ) ) {
const italicCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'italic' );

// The italic autoformatter cannot be triggered by the bold markers, so we need to check the
// text before the pattern (e.g. `(?:^|[^\*])`).
inlineAutoformatEditing( this.editor, this, /(?:^|[^*])(\*)([^*_]+)(\*)$/g, italicCallback );
inlineAutoformatEditing( this.editor, this, /(?:^|[^_])(_)([^_]+)(_)$/g, italicCallback );
inlineAutoformatEditing( this.editor, this, /(?:^|\s)(\*)([^*_]+)(\*)$/g, italicCallback );
inlineAutoformatEditing( this.editor, this, /(?:^|\s)(_)([^_]+)(_)$/g, italicCallback );
}

if ( commands.get( 'code' ) ) {
Expand Down
60 changes: 48 additions & 12 deletions packages/ckeditor5-autoformat/tests/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -485,28 +485,64 @@ describe( 'Autoformat', () => {
expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
} );

describe( 'should not format', () => {
it( '* without space preceding it', () => {
setData( model, '<paragraph>fo*ob*ar[]</paragraph>' );

model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph>fo*ob*ar*[]</paragraph>' );
} );

it( '__ without space preceding it', () => {
setData( model, '<paragraph>fo__ob__ar_[]</paragraph>' );

model.change( writer => {
writer.insertText( '_', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph>fo__ob__ar__[]</paragraph>' );
} );

// https://github.com/ckeditor/ckeditor5/issues/2388
it( 'snake_case sentences', () => {
setData( model, '<paragraph>foo_bar baz[]</paragraph>' );

model.change( writer => {
writer.insertText( '_', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph>foo_bar baz_[]</paragraph>' );
} );
} );

describe( 'with code element', () => {
describe( 'should not format (inside)', () => {
it( '* inside', () => {
setData( model, '<paragraph><$text code="true">fo*obar[]</$text></paragraph>' );
setData( model, '<paragraph><$text code="true">fo *obar[]</$text></paragraph>' );

model.change( writer => {
writer.insertText( '*', { code: true }, doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph><$text code="true">fo*obar*[]</$text></paragraph>' );
.equal( '<paragraph><$text code="true">fo *obar*[]</$text></paragraph>' );
} );

it( '__ inside', () => {
setData( model, '<paragraph><$text code="true">fo__obar_[]</$text></paragraph>' );
setData( model, '<paragraph><$text code="true">fo __obar_[]</$text></paragraph>' );

model.change( writer => {
writer.insertText( '_', { code: true }, doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph><$text code="true">fo__obar__[]</$text></paragraph>' );
.equal( '<paragraph><$text code="true">fo __obar__[]</$text></paragraph>' );
} );

it( '~~ inside', () => {
Expand Down Expand Up @@ -534,24 +570,24 @@ describe( 'Autoformat', () => {

describe( 'should not format (across)', () => {
it( '* across', () => {
setData( model, '<paragraph><$text code="true">fo*o</$text>bar[]</paragraph>' );
setData( model, '<paragraph><$text code="true">fo *o</$text>bar[]</paragraph>' );

model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph><$text code="true">fo*o</$text>bar*[]</paragraph>' );
.equal( '<paragraph><$text code="true">fo *o</$text>bar*[]</paragraph>' );
} );
it( '__ across', () => {
setData( model, '<paragraph><$text code="true">fo__o</$text>bar_[]</paragraph>' );
setData( model, '<paragraph><$text code="true">fo __o</$text>bar_[]</paragraph>' );

model.change( writer => {
writer.insertText( '_', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph><$text code="true">fo__o</$text>bar__[]</paragraph>' );
.equal( '<paragraph><$text code="true">fo __o</$text>bar__[]</paragraph>' );
} );
it( '~~ across', () => {
setData( model, '<paragraph><$text code="true">fo~~o</$text>bar~[]</paragraph>' );
Expand All @@ -577,24 +613,24 @@ describe( 'Autoformat', () => {

describe( 'should format', () => {
it( '* after', () => {
setData( model, '<paragraph><$text code="true">fo*o</$text>b*ar[]</paragraph>' );
setData( model, '<paragraph><$text code="true">fo*o</$text>b *ar[]</paragraph>' );

model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph><$text code="true">fo*o</$text>b<$text italic="true">ar</$text>[]</paragraph>' );
.equal( '<paragraph><$text code="true">fo*o</$text>b <$text italic="true">ar</$text>[]</paragraph>' );
} );
it( '__ after', () => {
setData( model, '<paragraph><$text code="true">fo__o</$text>b__ar_[]</paragraph>' );
setData( model, '<paragraph><$text code="true">fo__o</$text>b __ar_[]</paragraph>' );

model.change( writer => {
writer.insertText( '_', doc.selection.getFirstPosition() );
} );

expect( getData( model ) ).to
.equal( '<paragraph><$text code="true">fo__o</$text>b<$text bold="true">ar</$text>[]</paragraph>' );
.equal( '<paragraph><$text code="true">fo__o</$text>b <$text bold="true">ar</$text>[]</paragraph>' );
} );
it( '~~ after', () => {
setData( model, '<paragraph><$text code="true">fo~~o</$text>b~~ar~[]</paragraph>' );
Expand Down

0 comments on commit 82d486d

Please sign in to comment.