/**
* @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
import Document from '../../src/model/document';
import DocumentFragment from '../../src/model/documentfragment';
import getSelectedContent from '../../src/controller/getselectedcontent';
import { setData, stringify } from '../../src/dev-utils/model';
describe( 'Delete utils', () => {
let doc;
describe( 'getSelectedContent', () => {
describe( 'in simple scenarios', () => {
beforeEach( () => {
doc = new Document();
doc.createRoot();
const schema = doc.schema;
schema.registerItem( 'image', '$inline' );
schema.allow( { name: '$text', inside: '$root' } );
schema.allow( { name: 'image', inside: '$root' } );
schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
schema.allow( { name: '$inline', attributes: [ 'italic' ] } );
} );
it( 'returns empty fragment for no selection', () => {
setData( doc, 'abc' );
const frag = getSelectedContent( doc.selection );
expect( frag ).instanceOf( DocumentFragment );
expect( frag.isEmpty ).to.be.true;
} );
it( 'returns empty fragment for empty selection', () => {
setData( doc, 'a[]bc' );
const frag = getSelectedContent( doc.selection );
expect( frag ).instanceOf( DocumentFragment );
expect( frag.isEmpty ).to.be.true;
} );
it( 'gets one character', () => {
setData( doc, 'a[b]c' );
const frag = getSelectedContent( doc.selection );
const content = stringify( frag );
expect( frag ).instanceOf( DocumentFragment );
expect( content ).to.equal( 'b' );
} );
it( 'gets full text', () => {
setData( doc, '[abc]' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'abc' );
} );
it( 'gets text with an attribute', () => {
setData( doc, 'xxx<$text bold="true">a[b]c$text>' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( '<$text bold="true">b$text>' );
} );
it( 'gets text with attributes', () => {
setData( doc, 'x<$text bold="true">a[b$text><$text italic="true">c]d$text>x' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( '<$text bold="true">b$text><$text italic="true">c$text>' );
} );
it( 'gets text with and without attribute', () => {
setData( doc, '<$text bold="true">a[b$text>c]d' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( '<$text bold="true">b$text>c' );
} );
it( 'gets text and element', () => {
setData( doc, '[abc]' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'abc' );
} );
it( 'gets one element', () => {
setData( doc, 'a[]b' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( '' );
} );
it( 'gets multiple elements', () => {
setData( doc, '[]' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( '' );
} );
} );
describe( 'in blocks', () => {
beforeEach( () => {
doc = new Document();
doc.createRoot();
const schema = doc.schema;
schema.registerItem( 'paragraph', '$block' );
schema.registerItem( 'heading1', '$block' );
schema.registerItem( 'blockImage' );
schema.registerItem( 'caption' );
schema.registerItem( 'image', '$inline' );
schema.allow( { name: 'blockImage', inside: '$root' } );
schema.allow( { name: 'caption', inside: 'blockImage' } );
schema.allow( { name: '$inline', inside: 'caption' } );
schema.allow( { name: '$inline', attributes: [ 'bold' ] } );
} );
it( 'gets one character', () => {
setData( doc, 'a[b]c' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'b' );
} );
it( 'gets entire paragraph content', () => {
setData( doc, '[ab]' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'ab' );
} );
it( 'gets two blocks - partial, partial', () => {
setData( doc, 'a[bcde]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bcde' );
} );
it( 'gets two blocks - full, partial', () => {
setData( doc, '[abcde]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'abcde' );
} );
it( 'gets two blocks - full, partial 2', () => {
setData( doc, '[abcde]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'abcde' );
} );
it( 'gets two blocks - full, partial 3', () => {
setData( doc, 'x[abcde]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'abcde' );
} );
it( 'gets two blocks - full, partial 4', () => {
setData( doc, '[abcde]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'abcde' );
} );
it( 'gets two blocks - partial, full', () => {
setData( doc, 'a[bcdef]' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bcdef' );
} );
it( 'gets two blocks - partial, full 2', () => {
setData( doc, 'a[bcdef]' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bcdef' );
} );
// See https://github.com/ckeditor/ckeditor5-engine/issues/652#issuecomment-261358484
it( 'gets two blocks - empty, full', () => {
setData( doc, 'abc[def]' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'def' );
} );
// See https://github.com/ckeditor/ckeditor5-engine/issues/652#issuecomment-261358484
it( 'gets two blocks - partial, empty', () => {
setData( doc, 'a[bc]def' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bc' );
} );
it( 'gets three blocks', () => {
setData( doc, 'a[bcxde]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bcxde' );
} );
it( 'gets block image', () => {
setData( doc, 'a[Foo]b' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'Foo' );
} );
it( 'gets two blocks', () => {
setData( doc, 'a[]b' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( '' );
} );
// Purely related to the current implementation.
it( 'gets content when multiple text items needs to be removed from the right excess', () => {
setData( doc, 'a[bc]d<$text bold="true">e$text>f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content )
.to.equal( 'bc' );
} );
// Purely related to the current implementation.
it( 'gets content when multiple text items needs to be removed from the left excess', () => {
setData( doc, 'a<$text bold="true">b$text>c[de]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content )
.to.equal( 'de' );
} );
} );
describe( 'in blocks (deeply nested)', () => {
beforeEach( () => {
doc = new Document();
doc.createRoot();
const schema = doc.schema;
schema.registerItem( 'paragraph', '$block' );
schema.registerItem( 'heading1', '$block' );
schema.registerItem( 'quote' );
schema.allow( { name: '$block', inside: 'quote' } );
schema.allow( { name: 'quote', inside: '$root' } );
} );
it( 'gets content when ends are equally deeply nested', () => {
setData( doc, 'xa[bcde]f
' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bcde' );
} );
it( 'gets content when left end nested deeper', () => {
setData( doc, 'a[bc
de]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bc
de' );
} );
it( 'gets content when left end nested deeper 2', () => {
setData( doc, 'a[bcx
de]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bcx
de' );
} );
it( 'gets content when left end nested deeper 3', () => {
setData( doc, 'xa[bc
de]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bc
de' );
} );
// See https://github.com/ckeditor/ckeditor5-engine/issues/652#issuecomment-261358484
it( 'gets content when left end nested deeper 4', () => {
setData( doc, 'x[abc
de]f' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'abc
de' );
} );
it( 'gets content when right end nested deeper', () => {
setData( doc, 'a[bcde]f
' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content ).to.equal( 'bcde
' );
} );
it( 'gets content when both ends nested deeper than the middle element', () => {
setData( doc, 'a[bc
xde]f
' );
const content = stringify( getSelectedContent( doc.selection ) );
expect( content )
.to.equal( 'bc
xde
' );
} );
} );
} );
} );