/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* bender-tags: view, browser-only */
import { wrapPosition } from 'ckeditor5/engine/view/writer.js';
import Text from 'ckeditor5/engine/view/text.js';
import Element from 'ckeditor5/engine/view/element.js';
import ContainerElement from 'ckeditor5/engine/view/containerelement.js';
import AttributeElement from 'ckeditor5/engine/view/attributeelement.js';
import EmptyElement from 'ckeditor5/engine/view/emptyelement.js';
import Position from 'ckeditor5/engine/view/position.js';
import CKEditorError from 'ckeditor5/utils/ckeditorerror.js';
import { stringify, parse } from 'ckeditor5/engine/dev-utils/view.js';
describe( 'wrapPosition', () => {
/**
* Executes test using `parse` and `stringify` utils functions.
*
* @param {String} input
* @param {String} unwrapAttribute
* @param {String} expected
*/
function test( input, unwrapAttribute, expected ) {
let { view, selection } = parse( input );
const newPosition = wrapPosition( selection.getFirstPosition(), parse( unwrapAttribute ) );
expect( stringify( view, newPosition, { showType: true, showPriority: true } ) ).to.equal( expected );
}
it( 'should throw error when element is not instance of AttributeElement', () => {
const container = new ContainerElement( 'p', null, new Text( 'foo' ) );
const position = new Position( container, 0 );
const b = new Element( 'b' );
expect( () => {
wrapPosition( position, b );
} ).to.throw( CKEditorError, 'view-writer-wrap-invalid-attribute' );
} );
it( 'should wrap position at the beginning of text node', () => {
test(
'{}foobar',
'',
'[]foobar'
);
} );
it( 'should wrap position inside text node', () => {
test(
'foo{}bar',
'',
'foo[]bar'
);
} );
it( 'should support unicode', () => {
test(
'நிலை{}க்கு',
'',
'நிலை[]க்கு'
);
} );
it( 'should wrap position inside document fragment', () => {
test(
'foo[]bar',
'',
'foo[]' +
'bar'
);
} );
it( 'should wrap position at the end of text node', () => {
test(
'foobar{}',
'',
'foobar[]'
);
} );
it( 'should merge with existing attributes #1', () => {
test(
'foo[]',
'',
'foo{}'
);
} );
it( 'should merge with existing attributes #2', () => {
test(
'[]foo',
'',
'{}foo'
);
} );
it( 'should wrap when inside nested attributes', () => {
test(
'foo{}bar',
'',
'' +
'foo' +
'[]' +
'bar' +
''
);
} );
it( 'should merge when wrapping between same attribute', () => {
test(
'foo[]bar',
'',
'foo{}bar'
);
} );
it( 'should move position to text node if in same attribute', () => {
test(
'foobar[]',
'',
'foobar{}'
);
} );
it( 'should throw if position is set inside EmptyElement', () => {
const emptyElement = new EmptyElement( 'img' );
new ContainerElement( 'p', null, emptyElement );
const attributeElement = new AttributeElement( 'b' );
const position = new Position( emptyElement, 0 );
expect( () => {
wrapPosition( position, attributeElement );
} ).to.throw( CKEditorError, 'view-emptyelement-cannot-add' );
} );
} );