Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 21 additions & 15 deletions packages/ckeditor5-remove-format/src/removeformatcommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* @module remove-format/removeformatcommand
*/

import type { ModelDocumentSelection, ModelItem, ModelSchema, ModelRange, ModelWriter } from 'ckeditor5/src/engine.js';
import type { ModelDocumentSelection, ModelItem, ModelRange, ModelWriter } from 'ckeditor5/src/engine.js';
import { Command } from 'ckeditor5/src/core.js';
import { first } from 'ckeditor5/src/utils.js';

Expand Down Expand Up @@ -38,28 +38,27 @@ export class RemoveFormatCommand extends Command {
public override refresh(): void {
const model = this.editor.model;

this.isEnabled = !!first( this._getFormattingItems( model.document.selection, model.schema ) );
this.isEnabled = !!first( this._getFormattingItems( model.document.selection ) );
}

/**
* @inheritDoc
*/
public override execute(): void {
const model = this.editor.model;
const schema = model.schema;

model.change( writer => {
for ( const item of this._getFormattingItems( model.document.selection, schema ) ) {
for ( const item of this._getFormattingItems( model.document.selection ) ) {
if ( item.is( 'selection' ) ) {
for ( const attributeName of this._getFormattingAttributes( item, schema ) ) {
for ( const attributeName of this._getFormattingAttributes( item ) ) {
writer.removeSelectionAttribute( attributeName );
}
} else {
// Workaround for items with multiple removable attributes. See
// https://github.com/ckeditor/ckeditor5-remove-format/pull/1#pullrequestreview-220515609
const itemRange = writer.createRangeOn( item );

for ( const attributeName of this._getFormattingAttributes( item, schema ) ) {
for ( const attributeName of this._getFormattingAttributes( item ) ) {
this._removeFormatting( attributeName, item, itemRange, writer );
}
}
Expand Down Expand Up @@ -100,24 +99,30 @@ export class RemoveFormatCommand extends Command {
/**
* Returns an iterable of items in a selection (including the selection itself) that have formatting model
* attributes to be removed by the feature.
*
* @param schema The schema describing the item.
*/
private* _getFormattingItems( selection: ModelDocumentSelection, schema: ModelSchema ) {
private* _getFormattingItems( selection: ModelDocumentSelection ) {
const model = this.editor.model;
const schema = model.schema;

const itemHasRemovableFormatting = ( item: ModelItem | ModelDocumentSelection ) => {
return !!first( this._getFormattingAttributes( item, schema ) );
return !!first( this._getFormattingAttributes( item ) );
};

// Check formatting on selected items that are not blocks.
// Check formatting on selected items.
for ( const curRange of selection.getRanges() ) {
for ( const item of curRange.getItems() ) {
if ( !schema.isBlock( item ) && itemHasRemovableFormatting( item ) ) {
// Ignore last block if range ends at the beginning of it.
if ( schema.isBlock( item ) && curRange.end.isTouching( model.createPositionAt( item, 0 ) ) ) {
continue;
}

if ( itemHasRemovableFormatting( item ) ) {
yield item;
}
}
}

// Check formatting from selected blocks.
// Check formatting from selected blocks (to include partly selected blocks).
for ( const block of selection.getSelectedBlocks() ) {
if ( itemHasRemovableFormatting( block ) ) {
yield block;
Expand All @@ -135,10 +140,11 @@ export class RemoveFormatCommand extends Command {
*
* **Note:** Formatting items have the `isFormatting` property set to `true`.
*
* @param schema The schema describing the item.
* @returns The names of formatting attributes found in a given item.
*/
private* _getFormattingAttributes( item: ModelItem | ModelDocumentSelection, schema: ModelSchema ) {
private* _getFormattingAttributes( item: ModelItem | ModelDocumentSelection ) {
const schema = this.editor.model.schema;

for ( const [ attributeName ] of item.getAttributes() ) {
for ( const { isFormatting } of this._customAttributesHandlers ) {
if ( isFormatting( attributeName, item ) ) {
Expand Down
102 changes: 102 additions & 0 deletions packages/ckeditor5-remove-format/tests/removeformatcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,18 @@ describe( 'RemoveFormatCommand', () => {
}
}
);

model.schema.register( 'mockTable', {
inheritAllFrom: '$blockObject'
} );

model.schema.register( 'mockCell', {
allowContentOf: '$container',
allowIn: 'mockTable',
allowAttributes: 'someBlockFormatting',
isLimit: true,
isSelectable: true
} );
} );
} );

Expand Down Expand Up @@ -191,6 +203,96 @@ describe( 'RemoveFormatCommand', () => {
'state with custom block formatting': {
input: '<p fooA="bar">f[oo</p><p fooB="baz">b]ar</p>',
assert: () => expectModelToBeEqual( '<p fooA="BAR">f[oo</p><p fooB="BAZ">b]ar</p>' )
},

'removes formatting from a nested table (selection within paragraph in cell)': {
input:
'<mockTable>' +
'<mockCell someBlockFormatting="blue">' +
'<p someBlockFormatting="red">Foo</p>' +
'</mockCell>' +
'<mockCell>' +
'<mockTable>' +
'<mockCell someBlockFormatting="yellow">' +
'<p someBlockFormatting="orange">B[a<$text bold="true">r</$text> B]az</p>' +
'</mockCell>' +
'</mockTable>' +
'</mockCell>' +
'</mockTable>',
assert: () => expectModelToBeEqual(
'<mockTable>' +
'<mockCell someBlockFormatting="blue">' +
'<p someBlockFormatting="red">Foo</p>' +
'</mockCell>' +
'<mockCell>' +
'<mockTable>' +
'<mockCell someBlockFormatting="yellow">' +
'<p>B[ar B]az</p>' +
'</mockCell>' +
'</mockTable>' +
'</mockCell>' +
'</mockTable>'
)
},

'removes formatting from a nested table (whole nested cell selected)': {
input:
'<mockTable>' +
'<mockCell someBlockFormatting="blue">' +
'<p someBlockFormatting="red">Foo</p>' +
'</mockCell>' +
'<mockCell>' +
'<mockTable>' +
'[<mockCell someBlockFormatting="yellow">' +
'<p someBlockFormatting="orange"><$text bold="true">Bar</$text> Baz</p>' +
'</mockCell>]' +
'</mockTable>' +
'</mockCell>' +
'</mockTable>',
assert: () => expectModelToBeEqual(
'<mockTable>' +
'<mockCell someBlockFormatting="blue">' +
'<p someBlockFormatting="red">Foo</p>' +
'</mockCell>' +
'<mockCell>' +
'<mockTable>' +
'[<mockCell>' +
'<p>Bar Baz</p>' +
'</mockCell>]' +
'</mockTable>' +
'</mockCell>' +
'</mockTable>'
)
},

'removes formatting from a table content (whole content selected)': {
input:
'<p someBlockFormatting="foo">start</p>' +
'<p someBlockFormatting="bar">abc[def</p>' +
'<mockTable>' +
'<mockCell someBlockFormatting="blue">' +
'<p someBlockFormatting="abc">Foo</p>' +
'</mockCell>' +
'<mockCell>' +
'<p someBlockFormatting="123"><$text bold="red">Bar</$text> Baz</p>' +
'</mockCell>' +
'</mockTable>' +
'<p someBlockFormatting="foo">abc]def</p>' +
'<p someBlockFormatting="bar">end</p>',
assert: () => expectModelToBeEqual(
'<p someBlockFormatting="foo">start</p>' +
'<p>abc[def</p>' +
'<mockTable>' +
'<mockCell>' +
'<p>Foo</p>' +
'</mockCell>' +
'<mockCell>' +
'<p>Bar Baz</p>' +
'</mockCell>' +
'</mockTable>' +
'<p>abc]def</p>' +
'<p someBlockFormatting="bar">end</p>'
)
}
};

Expand Down