8
0
Quellcode durchsuchen

Other: Do nothing on collapsed ranges in `HighlightCommand#exectue()`.

Maciej Gołaszewski vor 8 Jahren
Ursprung
Commit
35a15431ed

+ 10 - 14
packages/ckeditor5-highlight/src/highlightcommand.js

@@ -66,23 +66,19 @@ export default class HighlightCommand extends Command {
 		const selection = doc.selection;
 		const value = options.class;
 
+		if ( selection.isCollapsed ) {
+			return;
+		}
+
 		doc.enqueueChanges( () => {
-			if ( selection.isCollapsed ) {
+			const ranges = doc.schema.getValidRanges( selection.getRanges(), 'highlight' );
+			const batch = options.batch || doc.batch();
+
+			for ( const range of ranges ) {
 				if ( value ) {
-					selection.setAttribute( 'highlight', value );
+					batch.setAttribute( range, 'highlight', value );
 				} else {
-					selection.removeAttribute( 'highlight' );
-				}
-			} else {
-				const ranges = doc.schema.getValidRanges( selection.getRanges(), 'highlight' );
-				const batch = options.batch || doc.batch();
-
-				for ( const range of ranges ) {
-					if ( value ) {
-						batch.setAttribute( range, 'highlight', value );
-					} else {
-						batch.removeAttribute( range, 'highlight' );
-					}
+					batch.removeAttribute( range, 'highlight' );
 				}
 			}
 		} );

+ 12 - 0
packages/ckeditor5-highlight/tests/highlightcommand.js

@@ -98,5 +98,17 @@ describe( 'HighlightCommand', () => {
 
 			expect( command.value ).to.be.undefined;
 		} );
+
+		it( 'should do nothing on collapsed range', () => {
+			setData( doc, '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.equal( 'marker' );
+
+			command.execute();
+
+			expect( getData( doc ) ).to.equal( '<paragraph>abc<$text highlight="marker">foo[]bar</$text>xyz</paragraph>' );
+
+			expect( command.value ).to.equal( 'marker' );
+		} );
 	} );
 } );