Sfoglia il codice sorgente

Mention post-fixer now correctly handles undo.

Maciej Gołaszewski 6 anni fa
parent
commit
7a5e5e9afc

+ 18 - 12
packages/ckeditor5-mention/src/mentionediting.js

@@ -149,10 +149,10 @@ function removePartialMentionPostFixer( writer, doc ) {
 
 	for ( const change of changes ) {
 		// Check if user edited part of a mention.
-		if ( change.type == 'insert' || change.type == 'remove' ) {
+		if ( change.name == '$text' && ( change.type == 'insert' || change.type == 'remove' ) ) {
 			const textNode = change.position.textNode;
 
-			if ( change.name == '$text' && textNode && textNode.hasAttribute( 'mention' ) ) {
+			if ( !checkMentionAttributeOnNode( textNode ) ) {
 				writer.removeAttribute( 'mention', textNode );
 				wasChanged = true;
 			}
@@ -162,19 +162,25 @@ function removePartialMentionPostFixer( writer, doc ) {
 		if ( change.type == 'remove' ) {
 			const nodeBefore = change.position.nodeBefore;
 
-			if ( nodeBefore && nodeBefore.hasAttribute( 'mention' ) ) {
-				const text = nodeBefore.data;
-				const mention = nodeBefore.getAttribute( 'mention' );
-
-				const expectedText = mention._marker + mention.name;
-
-				if ( text != expectedText ) {
-					writer.removeAttribute( 'mention', nodeBefore );
-					wasChanged = true;
-				}
+			if ( !checkMentionAttributeOnNode( nodeBefore ) ) {
+				writer.removeAttribute( 'mention', nodeBefore );
+				wasChanged = true;
 			}
 		}
 	}
 
 	return wasChanged;
 }
+
+function checkMentionAttributeOnNode( node ) {
+	if ( !node || !node.is( 'text' ) || !node.hasAttribute( 'mention' ) ) {
+		return true;
+	}
+
+	const text = node.data;
+	const mention = node.getAttribute( 'mention' );
+
+	const expectedText = mention._marker + mention.name;
+
+	return text == expectedText;
+}

+ 2 - 2
packages/ckeditor5-mention/tests/mention-integration.js

@@ -57,7 +57,7 @@ describe( 'MentionEditing - integration', () => {
 			editor.execute( 'undo' );
 
 			expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
-			expect( getViewData( editor.editing.view ) )
+			expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
 				.to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
 		} );
 
@@ -82,7 +82,7 @@ describe( 'MentionEditing - integration', () => {
 			editor.execute( 'undo' );
 
 			expect( editor.getData() ).to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
-			expect( getViewData( editor.editing.view ) )
+			expect( getViewData( editor.editing.view, { withoutSelection: true } ) )
 				.to.equal( '<p>foo <span class="mention" data-mention="John">@John</span> bar</p>' );
 		} );
 	} );