8
0
Просмотр исходного кода

Fix: Mutation handling crashed in particular scenarios (when old text was same as new text).

Szymon Cofalik 7 лет назад
Родитель
Сommit
90e9c24e82

+ 5 - 0
packages/ckeditor5-typing/src/utils/injecttypingmutationshandling.js

@@ -200,6 +200,11 @@ class MutationHandler {
 		// To have correct `diffResult`, we also compare view node text data with   replaced by space.
 		const oldText = mutation.oldText.replace( /\u00A0/g, ' ' );
 
+		// Do nothing if mutations created same text.
+		if ( oldText === newText ) {
+			return;
+		}
+
 		const diffResult = diff( oldText, newText );
 
 		const { firstChangeAt, insertions, deletions } = calculateChanges( diffResult );

+ 26 - 0
packages/ckeditor5-typing/tests/input.js

@@ -647,6 +647,32 @@ describe( 'Input feature', () => {
 
 			expect( getViewData( view ) ).to.equal( '<p>foo<placeholder></placeholder>bar<placeholder></placeholder>baz!{}</p>' );
 		} );
+
+		// https://github.com/ckeditor/ckeditor5-typing/issues/181
+		it( 'should not crash if the mutation old text is same as new text', () => {
+			// It shouldn't matter what data is here, I am putting it like it is in the test scenario, but it is really about
+			// what mutations are generated.
+			editor.setData( '<p>Foo<strong> </strong>&nbsp;Bar</p>' );
+
+			const p = viewRoot.getChild( 0 );
+
+			viewDocument.fire( 'mutations', [
+				{
+					type: 'text',
+					oldText: ' ',
+					newText: ' ',
+					node: p.getChild( 1 )
+				},
+				{
+					type: 'text',
+					oldText: 'Foo',
+					newText: 'Foox',
+					node: p.getChild( 0 )
+				}
+			] );
+
+			expect( getViewData( view ) ).to.equal( '<p>Foox{}<strong> </strong> Bar</p>' );
+		} );
 	} );
 
 	describe( 'keystroke handling', () => {