Przeglądaj źródła

BlockAutoformat should not react to text after inline element.

Maciej Gołaszewski 6 lat temu
rodzic
commit
16cf16cc8c

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

@@ -94,7 +94,9 @@ export default class BlockAutoformatEditing {
 				return;
 			}
 
-			const match = pattern.exec( item.data );
+			const text = getStartText( editor.model.createRangeIn( item.parent ) );
+
+			const match = pattern.exec( text );
 
 			if ( !match ) {
 				return;
@@ -119,3 +121,23 @@ export default class BlockAutoformatEditing {
 		} );
 	}
 }
+
+/**
+ * Returns the text from given renge up to the first inline element.
+ *
+ * @param {module:engine/model/range~Range} range
+ * @returns {String}
+ */
+export function getStartText( range ) {
+	let text = '';
+
+	for ( const item of range.getItems() ) {
+		if ( !( item.is( 'text' ) || item.is( 'textProxy' ) ) ) {
+			return text;
+		}
+
+		text = text + item.data;
+	}
+
+	return text;
+}

+ 36 - 0
packages/ckeditor5-autoformat/tests/autoformat.js

@@ -81,6 +81,15 @@ describe( 'Autoformat', () => {
 
 			expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">- []</listItem>' );
 		} );
+
+		it( 'should not replace asterisk character after <softBreak>', () => {
+			setData( model, '<paragraph>Foo<softBreak></softBreak>*[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( ' ', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>* []</paragraph>' );
+		} );
 	} );
 
 	describe( 'Numbered list', () => {
@@ -128,6 +137,15 @@ describe( 'Autoformat', () => {
 
 			expect( getData( model ) ).to.equal( '<paragraph>3. []</paragraph>' );
 		} );
+
+		it( 'should not replace digit character after <softBreak>', () => {
+			setData( model, '<paragraph>Foo<softBreak></softBreak>1.[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( ' ', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>1. []</paragraph>' );
+		} );
 	} );
 
 	describe( 'Heading', () => {
@@ -212,6 +230,15 @@ describe( 'Autoformat', () => {
 
 			expect( getData( model ) ).to.equal( '<paragraph># []</paragraph>' );
 		} );
+
+		it( 'should not replace hash character after <softBreak>', () => {
+			setData( model, '<paragraph>Foo<softBreak></softBreak>#[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( ' ', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak># []</paragraph>' );
+		} );
 	} );
 
 	describe( 'Block quote', () => {
@@ -250,6 +277,15 @@ describe( 'Autoformat', () => {
 
 			expect( getData( model ) ).to.equal( '<listItem listIndent="0" listType="bulleted">1. > []</listItem>' );
 		} );
+
+		it( 'should not replace greater-than character after <softBreak>', () => {
+			setData( model, '<paragraph>Foo<softBreak></softBreak>>[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( ' ', doc.selection.getFirstPosition() );
+			} );
+
+			expect( getData( model ) ).to.equal( '<paragraph>Foo<softBreak></softBreak>> []</paragraph>' );
+		} );
 	} );
 
 	describe( 'Inline autoformat', () => {

+ 28 - 0
packages/ckeditor5-autoformat/tests/blockautoformatediting.js

@@ -128,6 +128,34 @@ describe( 'BlockAutoformatEditing', () => {
 			sinon.assert.notCalled( spy );
 		} );
 
+		it( 'should not call callback when after inline element', () => {
+			// Configure the schema.
+			model.schema.register( 'softBreak', {
+				allowWhere: '$text',
+				isInline: true
+			} );
+			editor.conversion.for( 'upcast' )
+				.elementToElement( {
+					model: 'softBreak',
+					view: 'br'
+				} );
+			editor.conversion.for( 'downcast' )
+				.elementToElement( {
+					model: 'softBreak',
+					view: ( modelElement, viewWriter ) => viewWriter.createEmptyElement( 'br' )
+				} );
+
+			const spy = testUtils.sinon.spy();
+			new BlockAutoformatEditing( editor, /^[*]\s/, spy ); // eslint-disable-line no-new
+
+			setData( model, '<paragraph>*<softBreak></softBreak>[]</paragraph>' );
+			model.change( writer => {
+				writer.insertText( ' ', doc.selection.getFirstPosition() );
+			} );
+
+			sinon.assert.notCalled( spy );
+		} );
+
 		it( 'should stop if callback returned false', () => {
 			new BlockAutoformatEditing( editor, /^[*]\s$/, () => false ); // eslint-disable-line no-new