Skip to content

Commit

Permalink
Do not format if the plugin is disabled.
Browse files Browse the repository at this point in the history
Implements 1. from #1239 (comment).
  • Loading branch information
tomalec committed May 27, 2020
1 parent 5c76f05 commit 72a5de2
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 35 deletions.
22 changes: 11 additions & 11 deletions packages/ckeditor5-autoformat/src/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ export default class Autoformat extends Plugin {

if ( commands.get( 'bulletedList' ) ) {
// eslint-disable-next-line no-new
new BlockAutoformatEditing( this.editor, /^[*-]\s$/, 'bulletedList' );
new BlockAutoformatEditing( this.editor, this, /^[*-]\s$/, 'bulletedList' );
}

if ( commands.get( 'numberedList' ) ) {
// eslint-disable-next-line no-new
new BlockAutoformatEditing( this.editor, /^1[.|)]\s$/, 'numberedList' );
new BlockAutoformatEditing( this.editor, this, /^1[.|)]\s$/, 'numberedList' );
}
}

Expand All @@ -83,8 +83,8 @@ export default class Autoformat extends Plugin {
/* eslint-disable no-new */
const boldCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'bold' );

new InlineAutoformatEditing( this.editor, /(\*\*)([^*]+)(\*\*)$/g, boldCallback );
new InlineAutoformatEditing( this.editor, /(__)([^_]+)(__)$/g, boldCallback );
new InlineAutoformatEditing( this.editor, this, /(\*\*)([^*]+)(\*\*)$/g, boldCallback );
new InlineAutoformatEditing( this.editor, this, /(__)([^_]+)(__)$/g, boldCallback );
/* eslint-enable no-new */
}

Expand All @@ -94,24 +94,24 @@ export default class Autoformat extends Plugin {

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

if ( commands.get( 'code' ) ) {
/* eslint-disable no-new */
const codeCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'code' );

new InlineAutoformatEditing( this.editor, /(`)([^`]+)(`)$/g, codeCallback );
new InlineAutoformatEditing( this.editor, this, /(`)([^`]+)(`)$/g, codeCallback );
/* eslint-enable no-new */
}

if ( commands.get( 'strikethrough' ) ) {
/* eslint-disable no-new */
const strikethroughCallback = getCallbackFunctionForInlineAutoformat( this.editor, 'strikethrough' );

new InlineAutoformatEditing( this.editor, /(~~)([^~]+)(~~)$/g, strikethroughCallback );
new InlineAutoformatEditing( this.editor, this, /(~~)([^~]+)(~~)$/g, strikethroughCallback );
/* eslint-enable no-new */
}
}
Expand All @@ -138,7 +138,7 @@ export default class Autoformat extends Plugin {
const pattern = new RegExp( `^(#{${ level }})\\s$` );

// eslint-disable-next-line no-new
new BlockAutoformatEditing( this.editor, pattern, () => {
new BlockAutoformatEditing( this.editor, this, pattern, () => {
if ( !command.isEnabled ) {
return false;
}
Expand All @@ -160,7 +160,7 @@ export default class Autoformat extends Plugin {
_addBlockQuoteAutoformats() {
if ( this.editor.commands.get( 'blockQuote' ) ) {
// eslint-disable-next-line no-new
new BlockAutoformatEditing( this.editor, /^>\s$/, 'blockQuote' );
new BlockAutoformatEditing( this.editor, this, /^>\s$/, 'blockQuote' );
}
}

Expand All @@ -175,7 +175,7 @@ export default class Autoformat extends Plugin {
_addCodeBlockAutoformats() {
if ( this.editor.commands.get( 'codeBlock' ) ) {
// eslint-disable-next-line no-new
new BlockAutoformatEditing( this.editor, /^```$/, 'codeBlock' );
new BlockAutoformatEditing( this.editor, this, /^```$/, 'codeBlock' );
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions packages/ckeditor5-autoformat/src/blockautoformatediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,13 @@ export default class BlockAutoformatEditing {
* } );
*
* @param {module:core/editor/editor~Editor} editor The editor instance.
* @param {module:autoformat/autoformat~Autoformat} plugin The autoformat plugin instance.
* @param {RegExp} pattern The regular expression to execute on just inserted text.
* @param {Function|String} callbackOrCommand The callback to execute or the command to run when the text is matched.
* In case of providing the callback, it receives the following parameter:
* * {Object} match RegExp.exec() result of matching the pattern to inserted text.
*/
constructor( editor, pattern, callbackOrCommand ) {
constructor( editor, plugin, pattern, callbackOrCommand ) {
let callback;
let command = null;

Expand All @@ -65,7 +66,7 @@ export default class BlockAutoformatEditing {
}

editor.model.document.on( 'change', ( evt, batch ) => {
if ( command && !command.isEnabled ) {
if ( command && !command.isEnabled || !plugin.isEnabled ) {
return;
}

Expand Down
5 changes: 3 additions & 2 deletions packages/ckeditor5-autoformat/src/inlineautoformatediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export default class InlineAutoformatEditing {
* and executes the provided action if the text matches given criteria (regular expression or callback).
*
* @param {module:core/editor/editor~Editor} editor The editor instance.
* @param {module:autoformat/autoformat~Autoformat} plugin The autoformat plugin instance.
* @param {Function|RegExp} testRegexpOrCallback The regular expression or callback to execute on text.
* Provided regular expression *must* have three capture groups. The first and the third capture group
* should match opening and closing delimiters. The second capture group should match the text to format.
Expand Down Expand Up @@ -74,7 +75,7 @@ export default class InlineAutoformatEditing {
* }
* } );
*/
constructor( editor, testRegexpOrCallback, attributeOrCallback ) {
constructor( editor, plugin, testRegexpOrCallback, attributeOrCallback ) {
let regExp;
let attributeKey;
let testCallback;
Expand Down Expand Up @@ -151,7 +152,7 @@ export default class InlineAutoformatEditing {
} );

editor.model.document.on( 'change', ( evt, batch ) => {
if ( batch.type == 'transparent' ) {
if ( batch.type == 'transparent' || !plugin.isEnabled ) {
return;
}

Expand Down
12 changes: 12 additions & 0 deletions packages/ckeditor5-autoformat/tests/autoformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,18 @@ describe( 'Autoformat', () => {
expect( getData( model ) ).to.equal( '<paragraph>**foobar**[]</paragraph>' );
} );

it( 'should not format if the plugin is not enabled', () => {
editor.plugins.get( 'Autoformat' ).forceDisabled( 'Test' );

setData( model, '<paragraph>**foobar*[]</paragraph>' );

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

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

it( 'should work with <softBreak>s in paragraph', () => {
setData( model, '<paragraph>foo<softBreak></softBreak>**barbaz*[]</paragraph>' );
model.change( writer => {
Expand Down
37 changes: 26 additions & 11 deletions packages/ckeditor5-autoformat/tests/blockautoformatediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
import Command from '@ckeditor/ckeditor5-core/src/command';

describe( 'BlockAutoformatEditing', () => {
let editor, model, doc;
let editor, model, doc, plugin;

testUtils.createSinonSandbox();

Expand All @@ -26,6 +26,7 @@ describe( 'BlockAutoformatEditing', () => {
editor = newEditor;
model = editor.model;
doc = model.document;
plugin = editor.plugins.get( 'Autoformat' );
} );
} );

Expand All @@ -36,7 +37,7 @@ describe( 'BlockAutoformatEditing', () => {

editor.commands.add( 'testCommand', testCommand );

new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );

Expand All @@ -53,7 +54,7 @@ describe( 'BlockAutoformatEditing', () => {

editor.commands.add( 'testCommand', testCommand );

new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );

Expand All @@ -75,7 +76,7 @@ describe( 'BlockAutoformatEditing', () => {

editor.commands.add( 'testCommand', testCommand );

new BlockAutoformatEditing( editor, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, 'testCommand' ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );

Expand All @@ -90,7 +91,7 @@ describe( 'BlockAutoformatEditing', () => {
describe( 'callback', () => {
it( 'should run callback when the pattern is matched', () => {
const spy = testUtils.sinon.spy();
new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, spy ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );
model.change( writer => {
Expand All @@ -100,9 +101,23 @@ describe( 'BlockAutoformatEditing', () => {
sinon.assert.calledOnce( spy );
} );

it( 'should not call callback when the pattern is matched and plugin is disabled', () => {
const callbackSpy = testUtils.sinon.spy().named( 'callback' );
new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, callbackSpy ); // eslint-disable-line no-new

plugin.isEnabled = false;

setData( model, '<paragraph>*[]</paragraph>' );
model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
} );

sinon.assert.notCalled( callbackSpy );
} );

it( 'should ignore other delta operations', () => {
const spy = testUtils.sinon.spy();
new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s/, spy ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );
model.change( writer => {
Expand All @@ -114,7 +129,7 @@ describe( 'BlockAutoformatEditing', () => {

it( 'should stop if there is no text to run matching on', () => {
const spy = testUtils.sinon.spy();
new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s/, spy ); // eslint-disable-line no-new

setData( model, '<paragraph>[]</paragraph>' );
model.change( writer => {
Expand Down Expand Up @@ -142,7 +157,7 @@ describe( 'BlockAutoformatEditing', () => {
} );

const spy = testUtils.sinon.spy();
new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s/, spy ); // eslint-disable-line no-new

setData( model, '<paragraph>*<softBreak></softBreak>[]</paragraph>' );
model.change( writer => {
Expand Down Expand Up @@ -170,7 +185,7 @@ describe( 'BlockAutoformatEditing', () => {
} );

const spy = testUtils.sinon.spy();
new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s/, spy ); // eslint-disable-line no-new

setData( model, '<paragraph>* <softBreak></softBreak>[]</paragraph>' );

Expand All @@ -182,7 +197,7 @@ describe( 'BlockAutoformatEditing', () => {
} );

it( 'should stop if callback returned false', () => {
new BlockAutoformatEditing( editor, /^[*]\s$/, () => false ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, () => false ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );
model.change( writer => {
Expand All @@ -195,7 +210,7 @@ describe( 'BlockAutoformatEditing', () => {

it( 'should ignore transparent batches', () => {
const spy = testUtils.sinon.spy();
new BlockAutoformatEditing( editor, /^[*]\s$/, spy ); // eslint-disable-line no-new
new BlockAutoformatEditing( editor, plugin, /^[*]\s$/, spy ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );
model.enqueueChange( 'transparent', writer => {
Expand Down
33 changes: 24 additions & 9 deletions packages/ckeditor5-autoformat/tests/inlineautoformatediting.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model
import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';

describe( 'InlineAutoformatEditing', () => {
let editor, model, doc;
let editor, model, doc, plugin;

testUtils.createSinonSandbox();

Expand All @@ -25,14 +25,15 @@ describe( 'InlineAutoformatEditing', () => {
editor = newEditor;
model = editor.model;
doc = model.document;
plugin = editor.plugins.get( 'Autoformat' );

model.schema.extend( '$text', { allowAttributes: 'testAttribute' } );
} );
} );

describe( 'attribute', () => {
it( 'should stop early if there are less than 3 capture groups', () => {
new InlineAutoformatEditing( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new

setData( model, '<paragraph>*foobar[]</paragraph>' );
model.change( writer => {
Expand All @@ -43,7 +44,7 @@ describe( 'InlineAutoformatEditing', () => {
} );

it( 'should apply an attribute when the pattern is matched', () => {
new InlineAutoformatEditing( editor, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new

setData( model, '<paragraph>*foobar[]</paragraph>' );
model.change( writer => {
Expand All @@ -54,7 +55,7 @@ describe( 'InlineAutoformatEditing', () => {
} );

it( 'should stop early if selection is not collapsed', () => {
new InlineAutoformatEditing( editor, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new
new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)\*/g, 'testAttribute' ); // eslint-disable-line no-new

setData( model, '<paragraph>*foob[ar]</paragraph>' );
model.change( writer => {
Expand All @@ -73,7 +74,7 @@ describe( 'InlineAutoformatEditing', () => {
remove: []
} );

new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
new InlineAutoformatEditing( editor, plugin, testStub, formatSpy ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );
model.change( writer => {
Expand All @@ -90,7 +91,7 @@ describe( 'InlineAutoformatEditing', () => {
remove: [ [] ]
} );

new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
new InlineAutoformatEditing( editor, plugin, testStub, formatSpy ); // eslint-disable-line no-new

setData( model, '<paragraph>*[]</paragraph>' );
model.change( writer => {
Expand All @@ -107,7 +108,7 @@ describe( 'InlineAutoformatEditing', () => {
remove: [ [] ]
} );

new InlineAutoformatEditing( editor, testStub, formatSpy ); // eslint-disable-line no-new
new InlineAutoformatEditing( editor, plugin, testStub, formatSpy ); // eslint-disable-line no-new

setData( model, '<paragraph>[]</paragraph>' );
model.change( writer => {
Expand All @@ -117,6 +118,20 @@ describe( 'InlineAutoformatEditing', () => {
sinon.assert.notCalled( formatSpy );
} );

it( 'should not run callback when the pattern is matched and plugin is disabled', () => {
const callbackSpy = testUtils.sinon.spy().named( 'callback' );
new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, callbackSpy ); // eslint-disable-line no-new

plugin.isEnabled = false;

setData( model, '<paragraph>*foobar[]</paragraph>' );
model.change( writer => {
writer.insertText( '*', doc.selection.getFirstPosition() );
} );

sinon.assert.notCalled( callbackSpy );
} );

it( 'should not autoformat if callback returned false', () => {
setData( model, '<paragraph>Foobar[]</paragraph>' );

Expand All @@ -129,7 +144,7 @@ describe( 'InlineAutoformatEditing', () => {

const formatCallback = () => false;

new InlineAutoformatEditing( editor, testCallback, formatCallback ); // eslint-disable-line no-new
new InlineAutoformatEditing( editor, plugin, testCallback, formatCallback ); // eslint-disable-line no-new

model.change( writer => {
writer.insertText( ' ', doc.selection.getFirstPosition() );
Expand All @@ -140,7 +155,7 @@ describe( 'InlineAutoformatEditing', () => {
} );

it( 'should ignore transparent batches', () => {
new InlineAutoformatEditing( editor, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new
new InlineAutoformatEditing( editor, plugin, /(\*)(.+?)(\*)/g, 'testAttribute' ); // eslint-disable-line no-new

setData( model, '<paragraph>*foobar[]</paragraph>' );
model.enqueueChange( 'transparent', writer => {
Expand Down

0 comments on commit 72a5de2

Please sign in to comment.