/** * @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 ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; import RestrictedEditingExceptionCommand from '../src/restrictededitingexceptioncommand'; 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 the selection is inside a text with the 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 the selection is collapsed', () => { it( 'should return true if the attribute is allowed at the caret position', () => { setData( model, 'f[]oo
' ); expect( command.isEnabled ).to.be.true; } ); it( 'should return true if the attribute is not allowed at the caret position', () => { setData( model, '[foo]
' ); expect( command.isEnabled ).to.be.true; } ); it( 'should return false if there are no nodes in the selection that can have the attribute', () => { setData( model, 'abcfoo[]barbaz
' ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true; } ); it( 'should not set the selection attribute if there is a text without the attribute (forceValue="false")', () => { setData( model, 'abcfoo[]barbaz
' ); command.execute( { forceValue: false } ); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false; } ); it( 'should not change the selection attribute if selection has the attribute already (forceValue="true")', () => { setData( model, 'abcfoo[]barbaz
' ); model.change( writer => { writer.setSelectionAttribute( 'restrictedEditingException', true ); } ); command.execute( { forceValue: true } ); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true; } ); it( 'should remove an attribute from text node if a text has the non-restricted attribute', () => { setData( model, 'abc<$text restrictedEditingException="true">foo[]bar$text>baz
' ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false; expect( getData( model ) ).to.equal( 'abcfoo[]barbaz
' ); } ); it( 'should remove attribute from text node if a text has the non-restricted attribute (forceValue="false")', () => { setData( model, 'abc<$text restrictedEditingException="true">foo[]bar$text>baz
' ); command.execute( { forceValue: false } ); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false; expect( getData( model ) ).to.equal( 'abcfoo[]barbaz
' ); } ); it( 'should not remove attribute from text node if a text has the non-restricted attribute (forceValue="true")', () => { setData( model, 'abc<$text restrictedEditingException="true">foo[]bar$text>baz
' ); command.execute( { forceValue: true } ); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true; expect( getData( model ) ).to.equal( 'abc<$text restrictedEditingException="true">foo[]bar$text>baz
' ); } ); it( 'should not remove exception when selection is at the beginning of restricted text', () => { setData( model, 'abc<$text restrictedEditingException="true">[]foobar$text>baz
' ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true; expect( getData( model ) ).to.equal( 'abc<$text restrictedEditingException="true">[]foobar$text>baz
' ); } ); it( 'should remove attribute from text nodes when other attributes are present', () => { setData( model, '' + '<$text bold="true">abc$text>' + '<$text bold="true" restrictedEditingException="true">foo[]$text>' + '<$text restrictedEditingException="true">bar$text>' + 'baz' + '
' ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false; expect( getData( model ) ).to.equal( '<$text bold="true">abcfoo[]$text>barbaz
' ); } ); it( 'should remove selection attribute if selection does not have it (selection at the beginning)', () => { setData( model, 'abc<$text restrictedEditingException="true">[]foobar$text>baz
' ); model.change( writer => { writer.setSelectionAttribute( 'restrictedEditingException', 'true' ); } ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false; expect( getData( model ) ).to.equal( 'abc[]<$text restrictedEditingException="true">foobar$text>baz
' ); } ); it( 'should not remove exception when selection is at the end of restricted text', () => { setData( model, 'abc<$text restrictedEditingException="true">foobar[]$text>baz
' ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.false; expect( getData( model ) ).to.equal( 'abc<$text restrictedEditingException="true">foobar$text>[]baz
' ); } ); it( 'should set selection attribute if selection does not have it (selection at the end)', () => { setData( model, 'abc<$text restrictedEditingException="true">foobar[]$text>baz
' ); model.change( writer => { writer.removeSelectionAttribute( 'restrictedEditingException' ); } ); command.execute(); expect( model.document.selection.hasAttribute( 'restrictedEditingException' ) ).to.be.true; expect( getData( model ) ).to.equal( 'abc<$text restrictedEditingException="true">foobar[]$text>baz
' ); } ); } ); 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 the attribute on a text without the attribute', () => { setData( model, 'foo[bar]baz
' ); command.execute(); expect( getData( model ) ).to.equal( 'foo[<$text restrictedEditingException="true">bar$text>]baz
' ); } ); it( 'should add the attribute on a selected text if a selected part already has the attribute', () => { 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 the attribute only from the selected part of a 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 the attribute from the selected text if all text contains the 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 the attribute on a selected text if the "forceValue" parameter was 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 the attribute on selected nodes if the "forceValue" parameter was set 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
' ); } ); } ); } ); } );