| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import RemoveFormatCommand from '../src/removeformatcommand';
- import Command from '@ckeditor/ckeditor5-core/src/command';
- import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor';
- import {
- getData,
- setData
- } from '@ckeditor/ckeditor5-engine/src/dev-utils/model';
- 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'
- } );
- 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
- } );
- } );
- } );
- afterEach( () => {
- editor.destroy();
- } );
- it( 'is a command', () => {
- expect( RemoveFormatCommand.prototype ).to.be.instanceOf( Command );
- expect( command ).to.be.instanceOf( Command );
- } );
- describe( 'isEnabled', () => {
- const expectEnabledPropertyToBe = expectedValue => expect( command ).to.have.property( 'isEnabled', expectedValue );
- const cases = {
- 'state when in non-formatting markup': {
- input: '<p>fo[]o</p>',
- assert: () => expectEnabledPropertyToBe( false )
- },
- 'state with collapsed selection in formatting markup': {
- input: '<p>f<$text bold="true">o[]o</$text></p>',
- assert: () => expectEnabledPropertyToBe( true )
- },
- 'state with selection containing formatting in the middle': {
- input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
- assert: () => expectEnabledPropertyToBe( true )
- },
- 'state with partially selected formatting at the start': {
- input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
- assert: () => expectEnabledPropertyToBe( true )
- },
- 'state with partially selected formatting at the end': {
- input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
- assert: () => expectEnabledPropertyToBe( true )
- },
- 'state with formatted selection alone': {
- input: '<p>fo[]o</p>',
- setDataOptions: {
- selectionAttributes: {
- bold: true,
- irrelevant: true
- }
- },
- assert: () => expectEnabledPropertyToBe( true )
- }
- };
- generateTypicalUseCases( cases );
- } );
- describe( 'execute()', () => {
- const expectModelToBeEqual = expectedValue => expect( getData( model ) ).to.equal( expectedValue );
- const cases = {
- 'state when in non-formatting markup': {
- input: '<p>fo[]o</p>',
- assert: () => expectModelToBeEqual( '<p>fo[]o</p>' )
- },
- 'state with collapsed selection in formatting markup': {
- input: '<p>f<$text bold="true">o[]o</$text></p>',
- assert: () => expectModelToBeEqual( '<p>f<$text bold="true">o</$text>[]<$text bold="true">o</$text></p>' )
- },
- 'state with selection containing formatting in the middle': {
- input: '<p>f[oo <$text bold="true">bar</$text> ba]z</p>',
- assert: () => expectModelToBeEqual( '<p>f[oo bar ba]z</p>' )
- },
- 'state with partially selected formatting at the start': {
- input: '<p><$text bold="true">b[ar</$text> ba]z</p>',
- assert: () => expectModelToBeEqual( '<p><$text bold="true">b</$text>[ar ba]z</p>' )
- },
- 'state with partially selected formatting at the end': {
- input: '<p>f[oo <$text bold="true">ba]z</$text></p>',
- assert: () => expectModelToBeEqual( '<p>f[oo ba]<$text bold="true">z</$text></p>' )
- },
- 'state with formatted selection alone': {
- input: '<p>fo[]o</p>',
- setDataOptions: {
- selectionAttributes: {
- bold: true,
- irrelevant: true
- }
- },
- assert: () => {
- expect( model.document.selection.hasAttribute( 'bold' ) ).to.equal( false );
- expect( model.document.selection.hasAttribute( 'irrelevant' ) ).to.equal( true );
- }
- }
- };
- generateTypicalUseCases( cases, {
- beforeAssert: () => command.execute()
- } );
- } );
- function generateTypicalUseCases( useCases, options ) {
- for ( const [ key, testConfig ] of Object.entries( useCases ) ) {
- it( key, () => {
- setData( model, testConfig.input, testConfig.setDataOptions );
- if ( options && options.beforeAssert ) {
- options.beforeAssert();
- }
- testConfig.assert();
- } );
- }
- }
- } );
|