From ba1e49d3e98d75469def10e3da4a51f015290b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Wr=C3=B3bel?= Date: Mon, 19 Aug 2019 15:48:29 +0200 Subject: [PATCH] Introduce forceValue parameter to TodoListCheckCommand. --- src/todolistcheckcommand.js | 13 ++++++++++--- tests/todolistcheckcommand.js | 20 ++++++++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/todolistcheckcommand.js b/src/todolistcheckcommand.js index a6b6526..73973a8 100644 --- a/src/todolistcheckcommand.js +++ b/src/todolistcheckcommand.js @@ -90,12 +90,19 @@ export default class TodoListCheckCommand extends Command { } /** - * @inheritDoc + * Executes the command. + * + * @param {Object} [options] + * @param {Boolean} [options.forceValue] If set, it will force the command behavior. If `true`, the command will apply + * the attribute, otherwise the command will remove the attribute. If not set, the command will look for its current + * value to decide what it should do. */ - execute() { + execute( options = {} ) { this.editor.model.change( writer => { for ( const element of this._selectedElements ) { - if ( !this.value ) { + const value = ( options.forceValue === undefined ) ? !this.value : options.forceValue; + + if ( value ) { writer.setAttribute( attributeKey, true, element ); } else { writer.removeAttribute( attributeKey, element ); diff --git a/tests/todolistcheckcommand.js b/tests/todolistcheckcommand.js index 983b83e..3c5e43c 100644 --- a/tests/todolistcheckcommand.js +++ b/tests/todolistcheckcommand.js @@ -216,5 +216,25 @@ describe( 'TodoListCheckCommand', () => { command.execute(); } ); } ); + + it( 'should set attribute if `forceValue` parameter is set to `true`', () => { + setModelData( model, 'b[]ar' ); + + command.execute( { forceValue: true } ); + + expect( getModelData( model ) ).to.equal( + 'b[]ar' + ); + } ); + + it( 'should remove attribute if `forceValue` parameter is set to `false`', () => { + setModelData( model, 'b[]ar' ); + + command.execute( { forceValue: false } ); + + expect( getModelData( model ) ).to.equal( + 'b[]ar' + ); + } ); } ); } );