Browse Source

Tests in Input plugin checking element switch mutation.

Szymon Kupś 8 years ago
parent
commit
9d9663ce31

+ 1 - 0
packages/ckeditor5-typing/package.json

@@ -14,6 +14,7 @@
     "@ckeditor/ckeditor5-editor-classic": "^0.7.3",
     "@ckeditor/ckeditor5-enter": "^0.9.1",
     "@ckeditor/ckeditor5-heading": "^0.9.1",
+    "@ckeditor/ckeditor5-link": "^0.7.0",
     "@ckeditor/ckeditor5-paragraph": "^0.8.0",
     "@ckeditor/ckeditor5-undo": "^0.8.1",
     "@ckeditor/ckeditor5-presets": "^0.2.2",

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

@@ -186,6 +186,10 @@ class MutationHandler {
 		// Get common ancestor in DOM.
 		const domMutationCommonAncestor = domConverter.mapViewToDom( mutationsCommonAncestor );
 
+		if ( !domMutationCommonAncestor ) {
+			return;
+		}
+
 		// Create fresh DomConverter so it will not use existing mapping and convert current DOM to model.
 		// This wouldn't be needed if DomConverter would allow to create fresh view without checking any mappings.
 		const freshDomConverter = new DomConverter();
@@ -207,6 +211,12 @@ class MutationHandler {
 		// See comment in `_handleTextMutation`.
 		const newText = modelFromDomChildren.map( item => item.data ).join( '' ).replace( /\u00A0/g, ' ' );
 		const oldText = currentModelChildren.map( item => item.data ).join( '' );
+
+		// Do nothing if mutations created same text.
+		if ( oldText === newText ) {
+			return;
+		}
+
 		const diffResult = diff( oldText, newText );
 
 		const { firstChangeAt, insertions, deletions } = calculateChanges( diffResult );

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

@@ -3,9 +3,15 @@
  * For licensing, see LICENSE.md.
  */
 
+/* global document */
+
 import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
+import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
 import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
 import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
+import Bold from '@ckeditor/ckeditor5-basic-styles/src/boldengine';
+import Italic from '@ckeditor/ckeditor5-basic-styles/src/italicengine';
+import LinkEngine from '@ckeditor/ckeditor5-link/src/linkengine';
 import Input from '../src/input';
 
 import Batch from '@ckeditor/ckeditor5-engine/src/model/batch';
@@ -541,5 +547,75 @@ describe( 'Input feature', () => {
 			expect( getModelData( model ) ).to.equal( '<paragraph>fo[ob]ar</paragraph>' );
 		} );
 	} );
+
+	describe( '#100', () => {
+		let domElement, domRoot;
+
+		beforeEach( () => {
+			domElement = document.createElement( 'div' );
+			document.body.appendChild( domElement );
+
+			return ClassicTestEditor.create( domElement, { plugins: [ Input, Paragraph, Bold, Italic, LinkEngine ] } )
+				.then( newEditor => {
+					editor = newEditor;
+					model = editor.editing.model;
+					modelRoot = model.getRoot();
+					view = editor.editing.view;
+					viewRoot = view.getRoot();
+					domRoot = view.getDomRoot();
+				} );
+		} );
+
+		afterEach( () => {
+			domElement.remove();
+		} );
+
+		it( 'should handle typing when wrapping is switched', () => {
+			setModelData( model,
+				'<paragraph>' +
+					'<$text italic="true" linkHref="foo">' +
+						'text[]' +
+					'</$text>' +
+				'</paragraph>'
+			);
+
+			const paragraph = viewRoot.getChild( 0 );
+			const link = paragraph.getChild( 0 );
+			const italic = link.getChild( 0 );
+			const text = italic.getChild( 0 );
+
+			expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>text{}</i></a></p>' );
+
+			// Simulate mutations and DOM change.
+			domRoot.childNodes[ 0 ].innerHTML = '<i><a href="foo">text</a>x</i>';
+			view.fire( 'mutations', [
+				// First mutation - remove all children from link element.
+				{
+					type: 'children',
+					node: link,
+					oldChildren: [ italic ],
+					newChildren: []
+				},
+
+				// Second mutation - remove link from paragraph and put italic there.
+				{
+					type: 'children',
+					node: paragraph,
+					oldChildren: [ link ],
+					newChildren: [ italic ]
+				},
+
+				// Third mutation - italic's new children.
+				{
+					type: 'children',
+					node: italic,
+					oldChildren: [ text ],
+					newChildren: [ new ViewElement( 'a', null, text ), new ViewText( 'x' ) ]
+				}
+			] );
+
+			expect( getViewData( view ) ).to.equal( '<p><a href="foo"><i>textx{}</i></a></p>' );
+		} );
+	} );
 } );