Browse Source

Fire selectionAttribute only if the attribute is not consumed.

Piotr Jasiun 9 năm trước cách đây
mục cha
commit
f5a53d8a09

+ 4 - 1
packages/ckeditor5-engine/src/treecontroller/modelconversiondispatcher.js

@@ -266,7 +266,10 @@ export default class ModelConversionDispatcher {
 				value: attr[ 1 ]
 			};
 
-			this.fire( 'selectionAttribute:' + data.key, data, consumable, this.conversionApi );
+			// Do not fire event if the attribute has been consumed.
+			if ( consumable.test( selection, 'selectionAttribute:' + data.key ) ) {
+				this.fire( 'selectionAttribute:' + data.key, data, consumable, this.conversionApi );
+			}
 		}
 	}
 

+ 20 - 0
packages/ckeditor5-engine/tests/treecontroller/modelconversiondispatcher.js

@@ -356,6 +356,8 @@ describe( 'ModelConversionDispatcher', () => {
 
 	describe( 'convertSelection', () => {
 		beforeEach( () => {
+			dispatcher.off( 'selection' );
+
 			root.appendChildren( 'foobar' );
 			doc.selection.setRanges( [
 				new ModelRange( new ModelPosition( root, [ 1 ] ), new ModelPosition( root, [ 3 ] ) ),
@@ -401,5 +403,23 @@ describe( 'ModelConversionDispatcher', () => {
 			expect( dispatcher.fire.calledWith( 'selectionAttribute:bold' ) ).to.be.true;
 			expect( dispatcher.fire.calledWith( 'selectionAttribute:italic' ) ).to.be.false;
 		} );
+
+		it( 'should not fire attributes events if attribute has been consumed', () => {
+			sinon.spy( dispatcher, 'fire' );
+
+			dispatcher.on( 'selection', ( evt, selection, consumable ) => {
+				consumable.consume( selection, 'selectionAttribute:bold' );
+			} );
+
+			doc.enqueueChanges( () => {
+				doc.batch()
+					.setAttr( 'bold', true, ModelRange.createFromElement( root ) )
+					.setAttr( 'italic', true, ModelRange.createFromParentsAndOffsets( root, 4, root, 5 ) );
+			} );
+
+			dispatcher.convertSelection( doc.selection );
+
+			expect( dispatcher.fire.calledWith( 'selectionAttribute:bold' ) ).to.be.false;
+		} );
 	} );
 } );