瀏覽代碼

Added tests, fixed code coverage.

Szymon Kupś 9 年之前
父節點
當前提交
fd88f2d358

+ 5 - 4
packages/ckeditor5-heading/src/formatscommand.js

@@ -20,11 +20,12 @@ export default class FormatsCommand extends Command {
 		// Listen on selection change and set current command's format to format in current selection.
 		this.listenTo( editor.document.selection, 'change', () => {
 			const position = editor.document.selection.getFirstPosition();
-			const parent = position.parent;
-			const format = this._getFormatById( parent.name );
+			const block = findTopmostBlock( position );
 
-			// TODO: What should happen if format is not found?
-			if ( format !== undefined ) {
+			if ( block ) {
+				const format = this._getFormatById( block.name );
+
+				// TODO: What should happen if format is not found?
 				this.format = format;
 			}
 		} );

+ 9 - 2
packages/ckeditor5-heading/tests/formatscommand.js

@@ -80,6 +80,13 @@ describe( 'FormatsCommand', () => {
 				expect( getData( document ) ).to.equal( '<paragraph>foo<selection />bar</paragraph>' );
 			} );
 
+			it( 'converts topmost blocks', () => {
+				setData( document, '<heading1><b>foo<selection /></b>bar</heading1>' );
+				command._doExecute( 'heading1' );
+
+				expect( getData( document ) ).to.equal( '<paragraph><b>foo<selection /></b>bar</paragraph>' );
+			} );
+
 			function test( from, to ) {
 				it( `converts ${ from.id } to ${ to.id } on collapsed selection`, () => {
 					setData( document, `<${ from.id }>foo<selection />bar</${ from.id }>` );
@@ -108,11 +115,11 @@ describe( 'FormatsCommand', () => {
 			} );
 
 			it( 'resets to default value all elements with same format', () => {
-				setData( document, '<heading1>foo<selection></heading1><heading1>bar</heading1><heading2></selection>baz</heading2>' );
+				setData( document, '<heading1>foo<selection></heading1><heading1>bar</heading1><heading2>baz</heading2></selection>' );
 				command._doExecute( 'heading1' );
 
 				expect( getData( document ) ).to.equal(
-					'<paragraph>foo<selection></paragraph><paragraph>bar</paragraph><heading2></selection>baz</heading2>'
+					'<paragraph>foo<selection></paragraph><paragraph>bar</paragraph><heading2>baz</heading2></selection>'
 				);
 			} );