/** * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import Editor from '/ckeditor5/core/editor/editor.js'; import Document from '/ckeditor5/engine/model/document.js'; import ToggleAttributeCommand from '/ckeditor5/core/command/toggleattributecommand.js'; import Range from '/ckeditor5/engine/model/range.js'; import Position from '/ckeditor5/engine/model/position.js'; import { setData, getData } from '/tests/engine/_utils/model.js'; describe( 'ToggleAttributeCommand', () => { const attrKey = 'bold'; let editor, command, modelDoc, root; beforeEach( () => { editor = new Editor(); editor.document = new Document(); modelDoc = editor.document; root = modelDoc.createRoot(); command = new ToggleAttributeCommand( editor, attrKey ); modelDoc.schema.registerItem( 'p', '$block' ); modelDoc.schema.registerItem( 'h1', '$block' ); modelDoc.schema.registerItem( 'img', '$inline' ); // Allow block in "root" (DIV) modelDoc.schema.allow( { name: '$block', inside: '$root' } ); // Bold text is allowed only in P. modelDoc.schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } ); modelDoc.schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } ); // Disallow bold on image. modelDoc.schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } ); } ); afterEach( () => { command.destroy(); } ); describe( 'value', () => { it( 'should be set to true or false basing on selection attribute', () => { modelDoc.selection.setAttribute( attrKey, true ); expect( command.value ).to.be.true; modelDoc.selection.removeAttribute( attrKey ); expect( command.value ).to.be.false; } ); } ); describe( '_doExecute', () => { it( 'should add attribute on selected nodes if the command value was false', () => { setData( modelDoc, '
a[bc<$text bold="true">fo]obar$text>xyz
' ); expect( command.value ).to.be.false; command._doExecute(); expect( command.value ).to.be.true; expect( getData( modelDoc ) ).to.equal( 'a[<$text bold="true">bcfo]obar$text>xyz
' ); } ); it( 'should remove attribute from selected nodes if the command value was true', () => { setData( modelDoc, 'abc[<$text bold="true">foo]bar$text>xyz
' ); expect( command.value ).to.be.true; command._doExecute(); expect( getData( modelDoc ) ).to.equal( 'abc[foo]<$text bold="true">bar$text>xyz
' ); expect( command.value ).to.be.false; } ); it( 'should add attribute on selected nodes if execute parameter was set to true', () => { setData( modelDoc, 'abc<$text bold="true">foob[ar$text>x]yz
' ); expect( command.value ).to.be.true; command._doExecute( true ); expect( command.value ).to.be.true; expect( getData( modelDoc ) ).to.equal( 'abc<$text bold="true">foob[arx$text>]yz
' ); } ); it( 'should remove attribute on selected nodes if execute parameter was set to false', () => { setData( modelDoc, 'a[bc<$text bold="true">fo]obar$text>xyz
' ); command._doExecute( false ); expect( command.value ).to.be.false; expect( getData( modelDoc ) ).to.equal( 'a[bcfo]<$text bold="true">obar$text>xyz
' ); } ); it( 'should change selection attribute if selection is collapsed in non-empty parent', () => { setData( modelDoc, 'a[]bc<$text bold="true">foobar$text>xyz
' ); expect( command.value ).to.be.false; command._doExecute(); expect( command.value ).to.be.true; expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true; command._doExecute(); expect( command.value ).to.be.false; expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false; } ); it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => { setData( modelDoc, 'a[]bc<$text bold="true">foobar$text>xyz
' ); command._doExecute(); // It should not save that bold was executed at position ( root, [ 0, 1 ] ). // Simulate clicking right arrow key by changing selection ranges. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] ); // Get back to previous selection. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 1 ] ), new Position( root, [ 0, 1 ] ) ) ] ); expect( command.value ).to.be.false; } ); it( 'should change selection attribute and store it if selection is collapsed in empty parent', () => { setData( modelDoc, 'abc<$text bold="true">foobar$text>xyz
[]
' ); expect( command.value ).to.be.false; command._doExecute(); expect( command.value ).to.be.true; expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.true; // Attribute should be stored. // Simulate clicking somewhere else in the editor. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] ); expect( command.value ).to.be.false; // Go back to where attribute was stored. modelDoc.selection.setRanges( [ new Range( new Position( root, [ 1, 0 ] ), new Position( root, [ 1, 0 ] ) ) ] ); // Attribute should be restored. expect( command.value ).to.be.true; command._doExecute(); expect( command.value ).to.be.false; expect( modelDoc.selection.hasAttribute( 'bold' ) ).to.be.false; } ); it( 'should not apply attribute change where it would invalid schema', () => { modelDoc.schema.registerItem( 'image', '$block' ); setData( modelDoc, 'ab[c
ab[<$text bold="true">c$text>
f[]oo
' ); expect( command._checkEnabled() ).to.be.true; } ); it( 'should return false if characters with the attribute cannot be placed at caret position', () => { setData( modelDoc, '[foo]
' ); expect( command._checkEnabled() ).to.be.true; } ); it( 'should return false if there are no nodes in selection that can have the attribute', () => { setData( modelDoc, '