/** * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import RestrictedEditingExceptionCommand from '../src/restricteddocumentcommand'; describe( 'RestrictedEditingExceptionCommand', () => { let editor, command, model; beforeEach( () => { return ModelTestEditor .create() .then( newEditor => { editor = newEditor; model = editor.model; command = new RestrictedEditingExceptionCommand( editor ); model.schema.register( 'p', { inheritAllFrom: '$block' } ); model.schema.register( 'h1', { inheritAllFrom: '$block' } ); model.schema.register( 'img', { allowWhere: [ '$block', '$text' ], isObject: true } ); editor.model.schema.extend( '$text', { allowAttributes: [ 'restrictedEditingException' ] } ); } ); } ); afterEach( () => { command.destroy(); return editor.destroy(); } ); describe( 'value', () => { it( 'is true when collapsed selection has the attribute', () => { model.change( writer => { writer.setSelectionAttribute( 'restrictedEditingException', true ); } ); expect( command.value ).to.be.true; } ); it( 'is false when collapsed selection does not have the attribute', () => { model.change( writer => { writer.setSelectionAttribute( 'restrictedEditingException', true ); } ); model.change( writer => { writer.removeSelectionAttribute( 'restrictedEditingException' ); } ); expect( command.value ).to.be.false; } ); it( 'is true when selection is inside text with attribute', () => { setData( model, '
<$text restrictedEditingException="true">fo[]o$text>
foo[<$text restrictedEditingException="true">bar$text>]baz
' ); expect( command.value ).to.be.true; } ); } ); describe( 'isEnabled', () => { beforeEach( () => { model.schema.register( 'x', { inheritAllFrom: '$block' } ); model.schema.addAttributeCheck( ( ctx, attributeName ) => { if ( ctx.endsWith( 'x $text' ) && attributeName == 'restrictedEditingException' ) { return false; } } ); } ); describe( 'when selection is collapsed', () => { it( 'should return true if attribute is allowed at caret position', () => { setData( model, 'f[]oo
' ); expect( command.isEnabled ).to.be.true; } ); it( 'should return true if attribute is not allowed at caret position', () => { setData( model, '[foo]
' ); expect( command.isEnabled ).to.be.true; } ); it( 'should return false if there are no nodes in selection that can have the attribute', () => { setData( model, 'abcfoo[]barbaz
' ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true; } ); it( 'should remove selection attribute if text has non-restricted attribute', () => { setData( model, 'abc<$text restrictedEditingException="true">foo[]bar$text>baz
' ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false; } ); } ); describe( 'non-collapsed selection', () => { it( 'should do nothing if the command is disabled', () => { setData( model, 'fo[ob]ar
' ); command.isEnabled = false; command.execute(); expect( getData( model ) ).to.equal( 'fo[ob]ar
' ); } ); it( 'should add attribute on text without attribute', () => { setData( model, 'foo[bar]baz
' ); command.execute(); expect( getData( model ) ).to.equal( 'foo[<$text restrictedEditingException="true">bar$text>]baz
' ); } ); it( 'should add attribute on selected text if part of selected text have attribute already', () => { setData( model, '[foo<$text restrictedEditingException="true">bar$text>]baz
' ); command.execute(); expect( getData( model ) ).to.equal( '[<$text restrictedEditingException="true">foobar$text>]baz
' ); } ); it( 'should remove attribute only from selected part of non-restricted text', () => { setData( model, '<$text restrictedEditingException="true">foo[bar]baz$text>
' ); command.execute(); expect( getData( model ) ).to.equal( '<$text restrictedEditingException="true">foo$text>[bar]<$text restrictedEditingException="true">baz$text>
' ); } ); it( 'should remove attribute from selected text if all text contains attribute', () => { setData( model, 'abc[<$text restrictedEditingException="true">foo]bar$text>baz
' ); command.execute(); expect( getData( model ) ).to.equal( 'abc[foo]<$text restrictedEditingException="true">bar$text>baz
' ); } ); it( 'should add attribute on selected text if execute parameter was set to true', () => { setData( model, 'abc<$text restrictedEditingException="true">foob[ar$text>x]yz
' ); expect( command.value ).to.be.true; command.execute( { forceValue: true } ); expect( command.value ).to.be.true; expect( getData( model ) ).to.equal( 'abc<$text restrictedEditingException="true">foob[arx$text>]yz
' ); } ); it( 'should remove attribute on selected nodes if execute parameter was set to false', () => { setData( model, 'a[bc<$text restrictedEditingException="true">fo]obar$text>xyz
' ); command.execute( { forceValue: false } ); expect( command.value ).to.be.false; expect( getData( model ) ).to.equal( 'a[bcfo]<$text restrictedEditingException="true">obar$text>xyz
' ); } ); } ); } ); } );