/** * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved. * For licensing, see LICENSE.md. */ import { wrapPosition } from '../../../src/view/writer'; import Text from '../../../src/view/text'; import Element from '../../../src/view/element'; import ContainerElement from '../../../src/view/containerelement'; import AttributeElement from '../../../src/view/attributeelement'; import EmptyElement from '../../../src/view/emptyelement'; import UIElement from '../../../src/view/uielement'; import Position from '../../../src/view/position'; import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror'; import { stringify, parse } from '../../../src/dev-utils/view'; describe( 'wrapPosition', () => { /** * Executes test using `parse` and `stringify` utils functions. * * @param {String} input * @param {String} unwrapAttribute * @param {String} expected */ function test( input, unwrapAttribute, expected ) { const { 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 ); // eslint-disable-line no-new const attributeElement = new AttributeElement( 'b' ); const position = new Position( emptyElement, 0 ); expect( () => { wrapPosition( position, attributeElement ); } ).to.throw( CKEditorError, 'view-emptyelement-cannot-add' ); } ); it( 'should throw if position is set inside UIElement', () => { const uiElement = new UIElement( 'span' ); new ContainerElement( 'p', null, uiElement ); // eslint-disable-line no-new const attributeElement = new AttributeElement( 'b' ); const position = new Position( uiElement, 0 ); expect( () => { wrapPosition( position, attributeElement ); } ).to.throw( CKEditorError, 'view-uielement-cannot-add' ); } ); } );