Skip to content

Commit

Permalink
The removeFormat command should handle block formatting differently.
Browse files Browse the repository at this point in the history
  • Loading branch information
niegowski committed Jul 25, 2020
1 parent c571167 commit 437e29b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/ckeditor5-remove-format/src/removeformatcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,22 @@ export default class RemoveFormatCommand extends Command {
return !!first( this._getFormattingAttributes( item, schema ) );
};

// Check formatting on selected items that are not blocks.
for ( const curRange of selection.getRanges() ) {
for ( const item of curRange.getItems() ) {
if ( itemHasRemovableFormatting( item ) ) {
if ( !schema.isBlock( item ) && itemHasRemovableFormatting( item ) ) {
yield item;
}
}
}

// Check formatting from selected blocks.
for ( const block of selection.getSelectedBlocks() ) {
if ( itemHasRemovableFormatting( block ) ) {
yield block;
}
}

// Finally the selection might be formatted as well, so make sure to check it.
if ( itemHasRemovableFormatting( selection ) ) {
yield selection;
Expand Down
28 changes: 27 additions & 1 deletion packages/ckeditor5-remove-format/tests/removeformatcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ describe( 'RemoveFormatCommand', () => {
editor.commands.add( 'removeFormat', command );

model.schema.register( 'p', {
inheritAllFrom: '$block'
inheritAllFrom: '$block',
allowAttributes: 'someBlockFormatting'
} );

model.schema.addAttributeCheck( ( ctx, attributeName ) => {
Expand All @@ -45,6 +46,10 @@ describe( 'RemoveFormatCommand', () => {
model.schema.setAttributeProperties( 'bold', {
isFormatting: true
} );

model.schema.setAttributeProperties( 'someBlockFormatting', {
isFormatting: true
} );
} );
} );

Expand Down Expand Up @@ -94,6 +99,16 @@ describe( 'RemoveFormatCommand', () => {
}
},
assert: () => expectEnabledPropertyToBe( true )
},

'state with block formatting': {
input: '<p someBlockFormatting="foo">f[oo</p><p>]bar</p>',
assert: () => expectEnabledPropertyToBe( true )
},

'state with block formatting (collapsed selection)': {
input: '<p someBlockFormatting="foo">f[]oo</p>',
assert: () => expectEnabledPropertyToBe( true )
}
};

Expand Down Expand Up @@ -140,7 +155,18 @@ describe( 'RemoveFormatCommand', () => {
expect( model.document.selection.hasAttribute( 'bold' ) ).to.equal( false );
expect( model.document.selection.hasAttribute( 'irrelevant' ) ).to.equal( true );
}
},

'state with block formatting': {
input: '<p someBlockFormatting="foo">f[oo</p><p someBlockFormatting="bar">]bar</p>',
assert: () => expectModelToBeEqual( '<p>f[oo</p><p someBlockFormatting="bar">]bar</p>' )
},

'state with block formatting (collapsed selection)': {
input: '<p someBlockFormatting="foo">f[]oo</p><p someBlockFormatting="bar">bar</p>',
assert: () => expectModelToBeEqual( '<p>f[]oo</p><p someBlockFormatting="bar">bar</p>' )
}

};

generateTypicalUseCases( cases, {
Expand Down

0 comments on commit 437e29b

Please sign in to comment.