Преглед на файлове

Merge pull request #16 from ckeditor/t/13

t/13 Ugly bug.
Piotrek Koszuliński преди 9 години
родител
ревизия
fdc534c8a7
променени са 2 файла, в които са добавени 67 реда и са изтрити 7 реда
  1. 22 5
      packages/ckeditor5-typing/src/typing.js
  2. 45 2
      packages/ckeditor5-typing/tests/typing.js

+ 22 - 5
packages/ckeditor5-typing/src/typing.js

@@ -170,7 +170,8 @@ class MutationHandler {
 			return;
 		}
 
-		const changes = diffToChanges( diff( mutation.oldText, mutation.newText ), mutation.newText );
+		const diffResult = diff( mutation.oldText, mutation.newText );
+		const changes = diffToChanges( diffResult, mutation.newText );
 
 		for ( let change of changes ) {
 			const viewPos = new ViewPosition( mutation.node, change.index );
@@ -197,23 +198,39 @@ class MutationHandler {
 
 		// One new node.
 		if ( mutation.newChildren.length - mutation.oldChildren.length != 1 ) {
-			return false;
+			return;
 		}
+
 		// Which is text.
-		const changes = diffToChanges( diff( mutation.oldChildren, mutation.newChildren ), mutation.newChildren );
+		const diffResult = diff( mutation.oldChildren, mutation.newChildren, compare );
+		const changes = diffToChanges( diffResult, mutation.newChildren );
+
+		// In case of [ REMOVE, INSERT, INSERT ] the previous check will not exit.
+		if ( changes.length > 1 ) {
+			return;
+		}
+
 		const change = changes[ 0 ];
 
 		if ( !( change.values[ 0 ] instanceof ViewText ) ) {
-			return false;
+			return;
 		}
 
 		const viewPos = new ViewPosition( mutation.node, change.index );
 		const modelPos = this.editing.mapper.toModelPosition( viewPos );
-		const insertedText = mutation.newChildren[ 0 ].data;
+		const insertedText = change.values[ 0 ].data;
 
 		this._insert( modelPos, insertedText );
 
 		this.selectionPosition = ModelPosition.createAt( modelPos.parent, 'END' );
+
+		function compare( oldChild, newChild ) {
+			if ( oldChild instanceof ViewText && newChild instanceof ViewText ) {
+				return oldChild.data === newChild.data;
+			} else {
+				return oldChild === newChild;
+			}
+		}
 	}
 
 	_insert( position, text ) {

+ 45 - 2
packages/ckeditor5-typing/tests/typing.js

@@ -10,6 +10,8 @@ import Typing from '/ckeditor5/typing/typing.js';
 import Paragraph from '/ckeditor5/paragraph/paragraph.js';
 
 import ModelRange from '/ckeditor5/engine/model/range.js';
+import BuildModelConverterFor from '/ckeditor5/engine/conversion/model-converter-builder.js';
+import BuildViewConverterFor from '/ckeditor5/engine/conversion/view-converter-builder.js';
 
 import ViewText from '/ckeditor5/engine/view/text.js';
 import ViewElement from '/ckeditor5/engine/view/element.js';
@@ -30,6 +32,17 @@ describe( 'Typing feature', () => {
 				features: [ Typing, Paragraph ]
 			} )
 			.then( newEditor => {
+				// Mock image feature.
+				newEditor.document.schema.registerItem( 'image', '$inline' );
+
+				BuildModelConverterFor( newEditor.data.modelToView, newEditor.editing.modelToView )
+					.fromElement( 'image' )
+					.toElement( 'img' );
+
+				BuildViewConverterFor( newEditor.data.viewToModel )
+					.fromElement( 'img' )
+					.toElement( 'image' );
+
 				editor = newEditor;
 				model = editor.editing.model;
 				modelRoot = model.getRoot();
@@ -112,7 +125,7 @@ describe( 'Typing feature', () => {
 			expect( getViewData( view ) ).to.equal( '<p>x{}</p>' );
 		} );
 
-		it( 'should do nothing when two nodes where inserted', () => {
+		it( 'should do nothing when two nodes were inserted', () => {
 			editor.setData( '<p></p>' );
 
 			view.fire( 'mutations', [
@@ -128,11 +141,41 @@ describe( 'Typing feature', () => {
 			expect( getViewData( view ) ).to.equal( '<p></p>' );
 		} );
 
+		it( 'should do nothing when two nodes were inserted and one removed', () => {
+			view.fire( 'mutations', [
+				{
+					type: 'children',
+					oldChildren: [ new ViewText( 'foobar' ) ],
+					newChildren: [ new ViewText( 'x' ), new ViewElement( 'img' ) ],
+					node: viewRoot.getChild( 0 )
+				}
+			] );
+
+			expect( getModelData( model ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
+			expect( getViewData( view ) ).to.equal( '<p>foo{}bar</p>' );
+		} );
+
+		it( 'should handle multiple children in the node', () => {
+			editor.setData( '<p>foo<img></img></p>' );
+
+			view.fire( 'mutations', [
+				{
+					type: 'children',
+					oldChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ) ],
+					newChildren: [ new ViewText( 'foo' ), viewRoot.getChild( 0 ).getChild( 1 ), new ViewText( 'x' ) ],
+					node: viewRoot.getChild( 0 )
+				}
+			] );
+
+			expect( getModelData( model ) ).to.equal( '<paragraph>foo<image></image>x<selection /></paragraph>' );
+			expect( getViewData( view ) ).to.equal( '<p>foo<img></img>x{}</p>' );
+		} );
+
 		it( 'should do nothing when node was removed', () => {
 			view.fire( 'mutations', [
 				{
 					type: 'children',
-					oldChildren: [ viewRoot.getChild( 0 ).getChild( 0 ) ],
+					oldChildren: [ new ViewText( 'foobar' ) ],
 					newChildren: [],
 					node: viewRoot.getChild( 0 )
 				}