/**
* @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, model, 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;
model = editor.model;
} );
} );
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( model, '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( model, '
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( model, '[]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( model, '[]
' );
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( model,
'x' +
'' +
'a' +
'[]' +
'
' +
'x'
);
editor.editing.view.fire( 'enter', data );
expect( data.preventDefault.called ).to.be.true;
expect( getModelData( model ) ).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( model, '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( model ) ).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( model,
'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( model ) ).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( model,
'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( model ) ).to.equal(
'x' +
'a
' +
'[]' +
'x'
);
} );
it( 'scrolls the view document to the selection after the command is executed', () => {
const data = fakeEventData();
const execSpy = sinon.spy( editor, 'execute' );
const scrollSpy = sinon.stub( editor.editing.view, 'scrollToTheSelection' );
setModelData( model,
'x' +
'a[]
' +
'x'
);
editor.editing.view.fire( 'enter', data );
sinon.assert.calledOnce( scrollSpy );
sinon.assert.callOrder( execSpy, scrollSpy );
} );
} );
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( model,
'ab
' +
'[]c' +
'd'
);
editor.editing.view.fire( 'delete', data );
expect( getModelData( model ) ).to.equal(
'ab[]c
' +
'd'
);
} );
it( 'merges paragraph from a quote into a paragraph before quote', () => {
const data = fakeEventData();
setModelData( model,
'x' +
'[]ab
' +
'y'
);
editor.editing.view.fire( 'delete', data );
expect( getModelData( model ) ).to.equal(
'x[]a' +
'b
' +
'y'
);
} );
it( 'merges two quotes', () => {
const data = fakeEventData();
setModelData( model,
'x' +
'ab
' +
'[]cd
' +
'y'
);
editor.editing.view.fire( 'delete', data );
expect( getModelData( model ) ).to.equal(
'x' +
'ab[]cd
' +
'y'
);
} );
it( 'removes empty quote when merging into another quote', () => {
const data = fakeEventData();
setModelData( model,
'x' +
'a
' +
'[]
' +
'y'
);
editor.editing.view.fire( 'delete', data );
expect( getModelData( model ) ).to.equal(
'x' +
'a[]
' +
'y'
);
} );
it( 'removes empty quote when merging into a paragraph', () => {
const data = fakeEventData();
setModelData( model,
'x' +
'[]
' +
'y'
);
editor.editing.view.fire( 'delete', data );
expect( getModelData( model ) ).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.model,
'fo[o' +
'' +
'b]ar'
);
editor.execute( 'blockQuote' );
expect( getModelData( editor.model ) ).to.equal(
'fo[o
' +
'' +
'b]ar
'
);
element.remove();
return editor.destroy();
} );
} );
it( 'does not quote an image with caption', () => {
setModelData( model,
'fo[o' +
'' +
'xxx' +
'' +
'b]ar'
);
editor.execute( 'blockQuote' );
expect( getModelData( model ) ).to.equal(
'fo[o
' +
'' +
'xxx' +
'' +
'b]ar
'
);
} );
it( 'does not add an image to existing quote', () => {
setModelData( model,
'fo[o' +
'' +
'xxx' +
'' +
'b]ar
'
);
editor.execute( 'blockQuote' );
expect( getModelData( model ) ).to.equal(
'fo[o
' +
'' +
'xxx' +
'' +
'b]ar
'
);
} );
} );
} );