Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Introduce forceValue parameter to TodoListCheckCommand.
Browse files Browse the repository at this point in the history
  • Loading branch information
oskarwrobel committed Aug 19, 2019
1 parent 2e95c61 commit ba1e49d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/todolistcheckcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
20 changes: 20 additions & 0 deletions tests/todolistcheckcommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,5 +216,25 @@ describe( 'TodoListCheckCommand', () => {
command.execute();
} );
} );

it( 'should set attribute if `forceValue` parameter is set to `true`', () => {
setModelData( model, '<listItem listIndent="0" listType="todo" todoListChecked="true">b[]ar</listItem>' );

command.execute( { forceValue: true } );

expect( getModelData( model ) ).to.equal(
'<listItem listIndent="0" listType="todo" todoListChecked="true">b[]ar</listItem>'
);
} );

it( 'should remove attribute if `forceValue` parameter is set to `false`', () => {
setModelData( model, '<listItem listIndent="0" listType="todo">b[]ar</listItem>' );

command.execute( { forceValue: false } );

expect( getModelData( model ) ).to.equal(
'<listItem listIndent="0" listType="todo">b[]ar</listItem>'
);
} );
} );
} );

0 comments on commit ba1e49d

Please sign in to comment.