浏览代码

Fixed fastDiff for multi-byte unicode sequences.

Kuba Niegowski 5 年之前
父节点
当前提交
db8d6999b6

+ 3 - 2
packages/ckeditor5-utils/src/fastdiff.js

@@ -101,12 +101,13 @@ export default function fastDiff( a, b, cmp, atomicChanges = false ) {
 	};
 
 	// Transform text or any iterable into arrays for easier, consistent processing.
+	// Array.from was used here but it generated incorrect results for multi-byte unicode sequences.
 	if ( !Array.isArray( a ) ) {
-		a = Array.from( a );
+		a = Array.prototype.map.call( a, c => c );
 	}
 
 	if ( !Array.isArray( b ) ) {
-		b = Array.from( b );
+		b = Array.prototype.map.call( b, c => c );
 	}
 
 	// Find first and last change.

+ 4 - 0
packages/ckeditor5-utils/tests/diff.js

@@ -67,4 +67,8 @@ describe( 'diff', () => {
 		diff( getLongText( 10 ), getLongText( 1990 ) );
 		testUtils.sinon.assert.called( fastDiffSpy );
 	} );
+
+	it( 'should diff insertion on the end handle multi-byte unicode properly', () => {
+		expect( diff( '123🙂', '123🙂x' ) ).to.deep.equals( [ 'equal', 'equal', 'equal', 'equal', 'equal', 'insert' ] );
+	} );
 } );

+ 4 - 0
packages/ckeditor5-utils/tests/fastdiff.js

@@ -120,6 +120,10 @@ describe( 'fastDiff', () => {
 					{ index: 2, type: 'insert', values: [ { text: 'baz' } ] }
 				], true, ( a, b ) => a.text === b.text );
 			} );
+
+			it( 'should diff insertion on the end handle multi-byte unicode properly', () => {
+				expectDiff( '123🙂', '123🙂x', [ { index: 5, type: 'insert', values: [ 'x' ] } ] );
+			} );
 		} );
 
 		describe( 'deletion', () => {