/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* bender-tags: model, composer */
import Document from '/ckeditor5/engine/model/document.js';
import Selection from '/ckeditor5/engine/model/selection.js';
import modifySelection from '/ckeditor5/engine/model/composer/modifyselection.js';
import { setData, stringify } from '/tests/engine/_utils/model.js';
describe( 'Delete utils', () => {
let document;
beforeEach( () => {
document = new Document();
document.schema.registerItem( 'p', '$block' );
document.createRoot();
} );
describe( 'modifySelection', () => {
describe( 'unit=character', () => {
describe( 'within element', () => {
test(
'does nothing on empty content',
'',
''
);
test(
'does nothing on empty content (with empty element)',
'
',
'
'
);
test(
'does nothing on empty content (backward)',
'',
'',
{ direction: 'backward' }
);
test(
'does nothing on root boundary',
'foo
',
'foo
'
);
test(
'does nothing on root boundary (backward)',
'foo
',
'foo
',
{ direction: 'backward' }
);
test(
'extends one character forward',
'foo
',
'foo
'
);
test(
'extends one character backward',
'foo
',
'foo
',
{ direction: 'backward' }
);
test(
'extends one character forward (non-collapsed)',
'foobar
',
'foobar
'
);
test(
'extends one character backward (non-collapsed)',
'foobar
',
'foobar
',
{ direction: 'backward' }
);
test(
'extends to element boundary',
'foo
',
'foo
'
);
test(
'extends to element boundary (backward)',
'foo
',
'foo
',
{ direction: 'backward' }
);
test(
'shrinks forward selection (to collapsed)',
'foobar
',
'foobar
',
{ direction: 'backward' }
);
test(
'shrinks backward selection (to collapsed)',
'foobar
',
'foobar
'
);
test(
'extends one element forward',
'f
oo
',
'f
oo
'
);
test(
'extends one non-empty element forward',
'f
xoo
',
'f
xoo
'
);
test(
'extends one element backward',
'fo
o
',
'fo
o
',
{ direction: 'backward' }
);
//test(
// 'unicode support - forward',
// 'நிலைக்கு
',
// 'நிலைக்கு
'
//);
//
//test(
// 'unicode support - backward',
// 'நிலைக்கு
',
// 'நிலைக்கு
',
// { direction: 'backward' }
//);
test(
'unicode support - combining mark forward',
'foob̂ar
',
'foob̂ar
'
);
test(
'unicode support - combining mark backward',
'foob̂ar
',
'foob̂ar
',
{ direction: 'backward' }
);
test(
'unicode support - combining mark multiple',
'foo̻̐ͩbar
',
'foo̻̐ͩbar
'
);
test(
'unicode support - combining mark multiple backward',
'foo̻̐ͩbar
',
'foo̻̐ͩbar
',
{ direction: 'backward' }
);
test(
'unicode support - combining mark to the end',
'foo̻̐ͩ
',
'foo̻̐ͩ
'
);
test(
'unicode support - surrogate pairs forward',
'\uD83D\uDCA9
',
'\uD83D\uDCA9
'
);
test(
'unicode support - surrogate pairs backward',
'\uD83D\uDCA9
',
'\uD83D\uDCA9
',
{ direction: 'backward' }
);
} );
describe( 'beyond element', () => {
test(
'extends over boundary of empty elements',
'
',
'
'
);
test(
'extends over boundary of empty elements (backward)',
'
',
'
',
{ direction: 'backward' }
);
test(
'extends over boundary of non-empty elements',
'a
bcd
',
'a
bcd
'
);
test(
'extends over boundary of non-empty elements (backward)',
'a
bcd
',
'a
bcd
',
{ direction: 'backward' }
);
test(
'extends over character after boundary',
'a
bcd
',
'a
bcd
'
);
test(
'extends over character after boundary (backward)',
'abc
d
',
'abc
d
',
{ direction: 'backward' }
);
test(
'extends over boundary when next element has nested elements',
'a
bcd
',
'a
bcd
'
);
test(
'extends over element when next element has nested elements',
'a
bcdef
',
'a
bcdef
'
);
test(
'extends over element when next node is a text',
'a
bc',
'a
bc'
);
test(
'extends over element when next node is a text (backward)',
'abc
',
'abc',
{ direction: 'backward' }
);
test(
'shrinks over boundary of empty elements',
'
',
'
'
);
test(
'shrinks over boundary of empty elements (backward)',
'
',
'
',
{ direction: 'backward' }
);
test(
'shrinks over boundary of non-empty elements',
'a
b
',
'a
b
'
);
test(
'shrinks over boundary of non-empty elements (backward)',
'a
b
',
'a
b
',
{ direction: 'backward' }
);
} );
} );
describe( 'unit=codePoint', () => {
test(
'does nothing on empty content',
'',
'',
{ unit: 'codePoint' }
);
test(
'does nothing on empty content (with empty element)',
'
',
'
',
{ unit: 'codePoint' }
);
test(
'extends one user-perceived character forward - latin letters',
'foo
',
'foo
',
{ unit: 'codePoint' }
);
test(
'extends one user-perceived character backward - latin letters',
'foo
',
'foo
',
{ unit: 'codePoint', direction: 'backward' }
);
test(
'unicode support - combining mark forward',
'foob̂ar
',
'foob̂ar
',
{ unit: 'codePoint' }
);
test(
'unicode support - combining mark backward',
'foob̂ar
',
'foob̂ar
',
{ unit: 'codePoint', direction: 'backward' }
);
test(
'unicode support - combining mark multiple',
'foo̻̐ͩbar
',
'foo̻̐ͩbar
',
{ unit: 'codePoint' }
);
//test(
// 'extends one unicode code point forward',
// 'நிலைக்கு
',
// 'நிலைக்கு
',
// { unit: 'codePoint' }
//);
//
//test(
// 'shrinks one unicode code point backward (combining mark case) ',
// 'நிலைக்கு
',
// 'நிலைக்கு
',
// { unit: 'codePoint', direction: 'backward' }
//);
test(
'unicode support - surrogate pairs forward',
'\uD83D\uDCA9
',
'\uD83D\uDCA9
',
{ unit: 'codePoint' }
);
test(
'unicode support surrogate pairs backward',
'\uD83D\uDCA9
',
'\uD83D\uDCA9
',
{ unit: 'codePoint', direction: 'backward' }
);
} );
} );
function test( title, input, output, options ) {
it( title, () => {
input = input.normalize();
output = output.normalize();
setData( document, input );
// Creating new instance of selection instead of operation on engine.model.Document#selection.
// Document's selection will throw errors in some test cases (which are correct cases, but only for
// non-document selections).
const testSelection = Selection.createFromSelection( document.selection );
modifySelection( testSelection, options );
expect( stringify( document.getRoot(), testSelection ) ).to.equal( output );
} );
}
} );