/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* bender-tags: view, browser-only */
import { mergeAttributes } from 'ckeditor5/engine/view/writer.js';
import ContainerElement from 'ckeditor5/engine/view/containerelement.js';
import Text from 'ckeditor5/engine/view/text.js';
import Position from 'ckeditor5/engine/view/position.js';
import { stringify, parse } from 'ckeditor5/engine/dev-utils/view.js';
describe( 'writer', () => {
/**
* Executes test using `parse` and `stringify` utils functions. Uses range delimiters `[]{}` to create and
* test merge position.
*
* @param {String} input
* @param {String} expected
*/
function test( input, expected ) {
const { view, selection } = parse( input );
const newPosition = mergeAttributes( selection.getFirstPosition() );
expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected );
}
describe( 'mergeAttributes', () => {
it( 'should not merge if inside text node', () => {
test( 'fo{}bar', 'fo{}bar' );
} );
it( 'should not merge if between containers', () => {
test(
'foo[]bar',
'foo[]bar'
);
} );
it( 'should return same position when inside empty container', () => {
test(
'[]',
'[]'
);
} );
it( 'should not merge when position is placed at the beginning of the container', () => {
test(
'[]',
'[]'
);
} );
it( 'should not merge when position is placed at the end of the container', () => {
test(
'[]',
'[]'
);
} );
it( 'should merge when placed between two text nodes', () => {
//
foobar
-> foo|bar
const t1 = new Text( 'foo' );
const t2 = new Text( 'bar' );
const p = new ContainerElement( 'p', null, [ t1, t2 ] );
const position = new Position( p, 1 );
const newPosition = mergeAttributes( position );
expect( stringify( p, newPosition ) ).to.equal( 'foo{}bar
' );
} );
it( 'should merge when placed between similar attribute nodes', () => {
test(
'' +
'baz[]qux' +
'',
'baz{}qux'
);
} );
it( 'should not merge when placed between non-similar attribute nodes', () => {
test(
'' +
'[]' +
'',
'' +
'[]' +
''
);
} );
it( 'should not merge when placed between similar attribute nodes with different priority', () => {
test(
'' +
'[]' +
'',
'' +
'[]' +
''
);
} );
it( 'should merge attribute nodes and their contents if possible', () => {
test(
'' +
'foo[]bar' +
'',
'foo{}bar'
);
} );
it( 'should remove empty attributes after merge #1', () => {
test(
'[]',
'[]'
);
} );
it( 'should remove empty attributes after merge #2', () => {
test(
'foo[]bar',
'foo{}bar'
);
} );
it( 'should remove empty attributes after merge #3', () => {
test(
'[]',
'[]'
);
} );
it( 'should not merge when placed between EmptyElements', () => {
test(
'[]',
'[]'
);
} );
it( 'should merge inside WidgetElement', () => {
test(
'foo[]bar',
'' +
'foo{}bar' +
''
);
} );
it( 'should not merge WidgetElements', () => {
test(
'foo[]bar',
'foo' +
'[]' +
'bar'
);
} );
} );
} );