/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import AttributeCommand from '../src/attributecommand'; import ModelTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/modeltesteditor'; import Document from '@ckeditor/ckeditor5-engine/src/model/document'; import Batch from '@ckeditor/ckeditor5-engine/src/model/batch'; import Range from '@ckeditor/ckeditor5-engine/src/model/range'; import Position from '@ckeditor/ckeditor5-engine/src/model/position'; import { setData, getData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; describe( 'AttributeCommand', () => { const attrKey = 'bold'; let editor, command, doc, root; beforeEach( () => { return ModelTestEditor .create() .then( newEditor => { editor = newEditor; doc = editor.document; root = doc.getRoot(); command = new AttributeCommand( editor, attrKey ); doc.schema.registerItem( 'p', '$block' ); doc.schema.registerItem( 'h1', '$block' ); doc.schema.registerItem( 'img', '$inline' ); doc.schema.objects.add( 'img' ); // Allow block in "root" (DIV) doc.schema.allow( { name: '$block', inside: '$root' } ); // Bold text is allowed only in P. doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'p' } ); doc.schema.allow( { name: 'p', attributes: 'bold', inside: '$root' } ); // Disallow bold on image. doc.schema.disallow( { name: 'img', attributes: 'bold', inside: '$root' } ); } ); } ); afterEach( () => { command.destroy(); return editor.destroy(); } ); describe( 'value', () => { it( 'is true when selection has the attribute', () => { doc.enqueueChanges( () => { doc.selection.setAttribute( attrKey, true ); } ); expect( command.value ).to.be.true; } ); it( 'is false when selection does not have the attribute', () => { doc.enqueueChanges( () => { doc.selection.removeAttribute( attrKey ); } ); expect( command.value ).to.be.false; } ); // See https://github.com/ckeditor/ckeditor5-core/issues/73#issuecomment-311572827. it( 'is false when selection contains object with nested editable', () => { doc.schema.registerItem( 'caption', '$block' ); doc.schema.allow( { name: '$inline', inside: 'caption' } ); doc.schema.allow( { name: 'caption', inside: 'img' } ); doc.schema.allow( { name: '$text', attributes: 'bold', inside: 'caption' } ); setData( doc, '

[Some caption inside the image.]

' ); expect( command.value ).to.be.false; command.execute(); expect( command.value ).to.be.false; expect( getData( doc ) ).to.equal( '

[<$text bold="true">Some caption inside the image.]

' ); } ); } ); describe( 'isEnabled', () => { // This test doesn't tests every possible case. // Method `refresh()` uses `checkAttributeInSelection()` which is fully tested in its own test. beforeEach( () => { doc.schema.registerItem( 'x', '$block' ); doc.schema.disallow( { name: '$text', inside: 'x', attributes: 'link' } ); } ); describe( 'when selection is collapsed', () => { it( 'should return true if characters with the attribute can be placed at caret position', () => { setData( doc, '

f[]oo

' ); expect( command.isEnabled ).to.be.true; } ); it( 'should return false if characters with the attribute cannot be placed at caret position', () => { setData( doc, 'fo[]o' ); expect( command.isEnabled ).to.be.false; } ); } ); describe( 'when selection is not collapsed', () => { it( 'should return true if there is at least one node in selection that can have the attribute', () => { setData( doc, '

[foo]

' ); expect( command.isEnabled ).to.be.true; } ); it( 'should return false if there are no nodes in selection that can have the attribute', () => { setData( doc, '[foo]' ); expect( command.isEnabled ).to.be.false; } ); } ); } ); describe( 'execute()', () => { it( 'should do nothing if the command is disabled', () => { setData( doc, '

fo[ob]ar

' ); command.isEnabled = false; command.execute(); expect( getData( doc ) ).to.equal( '

fo[ob]ar

' ); } ); it( 'should add attribute on selected nodes if the command value was false', () => { setData( doc, '

a[bc<$text bold="true">fo]obarxyz

' ); expect( command.value ).to.be.false; command.execute(); expect( command.value ).to.be.true; expect( getData( doc ) ).to.equal( '

a[<$text bold="true">bcfo]obarxyz

' ); } ); it( 'should remove attribute from selected nodes if the command value was true', () => { setData( doc, '

abc[<$text bold="true">foo]barxyz

' ); expect( command.value ).to.be.true; command.execute(); expect( getData( doc ) ).to.equal( '

abc[foo]<$text bold="true">barxyz

' ); expect( command.value ).to.be.false; } ); it( 'should add attribute on selected nodes if execute parameter was set to true', () => { setData( doc, '

abc<$text bold="true">foob[arx]yz

' ); expect( command.value ).to.be.true; command.execute( { forceValue: true } ); expect( command.value ).to.be.true; expect( getData( doc ) ).to.equal( '

abc<$text bold="true">foob[arx]yz

' ); } ); it( 'should remove attribute on selected nodes if execute parameter was set to false', () => { setData( doc, '

a[bc<$text bold="true">fo]obarxyz

' ); command.execute( { forceValue: false } ); expect( command.value ).to.be.false; expect( getData( doc ) ).to.equal( '

a[bcfo]<$text bold="true">obarxyz

' ); } ); it( 'should change selection attribute if selection is collapsed in non-empty parent', () => { setData( doc, '

a[]bc<$text bold="true">foobarxyz

' ); expect( command.value ).to.be.false; command.execute(); expect( command.value ).to.be.true; expect( doc.selection.hasAttribute( 'bold' ) ).to.be.true; command.execute(); expect( command.value ).to.be.false; expect( doc.selection.hasAttribute( 'bold' ) ).to.be.false; } ); it( 'should not store attribute change on selection if selection is collapsed in non-empty parent', () => { setData( doc, '

a[]bc<$text bold="true">foobarxyz

' ); command.execute(); // It should not save that bold was executed at position ( root, [ 0, 1 ] ). doc.enqueueChanges( () => { // Simulate clicking right arrow key by changing selection ranges. doc.selection.setRanges( [ new Range( new Position( root, [ 0, 2 ] ), new Position( root, [ 0, 2 ] ) ) ] ); // Get back to previous selection. doc.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( doc, '

abc<$text bold="true">foobarxyz

[]

' ); expect( command.value ).to.be.false; command.execute(); expect( command.value ).to.be.true; expect( doc.selection.hasAttribute( 'bold' ) ).to.be.true; // Attribute should be stored. // Simulate clicking somewhere else in the editor. doc.enqueueChanges( () => { doc.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. doc.enqueueChanges( () => { doc.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.execute(); expect( command.value ).to.be.false; expect( doc.selection.hasAttribute( 'bold' ) ).to.be.false; } ); it( 'should not apply attribute change where it would invalid schema', () => { doc.schema.registerItem( 'image', '$block' ); setData( doc, '

ab[c<$text bold="true">foobarxy]z

' ); expect( command.isEnabled ).to.be.true; command.execute(); expect( getData( doc ) ) .to.equal( '

ab[<$text bold="true">c<$text bold="true">foobarxy]z

' ); } ); it( 'should use provided batch for storing undo steps', () => { const batch = new Batch( new Document() ); setData( doc, '

a[bc<$text bold="true">fo]obarxyz

' ); expect( batch.deltas.length ).to.equal( 0 ); command.execute( { batch } ); expect( batch.deltas.length ).to.equal( 1 ); expect( getData( doc ) ).to.equal( '

a[<$text bold="true">bcfo]obarxyz

' ); } ); describe( 'should cause firing model document changesDone event', () => { let spy; beforeEach( () => { spy = sinon.spy(); } ); it( 'collapsed selection in non-empty parent', () => { setData( doc, '

x[]y

' ); doc.on( 'changesDone', spy ); command.execute(); expect( spy.calledOnce ).to.be.true; } ); it( 'non-collapsed selection', () => { setData( doc, '

[xy]

' ); doc.on( 'changesDone', spy ); command.execute(); expect( spy.calledOnce ).to.be.true; } ); it( 'in empty parent', () => { setData( doc, '

[]

' ); doc.on( 'changesDone', spy ); command.execute(); expect( spy.calledOnce ).to.be.true; } ); } ); } ); } );