/**
* @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
* For licensing, see LICENSE.md.
*/
/* bender-tags: view, browser-only */
import { wrap } from '/ckeditor5/engine/view/writer.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 Position from '/ckeditor5/engine/view/position.js';
import Range from '/ckeditor5/engine/view/range.js';
import Text from '/ckeditor5/engine/view/text.js';
import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
import { stringify, parse } from '/tests/engine/_utils/view.js';
describe( 'writer', () => {
/**
* Executes test using `parse` and `stringify` utils functions.
*
* @param {String} input
* @param {String} wrapAttribute
* @param {String} expected
*/
function test( input, wrapAttribute, expected ) {
let { view, selection } = parse( input );
const newRange = wrap( selection.getFirstRange(), parse( wrapAttribute ) );
expect( stringify( view.root, newRange, { showType: true, showPriority: true } ) ).to.equal( expected );
}
describe( 'wrap', () => {
it( 'should do nothing on collapsed ranges', () => {
test(
'f{}oo',
'',
'f{}oo'
);
} );
it( 'wraps single text node', () => {
test(
'[foobar]',
'',
'[foobar]'
);
} );
it( 'wraps single text node in document fragment', () => {
test(
'{foobar}',
'',
'[foobar]'
);
} );
it( 'should throw error when element is not instance of AttributeElement', () => {
const container = new ContainerElement( 'p', null, new Text( 'foo' ) );
const range = new Range(
new Position( container, 0 ),
new Position( container, 1 )
);
const b = new Element( 'b' );
expect( () => {
wrap( range, b );
} ).to.throw( CKEditorError, 'view-writer-wrap-invalid-attribute' );
} );
it( 'should throw error when range placed in two containers', () => {
const container1 = new ContainerElement( 'p' );
const container2 = new ContainerElement( 'p' );
const range = new Range(
new Position( container1, 0 ),
new Position( container2, 1 )
);
const b = new AttributeElement( 'b' );
expect( () => {
wrap( range, b );
} ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
} );
it( 'should throw when range has no parent container', () => {
const el = new AttributeElement( 'b' );
const b = new AttributeElement( 'b' );
expect( () => {
wrap( Range.createFromParentsAndOffsets( el, 0, el, 0 ), b );
} ).to.throw( CKEditorError, 'view-writer-invalid-range-container' );
} );
it( 'wraps part of a single text node #1', () => {
test(
'[foo}bar',
'',
'[foo]bar'
);
} );
it( 'wraps part of a single text node #2', () => {
test(
'{foo}bar',
'',
'[foo]bar'
);
} );
it( 'should support unicode', () => {
test(
'நி{லை}க்கு',
'',
'நி[லை]க்கு'
);
} );
it( 'wraps part of a single text node #3', () => {
test(
'foo{bar}',
'',
'foo[bar]'
);
} );
it( 'should not wrap inside nested containers', () => {
test(
'[foobarbaz]',
'',
'[foobarbaz]'
);
} );
it( 'wraps according to priorities', () => {
test(
'[foobar]',
'',
'[foobar]'
);
} );
it( 'merges wrapped nodes #1', () => {
test(
'[foobarbaz]',
'',
'[foobarbaz]'
);
} );
it( 'merges wrapped nodes #2', () => {
test(
'foo[bar}baz',
'',
'foo{bar]baz'
);
} );
it( 'merges wrapped nodes #3', () => {
test(
'foobar[baz]',
'',
'foobar{baz]'
);
} );
it( 'merges wrapped nodes #4', () => {
test(
'[foobar]baz',
'',
'[foobar]baz'
);
} );
it( 'merges wrapped nodes #5', () => {
test(
'[foobarbaz]',
'',
'' +
'[' +
'foo' +
'' +
'bar' +
'' +
'baz' +
']' +
''
);
} );
it( 'should wrap single element by merging attributes', () => {
test(
'[]',
'',
'[]'
);
} );
it( 'should not merge attributes when they differ', () => {
test(
'[]',
'',
'[]'
);
} );
it( 'should wrap single element by merging classes', () => {
test(
'[]',
'',
'[]'
);
} );
it( 'should wrap single element by merging styles', () => {
test(
'[]',
'',
'[]'
);
} );
it( 'should not merge styles when they differ', () => {
test(
'[]',
'',
'' +
'[' +
'' +
'' +
'' +
']' +
''
);
} );
it( 'should not merge single elements when they have different priority', () => {
test(
'[]',
'',
'' +
'[' +
'' +
'' +
'' +
']'
);
} );
it( 'should be merged with outside element when wrapping all children', () => {
test(
'[foobarbaz]',
'',
'' +
'[' +
'' +
'foobar' +
'baz' +
'' +
']' +
''
);
} );
} );
} );