/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ /* global document */ import BlockQuote from '../src/blockquote'; import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph'; import Image from '@ckeditor/ckeditor5-image/src/image'; import ImageCaption from '@ckeditor/ckeditor5-image/src/imagecaption'; import List from '@ckeditor/ckeditor5-list/src/list'; import Enter from '@ckeditor/ckeditor5-enter/src/enter'; import Delete from '@ckeditor/ckeditor5-typing/src/delete'; import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor'; import { getData as getModelData, setData as setModelData } from '@ckeditor/ckeditor5-engine/src/dev-utils/model'; describe( 'BlockQuote', () => { let editor, doc, element; beforeEach( () => { element = document.createElement( 'div' ); document.body.appendChild( element ); return ClassicTestEditor.create( element, { plugins: [ BlockQuote, Paragraph, Image, ImageCaption, List, Enter, Delete ] } ) .then( newEditor => { editor = newEditor; doc = editor.document; } ); } ); afterEach( () => { element.remove(); return editor.destroy(); } ); describe( 'enter key support', () => { function fakeEventData() { return { preventDefault: sinon.spy() }; } it( 'does nothing if selection is in an empty block but not in a block quote', () => { const data = fakeEventData(); const execSpy = sinon.spy( editor, 'execute' ); setModelData( doc, 'x[]x' ); editor.editing.view.fire( 'enter', data ); // Only enter command should be executed. expect( data.preventDefault.called ).to.be.true; expect( execSpy.calledOnce ).to.be.true; expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'enter' ); } ); it( 'does nothing if selection is in a non-empty block (at the end) in a block quote', () => { const data = fakeEventData(); const execSpy = sinon.spy( editor, 'execute' ); setModelData( doc, '
xx[]
' ); editor.editing.view.fire( 'enter', data ); // Only enter command should be executed. expect( data.preventDefault.called ).to.be.true; expect( execSpy.calledOnce ).to.be.true; expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'enter' ); } ); it( 'does nothing if selection is in a non-empty block (at the beginning) in a block quote', () => { const data = fakeEventData(); const execSpy = sinon.spy( editor, 'execute' ); setModelData( doc, '
[]xx
' ); editor.editing.view.fire( 'enter', data ); // Only enter command should be executed. expect( data.preventDefault.called ).to.be.true; expect( execSpy.calledOnce ).to.be.true; expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'enter' ); } ); it( 'does nothing if selection is not collapsed', () => { const data = fakeEventData(); const execSpy = sinon.spy( editor, 'execute' ); setModelData( doc, '
[]
' ); editor.editing.view.fire( 'enter', data ); // Only enter command should be executed. expect( data.preventDefault.called ).to.be.true; expect( execSpy.calledOnce ).to.be.true; expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'enter' ); } ); it( 'does not interfere with a similar handler in the list feature', () => { const data = fakeEventData(); setModelData( doc, 'x' + '
' + 'a' + '[]' + '
' + 'x' ); editor.editing.view.fire( 'enter', data ); expect( data.preventDefault.called ).to.be.true; expect( getModelData( doc ) ).to.equal( 'x' + '
' + 'a' + '[]' + '
' + 'x' ); } ); it( 'escapes block quote if selection is in an empty block in an empty block quote', () => { const data = fakeEventData(); const execSpy = sinon.spy( editor, 'execute' ); setModelData( doc, 'x
[]
x' ); editor.editing.view.fire( 'enter', data ); expect( data.preventDefault.called ).to.be.true; expect( execSpy.calledOnce ).to.be.true; expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'blockQuote' ); expect( getModelData( doc ) ).to.equal( 'x[]x' ); } ); it( 'escapes block quote if selection is in an empty block in the middle of a block quote', () => { const data = fakeEventData(); const execSpy = sinon.spy( editor, 'execute' ); setModelData( doc, 'x' + '
a[]b
' + 'x' ); editor.editing.view.fire( 'enter', data ); expect( data.preventDefault.called ).to.be.true; expect( execSpy.calledOnce ).to.be.true; expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'blockQuote' ); expect( getModelData( doc ) ).to.equal( 'x' + '
a
' + '[]' + '
b
' + 'x' ); } ); it( 'escapes block quote if selection is in an empty block at the end of a block quote', () => { const data = fakeEventData(); const execSpy = sinon.spy( editor, 'execute' ); setModelData( doc, 'x' + '
a[]
' + 'x' ); editor.editing.view.fire( 'enter', data ); expect( data.preventDefault.called ).to.be.true; expect( execSpy.calledOnce ).to.be.true; expect( execSpy.args[ 0 ][ 0 ] ).to.equal( 'blockQuote' ); expect( getModelData( doc ) ).to.equal( 'x' + '
a
' + '[]' + 'x' ); } ); } ); describe( 'backspace key support', () => { function fakeEventData() { return { preventDefault: sinon.spy(), direction: 'backward', unit: 'character' }; } it( 'merges paragraph into paragraph in the quote', () => { const data = fakeEventData(); setModelData( doc, '
ab
' + '[]c' + 'd' ); editor.editing.view.fire( 'delete', data ); expect( getModelData( doc ) ).to.equal( '
ab[]c
' + 'd' ); } ); it( 'merges paragraph from a quote into a paragraph before quote', () => { const data = fakeEventData(); setModelData( doc, 'x' + '
[]ab
' + 'y' ); editor.editing.view.fire( 'delete', data ); expect( getModelData( doc ) ).to.equal( 'x[]a' + '
b
' + 'y' ); } ); it( 'merges two quotes', () => { const data = fakeEventData(); setModelData( doc, 'x' + '
ab
' + '
[]cd
' + 'y' ); editor.editing.view.fire( 'delete', data ); expect( getModelData( doc ) ).to.equal( 'x' + '
ab[]cd
' + 'y' ); } ); it( 'removes empty quote when merging into another quote', () => { const data = fakeEventData(); setModelData( doc, 'x' + '
a
' + '
[]
' + 'y' ); editor.editing.view.fire( 'delete', data ); expect( getModelData( doc ) ).to.equal( 'x' + '
a[]
' + 'y' ); } ); it( 'removes empty quote when merging into a paragraph', () => { const data = fakeEventData(); setModelData( doc, 'x' + '
[]
' + 'y' ); editor.editing.view.fire( 'delete', data ); expect( getModelData( doc ) ).to.equal( 'x[]' + 'y' ); } ); } ); describe( 'compatibility with images', () => { it( 'does not quote a simple image', () => { const element = document.createElement( 'div' ); document.body.appendChild( element ); // We can't load ImageCaption in this test because it adds to all images automatically. return ClassicTestEditor.create( element, { plugins: [ BlockQuote, Paragraph, Image ] } ) .then( ( editor ) => { setModelData( editor.document, 'fo[o' + '' + 'b]ar' ); editor.execute( 'blockQuote' ); expect( getModelData( editor.document ) ).to.equal( '
fo[o
' + '' + '
b]ar
' ); element.remove(); editor.destroy(); } ); } ); it( 'does not quote an image with caption', () => { setModelData( doc, 'fo[o' + '' + 'xxx' + '' + 'b]ar' ); editor.execute( 'blockQuote' ); expect( getModelData( doc ) ).to.equal( '
fo[o
' + '' + 'xxx' + '' + '
b]ar
' ); } ); it( 'does not add an image to existing quote', () => { setModelData( doc, 'fo[o' + '' + 'xxx' + '' + '
b]ar
' ); editor.execute( 'blockQuote' ); expect( getModelData( doc ) ).to.equal( '
fo[o
' + '' + 'xxx' + '' + '
b]ar
' ); } ); } ); } );