Преглед изворни кода

Updated code and added tests.

Maksymilian Barnaś пре 9 година
родитељ
комит
1e68bffa61

+ 1 - 1
packages/ckeditor5-autoformat/src/autoformat.js

@@ -94,7 +94,7 @@ export default class Autoformat extends Feature {
 		new InlineAutoformatEngine( this.editor, /(\*\*)(.+?)(\*\*)/g, 'bold' );
 
 		// Italicize text between `*`, e.g. `*text to italicize*`.
-		// Slightly more complicated because if the clashing with the Bold autoformat.
+		// Slightly more complicated because of the clashing with the Bold autoformat.
 		// Won't work for text shorter than 3 characters.
 		new InlineAutoformatEngine(
 			this.editor,

+ 2 - 16
packages/ckeditor5-autoformat/src/inlineautoformatengine.js

@@ -4,7 +4,6 @@
  */
 
 import Range from '../engine/model/range.js';
-import CKEditorError from '../utils/ckeditorerror.js';
 
 /**
  * A paragraph feature for editor.
@@ -52,7 +51,7 @@ export default class InlineAutoformatEngine {
 		let formatClb;
 
 		if ( typeof testCallbackOrPattern == 'string' ) {
-			pattern = new RegExp( testCallbackOrPattern );
+			pattern = new RegExp( testCallbackOrPattern, 'g' );
 		} else if ( testCallbackOrPattern instanceof RegExp ) {
 			pattern = testCallbackOrPattern;
 		} else {
@@ -71,19 +70,10 @@ export default class InlineAutoformatEngine {
 			let remove = [];
 			let format = [];
 
-			if ( !text ) {
-				return;
-			}
-
 			while ( ( result = pattern.exec( text ) ) !== null ) {
-				// If nothing matched, stop early.
-				if ( !result ) {
-					return;
-				}
-
 				// There should be full match and 3 capture groups.
 				if ( result && result.length < 4 ) {
-					throw new CKEditorError( 'inlineautoformat-missing-capture-groups: Less than 3 capture groups in regular expression.' );
+					break;
 				}
 
 				const {
@@ -136,10 +126,6 @@ export default class InlineAutoformatEngine {
 
 			const ranges = testClb( text );
 
-			if ( !ranges ) {
-				return;
-			}
-
 			// Apply format before deleting text.
 			ranges.format.forEach( ( range ) => {
 				if ( range[ 0 ] === undefined || range[ 1 ] === undefined ) {

+ 50 - 7
packages/ckeditor5-autoformat/tests/inlineautoformatengine.js

@@ -28,14 +28,40 @@ describe( 'InlineAutoformatEngine', () => {
 	} );
 
 	describe( 'Command name', () => {
+		it( 'should accept a string pattern', () => {
+			const spy = testUtils.sinon.spy();
+			editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
+			new InlineAutoformatEngine( editor, '(\\*)(.+?)(\\*)', 'testCommand' );
+
+			setData( doc, '<paragraph>*foobar[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), '*' );
+			} );
+
+			sinon.assert.calledOnce( spy );
+		} );
+
+		it( 'should stop early if there are less than 3 capture groups', () => {
+			const spy = testUtils.sinon.spy();
+			editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
+			new InlineAutoformatEngine( editor, /(\*)(.+?)\*/g, 'testCommand' );
+
+			setData( doc, '<paragraph>*foobar[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), '*' );
+			} );
+
+			sinon.assert.notCalled( spy );
+		} );
+
 		it( 'should run a command when the pattern is matched', () => {
 			const spy = testUtils.sinon.spy();
 			editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
-			new InlineAutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
+			new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testCommand' );
 
-			setData( doc, '<paragraph>*[]</paragraph>' );
+			setData( doc, '<paragraph>*foobar[]</paragraph>' );
 			doc.enqueueChanges( () => {
-				batch.insert( doc.selection.getFirstPosition(), ' ' );
+				batch.insert( doc.selection.getFirstPosition(), '*' );
 			} );
 
 			sinon.assert.calledOnce( spy );
@@ -44,15 +70,15 @@ describe( 'InlineAutoformatEngine', () => {
 		it( 'should remove found pattern', () => {
 			const spy = testUtils.sinon.spy();
 			editor.commands.set( 'testCommand', new TestCommand( editor, spy ) );
-			new InlineAutoformatEngine( editor, /^[\*]\s$/, 'testCommand' );
+			new InlineAutoformatEngine( editor, /(\*)(.+?)(\*)/g, 'testCommand' );
 
-			setData( doc, '<paragraph>*[]</paragraph>' );
+			setData( doc, '<paragraph>*foobar[]</paragraph>' );
 			doc.enqueueChanges( () => {
-				batch.insert( doc.selection.getFirstPosition(), ' ' );
+				batch.insert( doc.selection.getFirstPosition(), '*' );
 			} );
 
 			sinon.assert.calledOnce( spy );
-			expect( getData( doc ) ).to.equal( '<paragraph>[]</paragraph>' );
+			expect( getData( doc ) ).to.equal( '<paragraph>foobar[]</paragraph>' );
 		} );
 	} );
 
@@ -91,6 +117,23 @@ describe( 'InlineAutoformatEngine', () => {
 			sinon.assert.notCalled( formatSpy );
 		} );
 
+		it( 'should stop early when there is no text', () => {
+			const formatSpy = testUtils.sinon.spy();
+			const testStub = testUtils.sinon.stub().returns( {
+				format: [],
+				remove: [ [] ]
+			} );
+
+			new InlineAutoformatEngine( editor, testStub, formatSpy );
+
+			setData( doc, '<paragraph>[]</paragraph>' );
+			doc.enqueueChanges( () => {
+				batch.insert( doc.selection.getFirstPosition(), ' ' );
+			} );
+
+			sinon.assert.notCalled( formatSpy );
+		} );
+
 		it( 'takes text from nested elements', () => {
 			const formatSpy = testUtils.sinon.spy();
 			const testStub = testUtils.sinon.stub().returns( {

+ 6 - 2
packages/ckeditor5-autoformat/tests/manual/autoformat.md

@@ -9,7 +9,11 @@
 
 3. Type number from the range **1-3** to replace current paragraph with numbered list item.
 
-4. For every autoformat pattern: Undo until you'll see just the pattern (e.g. `- `). Typing should be then possible  without triggering autoformatting again.
+4. Type `*foobar*` to italicize `foobar`. `*` should be removed.
 
-5. Typing a different pattern in already converted block **must not** trigger autoformatting. For example, typing `- ` in heading should not convert heading to list.
+5. Type `**foobar**` to bold `foobar`. `**` should be removed.
+
+6. For every autoformat pattern: Undo until you'll see just the pattern (e.g. `- `). Typing should be then possible  without triggering autoformatting again.
+
+7. Typing a different pattern in already converted block **must not** trigger autoformatting. For example, typing `- ` in heading should not convert heading to list.