Przeglądaj źródła

Improved Input plugin code coverage. Added more test cases.

Szymon Kupś 8 lat temu
rodzic
commit
e3414491c3

+ 1 - 10
packages/ckeditor5-typing/src/input.js

@@ -291,11 +291,6 @@ class MutationHandler {
 		}
 
 		const change = getSingleTextNodeChange( mutation );
-
-		if ( !change ) {
-			return;
-		}
-
 		const viewPos = new ViewPosition( mutation.node, change.index );
 		const modelPos = this.editing.mapper.toModelPosition( viewPos );
 		const insertedText = change.values[ 0 ].data;
@@ -410,10 +405,6 @@ function getMutationsCommonAncestor( mutations ) {
 			return ancestors;
 		} ).filter( item => item !== shortestList );
 
-	if ( !shortestList ) {
-		return null;
-	}
-
 	// Find common ancestor from the lists. Go from the shortest list to the top. If common ancestor is found - return it.
 	for ( const ancestor of shortestList ) {
 		// Check only container and root elements.
@@ -448,7 +439,7 @@ function containerChildrenMutated( mutations ) {
 		return false;
 	}
 
-	// Check if all mutations are `children` type, and there is no single text node mutation
+	// Check if all mutations are `children` type, and there is no single text node mutation.
 	for ( const mutation of mutations ) {
 		if ( mutation.type !== 'children' || getSingleTextNodeChange( mutation ) ) {
 			return false;

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

@@ -21,6 +21,7 @@ import buildViewConverter from '@ckeditor/ckeditor5-engine/src/conversion/buildv
 
 import ViewText from '@ckeditor/ckeditor5-engine/src/view/text';
 import ViewElement from '@ckeditor/ckeditor5-engine/src/view/element';
+import ViewContainerElement from '@ckeditor/ckeditor5-engine/src/view/containerelement';
 import ViewSelection from '@ckeditor/ckeditor5-engine/src/view/selection';
 
 import EmitterMixin from '@ckeditor/ckeditor5-utils/src/emittermixin';
@@ -754,6 +755,36 @@ describe( 'Input feature', () => {
 			expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<p><strong>fixed text</strong></p>' );
 		} );
 
+		// Spell checker splits text inside attributes to two text nodes.
+		it( 'should handle mutations inside attribute element', () => {
+			setModelData( model,
+				'<paragraph>' +
+					'<$text bold="true">' +
+						'this is foo text[]' +
+					'</$text>' +
+				'</paragraph>'
+			);
+
+			expect( getViewData( view ) ).to.equal( '<p><strong>this is foo text{}</strong></p>' );
+
+			const paragraph = viewRoot.getChild( 0 );
+			const strong = paragraph.getChild( 0 );
+			const text = strong.getChild( 0 );
+
+			// Simulate mutations and DOM change.
+			domRoot.childNodes[ 0 ].innerHTML = '<strong>this is bar text</strong>';
+			view.fire( 'mutations', [
+				{
+					type: 'children',
+					node: strong,
+					oldChildren: [ text ],
+					newChildren: [ new ViewText( 'this is bar' ), new ViewText( ' text' ) ]
+				}
+			] );
+
+			expect( getViewData( view, { withoutSelection: true } ) ).to.equal( '<p><strong>this is bar text</strong></p>' );
+		} );
+
 		it( 'should do nothing if elements mutated', () => {
 			setModelData( model,
 				'<paragraph>' +
@@ -831,6 +862,40 @@ describe( 'Input feature', () => {
 			expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
 		} );
 
+		it( 'should do nothing if mutations does not have common ancestor', () => {
+			setModelData( model,
+				'<paragraph>' +
+					'<$text bold="true">' +
+						'text[]' +
+					'</$text>' +
+				'</paragraph>'
+			);
+
+			expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
+
+			const paragraph = viewRoot.getChild( 0 );
+			const strong = paragraph.getChild( 0 );
+
+			// Simulate mutations and DOM change.
+			domRoot.childNodes[ 0 ].innerHTML = '<strong>text</strong>';
+			view.fire( 'mutations', [
+				{
+					type: 'children',
+					node: paragraph,
+					oldChildren: [ strong ],
+					newChildren: [ strong ]
+				},
+				{
+					type: 'children',
+					node: new ViewContainerElement( 'div' ),
+					oldChildren: [],
+					newChildren: [ new ViewText( 'foo' ), new ViewText( 'bar' ) ]
+				}
+			] );
+
+			expect( getViewData( view ) ).to.equal( '<p><strong>text{}</strong></p>' );
+		} );
+
 		it( 'should handle view selection if one is returned from mutations', () => {
 			setModelData( model,
 				'<paragraph>' +