소스 검색

Merge pull request #31 from ckeditor/t/30

Changed: ToggleAttributeCommand moved setting selection attributes to enqueueChanges block.
Piotrek Koszuliński 9 년 전
부모
커밋
9b44cfd57e
2개의 변경된 파일48개의 추가작업 그리고 10개의 파일을 삭제
  1. 10 10
      packages/ckeditor5-core/src/command/toggleattributecommand.js
  2. 38 0
      packages/ckeditor5-core/tests/command/toggleattributecommand.js

+ 10 - 10
packages/ckeditor5-core/src/command/toggleattributecommand.js

@@ -84,15 +84,15 @@ export default class ToggleAttributeCommand extends Command {
 		const selection = document.selection;
 		const value = ( forceValue === undefined ) ? !this.value : forceValue;
 
-		if ( selection.isCollapsed ) {
-			if ( value ) {
-				selection.setAttribute( this.attributeKey, true );
+		// If selection has non-collapsed ranges, we change attribute on nodes inside those ranges.
+		document.enqueueChanges( () => {
+			if ( selection.isCollapsed ) {
+				if ( value ) {
+					selection.setAttribute( this.attributeKey, true );
+				} else {
+					selection.removeAttribute( this.attributeKey );
+				}
 			} else {
-				selection.removeAttribute( this.attributeKey );
-			}
-		} else {
-			// If selection has non-collapsed ranges, we change attribute on nodes inside those ranges.
-			document.enqueueChanges( () => {
 				const ranges = getSchemaValidRanges( this.attributeKey, selection.getRanges(), document.schema );
 
 				// Keep it as one undo step.
@@ -105,7 +105,7 @@ export default class ToggleAttributeCommand extends Command {
 						batch.removeAttribute( range, this.attributeKey );
 					}
 				}
-			} );
-		}
+			}
+		} );
 	}
 }

+ 38 - 0
packages/ckeditor5-core/tests/command/toggleattributecommand.js

@@ -166,6 +166,44 @@ describe( 'ToggleAttributeCommand', () => {
 			expect( getData( modelDoc ) )
 				.to.equal( '<p>ab[<$text bold="true">c</$text><image></image><$text bold="true">foobarxy</$text><image></image>]z</p>' );
 		} );
+
+		describe( 'should cause firing model document changesDone event', () => {
+			let spy;
+
+			beforeEach( () => {
+				spy = sinon.spy();
+			} );
+
+			it( 'collapsed selection in non-empty parent', () => {
+				setData( modelDoc, '<p>x[]y</p>' );
+
+				modelDoc.on( 'changesDone', spy );
+
+				command._doExecute();
+
+				expect( spy.calledOnce ).to.be.true;
+			} );
+
+			it( 'non-collapsed selection', () => {
+				setData( modelDoc, '<p>[xy]</p>' );
+
+				modelDoc.on( 'changesDone', spy );
+
+				command._doExecute();
+
+				expect( spy.calledOnce ).to.be.true;
+			} );
+
+			it( 'in empty parent', () => {
+				setData( modelDoc, '<p>[]</p>' );
+
+				modelDoc.on( 'changesDone', spy );
+
+				command._doExecute();
+
+				expect( spy.calledOnce ).to.be.true;
+			} );
+		} );
 	} );
 
 	describe( '_checkEnabled', () => {