/** * @license Copyright (c) 2003-2026, CKSource Holding sp. z o.o. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-licensing-options */ import { describe, it, expect, beforeEach, afterEach } from 'vitest'; import { RemoveFormatCommand } from '../src/removeformatcommand.js'; import { Command } from '@ckeditor/ckeditor5-core'; import { ModelTestEditor } from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor.js'; import { _getModelData, _setModelData } from '@ckeditor/ckeditor5-engine'; describe( 'RemoveFormatCommand', () => { let editor, model, command; beforeEach( () => { return ModelTestEditor.create() .then( newEditor => { editor = newEditor; model = editor.model; command = new RemoveFormatCommand( newEditor ); editor.commands.add( 'removeFormat', command ); model.schema.register( 'p', { inheritAllFrom: '$block', allowAttributes: 'someBlockFormatting' } ); model.schema.addAttributeCheck( ( ctx, attributeName ) => { // Bold will be used as an example formatting attribute. if ( ctx.endsWith( 'p $text' ) && attributeName == 'bold' ) { return true; } } ); model.schema.addAttributeCheck( ( ctx, attributeName ) => { // Text attribtue "irrelevant" will be used to make sure that non-formatting // is note being removed. if ( ctx.endsWith( 'p $text' ) && attributeName == 'irrelevant' ) { return true; } } ); model.schema.setAttributeProperties( 'bold', { isFormatting: true } ); model.schema.setAttributeProperties( 'someBlockFormatting', { isFormatting: true } ); // Custom attribute handling. model.schema.extend( 'p', { allowAttributes: [ 'fooA', 'fooB' ] } ); command.registerCustomAttribute( attributeName => attributeName.startsWith( 'foo' ), ( attributeName, itemRange, writer ) => { for ( const item of itemRange.getItems( { shallow: true } ) ) { const value = item.getAttribute( attributeName ); if ( value ) { writer.setAttribute( attributeName, value.toUpperCase(), item ); } } } ); model.schema.register( 'mockTable', { inheritAllFrom: '$blockObject' } ); model.schema.register( 'mockCell', { allowContentOf: '$container', allowIn: 'mockTable', allowAttributes: 'someBlockFormatting', isLimit: true, isSelectable: true } ); } ); } ); afterEach( async () => { await editor.destroy(); } ); it( 'is a command', () => { expect( RemoveFormatCommand.prototype ).toBeInstanceOf( Command ); expect( command ).toBeInstanceOf( Command ); } ); describe( 'isEnabled', () => { const expectEnabledPropertyToBe = expectedValue => expect( command ).toHaveProperty( 'isEnabled', expectedValue ); const cases = { 'state when in non-formatting markup': { input: '

fo[]o

', assert: () => expectEnabledPropertyToBe( false ) }, 'state with collapsed selection in formatting markup': { input: '

f<$text bold="true">o[]o

', assert: () => expectEnabledPropertyToBe( true ) }, 'state with selection containing formatting in the middle': { input: '

f[oo <$text bold="true">bar ba]z

', assert: () => expectEnabledPropertyToBe( true ) }, 'state with partially selected formatting at the start': { input: '

<$text bold="true">b[ar ba]z

', assert: () => expectEnabledPropertyToBe( true ) }, 'state with partially selected formatting at the end': { input: '

f[oo <$text bold="true">ba]z

', assert: () => expectEnabledPropertyToBe( true ) }, 'state with formatted selection alone': { input: '

fo[]o

', setDataOptions: { selectionAttributes: { bold: true, irrelevant: true } }, assert: () => expectEnabledPropertyToBe( true ) }, 'state with selection attribute markers on an element': { input: '

fo[]o

', beforeAssert: () => { model.change( writer => { const p = model.document.getRoot().getChild( 0 ); writer.setAttribute( 'selection:someBlockFormatting', 'foo', p ); writer.setAttribute( 'selection:fooA', 'bar', p ); writer.setAttribute( 'selection:irrelevant', 'true', p ); } ); }, assert: () => expectEnabledPropertyToBe( true ) }, 'state with block formatting': { input: '

f[oo

]bar

', assert: () => expectEnabledPropertyToBe( true ) }, 'state with block formatting (collapsed selection)': { input: '

f[]oo

', assert: () => expectEnabledPropertyToBe( true ) }, 'state with custom block formatting': { input: '

f[oo

b]ar

', assert: () => expectEnabledPropertyToBe( true ) } }; generateTypicalUseCases( cases ); } ); describe( 'execute()', () => { const expectModelToBeEqual = expectedValue => expect( _getModelData( model ) ).toEqual( expectedValue ); const cases = { 'state when in non-formatting markup': { input: '

fo[]o

', assert: () => expectModelToBeEqual( '

fo[]o

' ) }, 'state with collapsed selection in formatting markup': { input: '

f<$text bold="true">o[]o

', assert: () => expectModelToBeEqual( '

f<$text bold="true">o[]<$text bold="true">o

' ) }, 'state with selection containing formatting in the middle': { input: '

f[oo <$text bold="true">bar ba]z

', assert: () => expectModelToBeEqual( '

f[oo bar ba]z

' ) }, 'state with partially selected formatting at the start': { input: '

<$text bold="true">b[ar ba]z

', assert: () => expectModelToBeEqual( '

<$text bold="true">b[ar ba]z

' ) }, 'state with partially selected formatting at the end': { input: '

f[oo <$text bold="true">ba]z

', assert: () => expectModelToBeEqual( '

f[oo ba]<$text bold="true">z

' ) }, 'state with formatted selection alone': { input: '

fo[]o

', setDataOptions: { selectionAttributes: { bold: true, irrelevant: true } }, assert: () => { expect( model.document.selection.hasAttribute( 'bold' ) ).toEqual( false ); expect( model.document.selection.hasAttribute( 'irrelevant' ) ).toEqual( true ); } }, 'state with formatted selection alone (selection attribute marker)': { input: '

fo[]o

', setDataOptions: { selectionAttributes: { 'selection:someBlockFormatting': 'foo', 'selection:fooA': 'bar', 'selection:irrelevant': true } }, assert: () => { expect( model.document.selection.hasAttribute( 'selection:someBlockFormatting' ) ).toBe( false ); expect( model.document.selection.hasAttribute( 'selection:fooA' ) ).toBe( false ); expect( model.document.selection.hasAttribute( 'selection:irrelevant' ) ).toBe( true ); } }, 'state with selection attribute markers on an element': { input: '

fo[]o

', beforeAssert: () => { model.change( writer => { const p = model.document.getRoot().getChild( 0 ); writer.setAttribute( 'selection:someBlockFormatting', 'foo', p ); writer.setAttribute( 'selection:fooA', 'bar', p ); writer.setAttribute( 'selection:irrelevant', 'true', p ); } ); }, assert: () => expectModelToBeEqual( '

fo[]o

' ) }, 'state with block formatting': { input: '

f[oo

]bar

', assert: () => expectModelToBeEqual( '

f[oo

]bar

' ) }, 'state with block formatting (collapsed selection)': { input: '

f[]oo

bar

', assert: () => expectModelToBeEqual( '

f[]oo

bar

' ) }, 'state with custom block formatting': { input: '

f[oo

b]ar

', assert: () => expectModelToBeEqual( '

f[oo

b]ar

' ) }, 'removes formatting from a nested table (selection within paragraph in cell)': { input: '' + '' + '

Foo

' + '
' + '' + '' + '' + '

B[a<$text bold="true">r B]az

' + '
' + '
' + '
' + '
', assert: () => expectModelToBeEqual( '' + '' + '

Foo

' + '
' + '' + '' + '' + '

B[ar B]az

' + '
' + '
' + '
' + '
' ) }, 'removes formatting from a nested table (whole nested cell selected)': { input: '' + '' + '

Foo

' + '
' + '' + '' + '[' + '

<$text bold="true">Bar Baz

' + '
]' + '
' + '
' + '
', assert: () => expectModelToBeEqual( '' + '' + '

Foo

' + '
' + '' + '' + '[' + '

Bar Baz

' + '
]' + '
' + '
' + '
' ) }, 'removes formatting from a table content (whole content selected)': { input: '

start

' + '

abc[def

' + '' + '' + '

Foo

' + '
' + '' + '

<$text bold="red">Bar Baz

' + '
' + '
' + '

abc]def

' + '

end

', assert: () => expectModelToBeEqual( '

start

' + '

abc[def

' + '' + '' + '

Foo

' + '
' + '' + '

Bar Baz

' + '
' + '
' + '

abc]def

' + '

end

' ) } }; generateTypicalUseCases( cases, { beforeAssert: () => command.execute() } ); } ); describe( 'execute() with the _textFormattingOnly option', () => { const expectModelToBeEqual = expectedValue => expect( _getModelData( model ) ).toEqual( expectedValue ); beforeEach( () => { model.schema.extend( '$text', { allowAttributes: [ 'bold', 'fooText' ] } ); model.schema.register( 'mockInline', { inheritAllFrom: '$inlineObject', allowAttributes: 'someInlineFormatting' } ); model.schema.setAttributeProperties( 'someInlineFormatting', { isFormatting: true } ); } ); const cases = { 'text formatting removed and block formatting kept': { input: '

f[oo <$text bold="true">bar ba]z

', assert: () => expectModelToBeEqual( '

f[oo bar ba]z

' ) }, 'formatting of a selected cell and its blocks kept while the text formatting in it is removed': { input: '' + '[' + '

<$text bold="true">Bar Baz

' + '
]' + '
', assert: () => expectModelToBeEqual( '' + '[' + '

Bar Baz

' + '
]' + '
' ) }, 'text formatting of an inline object removed and its other formatting kept': { input: '

[foo]

', assert: () => expectModelToBeEqual( '

[foo]

' ) }, 'text formatting stored in an empty block where the selection starts removed': { input: '

[

bar]

', beforeAssert: () => { model.change( writer => { writer.setAttribute( 'selection:bold', true, model.document.getRoot().getChild( 0 ) ); } ); }, assert: () => expectModelToBeEqual( '

[

bar]

' ) }, 'text formatting stored in an empty block inside the selection removed': { input: '

f[oo

ba]r

', beforeAssert: () => { model.change( writer => { writer.setAttribute( 'selection:bold', true, model.document.getRoot().getChild( 1 ) ); } ); }, assert: () => expectModelToBeEqual( '

f[oo

ba]r

' ) }, 'text formatting of the selection removed': { input: '

fo[]o

', setDataOptions: { selectionAttributes: { bold: true, irrelevant: true } }, assert: () => { expect( model.document.selection.hasAttribute( 'bold' ) ).toBe( false ); expect( model.document.selection.hasAttribute( 'irrelevant' ) ).toBe( true ); } }, 'custom text formatting removed and custom block formatting kept': { input: '

f[oo <$text fooText="baz">bar]

', assert: () => expectModelToBeEqual( '

f[oo <$text fooText="BAZ">bar]

' ) } }; generateTypicalUseCases( cases, { beforeAssert: () => command.execute( { _textFormattingOnly: true } ) } ); it( 'keeps the command enabled when the selection has block formatting only', () => { _setModelData( model, '

f[]oo

' ); expect( command.isEnabled ).toBe( true ); command.execute( { _textFormattingOnly: true } ); expect( _getModelData( model ) ).toBe( '

f[]oo

' ); } ); it( 'removes all formatting when the option is set to false', () => { _setModelData( model, '

f[oo <$text bold="true">bar ba]z

' ); command.execute( { _textFormattingOnly: false } ); expect( _getModelData( model ) ).toBe( '

f[oo bar ba]z

' ); } ); } ); function generateTypicalUseCases( useCases, options ) { for ( const [ key, testConfig ] of Object.entries( useCases ) ) { it( 'handles ' + key, () => { _setModelData( model, testConfig.input, testConfig.setDataOptions ); if ( testConfig.beforeAssert ) { testConfig.beforeAssert(); } if ( options && options.beforeAssert ) { options.beforeAssert(); } testConfig.assert(); } ); } } } );